Search is not available for this dataset
name
stringlengths
2
88
description
stringlengths
31
8.62k
public_tests
dict
private_tests
dict
solution_type
stringclasses
2 values
programming_language
stringclasses
5 values
solution
stringlengths
1
983k
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #define int long long #define pb push_back using namespace std; const int N = 76; struct cell { int a, b, id; }; bool operator < (cell & a, cell & b) { return a.b < b.b; } int n, k, dp[N][N], from[N][N]; cell a[N]; vector<int> v; int take[N]; void go(int i, int j) { if (i != 1) go(i - 1, j - from[i][j]); if (from[i][j]) v.pb(i); } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int o; cin >> o; while (o--) { cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i].a >> a[i].b, a[i].id = i; sort(a + 1, a + n + 1); for (int i = 1; i <= n; i++) for (int j = 1; j <= i; j++) { dp[i][j] = 0; if (i != j) dp[i][j] = dp[i - 1][j] + a[i].b * (k - 1); from[i][j] = 0; if (dp[i - 1][j - 1] + (j - 1) * a[i].b + a[i].a > dp[i][j]) dp[i][j] = dp[i - 1][j - 1] + (j - 1) * a[i].b + a[i].a, from[i][j] = 1; } v.clear(); go(n, k); for (int i = 1; i <= n; i++) take[i] = 0; for (auto x: v) take[x] = 1; cout << n + (n - k) << "\n"; for (int i = 1; i <= n; i++) if (take[i] && i != v.back()) cout << a[i].id << ' '; for (int i = 1; i <= n; i++) if (!take[i]) cout << a[i].id << ' ' << -a[i].id << ' '; cout << a[v.back()].id << "\n"; } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct ANS { int sss, i; ANS *to; bool operator>(const ANS a) const { return sss > a.sss; } } dp[80][80]; ANS max_get(ANS a, ANS b, ANS *aa, ANS *bb, int i) { ANS s; if (a > b) { s.sss = a.sss; s.i = 0; s.to = aa; } else { s.sss = b.sss; s.i = i; s.to = bb; } return s; } struct node { int a, b, i; bool operator<(const node x) const { return b < x.b; } } mp[80]; int n, m, T; bool vis[80]; void print(ANS *x) { if (x == NULL) return; print(x->to); if (x->i) { printf("%d ", mp[x->i].i); vis[x->i] = 1; } } int main() { scanf("%d", &T); while (T--) { memset(vis, 0, sizeof(vis)); scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d%d", &mp[i].a, &mp[i].b); mp[i].i = i; } sort(mp + 1, mp + n + 1); if (n <= m) { printf("%d\n", n); for (int i = 1; i <= n; i++) printf("%d ", mp[i].i); printf("\n"); continue; } for (int i = 0; i <= n; i++) for (int j = 0; j <= m; j++) { dp[i][j].sss = -0x3f3f3f3f; dp[i][j].i = 0; dp[i][j].to = NULL; } dp[0][0].sss = 0; for (int i = 1; i <= n; i++) { dp[i][0].i = 0; dp[i][0].to = &dp[i - 1][0]; dp[i][0].sss = dp[i - 1][0].sss + mp[i].b * (n - 1); for (int j = 1; j <= m; j++) { ANS xa = dp[i - 1][j], xb = dp[i - 1][j - 1]; xa.sss += mp[i].b * (m - 1); xb.sss += mp[i].b * (j - 1) + mp[i].a; dp[i][j] = max_get(xa, xb, &dp[i - 1][j], &dp[i - 1][j - 1], i); } } printf("%d\n", 2 * n - m); ANS re = dp[n][m]; while (!re.i) re = *(re.to); print(re.to); vis[re.i] = 1; for (int i = 1; i <= n; i++) if (!vis[i]) printf("%d -%d ", mp[i].i, mp[i].i); printf("%d\n", mp[re.i].i); } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; #ifndef DEBUG #define endl '\n' #define debug(...) #else #define debug(...) __VA_ARGS__ #endif #define debugf(...) debug(printf(__VA_ARGS__);) const int oo = 0x3f3f3f3f; const int N = 128; int dp[N][N]; char pick[N][N]; struct Minion { int a, b, i; }; pair<vector<int>, vector<int>> solve(vector<Minion>& minions, int k) { int n = minions.size(); sort(minions.begin(), minions.end(), [&](const auto& x, const auto& y) { return x.b < y.b; }); dp[0][0] = 0; for (int p = 1; p <= k; p++) dp[p][0] = -oo; for (int i = 1; i <= n; i++) { auto [a, b, _] = minions[i-1]; dp[0][i] = (k - 1) * b; pick[0][i] = 'U'; } for (int p = 1; p <= k; p++) { for (int i = 1; i <= n; i++) { auto [a, b, _] = minions[i-1]; int pickF = dp[p-1][i-1] + (p-1)*b + a; int pickU = dp[p][i-1] + (k-1)*b; // Maybe? if (dp[p-1][i-1] == -oo) pickF = -oo; if (dp[p][i-1] == -oo) pickU = -oo; if (pickF > pickU) { pick[p][i] = 'F'; dp[p][i] = pickF; } else { pick[p][i] = 'U'; dp[p][i] = pickU; } } } debugf("ans=%d\n", dp[k][n]); vector<int> f; f.reserve(k); vector<int> u; u.reserve(n - k); int p = k; for (int i = n; i >= 1; i--) { if (pick[p][i] == 'F') { p--; f.emplace_back(minions[i-1].i); } else { u.emplace_back(minions[i-1].i); } } assert(p == 0); reverse(f.begin(), f.end()); return { f, u }; } signed (((((main)))))() <% ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { int n, k; cin >> n >> k; vector<Minion> minions(n); for (int i = 1; i <= n; i++) { auto& [a, b, j] = minions[i-1]; cin >> a >> b; j = i; } auto [f, u] = solve(minions, k); assert(f.size() + 2 * u.size() == k + 2 * (n - k)); cout << f.size() + 2 * u.size() << endl; for (int i = 0; i < k-1; i++) cout << f[i] << ' '; for (int i : u) { cout << i << ' ' << -i << ' '; } cout << f.back() << endl; } return 0; %>
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; void read(long long &x) { char ch = getchar(); x = 0; while (!isdigit(ch)) ch = getchar(); while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar(); } const long long N = 77; long long n, k, f[N][N], g[N][N]; pair<long long, pair<long long, long long> > a[N]; vector<long long> res, ans; signed main() { long long T; read(T); while (T--) { read(n), read(k); k = min(k, n); for (long long i = 1; i <= n; ++i) read(a[i].second.first), read(a[i].first), a[i].second.second = i; if (k == 1) { puts("1"); long long w = 0; for (long long i = 1; i <= n; ++i) if (a[i].second.first > a[w].second.first) w = i; printf("%lld\n", w); } sort(a + 1, a + n + 1); memset(f, 0xcf, sizeof(f)); f[0][0] = 0; for (long long i = 1; i <= n; ++i) for (long long j = 0; j <= i && j <= k; ++j) { if (j && f[i][j] < f[i - 1][j - 1] + a[i].second.first + a[i].first * (k - 1)) f[i][j] = f[i - 1][j - 1] + a[i].second.first + a[i].first * (j - 1), g[i][j] = 1; if (f[i][j] < f[i - 1][j] + a[i].first * (k - 1)) f[i][j] = f[i - 1][j] + a[i].first * (k - 1), g[i][j] = 0; } res.clear(); ans.clear(); for (long long i = n, j = k; i; --i) { if (g[i][j]) res.push_back(a[i].second.second), --j; else ans.push_back(a[i].second.second); } reverse(res.begin(), res.end()); printf("%lld\n", (long long)res.size() + ans.size() * 2); for (long long i = 0; i < k - 1; ++i) printf("%lld ", res[i]); for (long long i : ans) printf("%lld %lld ", i, -i); printf("%lld\n", res[k - 1]); } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
java
import java.util.*; import java.io.*; public class SummoningMinions { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); int t = Integer.parseInt(br.readLine()); Comparator<int[]> comp = (a, b) -> b[0] - a[0]; while (--t >= 0) { String[] line = br.readLine().split(" "); int n = Integer.parseInt(line[0]), k = Integer.parseInt(line[1]); int[][] ab = new int[n][3]; for (int i = 0; i < n; ++i) { line = br.readLine().split(" "); ab[i][0] = i; ab[i][1] = Integer.parseInt(line[0]); ab[i][2] = Integer.parseInt(line[1]); } Arrays.sort(ab, comp); int[][] dp = new int[n+1][n+1]; for (int i = 1; i <= n; ++i) { dp[i][0] = dp[i-1][0] + ab[i-1][2] * (k - 1); for (int j = 1; j < i; ++j) { dp[i][j] = Math.max(dp[i-1][j-1] + ab[i-1][2] * (j - 1) + ab[i-1][1], dp[i-1][j] + ab[i-1][2] * (k - 1)); } dp[i][i] = dp[i-1][i-1] + ab[i-1][2] * (i - 1) + ab[i-1][1]; } int[] res = new int[n*2-k]; int j1 = k, j2 = k - 1; for (int i = n; i > 0; --i) { if (i > j1 && dp[i][j1] == dp[i-1][j1] + ab[i-1][2] * (k - 1)) { res[++j2] = ab[i-1][0]; } else { res[--j1] = ab[i-1][0]; } } bw.write(res.length + '0'); bw.newLine(); for (int i = 0; i < k - 1; ++i) { bw.write(res[i] + '1'); bw.write(' '); } for (int i = k; i < n; ++i) { bw.write(res[i] + '1'); bw.write(' '); bw.write('-'); bw.write(res[i] + '1'); bw.write(' '); } bw.write(res[k-1] + '1'); bw.newLine(); } br.close(); bw.close(); } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include<bits/stdc++.h> using namespace std; #define ll long long #define int ll #define ull unsigned long long #define pii pair<int,int> #define X first #define Y second #define mod ((ll)1e9+7) #define pb push_back #define mp make_pair #define abs(x) ((x)>0?(x):(-(x))) #define F(n) Fi(i,n) #define Fi(i,n) Fl(i,0,n) #define Fl(i,l,n) for(int i=l;i<n;i++) #define memres(a) memset(a,0,sizeof(a)) #define all(a) a.begin(),a.end() #define sz(a) ((int)a.size()) #define ceiling(a,b) (((a)+(b)-1)/(b)) #define endl '\n' //#define LOCAL #ifdef LOCAL #define debug(a) cerr<<#a<<" "<<a<<endl; #else #define debug(a) #endif int gcd(int a,int b){if(b==0)return a;return gcd(b,a%b);} pii ar[75]; bool cmp(int a,int b){return ar[a-1].Y<ar[b-1].Y;} signed main(){ ios_base::sync_with_stdio(0);cin.tie(0); int t; cin>>t; while(t--){ int n,k; cin>>n>>k; F(n)cin>>ar[i].X>>ar[i].Y; pii dp[76][76]; F(n+1)Fi(j,k+1)dp[i][j]={-1,-1}; dp[0][0]={0,0}; F(n){ Fi(j,k){ if(dp[i][j].X!=-1){ if(dp[i+1][j+1].X<dp[i][j].X+ar[i].X+ar[i].Y*i)dp[i+1][j+1]={dp[i][j].X+ar[i].X+ar[i].Y*i,i+1}; if(dp[i+1][j].X<dp[i][j].X+ar[i].Y*(k-1))dp[i+1][j]={dp[i][j].X+ar[i].Y*(k-1),dp[i][j].Y}; } } } int p=dp[n][k].Y,c=k; vector<int> v; bool us[n+1]={false}; while(p&&c){ v.pb(p); us[p]=true; p=dp[p-1][--c].Y; } cout<<n*2-k<<endl; sort(all(v),cmp); F(sz(v)-1)cout<<v[i]<<" "; for(int i=1;i<=n;i++)if(!us[i])cout<<i<<" "<<-i<<" "; cout<<v[(sz(v)-1)]<<endl; } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int buf_size = 1e5 + 5; char buf[buf_size], *S, *T; inline char read_char() { if (S == T) T = (S = buf) + fread(buf, 1, buf_size, stdin); return S != T ? *(S++) : EOF; } inline int read_int() { int x = 0, f = 1; char c = read_char(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = read_char(); } while (c >= '0' && c <= '9') x = x * 10 + c - 48, c = read_char(); return x * f; } const int N = 5005; int n, m, cnt; int n1, n2, n3; int belong[N]; bool c[N], vis[N], mark[N]; bool flag; int a[N][2]; bool f[N][N], g[N][N]; vector<int> v[N]; void dfs(int x, int o) { a[o][c[x]]++; belong[x] = o; vis[x] = 1; for (int i = 0; i < v[x].size(); i++) { int y = v[x][i]; if (!vis[y]) { c[y] = c[x] ^ 1; dfs(y, o); } else if (c[y] == c[x]) { flag = 1; return; } } } int main() { n = read_int(), m = read_int(); n1 = read_int(), n2 = read_int(), n3 = read_int(); for (int i = 1; i <= m; i++) { int x = read_int(), y = read_int(); v[x].push_back(y); v[y].push_back(x); } for (int i = 1; i <= n; i++) { if (!vis[i]) dfs(i, ++cnt); } if (flag) { cout << "NO"; return 0; } f[0][0] = 1; for (int i = 1; i <= cnt; i++) { for (int j = a[i][0]; j <= n2; j++) { if (f[i - 1][j - a[i][0]]) f[i][j] = 1, g[i][j] = 0; } for (int j = a[i][1]; j <= n2; j++) { if (f[i - 1][j - a[i][1]]) f[i][j] = 1, g[i][j] = 1; } } if (!f[cnt][n2]) { cout << "NO"; return 0; } cout << "YES\n"; for (int i = cnt, j = n2; i >= 1; i--) { mark[i] = g[i][j]; j -= a[i][g[i][j]]; } for (int i = 1; i <= n; i++) { int j = belong[i]; if (c[i] == mark[j]) printf("2"); else if (n1) { printf("1"); n1--; } else printf("3"); } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; #define int long long int32_t main(){ ios::sync_with_stdio(0),cin.tie(0); int T; cin >> T; while(T--){ int n,k; cin >> n >> k; int a[n],b[n],vis[n]={0}; for(int i=0;i<n;i++) cin >> a[i] >> b[i]; vector <int> ans; int hold; for(int i=0;i<k;i++){ int mx=0,id=-1; for(int j=0;j<n;j++){ if(vis[j])continue; if(b[j]*i+a[j]>=mx)mx=b[j]*i+a[j],id=j; } vis[id]=1; if(i==k-1)hold=id+1; else ans.push_back(id+1); } for(int i=0;i<n;i++){ if(vis[i])continue; ans.push_back(i+1); ans.push_back(-i-1); } ans.push_back(hold); cout << ans.size() << '\n'; for(auto i:ans) cout << i << " "; cout << '\n'; } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; constexpr static int MAXN = 80; int n, k; int a[MAXN], b[MAXN]; int dp[MAXN][MAXN]; bool added[MAXN][MAXN]; bool used[MAXN]; void solve() { cin >> n >> k; for (int i = 0; i < n; i++) cin >> a[i] >> b[i]; vector<pair<int, int>> bval(n); for (int i = 0; i < n; i++) bval[i] = {b[i], i}; sort(bval.begin(), bval.end()); for (int i = 0; i <= n; i++) for (int j = 0; j <= k; j++) dp[i][j] = -1e9, added[i][j] = false; dp[0][0] = 0; for (int i = 0; i < n; i++) { int l = bval[i].second; for (int j = 1; j <= k; j++) { dp[i + 1][j] = dp[i][j] + (k - 1) * b[l]; int cost = a[l] + (j - 1) * b[l]; if (dp[i + 1][j] < dp[i][j - 1] + cost) { dp[i + 1][j] = dp[i][j - 1] + cost; added[i + 1][j] = true; } } } for (int i = 0; i < n; i++) used[i] = false; vector<int> c; int j = k; for (int i = n; i > 0; i--) { if (added[i][j]) { c.push_back(bval[i - 1].second); used[c.back()] = true; j--; } } reverse(c.begin(), c.end()); vector<int> moves; for (int i = 0; i + 1 < c.size(); i++) moves.push_back(c[i] + 1); for (int i = 0; i < n; i++) if (!used[i]) moves.push_back(i + 1), moves.push_back(-(i + 1)); moves.push_back(c.back() + 1); cout << moves.size() << endl; for (int i = 0; i < moves.size(); i++) cout << moves[i] << (i + 1 == moves.size() ? '\n' : ' '); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int T; cin >> T; while (T--) { solve(); } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct Edge { long long to, dis, next, cost; } edge[24050]; long long num = -1; bool vis[10010]; long long mincost; long long pre[10010], head[10010], cost[10010], last[10010], flow[10010], n, k, a[110], b[110], s, t, maxflow; long long to[110]; void add(long long f, long long t, long long dis, long long cost) { edge[++num].to = t; edge[num].dis = dis; edge[num].next = head[f]; edge[num].cost = cost; head[f] = num; edge[++num].to = f; edge[num].dis = 0; edge[num].cost = -cost; edge[num].next = head[t]; head[t] = num; } queue<long long> q; bool spfa(long long s, long long t) { memset(cost, 0x3f3f3f3f, sizeof cost); memset(flow, 0x3f3f3f3f, sizeof flow); memset(vis, 0, sizeof vis); q.push(s); vis[s] = 1; cost[s] = 0; pre[t] = -1; while (!q.empty()) { long long nowp = q.front(); q.pop(); vis[nowp] = 0; for (long long i = head[nowp]; i != -1; i = edge[i].next) { if (edge[i].dis > 0 && cost[edge[i].to] > cost[nowp] + edge[i].cost) { cost[edge[i].to] = cost[nowp] + edge[i].cost; pre[edge[i].to] = nowp; last[edge[i].to] = i; flow[edge[i].to] = min(flow[nowp], edge[i].dis); if (!vis[edge[i].to]) { vis[edge[i].to] = 1; q.push(edge[i].to); } } } } return pre[t] != -1; } void MCMF(long long s, long long t) { while (spfa(s, t)) { long long now = t; maxflow += flow[t]; mincost += flow[t] * cost[t]; while (now != s) { edge[last[now]].dis -= flow[t]; edge[last[now] ^ 1].dis += flow[t]; now = pre[now]; } } } signed main() { long long T; cin >> T; while (T--) { num = -1; memset(to, -1, sizeof to); memset(head, -1, sizeof head); cin >> n >> k; for (long long i = 1; i <= n; i++) cin >> a[i] >> b[i]; maxflow = mincost = 0; s = 0; t = 2 * n + 1; for (long long i = 1; i <= n; i++) add(s, i, 1, 0), add(i + n, t, 1, 0); for (long long i = 1; i <= n; i++) { for (long long j = 1; j <= n; j++) { long long nc = 0; if (j <= k - 1) nc = a[i] + b[i] * (j - 1ll); else if (j != n) nc = b[i] * (k - 1ll); else nc = a[i] + b[i] * (k - 1ll); nc = 0x3f3f3f3f - nc; add(i, j + n, 1, nc); } } MCMF(s, t); long long nowi = -1; for (long long i = n * 4; i <= num; i += 2) { nowi++; if (edge[i].dis == 0) to[1 + nowi % n] = 1 + nowi / n; } printf("%lld\n", 2 * n - k); cout << 2 * n - k << endl; for (long long i = 1; i <= k - 1; i++) cout << to[i] << " "; for (long long i = k; i < n; i++) cout << to[i] << " " << -to[i] << " "; cout << to[n]; cout << endl; } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC target("avx2") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") using namespace std; template <typename T> T sqr(T a) { return a * a; } template <typename T> int sign(T a) { if (a == 0) return 0; return (a > 0 ? 1 : -1); } template <typename T> bool uax(T& a, const T b) { if (a < b) { a = b; return true; } return false; } template <typename T> bool uin(T& a, const T b) { if (a > b) { a = b; return true; } return false; } template <typename T1, typename T2> istream& operator>>(istream& in, pair<T1, T2>& p) { in >> p.first >> p.second; return in; } template <typename T1, typename T2> ostream& operator<<(ostream& out, pair<T1, T2>& p) { out << p.first << ' ' << p.second; return out; } mt19937_64 rnd(0); const int LOG = 64; const int N = (int)1e5 + 7; const int MAXN = (int)4e5 + 7; const int MOD = (int)998244353; const int INF = (int)2e9 + 7; const int CONST = 450; const long long LINF = (int64_t)1e15; const long double PI = 3.1415926535897932384626433832795; const long double EPS = 1; struct Monster { int a, b, ind; Monster(){}; Monster(int a, int b, int ind) : a(a), b(b), ind(ind){}; }; bool operator<(const Monster& a, const Monster& b) { return a.b < b.b; } long long dp[100][100]; int par[100][100]; int n, k; Monster mon[N]; void solve() { cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> mon[i].a >> mon[i].b; mon[i].ind = i; } sort(mon + 1, mon + n + 1); for (int i = 0; i < 100; i++) { fill(dp[i], dp[i] + 100, -LINF); fill(par[i], par[i] + 100, -1); } dp[0][0] = 0; for (int i = 1; i <= n; i++) { dp[i][0] = dp[i - 1][0] + (k - 1) * mon[i].b; for (int j = 1; j <= k; j++) { if (dp[i - 1][j - 1] == -LINF && dp[i - 1][j] == -LINF) continue; if (dp[i - 1][j] == -LINF) { dp[i][j] = dp[i - 1][j - 1] + (j - 1) * mon[i].b + mon[i].a; par[i][j] = 1; continue; } if (dp[i - 1][j - 1] == -LINF) { dp[i][j] = dp[i - 1][j] + (k - 1) * mon[i].b; par[i][j] = 0; continue; } long long val1 = dp[i - 1][j - 1] + (j - 1) * mon[i].b + mon[i].a; long long val2 = dp[i - 1][j] + (k - 1) * mon[i].b; dp[i][j] = max(val1, val2); par[i][j] = (val1 >= val2 ? 1 : 0); } } long long ans = -LINF; int pos = n, cnt = k; vector<int> stay, rem; while (pos && cnt) { if (par[pos][cnt] == 0) { rem.push_back(mon[pos].ind); pos--; } else { stay.push_back(mon[pos].ind); pos--, cnt--; } } reverse(stay.begin(), stay.end()), reverse(rem.begin(), rem.end()); cout << (int)stay.size() + 2 * (int)rem.size() << "\n"; for (int i = 0; i < (int)stay.size() - 1; i++) cout << stay[i] << ' '; for (int x : rem) cout << x << ' ' << -x << ' '; cout << stay.back() << "\n"; } int main() { double start = clock(); cout << fixed << setprecision(20); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); rnd.seed(time(0)); int t = 1; cin >> t; for (int i = 0; i < t; i++) { solve(); } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
java
import java.io.*; import java.util.*; public class Sol{ static class specInt { int restore[]; int val; public specInt(int val) { this.val = val; restore = new int[n+1]; } } public static int mxN = 76; public static specInt dp[][] = new specInt[mxN][mxN]; public static int arr[][]; public static int n, k; public static int restore[]; public static void main(String[] args) throws IOException{ FastIO sc = new FastIO(System.in); PrintWriter out = new PrintWriter(System.out); int t = sc.nextInt(); while(t-->0) { n = sc.nextInt(); k = sc.nextInt(); arr = new int[n+1][3]; restore = new int[n+1]; for(int i=0; i<mxN; ++i) { for(int j=0; j<mxN; ++j) { dp[i][j] = new specInt(Integer.MIN_VALUE); } } for(int i=1; i<=n; ++i) { arr[i][0] = sc.nextInt(); arr[i][1] = sc.nextInt(); arr[i][2] = i; } ColumnSort(arr, 1); dp[0][0].val = 0; for(int i=1; i<=n; ++i) { for(int j=0; j<=k; ++j) { //if(dp[i-1][j].val!=Integer.MIN_VALUE) { if(arr[i][1]*(k-1)+dp[i-1][j].val>dp[i][j].val) { dp[i][j].val = arr[i][1]*(k-1)+dp[i-1][j].val; for(int b=1; b<=n; ++b) { dp[i][j].restore[b] = dp[i-1][j].restore[b]; } dp[i][j].restore[i] = 0; } if(j<k&&arr[i][0] + arr[i][1]*j+dp[i-1][j].val>dp[i][j+1].val) { dp[i][j+1].val = arr[i][0] + arr[i][1]*j+dp[i-1][j].val; for(int b=1; b<=n; ++b) { dp[i][j+1].restore[b] = dp[i-1][j].restore[b]; } dp[i][j+1].restore[i] = 1; } //} } } int idx = 0; /*for(int i=1; i<=n; ++i) { out.println(dp[n][k].restore[i]); }*/ for(int i=1; i<=n; ++i) { if(idx<k-1&&dp[n][k].restore[i]==1) { out.print(arr[i][2] + " "); idx++; } } for(int i=1; i<=n; ++i) { if(dp[n][k].restore[i]==0) { out.print(arr[i][2] + " " + (-arr[i][2]) + " "); } } for(int i=n; i>=0; --i) { if(dp[n][k].restore[i]==1) { out.println(arr[i][2]); break; } } out.println(); } out.close(); } public static void ColumnSort(int arr[][], int col) { Arrays.sort(arr, new Comparator<int[]>() { @Override public int compare(int[] entry1, int[] entry2) { Integer a = entry1[col]; Integer b = entry2[col]; return a.compareTo(b); } }); } static class FastIO { // Is your Fast I/O being bad? InputStream dis; byte[] buffer = new byte[1 << 17]; int pointer = 0; public FastIO(String fileName) throws IOException { dis = new FileInputStream(fileName); } public FastIO(InputStream is) throws IOException { dis = is; } int nextInt() throws IOException { int ret = 0; byte b; do { b = nextByte(); } while (b <= ' '); boolean negative = false; if (b == '-') { negative = true; b = nextByte(); } while (b >= '0' && b <= '9') { ret = 10 * ret + b - '0'; b = nextByte(); } return (negative) ? -ret : ret; } long nextLong() throws IOException { long ret = 0; byte b; do { b = nextByte(); } while (b <= ' '); boolean negative = false; if (b == '-') { negative = true; b = nextByte(); } while (b >= '0' && b <= '9') { ret = 10 * ret + b - '0'; b = nextByte(); } return (negative) ? -ret : ret; } byte nextByte() throws IOException { if (pointer == buffer.length) { dis.read(buffer, 0, buffer.length); pointer = 0; } return buffer[pointer++]; } String next() throws IOException { StringBuffer ret = new StringBuffer(); byte b; do { b = nextByte(); } while (b <= ' '); while (b > ' ') { ret.appendCodePoint(b); b = nextByte(); } return ret.toString(); } } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline long long mod(long long n, long long m) { long long ret = n % m; if (ret < 0) ret += m; return ret; } long long gcd(long long a, long long b) { return (b == 0LL ? a : gcd(b, a % b)); } long long exp(long long a, long long b, long long m) { if (b == 0LL) return 1LL; if (b == 1LL) return mod(a, m); long long k = mod(exp(a, b / 2, m), m); if (b & 1LL) { return mod(a * mod(k * k, m), m); } else return mod(k * k, m); } const long long N = 80; struct coisa { long long a, b, id; coisa() {} coisa(long long A, long long B, long long C) { a = A, b = B, id = C; } } v[N]; long long dp[N][N]; void solve() { long long n, k; cin >> n >> k; for (long long i = 1; i <= n; i++) { long long a, b; cin >> a >> b; v[i] = coisa(a, b, i); } sort(v + 1, v + 1 + n, [&](coisa a, coisa b) { return a.b < b.b; }); if (k == 1) { sort(v + 1, v + 1 + n, [&](coisa a, coisa b) { return a.a > b.a; }); cout << 1 << "\n"; cout << v[1].id << "\n"; return; } if (k == n) { cout << n << "\n"; for (long long i = 1; i <= n; i++) { cout << v[i].id << " "; } cout << "\n"; return; } for (long long i = 0; i < (N); ++i) for (long long j = 0; j < (N); ++j) dp[i][j] = -1e9; dp[0][0] = 0; for (long long i = 1; i <= n; i++) { for (long long j = 0; j <= min(i - 1, k); j++) { dp[i][j + 1] = max(dp[i][j + 1], dp[i - 1][j] + j * v[i].b + v[i].a); dp[i][j] = max(dp[i][j], dp[i - 1][j] + (k - 1) * v[i].b); } } vector<long long> tira, col; long long j = k; for (long long i = n; i >= 1; i--) { if (i == 1) { if (j == 0) tira.push_back(i); else { col.push_back(i); j--; } break; } if (j == 0) tira.push_back(i); else { if (dp[i][j] == dp[i - 1][j - 1] + (j - 1) * v[i - 1].b + v[i].a) { col.push_back(i); j--; } else { tira.push_back(i); } } } reverse(col.begin(), col.end()); vector<long long> ans; for (long long i = 0; i < (long long)col.size() - 1; i++) ans.push_back(v[col[i]].id); for (long long x : tira) ans.push_back(v[x].id); for (long long x : tira) ans.push_back(-v[x].id); ans.push_back(v[col[(long long)col.size() - 1]].id); cout << ans.size() << "\n"; for (long long x : ans) cout << x << " "; cout << "\n"; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long t; cin >> t; while (t--) { solve(); } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long maxn = 1e6 + 10; long long n, m, q; long long da[maxn], db[maxn]; inline long long read() { long long X = 0, w = 0; char ch = 0; while (!isdigit(ch)) w |= ch == '-', ch = getchar(); while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar(); return w ? -X : X; } bool cmp(long long a, long long b) { return da[a] + db[b] * (m - 1) > da[b] + db[a] * (m - 1); } signed main() { long long t = read(); while (t--) { vector<long long> cz, nc; long long n = read(), m = read(); long long idx[100]; for (long long i = 0; i < n; i++) { da[i] = read(); db[i] = read(); } long long res = 0; for (long long k = 0; k < n; k++) { long long cnt = 0; long long tot = 0; nc.clear(); for (long long i = 0; i < n; i++) { if (i != k) idx[tot++] = i; } sort(idx, idx + n - 1, cmp); for (long long u = 0; u < n - 1; u++) { long long i = idx[u]; if (u < m - 1) { nc.push_back(i + 1); cnt += da[i]; } else { nc.push_back(i + 1); nc.push_back(-i - 1); cnt += db[i] * (m - 1); } } nc.push_back(k + 1); cnt += da[k] + db[k] * (m - 1); if (cnt > res) { res = cnt; cz = nc; } } cout << cz.size() << endl; for (auto i : cz) cout << i << ' '; cout << endl; } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using i2 = pair<int, int>; const int N = 80; pair<i2, int> minions[N], A[N]; int dp[N][N]; bool take[N][N], is_taken[N]; int solve(int x, int n, int k) { for (int i = 1; i < x; i++) A[i] = minions[i]; for (int i = x + 1; i <= n; i++) A[i - 1] = minions[i]; for (int i = 1; i < n; i++) { int a = A[i].first.second; int b = A[i].first.first; for (int j = 0; j <= i; j++) dp[i][j] = -1; for (int j = 0; j < i; j++) { if (dp[i - 1][j] + (k - 1) * b > dp[i][j]) { dp[i][j] = dp[i - 1][j] + (k - 1) * b; take[i][j] = false; } } for (int j = 1; j <= i; j++) { if (dp[i - 1][j - 1] + a + (j - 1) * b > dp[i][j]) { dp[i][j] = dp[i - 1][j] + a + (j - 1) * b; take[i][j] = true; } } } int ax = minions[x].first.second; int bx = minions[x].first.first; return dp[n - 1][k - 1] + ax + (k - 1) * bx; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int T; cin >> T; while (T--) { int n, k; cin >> n >> k; for (int i = 1; i <= n; i++) { int a, b; cin >> a >> b; minions[i] = {{b, a}, i}; } sort(minions + 1, minions + n + 1); int mx = -1, ind = 0; for (int i = 1; i <= n; i++) { int res = solve(i, n, k); if (res > mx) { mx = res; ind = i; } } solve(ind, n, k); for (int i = n - 1, j = k - 1; i >= 1; i--) { if (take[i][j]) { is_taken[i] = true; j--; } else is_taken[i] = false; } cout << 2 * n - k << "\n"; for (int i = 1; i < n; i++) { if (is_taken[i]) cout << A[i].second << " "; } for (int i = 1; i < n; i++) { if (!is_taken[i]) cout << A[i].second << " " << -A[i].second << " "; } cout << minions[ind].second << "\n"; } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
python3
def read_int(): return int(input()) def read_ints(): return map(int, input().split(' ')) t = read_int() for case_num in range(t): n, k = read_ints() p = [] for i in range(n): ai, bi = read_ints() p.append((bi, ai, i + 1)) p.sort() dp = [[0 for j in range(k + 1)] for i in range(n + 1)] use = [[False for j in range(k + 1)] for i in range(n + 1)] for i in range(1, n + 1): for j in range(min(i, k) + 1): dp[i][j] = dp[i - 1][j] + (k - 1) * p[i - 1][0] if j > 0: x = dp[i - 1][j - 1] + (j - 1) * p[i - 1][0] + p[i - 1][1] if x > dp[i][j]: dp[i][j] = x use[i][j] = True used = [] curr = k for i in range(n, 0, -1): if use[i][curr]: used.append(p[i - 1][2]) curr -= 1 used.reverse() seq = used[:-1] st = set(used) for i in range(1, n): if not i in st: seq.append(i) seq.append(-i) seq.append(used[-1]) print(len(seq)) print(' '.join(map(str, seq)))
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
python3
def m(e): return e['m'] def h(e): return e['h'] a = int(input()) for i in range(a): num = [int(x) for x in input().split()] mi = [] for j in range(num[0]): a = [int(x) for x in input().split()] k = {} k['m'] = a[0] k['h'] = a[1] k['i'] = j+1 mi.append(k) mi.sort(key=m,reverse = True) cho = mi nch = mi cho = mi[:num[1]] nch = mi[num[1]:] ksum = 0 klen = 0 k = [] for element in cho[:num[1]-1]: #print(element['i'],end = " ") k.append(element['i']) klen +=1 ksum+=element['m'] ksum+=element['h']*(klen-1) fix = klen #print() #print(nch) if(num[1]!=1): for element in nch: if(element['h']!=0): #print(element['i'],end = " ") #print(-1*element['i'],end = " ") k.append(element['i']) klen +=1 k.append(-1*element['i']) klen +=1 ksum+=(element['h']*fix) k.append(cho[num[1]-1]['i']) klen+=1 ksum+=cho[num[1]-1]['m'] ksum+=cho[num[1]-1]['h']*(klen-1) #print(cho[num[1]-1]['i'],end = " ") mi.sort(key = h) csum = 0 clen = 0 c = [] if(num[1]>1): c.append(mi[0]['i']) clen +=1 csum+=mi[0]['m'] mi = mi[1:] mi.sort(key=m,reverse = True) cho = mi nch = mi cho = mi[:num[1]-1] nch = mi[num[1]-1:] for element in cho[:num[1]-2]: #print(element['i'],end = " ") c.append(element['i']) clen +=1 csum+=element['m'] csum+=element['h']*(clen-1) fix = clen #print() #print(nch) if(num[1]!=1): for element in nch: if(element['h']!=0): #print(element['i'],end = " ") #print(-1*element['i'],end = " ") c.append(element['i']) clen +=1 c.append(-1*element['i']) clen +=1 csum+=(element['h']*fix) c.append(cho[num[1]-2]['i']) clen+=1 csum+=cho[num[1]-2]['m'] csum+=cho[num[1]-2]['h']*(clen-1) #print(cho[num[1]-1]['i'],end = " ") if(csum>ksum): print(clen) for j in c: print(j,end = " ") print() else: print(klen) for j in k: print(j,end = " ") print() """"5 2 3538 43176 24258 77210 92123 70606 44495 37855 65913 67119 8 3 4 -4 2 -2 1 -1 5 8 4 2 -2 5 -5 1 -1 3 """
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
python3
for _ in range(int(input())): n, k = map(int, input().split()) values = [] for i in range(n): a, b = map(int, input().split()) values.append((i + 1, a, b)) if k == 1: best = -1 score = 0 for (i, a, b) in values: if a > score: best = i score = a print(1) print(best) else: values = sorted(values, key=lambda x: x[1], reverse=True) keepers = sorted(values[:k], key=lambda x: x[2]) destroyers = [i for i, a, b in values[k:] if b > 0] turns = [] for i, a, b in keepers[:-1]: turns.append(i) for i in destroyers: turns.append(i) turns.append(-i) turns.append(keepers[-1][0]) print(len(turns)) print(*turns)
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; vector<pair<pair<int, int>, int>> v; long long dp[80][80]; pair<int, int> from[80][80]; set<int> s; int main() { int t; cin >> t; while (t) { t--; int i, j, n, k; cin >> n >> k; v.clear(); s.clear(); for (i = 1; i <= n; i++) { int a, b; cin >> a >> b; a -= b; v.push_back({{b, a}, i}); s.insert(i); } sort(v.rbegin(), v.rend()); for (i = 0; i <= n; i++) { for (j = 0; j <= k; j++) { dp[i][j] = -100000000000; from[i][j] = {-1, -1}; } } dp[0][0] = 0; for (i = 0; i < n; i++) { int ind = v[i].second; long long a = v[i].first.second, b = v[i].first.first; if (dp[i + 1][0] < dp[i][0]) { from[i + 1][0] = {ind, 0}; dp[i + 1][0] = dp[i][0]; } for (j = 1; j <= k; j++) { if (dp[i + 1][j] < dp[i][j]) { from[i + 1][j] = {ind, j}; dp[i + 1][j] = dp[i][j]; } if (dp[i + 1][j] < dp[i][j - 1] + (k - j) * b + a) { from[i + 1][j] = {ind, j - 1}; dp[i + 1][j] = dp[i][j - 1] + (k - j) * b + a; } } } vector<int> ans; int nx = n, ny = k; while (ny != 0) { pair<int, int> p = from[nx][ny]; if (p.second != ny) { ans.push_back(p.first); s.erase(p.first); } nx--; ny = p.second; } for (i = 0; i < k - 1; i++) { cout << ans[i] << " "; } for (int x : s) { cout << x << " " << -x << " "; } cout << ans[k - 1] << endl; } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int Maxn = 88; struct node { int a, b, id; } g[Maxn]; int f[Maxn][Maxn], s[Maxn]; bool flag[Maxn][Maxn], vis[Maxn]; int a[Maxn], b[Maxn], id[Maxn]; int n, m, cnt; inline bool cmp(node x, node y) { return x.b < y.b; } void init() { for (int i = 1; i <= n; ++i) for (int j = 0; j <= m; ++j) f[i][j] = flag[i][j] = 0; for (int i = 1; i <= n; ++i) s[i] = vis[i] = a[i] = b[i] = id[i] = 0; cnt = n = m = 0; } inline int read() { int s = 0, w = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') w = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar(); return s * w; } int main() { int T = read(); while (T--) { n = read(), m = read(); for (int i = 1; i <= n; ++i) g[i].a = read(), g[i].b = read(), g[i].id = i; sort(g + 1, g + 1 + n, cmp); for (int i = 1; i <= n; ++i) a[i] = g[i].a, b[i] = g[i].b, id[i] = g[i].id; for (int i = 1; i <= n; ++i) for (int j = 0; j <= min(i, m); ++j) { f[i][j] = f[i - 1][j] + b[i] * (m - 1), flag[i][j] = 0; if (j && f[i - 1][j - 1] + a[i] + b[i] * (j - 1) > f[i][j]) f[i][j] = f[i - 1][j - 1] + a[i] + b[i] * (j - 1), flag[i][j] = 1; } int i = n, j = m; while (i) { if (flag[i][j]) s[++cnt] = i, vis[i] = 1, --j; --i; } printf("%d\n", (n << 1) - m); int last = 0; for (int i = 1; i <= n; ++i) { if (!vis[i]) continue; if (++last == m) { last = i; break; } printf("%d ", id[i]); } for (int i = 1; i <= n; ++i) if (!vis[i]) printf("%d %d ", id[i], -id[i]); printf("%d\n", id[last]); init(); } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long t; cin >> t; while (t--) { int k, n; cin >> n >> k; vector<pair<long long, long long> > m(n); map<pair<long long, long long>, long long> org; for (int i = 0; i < n; i++) { cin >> m[i].second >> m[i].first; org[m[i]] = i; } sort(m.begin(), m.end()); vector<vector<long long> > dp(n + 1, vector<long long>(k + 1, 0)); vector<vector<vector<long long> > > fr( n + 1, vector<vector<long long> >(k + 1, vector<long long>(n, -1))); for (int i = 1; i <= n; i++) { for (int j = 1; j <= min(k, i); j++) { if (dp[i - 1][j - 1] + m[i - 1].second + (j - 1) * m[i - 1].first > dp[i - 1][j] + (k - 1) * m[i - 1].first) { fr[i][j] = fr[i - 1][j - 1]; fr[i][j][org[m[i - 1]]] = 1; } else { fr[i][j] = fr[i - 1][j]; fr[i][j][org[m[i - 1]]] = 0; } dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + m[i - 1].second + (j - 1) * m[i - 1].first); dp[i][j] = max(dp[i][j], dp[i - 1][j] + (k - 1) * m[i - 1].first); } } vector<int> ber, ne; for (int i = 0; i < n; i++) { if (fr[n][k][org[m[i]]] == 1) ber.push_back(org[m[i]] + 1); else { ne.push_back(org[m[i]] + 1); ne.push_back(-org[m[i]] - 1); } } cout << ber.size() + ne.size() << endl; for (int i = 0; i < ber.size() - 1; i++) { cout << ber[i] << " "; } for (int i = 0; i < ne.size(); i++) { cout << ne[i] << " "; } cout << ber.back() << endl; } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
python3
from bisect import bisect_left as bl from bisect import bisect_right as br from heapq import heappush,heappop import math from collections import * from functools import reduce,cmp_to_key,lru_cache import io, os input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline # import sys # input = sys.stdin.readline M = mod = 10**9 + 7 def factors(n):return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))) def inv_mod(n):return pow(n, mod - 2, mod) def li():return [int(i) for i in input().rstrip().split()] def st():return str(input().rstrip())[2:-1] def val():return int(input().rstrip()) def li2():return [str(i)[2:-1] for i in input().rstrip().split()] def li3():return [int(i) for i in st()] for _ in range(val()): n,k = li() l = [] for i in range(n):l.append(li() + [i]) l.sort(key = lambda x:(-x[0],x[1])) he = [] ans = [] for i in range(k): for j in range(len(he)): he[j][0] += l[i][1] heappush(he,l[i]) ans.append(l[i][2]) maxans = sum(i[0] for i in he) maxiter = ans[:] l.sort(key= lambda x:(-x[1],x[0])) for i in range(k,n): ans.append(-heappop(he)[-1]) currsum = 0 for j in range(len(he)): he[j][0] += l[i][1] currsum += he[j][0] currsum += l[i][0] heappush(he,l[i]) ans.append(l[i][2]) if currsum > maxans: maxans = currsum maxiter = ans[:] for i in range(len(maxiter)): if maxiter[i] < 0: maxiter[i]-=1 else:maxiter[i] += 1 print(len(maxiter)) print(*maxiter)
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 80; const long long inf = (long long)1e18; long long dp[N][N][2]; int from_j[N][N][2]; int from_k[N][N][2]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int tn; cin >> tn; while (tn--) { int n, m; cin >> n >> m; vector<int> a(n), b(n), p(n); for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { for (int k = 0; k <= 1; k++) { dp[i][j][k] = -inf; } } } dp[0][0][0] = 0; memset(from_j, -1, sizeof from_j); memset(from_k, -1, sizeof from_k); for (int i = 0; i < n; i++) { cin >> a[i] >> b[i]; p[i] = i; } sort(p.begin(), p.end(), [&](const int& i, const int& j) { if (b[i] == b[j]) { return a[i] > a[j]; } return b[i] < b[j]; }); for (int i = 0; i < n; i++) { for (int j = 0; j <= m; j++) { for (int k = 0; k < 2; k++) { if (dp[i + 1][j + 1][k] < dp[i][j][k] + j * 1LL * b[p[i]] + a[p[i]]) { dp[i + 1][j + 1][k] = max( dp[i + 1][j + 1][k], dp[i][j][k] + j * 1LL * b[p[i]] + a[p[i]]); from_j[i + 1][j + 1][k] = j; from_k[i + 1][j + 1][k] = k; } if (k == 0) { if (dp[i + 1][j + 1][k + 1] < dp[i][j][k] + (m - 1) * 1LL * b[p[i]] + a[p[i]]) { dp[i + 1][j + 1][k + 1] = max(dp[i + 1][j + 1][k + 1], dp[i][j][k] + (m - 1) * 1LL * b[p[i]] + a[p[i]]); from_j[i + 1][j + 1][k + 1] = j; from_k[i + 1][j + 1][k + 1] = k; } } if (dp[i + 1][j][k] < dp[i][j][k] + (m - 1) * 1LL * b[p[i]]) { dp[i + 1][j][k] = max(dp[i + 1][j][k], dp[i][j][k] + (m - 1) * 1LL * b[p[i]]); from_j[i + 1][j][k] = j; from_k[i + 1][j][k] = k; } } } } int i = n, j = m, k = (m < n); vector<int> keep; vector<int> dont_keep; int last = -1; while (i > 0) { int prev_i = i - 1; int prev_j = from_j[i][j][k]; int prev_k = from_k[i][j][k]; if (prev_j + 1 == j) { if (prev_k + 1 == k) { last = p[i - 1]; } else { keep.push_back(p[i - 1]); } } else { dont_keep.push_back(p[i - 1]); } i = prev_i; j = prev_j; k = prev_k; } reverse(keep.begin(), keep.end()); reverse(dont_keep.begin(), dont_keep.end()); int ans_length = keep.size() + dont_keep.size() * 2 + (last > -1 ? 1 : 0); cout << ans_length << '\n'; for (auto& e : keep) { cout << e + 1 << ' '; } for (auto& e : dont_keep) { cout << e + 1 << ' '; cout << -(e + 1) << ' '; } if (last > -1) { cout << last + 1 << ' '; } cout << '\n'; } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
python3
from bisect import bisect_left as bl from bisect import bisect_right as br from heapq import heappush,heappop import math from collections import * from functools import reduce,cmp_to_key,lru_cache import io, os input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline # import sys # input = sys.stdin.readline M = mod = 10**9 + 7 def factors(n):return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))) def inv_mod(n):return pow(n, mod - 2, mod) def li():return [int(i) for i in input().rstrip().split()] def st():return str(input().rstrip())[2:-1] def val():return int(input().rstrip()) def li2():return [str(i)[2:-1] for i in input().rstrip().split()] def li3():return [int(i) for i in st()] for _ in range(val()): n,k = li() l = [] for i in range(n):l.append(li() + [i]) l.sort(key = lambda x:(-x[0],x[1])) he = [] ans = [] for i in range(k): for j in range(len(he)): he[j][0] += l[i][1] heappush(he,l[i]) ans.append(l[i][2]) maxans = sum(i[0] for i in he) maxiter = ans[:] for i in range(k): ans.append(-heappop(he)[-1]) currsum = 0 for j in range(len(he)): he[j][0] += l[i][1] currsum += he[j][0] currsum += l[i][0] heappush(he,l[i]) ans.append(l[i][2]) if currsum > maxans: maxans = currsum maxiter = ans[:] for i in range(len(maxiter)): if maxiter[i] < 0: maxiter[i]-=1 else:maxiter[i] += 1 print(len(maxiter)) print(*maxiter)
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, k; vector<int> a(n), b(n), id(n); vector<int> L; void solve() { cin >> n >> k; a.resize(n); b.resize(n); id.resize(n); for (int i = 0; i < n; ++i) { cin >> a[i] >> b[i]; id[i] = i; } int ans = 0, ret = 0; auto run = [](int cur, int &ans, int &ret) { for (int i = 0; i < n; ++i) b[i] *= (cur != k ? cur : cur - 1); L.resize(0); sort(id.begin(), id.end(), [](int i, int j) { return a[i] + b[j] > a[j] + b[i]; }); int sum = 0; for (int i = 0; i < n; ++i) { sum += (i < cur ? a[id[i]] : b[id[i]]); if (cur == k && i == 0) continue; L.push_back(id[i] + 1); if (i >= cur) L.push_back(-id[i] - 1); } if (cur == k) L.push_back(id[0] + 1); if (sum > ans) { ans = sum; ret = cur; } for (int i = 0; i <= n; ++i) b[i] /= cur; }; for (int cur = 1; cur <= k; ++cur) run(cur, ans, ret); run(ret, ans, ret); cout << L.size() << '\n'; for (auto i : L) cout << i << ' '; cout << '\n'; } int main() { ios_base::sync_with_stdio(0); int t; cin >> t; while (t--) solve(); return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.StringTokenizer; import java.io.BufferedReader; import java.util.Comparator; import java.util.ArrayDeque; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); SummoningMinions solver = new SummoningMinions(); solver.solve(1, in, out); out.close(); } static class SummoningMinions { public void solve(int testNumber, InputReader in, PrintWriter out) { int T = in.nextInt(); while (T-- > 0) { int N = in.nextInt(); int K = in.nextInt(); int[][] arr = new int[N][3]; for (int i = 0; i < N; i++) { arr[i][0] = in.nextInt(); arr[i][1] = in.nextInt(); arr[i][2] = i + 1; } Arrays.sort(arr, new Comparator<int[]>() { public int compare(int[] ints, int[] t1) { return Integer.compare(ints[1], t1[1]); } }); long[][] dp = new long[N][K + 1]; boolean[][] add = new boolean[N][K + 1]; Arrays.fill(dp[0], Long.MIN_VALUE); dp[0][0] = (K - 1) * arr[0][1]; dp[0][1] = (arr[0][0]); add[0][1] = true; for (int i = 1; i < N; i++) { for (int j = 0; j <= K; j++) { dp[i][j] = dp[i - 1][j] + (K - 1) * arr[i][1]; if (j > 0) { if (dp[i - 1][j - 1] + arr[i][0] + arr[i][1] > dp[i][j]) { add[i][j] = true; dp[i][j] = Math.max(dp[i][j], dp[i - 1][j - 1] + arr[i][0] + arr[i][1] * (j - 1)); } } } } int u = K; ArrayDeque<Integer> order = new ArrayDeque<>(); ArrayList<Integer> not_added = new ArrayList<>(); for (int i = N - 1; i >= 0; i--) { if (add[i][u]) { order.addFirst(arr[i][2]); u--; } else { not_added.add(arr[i][2]); } } StringBuilder str = new StringBuilder(); str.append((2 * N - K) + "\n"); // assert false; while (order.size() > 1) { str.append(order.removeFirst() + " "); } for (int i : not_added) { str.append(i + " "); str.append((-i) + " "); } str.append(order.removeFirst() + " "); out.println(str.toString()); } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
python3
def m(e): return e['m'] def h(e): return e['h'] a = int(input()) for i in range(a): num = [int(x) for x in input().split()] mi = [] for j in range(num[0]): a = [int(x) for x in input().split()] k = {} k['m'] = a[0] k['h'] = a[1] k['i'] = j+1 mi.append(k) mi.sort(key=m,reverse = True) cho = mi nch = mi cho = mi[:num[1]] nch = mi[num[1]:] last = 0 for element in cho[:num[1]-1]: print(element['i'],end = " ") #print() #print(nch) for element in nch: print(element['i'],end = " ") print(-1*element['i'],end = " ") print(cho[num[1]-1]['i'],end = " ") print()
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int dp[76][76]; bool taken[76][76]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t, n, m; cin >> t; while (t--) { cin >> n >> m; vector<pair<pair<int, int>, int>> data(n); for (int i = 0; i < n; ++i) { cin >> data[i].first.second >> data[i].first.first; data[i].second = i + 1; } sort(data.begin(), data.end()); for (int i = 1; i <= n; ++i) { int s = data[i - 1].first.first * (m - 1); int f = data[i - 1].first.second; f -= data[i - 1].first.first; for (int p = 0; p <= m && p <= i; ++p) { dp[i][p] = dp[i - 1][p] + s; taken[i][p] = false; } for (int p = 0; p < m && p < i; ++p) { f += data[i - 1].first.first; if (dp[i - 1][p] + f > dp[i][p + 1]) { dp[i][p + 1] = dp[i - 1][p] + f; taken[i][p + 1] = true; } } } int r = m; int pos = n; vector<int> order, others; while (pos > 0) { if (taken[pos][r]) { order.push_back(data[pos - 1].second); r--; } else others.push_back(data[pos - 1].second); pos--; } reverse(order.begin(), order.end()); int last = order.back(); order.pop_back(); cout << m + (n - m) * 2 << "\n"; for (auto &x : order) cout << x << " "; for (auto &x : others) cout << x << " " << -x << " "; cout << last << "\n"; } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename T> void r1(T &x) { x = 0; char c(getchar()); int f(1); for (; c < '0' || c > '9'; c = getchar()) if (c == '-') f = -1; for (; '0' <= c && c <= '9'; c = getchar()) x = (x * 10) + (c ^ 48); x *= f; } template <typename T, typename... Args> inline void r1(T &t, Args &...args) { r1(t); r1(args...); } const int maxn = 1e2 + 5; const int maxm = maxn << 1; const int mod = 1e9 + 7; typedef int room[maxn]; template <int mod> struct typemod { int z; typemod(int a = 0) : z(a) {} inline int inc(int a, int b) const { return a += b - mod, a + ((a >> 31) & mod); } inline int dec(int a, int b) const { return a -= b, a + ((a >> 31) & mod); } inline int mul(int a, int b) const { return 1ll * a * b % mod; } typemod<mod> operator+(const typemod<mod> &x) const { return typemod(inc(z, x.z)); } typemod<mod> operator-(const typemod<mod> &x) const { return typemod(dec(z, x.z)); } typemod<mod> operator*(const typemod<mod> &x) const { return typemod(mul(z, x.z)); } typemod<mod> &operator+=(const typemod<mod> &x) { *this = *this + x; return *this; } typemod<mod> &operator-=(const typemod<mod> &x) { *this = *this - x; return *this; } typemod<mod> &operator*=(const typemod<mod> &x) { *this = *this * x; return *this; } int operator==(const typemod<mod> &x) const { return x.z == z; } int operator!=(const typemod<mod> &x) const { return x.z != z; } }; int n, k; int f[maxn][maxn]; struct Node { int a, b, id; int operator<(const Node &z) const { return b == z.b ? a > z.a : b < z.b; } } a[maxn], tmp[maxn]; int pre[maxn][maxn], visin[maxn]; int qwq[maxn], qaq[maxn]; int ans1[maxn], ans2[maxn]; signed main() { int i, j, t; r1(t); while (t--) { int ans(0), last(0), C(0); r1(n), r1(k); for (i = 1; i <= n; ++i) r1(a[i].a), r1(a[i].b), a[i].id = i; sort(a + 1, a + n + 1); for (int st = 1; st <= n; ++st) { memset(f, -0x3f, sizeof(f)); for (i = 1; i <= n; ++i) visin[i] = 0; f[0][0] = 0; for (i = 1; i <= n; ++i) { for (j = 0; j <= k - 1; ++j) { if (!j || i == st) { pre[i][j] = 0, f[i][j] = f[i - 1][j]; continue; } int tmp = a[i].b * (j - k); if (f[i - 1][j] > f[i - 1][j - 1] + tmp + a[i].a) f[i][j] = f[i - 1][j], pre[i][j] = 0; else f[i][j] = f[i - 1][j - 1] + tmp + a[i].a, pre[i][j] = 1; } } i = n, j = k - 1; while (i && j) { if (!pre[i][j]) --i; else qaq[j] = i, visin[a[i].id] = 1, --i, --j; } int cnt(0); for (i = 1; i <= n; ++i) if (!visin[i] && i != st) qwq[++cnt] = i; int ct1(0); for (i = 1; i <= cnt; ++i) tmp[++ct1] = a[qwq[i]]; sort(tmp + 1, tmp + ct1 + 1); int res(0); for (i = 1; i <= ct1; ++i) res += (k - 1) * tmp[i].b; for (i = 1; i <= k - 1; ++i) res += a[qaq[i]].a + (j - 1) * a[qaq[i]].b; res += a[st].a + (k - 1) * a[st].b; if (res >= ans) { ans = res, last = st; C = cnt; for (i = 1; i <= k - 1; ++i) ans1[i] = qaq[i]; for (i = 1; i <= cnt; ++i) ans2[i] = qwq[i]; } } printf("%d\n", C * 2 + k); for (i = 1; i <= k - 1; ++i) printf("%d ", a[ans1[i]].id); for (i = 1; i <= C; ++i) printf("%d %d ", a[ans2[i]].id, -a[ans2[i]].id); printf("%d\n", a[last].id); } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
java
import java.io.*; import java.text.*; import java.util.*; import java.math.*; public class template { public static void main(String[] args) throws Exception { new template().run(); } public void run() throws Exception { FastScanner f = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int asdf = f.nextInt(); while(asdf-->0) { int n = f.nextInt(), k = f.nextInt(); int[] a = new int[n], b = new int[n]; for(int i = 0; i < n; i++) { a[i] = f.nextInt(); b[i] = f.nextInt(); } int best = -1; int[] bmoves = new int[0]; int[] temp = new int[4]; for(int end = 0; end < n; end++) { int[] moves = new int[2*n-k]; int mc = 0; ArrayList<Pair> al = new ArrayList<>(); int ans = 0; boolean[] used = new boolean[n]; used[end] = true; for(int qwer = 0;qwer < n-1; qwer++) { int min = -1; for(int i = 0; i < n; i++) { if(used[i]) continue; if(min == -1) min = i; else { int v1, v2; { v1 = a[min]+a[i]+b[min]; temp[0] = a[min]; temp[1] = a[i]+b[min]; if(al.size() >= 1) temp[2] = al.get(0).a+b[i]+b[min]; if(al.size() >= 2) temp[3] = al.get(1).a+b[i]+b[min]; Arrays.sort(temp); for(int j = 0; al.size()+2-j >= k; j++) v1 -= temp[j]; } { v2 = a[min]+a[i]+b[i]; temp[0] = a[min]+b[i]; temp[1] = a[i]; if(al.size() >= 1) temp[2] = al.get(0).a+b[i]+b[min]; if(al.size() >= 2) temp[3] = al.get(1).a+b[i]+b[min]; Arrays.sort(temp); for(int j = 0; al.size()+2-j >= k; j++) v2 -= temp[j]; } if(v2 < v1) min = i; } } used[min] = true; moves[mc++] = min+1; boolean add = false; for(int i = 0; i < al.size(); i++) { if(!add && al.get(i).a+b[min] > a[min]) { al.add(i,new Pair(a[min],min)); add = true; } else al.get(i).a += b[min]; } if(!add) al.add(new Pair(a[min],min)); ans += a[min] + (al.size()-1)*b[min]; if(al.size() >= k) { Pair p = al.remove(0); moves[mc++] = -(p.b+1); ans -= p.a; } } ans += a[end] + (k-1)*b[end]; moves[mc] = end+1; if(ans > best) { best = ans; bmoves = moves; } } out.println(bmoves.length); for(int i : bmoves) out.print(i + " "); out.println(); } out.flush(); } class Pair implements Comparable<Pair> { int a, b; public Pair(int a, int b) { this.a = a; this.b = b; } public int compareTo(Pair p) { return Integer.compare(a, p.a); } public String toString() { return a + "," + b; } } static class FastScanner { public BufferedReader reader; public StringTokenizer tokenizer; public FastScanner() { reader = new BufferedReader(new InputStreamReader(System.in), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { try { return reader.readLine(); } catch(IOException e) { throw new RuntimeException(e); } } } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline long long mod(long long n, long long m) { long long ret = n % m; if (ret < 0) ret += m; return ret; } long long gcd(long long a, long long b) { return (b == 0LL ? a : gcd(b, a % b)); } long long exp(long long a, long long b, long long m) { if (b == 0LL) return 1LL; if (b == 1LL) return mod(a, m); long long k = mod(exp(a, b / 2, m), m); if (b & 1LL) { return mod(a * mod(k * k, m), m); } else return mod(k * k, m); } const long long N = 80; struct coisa { long long a, b, id; coisa() {} coisa(long long A, long long B, long long C) { a = A, b = B, id = C; } } v[N]; long long dp[N][N]; long long n, k; long long solve(long long id, long long j) { if (id == n + 1) { return 0; } long long& x = dp[id][j]; if (x != -1) return x; long long nao = solve(id + 1, j) + (k - 1) * v[id].b; if (j == k) return x = nao; long long pega = solve(id + 1, j + 1) + v[id].b * (j) + v[id].a; return x = max(pega, nao); } vector<long long> tira, col; void rool(long long id, long long j) { if (id == n + 1) return; if (j == k) { tira.push_back(id); rool(id + 1, j); return; } if (solve(id, j) == solve(id + 1, j) + (k - 1) * v[id].b) { tira.push_back(id); rool(id + 1, j); return; } col.push_back(id); rool(id + 1, j + 1); return; } void solve() { cin >> n >> k; for (long long i = 1; i <= n; i++) { long long a, b; cin >> a >> b; v[i] = coisa(a, b, i); } sort(v + 1, v + 1 + n, [&](coisa a, coisa b) { return a.b < b.b; }); if (k == 1) { sort(v + 1, v + 1 + n, [&](coisa a, coisa b) { return a.a > b.a; }); cout << 1 << "\n"; cout << v[1].id << "\n"; return; } if (k == n) { cout << n << "\n"; for (long long i = 1; i <= n; i++) { cout << v[i].id << " "; } cout << "\n"; return; } memset(dp, -1, sizeof(dp)); tira.clear(); col.clear(); solve(1, 0); rool(1, 0); vector<long long> ans; for (long long i = 0; i < (long long)col.size() - 1; i++) ans.push_back(v[col[i]].id); for (long long x : tira) ans.push_back(v[x].id), ans.push_back(-v[x].id); if ((long long)col.size() - 1 >= 0) ans.push_back(v[col[(long long)col.size() - 1]].id); cout << ans.size() << "\n"; for (long long x : ans) cout << x << " "; cout << "\n"; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long t; cin >> t; while (t--) { solve(); } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct minion { int a, b, id; }; bool sorta(minion x, minion y) { return x.a > y.a; } int n, k; minion ar[100]; pair<int, vector<minion>> calc(int except) { int cur = ar[except].a + (k - 1) * ar[except].b; vector<minion> v; for (int i = 0; i < n; ++i) if (i != except) v.push_back(ar[i]); sort(v.begin(), v.end(), sorta); int cnt = n; while (cnt--) { for (int i = 0; i < v.size() - 1; ++i) { int val1 = v[i].b * min(i, k - 1) + v[i + 1].b * min(i + 1, k - 1); if (i < k - 1) val1 += v[i].a; if (i + 1 < k - 1) val1 += v[i + 1].a; int val2 = v[i + 1].b * min(i, k - 1) + v[i].b * min(i + 1, k - 1); if (i < k - 1) val2 += v[i + 1].a; if (i + 1 < k - 1) val2 += v[i].a; if (val2 > val1) swap(v[i], v[i + 1]); } } for (int i = 0; i < v.size(); ++i) cur += v[i].b * min(i, k - 1) + v[i].a * (i < k - 1); v.push_back(ar[except]); return {cur, v}; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t--) { cin >> n >> k; for (int i = 0; i < n; ++i) { cin >> ar[i].a >> ar[i].b; ar[i].id = i; } pair<int, vector<minion>> ans; ans.first = -1e9; for (int i = 0; i < n; ++i) { pair<int, vector<minion>> neww = calc(i); if (neww.first > ans.first) ans = neww; } cout << n + (n - k) << '\n'; for (int i = 0; i < k - 1; ++i) cout << ans.second[i].id + 1 << ' '; for (int i = k - 1; i < n - 1; ++i) cout << ans.second[i].id + 1 << " -" << ans.second[i].id + 1 << ' '; cout << ans.second[n - 1].id + 1 << '\n'; } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename T> inline bool upmin(T &x, T y) { return y < x ? x = y, 1 : 0; } template <typename T> inline bool upmax(T &x, T y) { return x < y ? x = y, 1 : 0; } const long double eps = 1e-11; const long double pi = acos(-1); const int oo = 1 << 30; const long long loo = 1ll << 62; const int mods = 1e9 + 7; const int MAXN = 5005; const int INF = 0x3f3f3f3f; inline int read() { int f = 1, x = 0; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 3) + (x << 1) + (c ^ 48); c = getchar(); } return x * f; } int a[55], b[55], f[55][55], flag[55], frm[55][55]; vector<int> V; signed main() { int Case = read(); while (Case--) { int n = read(), k = read(); for (int i = 1; i <= n; i++) a[i] = read(), b[i] = read(); memset(f, INF, sizeof f); memset(frm, 0, sizeof frm); memset(flag, 0, sizeof flag); V.clear(); f[0][0] = 0; for (int i = 1; i <= n; i++) for (int j = 0; j <= k; j++) { if (j && upmin(f[i][j], f[i - 1][j - 1] + a[i] + b[i] * (j - 1))) frm[i][j] = 1; if (upmin(f[i][j], f[i - 1][j] + b[i] * (k - 1))) frm[i][j] = 2; } int nw = k; for (int i = n; i >= 1; i--) if (frm[i][nw] == 1) V.push_back(i), nw--; else flag[i] = 1; sort(V.begin(), V.end(), [&](int x, int y) { return b[x] < b[y]; }); printf("%d\n", V.size() + (n - V.size()) * 2); for (int i = 0; i < V.size() - 1; i++) printf("%d ", V[i]); for (int i = 1; i <= n; i++) if (flag[i]) printf("%d -%d ", i, i); printf("%d\n", V[V.size() - 1]); } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; template <typename T1, typename T2> inline void chkmin(T1 &x, const T2 &y) { if (x > y) x = y; } template <typename T1, typename T2> inline void chkmax(T1 &x, const T2 &y) { if (x < y) x = y; } struct fa { int a, b, ind; fa() {} }; int n, k; const int MAXN = 76; fa a[MAXN]; const int INF = 1e9; int dp[MAXN][MAXN][2]; int par[MAXN][MAXN][2]; void relax(int pref, int cnt, int ind, int val, int fpar) { if (dp[pref][cnt][ind] >= val) return; dp[pref][cnt][ind] = val; par[pref][cnt][ind] = fpar; } void solve() { cin >> n >> k; for (int i = 0; i < n; i++) { cin >> a[i].a >> a[i].b; a[i].ind = i + 1; } sort(a, a + n, [&](fa i, fa j) { return i.b < j.b; }); if (k == 1) { int pos = max_element(a, a + n, [&](fa i, fa j) { return i.a < j.a; }) - a; cout << 1 << "\n" << a[pos].ind << "\n"; return; } if (k == n) { cout << n << "\n"; for (int i = 0; i < n; i++) { cout << a[i].ind << " "; } cout << "\n"; return; } for (int i = 0; i < MAXN; i++) { for (int j = 0; j < MAXN; j++) { for (int k = 0; k < 2; k++) { dp[i][j][k] = -INF; } } } dp[0][0][0] = 0; for (int i = 0; i < n; i++) { for (int cnt = 0; cnt < k - 1; cnt++) { for (int flag = 0; flag < 2; flag++) { if (dp[i][cnt][flag] == -INF) continue; relax(i + 1, cnt + 1, flag, dp[i][cnt][flag] + a[i].a + a[i].b * cnt, 1); relax(i + 1, cnt, flag, dp[i][cnt][flag] + a[i].b * (k - 1), 2); if (!flag) { relax(i + 1, cnt, 1, dp[i][cnt][flag] + a[i].a + a[i].b * (k - 1), 3); } } } } int pos = 0, cnt = 0, flag = 0; for (int i = 0; i < MAXN; i++) { for (int j = 0; j < MAXN; j++) { for (int k = 0; k < 2; k++) { if (dp[i][j][k] > dp[pos][cnt][flag]) { pos = i, cnt = j, flag = k; } } } } cerr << "my_ans = " << dp[pos][cnt][flag] << endl; vector<pair<int, int>> ans; for (; pos >= 1; pos--) { ans.push_back({par[pos][cnt][flag], a[pos - 1].ind}); int ncnt = cnt; if (par[pos][cnt][flag] == 1) ncnt--; int nflag = flag; if (par[pos][cnt][flag] == 3) nflag--; cnt = ncnt; flag = nflag; } sort(ans.begin(), ans.end()); vector<int> fout; for (auto i : ans) { fout.push_back(i.second); if (i.first == 2) { fout.push_back(-i.second); } } cout << fout.size() << "\n"; for (auto i : fout) { cout << i << " "; } cout << "\n"; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { solve(); } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int w, n, k, dp[77][77], l[77][77], pos; bool v[77]; vector<pair<pair<int, int>, int> > sz; vector<int> sol; int main() { cin >> w; while (w--) { cin >> n >> k, pos = k; for (int i = 1; i <= n; i++) v[i] = 0; sol.clear(), sz.clear(); for (int i = 1; i <= n; i++) { int a, b; cin >> a >> b; sz.push_back({{b, a}, i}); } sort(sz.begin(), sz.end()); for (int i = 0; i < n; i++) { for (int j = 1; j <= i + 1; j++) { int fi = sz[i].first.first, se = sz[i].first.second, id = sz[i].second, ans = dp[i][j - 1] + se + (i - n) * fi; dp[i + 1][j] = dp[i][j], l[i + 1][j] = l[i][j]; if (j > i || ans > dp[i + 1][j]) dp[i + 1][j] = ans, l[i + 1][j] = id; } } for (int i = n - 1; i >= 0; i--) if (l[i + 1][pos] == sz[i].second) pos--, sol.push_back(sz[i].second), v[sz[i].second] = 1; cout << 2 * n - k << "\n"; for (int i = k - 1; i >= 1; i--) cout << sol[i] << " "; for (int i = 1; i <= n; i++) if (!v[i]) cout << i << " " << -i << " "; cout << sol[0] << "\n"; } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update using namespace std; using namespace __gnu_pbds; //typedef tree< // pair<long long, int>, // change type // null_type, // less<pair<long long, int> >, // change type // rb_tree_tag, // tree_order_statistics_node_update> // ordered_set; template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; vector<string> vec_splitter(string s) { s += ','; vector<string> res; while(!s.empty()) { res.push_back(s.substr(0, s.find(','))); s = s.substr(s.find(',') + 1); } return res; } void debug_out( vector<string> __attribute__ ((unused)) args, __attribute__ ((unused)) int idx, __attribute__ ((unused)) int LINE_NUM) { cerr << endl; } template <typename Head, typename... Tail> void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) { if(idx > 0) cerr << ", "; else cerr << "Line(" << LINE_NUM << ") "; stringstream ss; ss << H; cerr << args[idx] << " = " << ss.str(); debug_out(args, idx + 1, LINE_NUM, T...); } #define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__) typedef long long ll; #define rep(i, start, end) for(int i = start; i < end; ++i) #define sz(x) (int)((x).size()) #define pb push_back #define X first #define Y second #define all(x) x.begin(), x.end() #define clr(d, v) memset(d, v, sizeof(d)) #define pii pair<int, int> //#define debug(x) cerr << #x << " : " << (x) << " " const double PI = 3.14159265358979323846; const double eps = (1e-10); int isGreater(long double x, long double y) { if (abs(x - y) < eps) return 0; if (x > y) return 1; return -1; } const int N = 80; struct S{ int a, b, idx; bool operator<(const S& other) const{ return b < other.b; } }; S v[N]; int n, k; vector<int> o, d; int lst; ll mem[N][N][2]; ll solve(int i, int c, int l) { if (i == n){ if (c == k - 1 && l) return 0; return -1e17; } ll &ret = mem[i][c][l]; if (ret != -1) return -1; ret = solve(i +1, c, l) + (k - 1) * v[i].b; if (c < k - 1) ret = max(ret, solve(i + 1, c + 1, l) + v[i].a + c * v[i].b); if (!l) ret = max(ret, solve(i + 1, c, true) + v[i].a + v[i].b * (k - 1)); return ret; } void build(int i, int c, int l) { if (i == n){ return; } ll &ret = mem[i][c][l]; if (ret == solve(i +1, c, l) + (k - 1) * v[i].b) { d.pb(v[i].idx); build(i + 1, c, l); return; } if (c < k - 1) { if (ret == solve(i + 1, c + 1, l) + v[i].a + c * v[i].b){ o.pb(v[i].idx); build(i + 1, c + 1, l); return; } } lst = v[i].idx; build(i + 1, c, true); } void SOLVE(){ cin >> n >> k; rep(i,0,n){ cin >> v[i].a >> v[i].b; v[i].idx = i + 1; } sort(v, v + n); clr(mem, -1); d.clear(); o.clear(); solve(0, 0, 0); build(0 ,0 , 0); cout << sz(o) + sz(d) * 2 + 1 << '\n'; for (int item : o) cout << item << " "; for (int item : d) cout << item << " " << -item << " "; cout << lst << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); // freopen("AC.txt", "w", stdout); #endif int tc; cin >> tc; while(tc--) SOLVE(); return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 2e9; int g[80][80]; int n, k, a[80], b[80]; int job[80]; int u[80], v[80]; bool used[80]; int dist[80]; int par[80]; void hungarian() { for (int i = 1; i <= n; i++) { job[0] = i; int j0 = 0; for (int l = 0; l < 80; l++) { used[l] = 0; dist[l] = INF; par[l] = 79; } while (job[j0]) { used[j0] = 1; int i0 = job[j0], d = INF, j1 = 79; for (int j = 1; j <= n; j++) { if (used[j]) continue; int w = g[i0][j] - u[i0] - v[j]; if (w < dist[j]) { dist[j] = w; par[j] = j0; } if (dist[j] < d) { d = dist[j]; j1 = j; } } for (int j = 1; j <= n; j++) { if (used[j]) { u[job[j]] += d; v[j] -= d; } else dist[j] -= d; } j0 = j1; } while (j0) { int j1 = par[j0]; job[j0] = job[j1]; j0 = j1; } } } void solve() { memset(g, 0, sizeof(g)); memset(u, 0, sizeof(u)); memset(v, 0, sizeof(v)); memset(job, 0, sizeof(job)); cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> a[i] >> b[i]; } for (int i = 1; i <= n; i++) { g[1][i] = a[i] + b[i] * (k - 1); for (int j = 2; j <= k; j++) { g[j][i] = a[i] + (j - 1) * b[i]; } for (int j = k + 1; j <= n; j++) { g[j][i] = b[i] * (k - 1); } } for (int i1 = 1; i1 <= n; i1++) for (int i2 = 1; i2 <= n; i2++) { g[i1][i2] = -g[i1][i2]; } hungarian(); vector<int> ans; for (int idx = 2; idx <= k; idx++) { for (int i = 1; i <= n; i++) { if (job[i] == idx) ans.push_back(i); } } for (int idx = k + 1; idx <= n; idx++) { for (int i = 1; i <= n; i++) { if (job[i] == idx) { ans.push_back(i); ans.push_back(-i); } } } for (int i = 1; i <= n; i++) { if (job[i] == 1) { ans.push_back(i); } } cout << ans.size() << endl; for (auto xd : ans) cout << xd << " "; cout << endl; } int main() { int t; cin >> t; while (t--) solve(); return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; #ifndef DEBUG #define endl '\n' #define debug(...) #else #define debug(...) __VA_ARGS__ #endif #define debugf(...) debug(printf(__VA_ARGS__);) const int oo = 0x3f3f3f3f; const int N = 128; int dp[N][N]; int pick[N][N]; struct Minion { int a, b, i; }; pair<vector<int>, vector<int>> solve(vector<Minion>& minions, int k) { int n = minions.size(); memset(dp, -oo, sizeof(dp)); sort(minions.begin(), minions.end(), [&](const auto& x, const auto& y) { return x.b < y.b; }); dp[0][0] = 0; for (int i = 1; i <= n; i++) { auto [a, b, _] = minions[i-1]; dp[0][i] = (k - 1) * b; pick[0][i] = 0; } for (int i = 1; i <= n; i++) { for (int p = 1; p <= k; p++) { auto [a, b, _] = minions[i-1]; int pickF = dp[p-1][i-1] + (p-1)*b + a; int pickU = dp[p][i-1] + (k-1)*b; // Maybe? if (dp[p-1][i-1] == -oo) pickF = -oo; if (dp[p][i-1] == -oo) pickU = -oo; if (pickF > pickU) { pick[p][i] = p-1; dp[p][i] = pickF; } else { pick[p][i] = p; dp[p][i] = pickU; } } } debugf("ans=%d\n", dp[k][n]); vector<int> f; f.reserve(k); vector<int> u; u.reserve(n - k); int p = k; for (int i = n; i >= 1; i--) { if (pick[p][i] == p-1) { f.emplace_back(minions[i-1].i); } else { u.emplace_back(minions[i-1].i); } p = pick[p][i]; } assert(p == 0); reverse(f.begin(), f.end()); return { f, u }; } signed (((((main)))))() <% ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { int n, k; cin >> n >> k; vector<Minion> minions(n); for (int i = 1; i <= n; i++) { auto& [a, b, j] = minions[i-1]; cin >> a >> b; j = i; } auto [f, u] = solve(minions, k); assert(f.size() + 2 * u.size() == k + 2 * (n - k)); cout << f.size() + 2 * u.size() << endl; for (int i = 0; i < k-1; i++) cout << f[i] << ' '; for (int i : u) cout << i << ' ' << -i << ' '; cout << f.back() << endl; } return 0; %>
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC target("avx2") #pragma GCC optimization("O3") #pragma GCC optimization("unroll-loops") using namespace std; template <typename T> void DBG(const char* name, T&& H) { cerr << name << " = " << H << ')' << '\n'; } template <typename T, typename... Args> void DBG(const char* names, T&& H, Args&&... args) { const char* NEXT = strchr(names + 1, ','); cerr.write(names, NEXT - names) << " = " << H << " |"; DBG(NEXT + 1, args...); } using ll = long long; using ld = long double; const ll mod = 1e9 + 7; const ld PI = acos(-1.0); const ll maxN = 1e5 + 1; struct Hungarian { using T = int; int N; vector<vector<T>> cost; vector<int> Lmate, Rmate; T minCost = 0; Hungarian(int n = 1e2) { N = n; cost.resize(n, vector<T>(n)); Lmate.resize(n, -1); Rmate.resize(n, -1); } void compute() { vector<T> a(N), b(N); for (int i = 0; i < N; i++) { a[i] = cost[i][0]; for (int j = 1; j < N; j++) { a[i] = min(a[i], cost[i][j]); } } for (int i = 0; i < N; i++) { b[i] = cost[0][i] - a[i]; for (int j = 1; j < N; j++) { b[i] = min(b[i], cost[j][i] - a[j]); } } int matched = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (Rmate[j] != -1) continue; if (abs(cost[i][j] - a[i] - b[j]) == 0) { Lmate[i] = j; Rmate[j] = i; matched++; break; } } } vector<T> dist(N); vector<int> dad(N), seen(N); while (matched < N) { int s = 0; while (Lmate[s] != -1) s++; fill(dad.begin(), dad.end(), -1); fill(seen.begin(), seen.end(), 0); for (int k = 0; k < N; k++) { dist[k] = cost[s][k] - a[s] - b[k]; } int j = 0; while (true) { j = -1; for (int k = 0; k < N; k++) { if (seen[k]) continue; if ((j == -1) || (dist[k] < dist[j])) j = k; } seen[j] = 1; if (Rmate[j] == -1) break; int i = Rmate[j]; for (int k = 0; k < N; k++) { if (seen[k]) continue; int newDist = dist[j] + cost[i][k] - a[i] - b[k]; if (dist[k] > newDist) { dist[k] = newDist; dad[k] = j; } } } for (int k = 0; k < N; k++) { if ((k == j) || (!seen[k])) continue; int i = Rmate[k]; a[i] -= dist[k] - dist[j]; b[k] += dist[k] - dist[j]; } a[s] += dist[j]; while (dad[j] >= 0) { int d = dad[j]; Rmate[j] = Rmate[d]; Lmate[Rmate[j]] = j; j = d; } Rmate[j] = s; Lmate[s] = j; matched++; } for (int i = 0; i < N; i++) { minCost += cost[i][Lmate[i]]; } } }; void Solve() { int n, k; cin >> n >> k; Hungarian order(n); vector<int> a(n), b(n); for (int i = 0; i < n; i++) { cin >> a[i] >> b[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j < k - 1; j++) { order.cost[i][j] = -(a[i] + j * b[i]); } for (int j = k - 1; j < n - 1; j++) { order.cost[i][j] = -((k - 1) * b[i]); } order.cost[i][n - 1] = -(a[i] + (k - 1) * b[i]); } order.compute(); cout << k + 2 * (n - k) << '\n'; for (int i = 0; i < k - 1; i++) { cout << order.Rmate[i] + 1 << " "; } for (int i = k - 1; i < n - 1; i++) { cout << order.Rmate[i] + 1 << " " << -(order.Rmate[i] + 1) << " "; } cout << order.Rmate[n - 1] + 1 << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int tt = 1; cin >> tt; while (tt--) { Solve(); } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#pragma optimization_level 3 #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //#pragma GCC target("avx,avx2,fma") #include<bits/stdc++.h> #define F first #define S second #define vec vector #define pb push_back #define pll pair<ll,ll> #define pdd pair<ld, ld> #define pq priority_queue #define umap unordered_map #define uset unordered_set #define pii pair<int, int> #define pnn pair<Node*, Node*> #define uid uniform_int_distribution #define FILE ifstream in("input.txt");ofstream out("output.txt"); #define fast cin.tie(0);cout.tie(0);cin.sync_with_stdio(0);cout.sync_with_stdio(0); using namespace std; typedef string str; typedef long long ll; typedef long double ld; typedef unsigned int uint; typedef unsigned long long ull; mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); void solve(){ int a,k; cin>>a>>k; vec<pair<pll, ll>> m(a+1); for(int q=1; q<=a; q++) { cin >> m[q].F.F >> m[q].F.S; m[q].S = q; } sort(m.begin(), m.end(), [](auto &p1, auto &p2){return p1.F.S<p2.F.S;}); vec<vec<ll>> dp(a+1, vec<ll>(k+1)), take(a+1, vec<ll>(k+1)); for(int q=1; q<=a; q++){ for(int w=0; w<=min(q,k); w++){ ll go = w==0 ? -1 : dp[q-1][w-1] + m[q].F.F + (w-1)*m[q].F.S; ll ne_go = w==q ? -1 : dp[q-1][w] + (k-1)*m[q].F.S; if(w==q) assert(go > ne_go); if(w==0) assert(ne_go > go); if(go > ne_go) take[q][w] = 1; dp[q][w] = max(go, ne_go); } } vec<ll> ord, loxi; ll cp = a, ck = k; for(; cp>0;){ if(take[cp][ck]){ ord.pb(cp); ck--; } else loxi.pb(cp); cp--; } assert(ord.size()==k); reverse(ord.begin(), ord.end()); ll lst = ord.back(); ord.pop_back(); vec<ll> o; for(ll i : ord) o.pb(m[i].S); for(ll i : loxi){ o.pb(m[i].S); o.pb(-m[i].S); } o.pb(m[lst].S); cout<<o.size()<<"\n"; for(ll i : o) cout<<i<<" "; cout<<"\n"; } int main() { fast; int z; cin>>z; for(; z--;){ solve(); } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long INF = (long long)1e18; const int mod = (int)1e9 + 7; const int MAXN = (int)3e5 + 5; pair<pair<int, int>, int> cr[MAXN]; int dp[80][80]; int pre[MAXN]; int par[80][80]; void solve() { int n, k; cin >> n >> k; for (int i = 1; i <= n; i++) { int a, b; cin >> a >> b; cr[i] = {{b, a}, i}; } sort(cr + 1, cr + n + 1); for (int i = 1; i <= n; i++) { pre[i] = pre[i - 1] + cr[i].first.first; } memset(dp, 128, sizeof(dp)); memset(par, -1, sizeof(par)); dp[0][0] = 0; int ans = -mod; int lans = -mod; for (int i = 1; i <= n; i++) { for (int j = 1; j <= k; j++) { for (int l = 0; l < i; l++) { int deg = (pre[i - 1] - pre[l]) * (k - 1) + dp[l][j - 1] + cr[i].first.first * (j - 1) + cr[i].first.second; if (deg > dp[i][j]) { dp[i][j] = deg; par[i][j] = l; } } } int deg = dp[i][k] + (pre[n] - pre[i]) * (k - 1); if (deg > ans) { ans = deg; lans = dp[i][k]; } } vector<int> dis; vector<int> ic; cout << 2 * n - k << '\n'; int cur = k; for (int i = n; i >= 1; i--) { if (dp[i][cur] == lans) { lans = dp[par[i][cur]][cur - 1]; cur--; ic.push_back(cr[i].second); } else { dis.push_back(cr[i].second); } } reverse(ic.begin(), ic.end()); for (int i = 0; i < (int)ic.size() - 1; i++) { cout << ic[i] << " "; } for (int i = 0; i < (int)dis.size(); i++) { cout << dis[i] << " " << -dis[i] << " "; } cout << ic.back() << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int t; cin >> t; while (t--) solve(); }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
python3
import sys readline = sys.stdin.readline read = sys.stdin.read ns = lambda: readline().rstrip() ni = lambda: int(readline().rstrip()) nm = lambda: map(int, readline().split()) nl = lambda: list(map(int, readline().split())) prn = lambda x: print(*x, sep='\n') def solve(): n, k = nm() mini = [tuple(nl() + [i+1]) for i in range(n)] mini.sort(key = lambda x: -x[1]*(10**5 + 1) -x[0]) # print(mini) dp = [-10**9]*(k+1) dp[0] = 0 f = [[0]*(k+1) for _ in range(n+1)] for i in range(n): for j in range(k-1, -1, -1): if dp[j+1] < dp[j] + mini[i][0] + (k - j - 1)*mini[i][1]: dp[j+1] = dp[j] + mini[i][0] + (k - j - 1)*mini[i][1] f[i+1][j+1] = 1 if dp[j] < dp[j] + (k - 1) * mini[i][1]: dp[j] = dp[j] + (k - 1) * mini[i][1] # print(dp) cx = k a = list() b = list() for i in range(n, 0, -1): if f[i][cx]: a.append(mini[i-1][2]) cx -= 1 else: if mini[i-1][1]: b.append(mini[i-1][2]) com = list() for x in a[:-1]: com.append(x) for x in b: com.append(x) com.append(-x) com.append(a[-1]) print(len(com)) print(*com) return # solve() T = ni() for _ in range(T): solve()
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 2e9; int solve() { int n, k; cin >> n >> k; vector<pair<pair<int, int>, int>> arr(n); for (int i = 0; i < n; i++) cin >> arr[i].first.second >> arr[i].first.first, arr[i].second = i + 1; sort(arr.begin(), arr.end()); vector<int> a(n), b(n); for (int i = 0; i < n; i++) a[i] = arr[i].first.second, b[i] = arr[i].first.first; vector<vector<int>> dp(n + 1, vector<int>(k + 1, -INF)); vector<vector<bool>> op(n + 1, vector<bool>(k + 1, 0)); dp[0][0] = 0; for (int i = 0; i < n; i++) { dp[i + 1][0] = dp[i][0] + (k - 1) * b[i]; for (int j = 1; j < k + 1; j++) { int v1 = dp[i][j] + (k - 1) * b[i], v2 = dp[i][j - 1] + (j - 1) * b[i] + a[i]; if (v2 > v1) op[i + 1][j] = 1; dp[i + 1][j] = max(v1, v2); } } cout << dp[n][k] << endl; int curj = k; vector<int> maj, bon; for (int i = n; i; i--) { int oo = op[i][curj]; if (oo) { maj.push_back(i - 1); curj--; } else { bon.push_back(i - 1); } } cout << n + n - k << endl; for (int i = k - 1; i >= 1; i--) cout << arr[maj[i]].second << " "; for (auto i : bon) cout << arr[i].second << " " << -arr[i].second << " "; cout << arr[maj[0]].second << endl; return 0; } int32_t main() { ios_base::sync_with_stdio(false); int t; cin >> t; for (int i = 0; i < t; i++) solve(); return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.StringTokenizer; import java.io.BufferedReader; import java.util.Comparator; import java.util.ArrayDeque; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); SummoningMinions solver = new SummoningMinions(); solver.solve(1, in, out); out.close(); } static class SummoningMinions { public void solve(int testNumber, InputReader in, PrintWriter out) { int T = in.nextInt(); while (T-- > 0) { int N = in.nextInt(); int K = in.nextInt(); int[][] arr = new int[N][3]; for (int i = 0; i < N; i++) { arr[i][0] = in.nextInt(); arr[i][1] = in.nextInt(); arr[i][2] = i + 1; } Arrays.sort(arr, new Comparator<int[]>() { public int compare(int[] ints, int[] t1) { return Integer.compare(ints[1], t1[1]); } }); long[][] dp = new long[N][K + 1]; boolean[][] add = new boolean[N][K + 1]; Arrays.fill(dp[0], Long.MIN_VALUE); dp[0][0] = (K - 1) * arr[0][1]; dp[0][1] = (arr[0][0]); add[0][1] = true; for (int i = 1; i < N; i++) { for (int j = 0; j <= K; j++) { dp[i][j] = dp[i - 1][j] + (K - 1) * arr[i][1]; if (j > 0) { if (dp[i - 1][j - 1] + arr[i][0] + arr[i][1] * (j - 1) > dp[i][j]) { add[i][j] = true; dp[i][j] = dp[i - 1][j - 1] + arr[i][0] + arr[i][1]; } } } } int u = K; ArrayDeque<Integer> order = new ArrayDeque<>(); ArrayList<Integer> not_added = new ArrayList<>(); for (int i = N - 1; i >= 0; i--) { if (add[i][u]) { order.addFirst(arr[i][2]); u--; } else { not_added.add(arr[i][2]); } } StringBuilder str = new StringBuilder(); str.append((2 * N - K) + "\n"); // assert false; while (order.size() > 1) { str.append(order.removeFirst() + " "); } for (int i : not_added) { str.append(i + " "); str.append((-i) + " "); } str.append(order.removeFirst() + " "); out.println(str.toString()); } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename T> inline bool upmin(T &x, T y) { return y < x ? x = y, 1 : 0; } template <typename T> inline bool upmax(T &x, T y) { return x < y ? x = y, 1 : 0; } const long double eps = 1e-11; const long double pi = acos(-1); const int oo = 1 << 30; const long long loo = 1ll << 62; const int mods = 1e9 + 7; const int MAXN = 5005; const int INF = 0x3f3f3f3f; inline int read() { int f = 1, x = 0; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 3) + (x << 1) + (c ^ 48); c = getchar(); } return x * f; } int a[105], b[105], f[105][105], flag[105], frm[105][105], id[105]; vector<int> V; signed main() { int Case = read(); while (Case--) { memset(f, -INF, sizeof f); memset(frm, 0, sizeof frm); memset(flag, 0, sizeof flag); int n = read(), k = read(); for (int i = 1; i <= n; i++) a[i] = read(), b[i] = read(), id[i] = i; sort(id + 1, id + n + 1, [&](int x, int y) { return b[x] < b[y]; }); V.clear(); f[0][0] = 0; for (int i = 1; i <= n; i++) for (int j = 0; j <= k; j++) { if (j && upmax(f[i][j], f[i - 1][j - 1] + a[id[i]] + b[id[i]] * (j - 1))) frm[i][j] = 1; if (upmax(f[i][j], f[i - 1][j] + b[id[i]] * (k - 1))) frm[i][j] = 2; } int nw = k; for (int i = n; i >= 1; i--) if (frm[i][nw] == 1) V.push_back(id[i]), nw--; else flag[i] = 1; for (int i = 0; i < V.size() - 1; i++) printf("%d ", V[i]); for (int i = 1; i <= n; i++) if (flag[i]) printf("%d -%d ", i, i); printf("%d\n", V[V.size() - 1]); } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int f[505][505], g[505][505], a[505], b[505], id[505], xinyue, n, m, vis[505], ed; int cmp(int x, int y) { return b[x] < b[y]; } void dfs(int n, int m) { if (!n) return; if (g[n][m] == 1) vis[n] = 1, m--; dfs(n - 1, m); } int main() { scanf("%d", &xinyue); while (xinyue--) { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d%d", &a[i], &b[i]), id[i] = i; sort(id + 1, id + n + 1, cmp); for (int i = 1; i <= n; i++) { for (int t = 0; t <= min(i, m); t++) { f[i][t] = f[i - 1][t] + b[id[i]] * (m - 1); g[i][t] = 0; if (t && f[i - 1][t - 1] + a[id[i]] + b[id[i]] * (t - 1) > f[i][t]) { f[i][t] = f[i - 1][t - 1] + a[id[i]] + b[id[i]] * (t - 1); g[i][t] = 1; } } } dfs(n, m); printf("%d\n", m + (n - m) * 2); ed = 0; for (int i = 1; i <= n; i++) { if (vis[i]) { ed++; if (ed == m) { ed = id[i]; break; } printf("%d ", id[i]); } } for (int i = 1; i <= n; i++) { if (!vis[i]) { printf("%d %d ", id[i], -id[i]); } } printf("%d\n", ed); } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int a[76], b[76]; pair<pair<int, int>, int> p[76]; long long dp[76][76]; bool vis[76][76]; int pv1[76][76], pv2[76][76]; bool ok[76]; int main() { int t; scanf("%d", &t); while (t--) { int n, k; scanf("%d%d", &n, &k); long long sum = 0; for (int i = 1; i <= n; i++) scanf("%d%d", &a[i], &b[i]), p[i] = pair<pair<int, int>, int>(pair<int, int>(b[i], a[i]), i), sum += b[i]; sort(p + 1, p + 1 + n); if (k == 1) { int idx = 1; for (int i = 1; i <= n; i++) { if (a[i] > a[idx]) idx = i; } printf("1\n%d\n", idx); continue; } memset(vis, 0, sizeof(vis)); dp[0][0] = 0; vis[0][0] = 1; for (int i = 1; i <= n; i++) { int a = p[i].first.second, b = p[i].first.first, idx = p[i].second; for (int j = k - 1; j >= 0; j--) { if (!vis[i - 1][j]) continue; if (!vis[i][j] || dp[i][j] < dp[i - 1][j]) { dp[i][j] = dp[i - 1][j]; pv1[i][j] = j; vis[i][j] = 1; } if (j == k - 1) { long long cost = a; if (!vis[i][j + 1] || dp[i][j] < dp[i - 1][j] + cost) { dp[i][j + 1] = dp[i - 1][j] + cost; pv1[i][j + 1] = j; pv2[i][j + 1] = idx; vis[i][j + 1] = 1; } } else { long long cost = a + sum - (k - 1 - j) * 1ll * b; if (!vis[i][j + 1] || dp[i][j] < dp[i - 1][j] + cost) { dp[i][j + 1] = dp[i - 1][j] + cost; pv1[i][j + 1] = j; pv2[i][j + 1] = idx; vis[i][j + 1] = 1; } } } } int cur = k; vector<int> ans; for (int i = n; i >= 1; i--) { if (pv1[i][cur] == cur) continue; else { ans.emplace_back(pv2[i][cur]); cur = pv1[i][cur]; } } reverse(ans.begin(), ans.end()); memset(ok, 0, sizeof(ok)); for (int i = 0; i < (int)ans.size(); i++) ok[ans[i]] = 1; vector<int> res; for (int i = 0; i < (int)ans.size() - 1; i++) res.emplace_back(ans[i]); for (int i = 1; i <= n; i++) { if (!ok[i]) { res.emplace_back(i); res.emplace_back(-i); } } res.emplace_back(ans[ans.size() - 1]); printf("%d\n", res.size()); for (int i = 0; i < res.size(); i++) printf("%d ", res[i]); printf("\n"); } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
java
import java.io.*; import java.util.*; public class Main { static final long MOD = 1000000007L; static final int INF = 50000000; static final int NINF = -50000000; static ArrayDeque<int[]>[] graph; public static void main(String[] args) { FastScanner sc = new FastScanner(); PrintWriter pw = new PrintWriter(System.out); int Q = sc.ni(); for (int q = 0; q < Q; q++) { int N = sc.ni(); int K = sc.ni(); int[][] nums = new int[N+1][3]; //A,B,i Arrays.fill(nums[0],-1); for (int i = 1; i <= N; i++) nums[i] = new int[] {sc.ni(),sc.ni(),i}; sort(nums); int[][] dp = new int[N+1][K+1]; //put minion i in position j int[][] from = new int[N+1][K+1]; //equal to k or k-1 for (int i = 1; i <= N; i++) { for (int j = 1; j <= Math.min(i, K); j++) { int v1 = nums[i][0]; //put this minion in the final roster if (j>0) { v1 = dp[i-1][j-1]+nums[i][0]+(j-1)*nums[i][1]; } int v2 = dp[i-1][j]+(K-1)*nums[i][1]; //this is a throwaway minion if (v1 > v2) { dp[i][j] = v1; from[i][j] = j-1; } else { dp[i][j] = v2; from[i][j] = j; } } } ArrayList<Integer> survived = new ArrayList<Integer>(); ArrayList<Integer> killed = new ArrayList<Integer>(); int pos = K; for (int i = N; i >= 1; i--) { if (from[i][pos]==pos) { killed.add(nums[i][2]); } else { survived.add(nums[i][2]); } pos = from[i][pos]; } Collections.reverse(survived); ArrayList<Integer> ans = new ArrayList<Integer>(); for (int i = 0; i < survived.size()-1; i++) { ans.add(survived.get(i)); } for (int i = 0; i < killed.size(); i++) { int idx = killed.get(i); ans.add(idx); ans.add(0-idx); } ans.add(survived.get(survived.size()-1)); pw.println(ans.size()); for (int a: ans) pw.print(a + " "); pw.println(); } pw.close(); } public static void sort(int[][] arr) { //Sort an array (immune to quicksort TLE) Random rgen = new Random(); for (int i = 0; i < arr.length; i++) { int randomPosition = rgen.nextInt(arr.length); int[] temp = arr[i]; arr[i] = arr[randomPosition]; arr[randomPosition] = temp; } Arrays.sort(arr, new Comparator<int[]>() { @Override public int compare(int[] arr1, int[] arr2) { return arr1[1]-arr2[1]; //Ascending order. } }); } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; const int inf = 0x3f3f3f3f; const long long int infl = 1e18; long long int din[109][109]; int prv[109][109], t; struct st { int x, y, id; }; st p[100]; int main() { cin >> t; while (t--) { int n, k; cin >> n >> k; memset(din, -1, sizeof(din)); for (int i = 1; i <= n; ++i) { cin >> p[i].x >> p[i].y; p[i].id = i; } sort(p, p + n, [](st a, st b) { return a.y < b.y; }); din[0][0] = 0; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= k + 1; ++j) { if (din[i][j] == -1) { continue; } if (j + 1 <= k) { long long int c = din[i][j] + p[i].x + p[i].y * j; if (din[i + 1][j + 1] < c) { prv[i + 1][j + 1] = j; din[i + 1][j + 1] = c; } } long long int c = din[i][j] + p[i].y * (k - 1); if (din[i + 1][j] < c) { prv[i + 1][j] = j; din[i + 1][j] = c; } } } int nx = n, ny = k; vector<int> A, B; while (nx) { if (prv[nx][ny] != ny) { A.push_back(p[nx - 1].id); } else { B.push_back(p[nx - 1].id); } ny = prv[nx][ny]; nx--; } reverse(A.begin(), A.end()); reverse(B.begin(), B.end()); cout << A.size() + B.size() * 2 << "\n"; for (int i = 0; i < A.size() - 1; ++i) { cout << A[i] + 1 << " "; } for (int i : B) { cout << i + 1 << " " << -i - 1 << " "; } cout << A.back() + 1 << "\n"; } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MX = 80; int n, k; array<int, 3> m[MX]; int dp[MX][MX], dpb[MX][MX]; void solve() { cin >> n >> k; for (int i = (0); i < (n); ++i) { cin >> m[i][1] >> m[i][0]; m[i][2] = i + 1; } sort(m, m + n); for (int i = (0); i < (n + 1); ++i) { memset((dp[i]), -1, (k + 1) * sizeof(dp[i][0])); memset((dpb[i]), -1, (k + 1) * sizeof(dpb[i][0])); } dp[0][0] = 0; for (int i = (0); i < (n); ++i) for (int j = (0); j < (k + 1); ++j) { if (dp[i][j] == -1) continue; auto [b, a, _id] = m[i]; if (dp[i + 1][j] < dp[i][j] + (k - 1) * b) { dp[i + 1][j] = dp[i][j] + (k - 1) * b; dpb[i + 1][j] = 0; } if (j < k && dp[i + 1][j + 1] < dp[i][j] + a + (j - 1) * b) { dp[i + 1][j + 1] = dp[i][j] + a + (j - 1) * b; dpb[i + 1][j + 1] = 1; } } vector<int> mf = vector<int>(k); vector<int> rit = vector<int>(); rit.reserve(n - k); int cp = k; for (int i = (n)-1; i >= (0); --i) { auto [b, a, id] = m[i]; if (dpb[i + 1][cp]) mf[--cp] = id; else rit.push_back(id); } cout << 2 * n - k << '\n'; for (int i = (0); i < (k - 1); ++i) cout << mf[i] << ' '; for (int i = (0); i < (n - k); ++i) cout << rit[i] << ' ' << -rit[i] << ' '; cout << mf[k - 1] << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { solve(); } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename T> T &read(T &x) { x = 0; bool f = 0; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') { f = 1; } ch = getchar(); } while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); } if (f) { x = -x; } return x; } const double eps = 1e-8; inline int sgn(double x) { if (x < -eps) { return -1; } return x > eps; } void fp() { freopen(".in", "r", stdin); freopen(".out", "w", stdout); } struct pai { int a, b, num; } c[80]; inline bool cmp(pai a, pai b) { return a.b < b.b; } int f[80][80], g[80][80], ans1[155], ans2[155]; int main() { int t = read(t); while (t--) { int n = read(n), k = read(k), i, j; for (i = 1; i <= n; i++) { read(c[i].a); read(c[i].b); c[i].num = i; } sort(c + 1, c + n + 1, cmp); for (i = 1; i <= n; i++) { for (j = 1; j <= min(k, i); j++) { if (j == i) { f[i][j] = f[i - 1][j - 1] + c[i].a + c[i].b * (j - 1); g[i][j] = 2; continue; } if (f[i - 1][j - 1] + c[i].a + c[i].b * (j - 1) < f[i - 1][j] + c[i].b * (k - 1)) { f[i][j] = f[i - 1][j] + c[i].b * (k - 1); g[i][j] = 1; } else { f[i][j] = f[i - 1][j - 1] + c[i].a + c[i].b * (j - 1); g[i][j] = 2; } } } printf("%d\n", 2 * n - k); int nowx = n, nowy = k, cnt1 = 0, cnt2 = 0; for (i = 1; i <= n; i++) { if (g[nowx][nowy] == 1) { ans1[++cnt1] = -c[nowx].num; ans1[++cnt1] = c[nowx].num; nowx--; } else { nowx--; nowy--; } } nowx = n; nowy = k; for (i = 1; i <= n; i++) { if (g[nowx][nowy] == 1) { nowx--; } else { ans2[++cnt2] = c[nowx].num; nowx--; nowy--; } } for (i = cnt2; i >= 2; i--) { printf("%d ", ans2[i]); } for (i = cnt1; i; i--) { printf("%d ", ans1[i]); } printf("%d ", ans2[1]); puts(""); } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
java
/* If you want to aim high, aim high Don't let that studying and grades consume you Just live life young ****************************** If I'm the sun, you're the moon Because when I go up, you go down ******************************* I'm working for the day I will surpass you https://www.a2oj.com/Ladder16.html */ import java.util.*; import java.io.*; import java.math.*; public class x1354F2 { public static void main(String omkar[]) throws Exception { BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(infile.readLine()); int T = Integer.parseInt(st.nextToken()); StringBuilder sb = new StringBuilder(); while(T-->0) { st = new StringTokenizer(infile.readLine()); int N = Integer.parseInt(st.nextToken()); int K = Integer.parseInt(st.nextToken()); Minion[] arr = new Minion[N]; for(int i=0; i < N; i++) { st = new StringTokenizer(infile.readLine()); int a = Integer.parseInt(st.nextToken()); int b = Integer.parseInt(st.nextToken()); arr[i] = new Minion(a, b, i); } Arrays.sort(arr); Node[][] par = new Node[N][K+1]; long[][] dp = new long[N][K+1]; dp[0][1] = arr[0].a; dp[0][0] = arr[0].b*(K-1); par[0][1] = new Node(-1, 420); par[0][0] = new Node(-1, 420); for(int i=1; i < N; i++) for(int k=0; k <= K; k++) { if(i+1 < k) break; long max = dp[i-1][k]+arr[i].b*(K-1); if(k > 0) max = Math.max(max, dp[i-1][k-1]+arr[i].a+arr[i].b*(k-1)); dp[i][k] = max; if(max == dp[i-1][k]+arr[i].b*(K-1)) par[i][k] = new Node(i-1, k); else par[i][k] = new Node(i-1, k-1); } //restore ArrayList<Integer> ls = new ArrayList<Integer>(); HashSet<Integer> set = new HashSet<Integer>(); for(int i=0; i < N; i++) set.add(i); Node curr = new Node(N-1, K); while(curr.dex > 0) { int i = curr.dex; int k = curr.cnt; Node next = par[i][k]; if(k == next.cnt+1) { ls.add(arr[i].dex); set.remove(arr[i].dex); } curr = next; } if(curr.cnt == 1) { ls.add(arr[0].dex); set.remove(arr[0].dex); } Collections.reverse(ls); ArrayList<Integer> res = new ArrayList<Integer>(); for(int i=0; i < ls.size()-1; i++) res.add(ls.get(i)+1); for(int x: set) { res.add(x+1); res.add(-x-1); } res.add(ls.get(ls.size()-1)+1); sb.append(res.size()+"\n"); for(int x: res) sb.append(x+" "); sb.append("\n"); } System.out.print(sb); } } class Minion implements Comparable<Minion> { public long a; public long b; public int dex; public Minion(long x, long y, int d) { a = x; b = y; dex = d; } public int compareTo(Minion oth) { if(b == oth.b) return (int)(oth.a-a); return (int)(b-oth.b); } } class Node { public int dex; public int cnt; public Node(int a, int c) { dex = a; cnt = c; } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
java
/* ID: tommatt1 LANG: JAVA TASK: */ import java.util.*; import java.io.*; public class cf1354f{ public static void main(String[] args)throws IOException { PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); BufferedReader bf=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(bf.readLine()); int tt=Integer.parseInt(st.nextToken()); while(tt-->0) { st=new StringTokenizer(bf.readLine()); int n=Integer.parseInt(st.nextToken()); int k=Integer.parseInt(st.nextToken()); pair[] mnn=new pair[n+1]; boolean[] inc=new boolean[n+1]; boolean[][] poss=new boolean[n+1][k+1]; int[][] dp=new int[n+1][k+1]; for(int i=1;i<=n;i++) { st=new StringTokenizer(bf.readLine()); int a=Integer.parseInt(st.nextToken()); int b=Integer.parseInt(st.nextToken()); mnn[i]=new pair(a,b,i); } for(int i=0;i<=n;i++) { Arrays.fill(dp[i], 1, k+1, Integer.MIN_VALUE); } Arrays.sort(mnn,1,n+1); for(int i=1;i<=n;i++) { for(int j=k;j>0;j--) { dp[i][j]=Math.max(dp[i-1][j], dp[i-1][j-1]+mnn[i].a-mnn[i].b*(k-j)); poss[i][j]=(dp[i][j]^dp[i-1][j])==0?false:true; } } for(int i=n,j=k;i>0;i--) { if(poss[i][j]) { inc[i]=true; j--; } } out.println(k+2*(n-k)); for(int i=1,j=0;i<=n;i++) { if(j==k-1) { for(int z=1;z<=n;z++) { if(!inc[z]) { out.print(mnn[z].c+" "+(-mnn[z].c)+" "); } } j++; } if(inc[i]) { out.print(mnn[i].c+" "); j++; } } out.println(); } out.close(); } static class pair implements Comparable<pair>{ int a,b,c; public pair(int x,int y,int z) { a=x;b=y;c=z; } public int compareTo(pair p) { return b-p.b; //if(a>p.a) return 1; } } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <iostream> #include <vector> #include <algorithm> #include <string> #include <cstring> #include <sstream> #include <map> #include <set> #include <cmath> #include <queue> #include <stack> #include <list> #include <numeric> #include <bitset> #include <ext/algorithm> #include <ext/numeric> #define ffor(_a,_f,_t) for(int _a=(_f),__t=(_t);_a<__t;_a++) #define all(_v) (_v).begin() , (_v).end() #define SZ(x) ((int)(x).size()) #define pb push_back #define eb emplace_back #define SET(__set, val) memset(__set, val, sizeof(__set)) #define FOR(__i, __n) ffor (__i, 0, __n) typedef long long LL; using namespace std; vector<vector<int>> BA; int n, k; int dp[100][100]; bool path[100][100]; void update(int &ret, int tmp, int summoned, int idx, bool take) { if (tmp > ret) { ret = tmp; path[summoned][idx] = take; } } int memo(int summoned, int idx) { if (idx == n) { if (summoned < k) return -(1 << 30); return 0; } int &ret = dp[summoned][idx]; if (ret != -1) return ret; ret = -1; if (summoned < k) update(ret, BA[idx][1] + summoned * BA[idx][0] + memo(summoned + 1, idx + 1), summoned, idx, true); update(ret, (k - 1) * BA[idx][0] + memo(summoned, idx + 1), summoned, idx, false); return ret; } int main() { std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); int _; cin >> _; while (_--) { cin >> n >> k; BA.clear(); int x, y; FOR (i, n) { cin >> x >> y; BA.pb({y, x, i}); } sort(all(BA)); SET(dp, 255); SET(path, 0); int ret = memo(0, 0); // recover int summoned = 0; vector<int> takeThem; takeThem.clear(); bool marked[100]; SET(marked, 0); if (BA.size() != n) cout << "BA does not have n elements." << endl; FOR (i, n) { if (path[summoned][i]) { takeThem.pb(BA[i][2]); summoned++; marked[BA[i][2]] = true; } } if (SZ(takeThem) != k) cout << SZ(takeThem) - k << " " << k << " " << SZ(takeThem) << "It's size should be k." << endl; cout << k + 2 * (n - k) << endl; FOR (i, SZ(takeThem) - 1) cout << takeThem[i]+1 << " "; FOR (i, n) if (!marked[i]) { cout << i + 1 << " -" << i + 1 << " "; } if (SZ(takeThem)) cout << takeThem[SZ(takeThem) - 1] + 1 << endl; else cout << endl; } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline long long mod(long long n, long long m) { long long ret = n % m; if (ret < 0) ret += m; return ret; } long long gcd(long long a, long long b) { return (b == 0LL ? a : gcd(b, a % b)); } long long exp(long long a, long long b, long long m) { if (b == 0LL) return 1LL; if (b == 1LL) return mod(a, m); long long k = mod(exp(a, b / 2, m), m); if (b & 1LL) { return mod(a * mod(k * k, m), m); } else return mod(k * k, m); } const long long N = 80; struct coisa { long long a, b, id; coisa() {} coisa(long long A, long long B, long long C) { a = A, b = B, id = C; } } v[N]; long long dp[N][N]; long long n, k; long long solve(long long id, long long j) { if (id == n + 1) { return 0; } long long& x = dp[id][j]; if (x != -1) return x; long long nao = solve(id + 1, j) + (k - 1) * v[id].b; if (j == k) return x = nao; long long pega = solve(id + 1, j + 1) + v[id].b * (j) + v[id].a; return x = max(pega, nao); } vector<long long> tira, col; void rool(long long id, long long j) { if (id == n + 1) return; if (j == k) { tira.push_back(id); rool(id + 1, j); return; } if (solve(id, j) == solve(id + 1, j) + (k - 1) * v[id].b) { tira.push_back(id); rool(id + 1, j); return; } col.push_back(id); rool(id + 1, j + 1); return; } void solve() { cin >> n >> k; for (long long i = 1; i <= n; i++) { long long a, b; cin >> a >> b; v[i] = coisa(a, b, i); } sort(v + 1, v + 1 + n, [&](coisa a, coisa b) { return a.b < b.b; }); if (k == 1) { sort(v + 1, v + 1 + n, [&](coisa a, coisa b) { return a.a > b.a; }); cout << 1 << "\n"; cout << v[1].id << "\n"; return; } if (k == n) { cout << n << "\n"; for (long long i = 1; i <= n; i++) { cout << v[i].id << " "; } cout << "\n"; return; } memset(dp, -1, sizeof(dp)); tira.clear(); col.clear(); solve(1, 0); rool(1, 0); if (col.size() == 0) { cout << tira.size() << "\n"; for (long long x : tira) cout << v[x].id << " "; cout << "\n"; return; } vector<long long> ans; for (long long i = 0; i < (long long)col.size() - 1; i++) ans.push_back(v[col[i]].id); for (long long x : tira) ans.push_back(v[x].id), ans.push_back(-v[x].id); ans.push_back(v[col[(long long)col.size() - 1]].id); cout << ans.size() << "\n"; for (long long x : ans) cout << x << " "; cout << "\n"; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long t; cin >> t; while (t--) { solve(); } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.StringTokenizer; import java.io.BufferedReader; import java.util.Comparator; import java.util.ArrayDeque; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); SummoningMinions solver = new SummoningMinions(); solver.solve(1, in, out); out.close(); } static class SummoningMinions { public void solve(int testNumber, InputReader in, PrintWriter out) { int T = in.nextInt(); while (T-- > 0) { int N = in.nextInt(); int K = in.nextInt(); int[][] arr = new int[N][3]; for (int i = 0; i < N; i++) { arr[i][0] = in.nextInt(); arr[i][1] = in.nextInt(); arr[i][2] = i + 1; } Arrays.sort(arr, new Comparator<int[]>() { public int compare(int[] ints, int[] t1) { return Integer.compare(ints[1], t1[1]); } }); long[][] dp = new long[N][K + 1]; boolean[][] add = new boolean[N][K + 1]; Arrays.fill(dp[0], Long.MIN_VALUE); dp[0][0] = (K - 1) * arr[0][1]; dp[0][1] = (arr[0][0]); add[0][1] = true; for (int i = 1; i < N; i++) { for (int j = 0; j <= K; j++) { dp[i][j] = dp[i - 1][j] + (K - 1) * arr[i][1]; if (j > 0) { if (dp[i - 1][j - 1] + arr[i][0] + arr[i][1] > dp[i][j]) { add[i][j] = true; dp[i][j] = Math.max(dp[i][j], dp[i - 1][j - 1] + arr[i][0] + arr[i][1] * (j - 1)); } } } } int u = K; ArrayDeque<Integer> order = new ArrayDeque<>(); ArrayList<Integer> not_added = new ArrayList<>(); for (int i = N - 1; i >= 0; i--) { if (add[i][u]) { order.addFirst(arr[i][2]); u--; } else { not_added.add(arr[i][2]); } } StringBuilder str = new StringBuilder(); assert order.size() == K; // assert false; while (order.size() > 1) { str.append(order.removeFirst() + " "); } for (int i : not_added) { str.append(i + " "); str.append((-i) + " "); } str.append(order.removeFirst() + " "); out.println(str.toString()); } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long mod = 998244353; long long powmod(long long a, long long b) { long long res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } double PI = 3.14159265358979323846; long double e = 2.71828182845904523536028747135266249775724709369995; long long fill(vector<pair<pair<int, int>, int> > &v, vector<vector<long long> > &dp, int idx, int k, int n, int realn) { if (dp[idx][k] != -1) return dp[idx][k]; if (idx + 1 == k) { if (idx == 0) { dp[idx][k] = v[idx].first.second; return dp[idx][k]; } dp[idx][k] = v[idx].first.second + v[idx].first.first * (n) + fill(v, dp, idx - 1, k - 1, n - 1, realn); return dp[idx][k]; } if (k == 0) { if (idx == 0) { dp[idx][k] = v[idx].first.first * (realn - 1); return dp[idx][k]; } long long a = fill(v, dp, idx - 1, k, n, realn); dp[idx][k] = a + v[idx].first.first * (realn - 1); return dp[idx][k]; } long long a = fill(v, dp, idx - 1, k, n, realn) + v[idx].first.first * (realn - 1); long long b = fill(v, dp, idx - 1, k - 1, n - 1, realn) + v[idx].first.second + v[idx].first.first * (n); dp[idx][k] = max(a, b); return dp[idx][k]; } void solve() { int k, n; cin >> n >> k; vector<pair<pair<int, int>, int> > v(n); for (int i = 0; i < n; i++) { cin >> v[i].first.second >> v[i].first.first; v[i].second = i + 1; } sort(v.begin(), v.end()); vector<vector<long long> > dp(n, vector<long long>(k + 1, -1)); fill(v, dp, n - 1, k, k - 1, k); vector<int> choose; int cnt = 0; for (int i = n - 1; i >= 0; i--) { if (cnt == k) break; if (i == 0) { choose.push_back(i); cnt++; break; } if (dp[i][k - cnt] == dp[i - 1][k - cnt] + (k - 1) * v[i].first.first) { continue; } else { cnt++; choose.push_back(i); } } cout << 2 * n - k << endl; set<int> s; for (int i = cnt - 1; i >= 1; i--) { cout << v[choose[i]].second << " "; s.insert(v[choose[i]].second); } for (int i = 1; i <= n; i++) { if (s.find(i) == s.end() && i != v[choose[0]].second) cout << i << " " << -i << " "; } cout << v[choose[0]].second << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.precision(20); int t = 1; cin >> t; for (int i = 0; i < t; i++) { solve(); } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename T> inline bool upmin(T &x, T y) { return y < x ? x = y, 1 : 0; } template <typename T> inline bool upmax(T &x, T y) { return x < y ? x = y, 1 : 0; } const long double eps = 1e-11; const long double pi = acos(-1); const int oo = 1 << 30; const long long loo = 1ll << 62; const int mods = 1e9 + 7; const int MAXN = 5005; const int INF = 0x3f3f3f3f; inline int read() { int f = 1, x = 0; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 3) + (x << 1) + (c ^ 48); c = getchar(); } return x * f; } int a[55], b[55], f[55][55], flag[55], frm[55][55]; vector<int> V; signed main() { int Case = read(); while (Case--) { int n = read(), k = read(); for (int i = 1; i <= n; i++) a[i] = read(), b[i] = read(); memset(f, -INF, sizeof f); memset(frm, 0, sizeof frm); memset(flag, 0, sizeof flag); V.clear(); f[0][0] = 0; for (int i = 1; i <= n; i++) for (int j = 0; j <= k; j++) { if (j && upmax(f[i][j], f[i - 1][j - 1] + a[i] + b[i] * (j - 1))) frm[i][j] = 1; if (upmax(f[i][j], f[i - 1][j] + b[i] * (k - 1))) frm[i][j] = 2; } int nw = k; for (int i = n; i >= 1; i--) if (frm[i][nw] == 1) V.push_back(i), nw--; else flag[i] = 1; sort(V.begin(), V.end(), [&](int x, int y) { return b[x] < b[y]; }); printf("%d\n", V.size() + (n - V.size()) * 2); for (int i = 0; i < V.size() - 1; i++) printf("%d ", V[i]); for (int i = 1; i <= n; i++) if (flag[i]) printf("%d -%d ", i, i); printf("%d\n", V[V.size() - 1]); } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
python3
from bisect import bisect_left as bl from bisect import bisect_right as br from heapq import heappush,heappop import math from collections import * from functools import reduce,cmp_to_key,lru_cache import io, os input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline # import sys # input = sys.stdin.readline M = mod = 10**9 + 7 def factors(n):return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))) def inv_mod(n):return pow(n, mod - 2, mod) def li():return [int(i) for i in input().rstrip().split()] def st():return str(input().rstrip())[2:-1] def val():return int(input().rstrip()) def li2():return [str(i)[2:-1] for i in input().rstrip().split()] def li3():return [int(i) for i in st()] for _ in range(val()): n,k = li() l = [] for i in range(n):l.append(li() + [i]) s = sum(i[1] for i in l) l.sort(key = lambda x:(x[0] + s - x[1],-x[1]),reverse = 1) ans = [] l[:k] = sorted(l[:k],key = lambda x:x[1]) for i in range(k-1): ans.append(l[i][-1] + 1) for i in range(k,n): ans.append(l[i][-1] + 1) ans.append(-l[i][-1] - 1) ans.append(l[k-1][-1] + 1) print(len(ans)) print(*ans)
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
python3
from bisect import bisect_left as bl from bisect import bisect_right as br from heapq import heappush,heappop import math from collections import * from functools import reduce,cmp_to_key,lru_cache import io, os input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline # import sys # input = sys.stdin.readline M = mod = 10**9 + 7 def factors(n):return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))) def inv_mod(n):return pow(n, mod - 2, mod) def li():return [int(i) for i in input().rstrip().split()] def st():return str(input().rstrip())[2:-1] def val():return int(input().rstrip()) def li2():return [str(i)[2:-1] for i in input().rstrip().split()] def li3():return [int(i) for i in st()] for _ in range(val()): n,k = li() l = [] for i in range(n):l.append(li() + [i]) s = sum(i[1] for i in l) l.sort(key = lambda x:(x[0],-x[1]),reverse = 1) ans = [] l[:k] = sorted(l[:k],key = lambda x:x[1]) for i in range(k-1): ans.append(l[i][-1] + 1) for i in range(k,n): ans.append(l[i][-1] + 1) ans.append(-l[i][-1] - 1) ans.append(l[k-1][-1] + 1) print(len(ans)) print(*ans)
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1); const double eps = 1e-12; const int inf = 2000000000; const long long int infLL = (long long int)1e18; long long int MOD = 1000000007; int MOD1 = 1000000007; int MOD2 = 1000000009; inline bool checkBit(long long int n, long long int i) { return n & (1LL << i); } inline long long int setBit(long long int n, long long int i) { return n | (1LL << i); ; } inline long long int resetBit(long long int n, long long int i) { return n & (~(1LL << i)); } int dx[] = {0, 0, +1, -1}; int dy[] = {+1, -1, 0, 0}; inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } inline bool isLeapYear(long long int year) { return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); } inline void normal(long long int &a) { a %= MOD; (a < 0) && (a += MOD); } inline long long int modMul(long long int a, long long int b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a * b) % MOD; } inline long long int modAdd(long long int a, long long int b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a + b) % MOD; } inline long long int modSub(long long int a, long long int b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; } inline long long int modPow(long long int b, long long int p) { long long int r = 1LL; while (p) { if (p & 1) r = modMul(r, b); b = modMul(b, b); p >>= 1LL; } return r; } inline long long int modDiv(long long int a, long long int b) { return modMul(a, modPow(b, MOD - 2)); } bool comp(const pair<long long int, pair<long long int, long long int> > &p1, const pair<long long int, pair<long long int, long long int> > &p2) { return p1.first > p2.first; } bool comp1(const pair<long long int, long long int> &p1, const pair<long long int, long long int> &p2) { if (p1.first == p2.first) { return p1.second > p2.second; } return p1.first < p2.first; } long long int converter(string a) { long long int i, mul = 1LL, r, t, ans = 0LL; if (a.length() == 0) return 0; for (i = a.length() - 1; i >= 0; i--) { t = a[i] - '0'; r = t % 10; ans += (mul * r); mul = mul * 10; } return ans; } int msb(unsigned x) { union { double a; int b[2]; }; a = x; return (b[1] >> 20) - 1023; } const int MAX = 78; long long int t, n, k; pair<pair<long long int, long long int>, long long int> p[MAX]; long long int dp[MAX][MAX]; vector<long long int> good, bad; long long int solve(long long int idx, long long int cnt) { if (idx == n + 1) { if (cnt != k) return -infLL; return 0; } if (dp[idx][cnt] != -1) return dp[idx][cnt]; long long int ret = 0; if (cnt + 1 <= k) { ret = max(ret, cnt * p[idx].first.first + p[idx].first.second + solve(idx + 1, cnt + 1)); } ret = max(ret, ((k - 1) * p[idx].first.first) + solve(idx + 1, cnt)); return dp[idx][cnt] = ret; } void trace(long long int idx, long long int cnt) { if (idx == n + 1) return; long long int ret1 = 0LL, ret2 = 0LL; if (cnt + 1 <= k) ret1 = cnt * p[idx].first.first + p[idx].first.second + solve(idx + 1, cnt + 1); ret2 = ((k - 1) * p[idx].first.first) + solve(idx + 1, cnt); if (ret1 > ret2) { trace(idx + 1, cnt + 1); good.push_back(p[idx].second); } else { trace(idx + 1, cnt); bad.push_back(p[idx].second); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cout.unsetf(ios::floatfield); cout.precision(20); cout.setf(ios::fixed, ios::floatfield); ; cin >> t; while (t--) { good.clear(); bad.clear(); long long int maxi = 0, idx; cin >> n >> k; for (int i = 1; i <= n; ++i) { cin >> p[i].first.second >> p[i].first.first; p[i].second = i; if (p[i].first.second >= maxi) maxi = p[i].first.second, idx = i; } if (k == 1) { cout << 1 << '\n'; cout << idx << '\n'; continue; } sort(p + 1, p + n + 1); memset(dp, -1, sizeof(dp)); long long int now = solve(1, 0); ; trace(1, 0); reverse((good).begin(), (good).end()); reverse((bad).begin(), (bad).end()); if (good.size() == 0) { string ss = ""; for (int i = 1; i <= n; ++i) { ss += to_string(p[i].first.first); ss += '#'; ss += to_string(p[i].first.second); ss += '&'; } cout << ss << '\n'; continue; } cout << good.size() + (2 * bad.size()) << '\n'; for (int i = 0; i < (int)good.size() - 1; ++i) { cout << good[i] << " "; } for (int i = 0; i < bad.size(); ++i) { cout << bad[i] << " " << -1 * bad[i] << " "; } cout << good[good.size() - 1] << '\n'; } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; void solve() { int n, k; cin >> n >> k; long long a1[n], b1[n]; for (int i = 0; i < n; i++) cin >> a1[i] >> b1[i]; long long dp[n][k]; vector<long long> a, b; vector<pair<pair<long long, long long>, long long> > e; vector<long long> ans; long long mx = 0; for (int p = 0; p < n; p++) { a.clear(); b.clear(); e.clear(); for (int i = 0; i < n; i++) if (i == p) continue; else e.push_back(make_pair(make_pair(b1[i], a1[i]), i)); sort(e.begin(), e.end()); a.push_back(0); b.push_back(0); for (int i = 0; i < e.size(); i++) { a.push_back(e[i].first.second); b.push_back(e[i].first.first); } for (int i = 0; i < n; i++) for (int j = 0; j < k; j++) dp[i][j] = -1e9; dp[0][0] = 0; for (int i = 1; i < n; i++) for (int j = 0; j < k; j++) { dp[i][j] = max(dp[i][j], dp[i - 1][j] + b[i] * (k - 1)); if (j > 0) dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + b[i] * (j - 1) + a[i]); } if (mx < a1[p] + b1[p] * (k - 1) + dp[n - 1][k - 1]) { mx = a1[p] + b1[p] * (k - 1) + dp[n - 1][k - 1]; vector<long long> x, bad; int i = n - 1, j = k - 1; while (i > 0 && j > 0) { if (j > 0) { if (dp[i - 1][j - 1] + b[i] * (j - 1) + a[i] == dp[i][j]) { x.push_back(e[i - 1].second + 1); i--; j--; } else { bad.push_back(-e[i - 1].second - 1); bad.push_back(e[i - 1].second + 1); i--; } } else { bad.push_back(-e[i - 1].second - 1); bad.push_back(e[i - 1].second + 1); i--; } } reverse(x.begin(), x.end()); for (int i = 0; i < bad.size(); i++) x.push_back(bad[i]); x.push_back(p + 1); ans = x; } } cout << ans.size() << "\n"; for (int i = 0; i < ans.size(); i++) cout << ans[i] << ' '; cout << "\n"; } int main() { ios::sync_with_stdio(0); int q; cin >> q; while (q--) solve(); }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int N = 110; int f[N][N]; struct node { int a, b, id; }; node arr[N]; bool st[N]; bool last[N][N]; bool cmp(node a, node b) { return a.b < b.b; } int main() { int T; cin >> T; while (T--) { int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> arr[i].a >> arr[i].b; arr[i].id = i; } sort(arr + 1, arr + n + 1, cmp); memset(f, -0x3f, sizeof f); memset(st, 0, sizeof st); memset(last, 0, sizeof last); for (int i = 1; i <= n; i++) { f[i][0] = 0; for (int j = 0; j <= m && j <= i; j++) { if (f[i - 1][j] != -INF) { f[i][j] = f[i - 1][j] + (m - 1) * arr[i].b; } if (j > 0) { if (f[i - 1][j - 1] != -INF) if (f[i][j] < f[i - 1][j - 1] + (j - 1) * arr[i].b + arr[i].a) { f[i][j] = f[i - 1][j - 1] + (j - 1) * arr[i].b + arr[i].a; last[i][j] = 1; } } } } for (int i = n, j = m; i >= 1; i--) { if (last[i][j] == 1) { st[arr[i].id] = true; j--; } } cout << (m - 1) + 2 * (n - m) + 1 << endl; int num = 0; for (int i = 1; i <= n; i++) { if (st[i]) { num++; if (num == m) { num = arr[i].id; break; } cout << i << ' '; } } for (int i = 1; i <= n; i++) { if (!st[i]) { cout << i << ' ' << -i << ' '; } } cout << num << endl; } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
// Problem : F. Summoning Minions // Contest : Codeforces - Educational Codeforces Round 87 (Rated for Div. 2) // URL : https://codeforces.com/contest/1354/problem/F // Memory Limit : 512 MB // Time Limit : 6000 ms // Powered by CP Editor (https://github.com/cpeditor/cpeditor) #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/rope> #define ll long long #define pb push_back #define f first #define s second #define sz(x) (int)(x).size() #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define ios ios_base::sync_with_stdio(false);cin.tie(NULL) #define D(x) cerr << #x << " is " << (x) << "\n"; #define ld long double #define li __int128 using namespace std; using namespace __gnu_pbds; using namespace __gnu_cxx; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; ///find_by_order(),order_of_key() template<int D, typename T>struct vec : public vector<vec<D - 1, T>> {static_assert(D >= 1, "Vector dimension must be greater than zero!");template<typename... Args>vec(int n = 0, Args... args) : vector<vec<D - 1, T>>(n, vec<D - 1, T>(args...)) {}}; template<typename T>struct vec<1, T> : public vector<T> {vec(int n = 0, T val = T()) : vector<T>(n, val) {}}; template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) { os << '{' << a.f << ", " << a.s << '}'; return os; } template<class T> ostream& operator<<(ostream& os, const vector<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;} template<class T> ostream& operator<<(ostream& os, const deque<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;} template<class T> ostream& operator<<(ostream& os, const set<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;} template<class T> ostream& operator<<(ostream& os, const set<T,greater<T> >& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;} template<class T> ostream& operator<<(ostream& os, const multiset<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;} template<class T> ostream& operator<<(ostream& os, const multiset<T,greater<T> >& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;} template<class T1,class T2> ostream& operator<<(ostream& os, const map<T1,T2>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;} template<class T1, class T2> istream& operator>>(istream& is,pair<T1,T2>& a) {is>>a.f>>a.s;return is;} template<class T> istream& operator>>(istream& is,vector<T>& a) {for(auto &p:a)is>>p;return is;} //istream& operator>>(istream& is,__int128& a){string s;is>>s;a=0;for(auto p:s)a=a*10+p-'0';return is;} //ostream& operator<<(ostream& os,__int128 a){bool neg=false;if(a<0)neg=true,a*=-1;ll high=(a/(__int128)1e18);ll low=(a-(__int128)1e18*high);string res;if(neg)res+='-';if(high>0){res+=to_string(high);string temp=to_string(low);res+=string(18-temp.size(),'0');res+=temp;}else res+=to_string(low);os<<res;return os;} const int N=76; int n,k,dp[N][N]; vector<pair<pair<int,int>,int> > a; int calc(int tr,int br){ if(tr==n){ if(br==k) return 0; return INT_MIN/5; } if(dp[tr][br]!=-1) return dp[tr][br]; dp[tr][br]=0; if(br<k) dp[tr][br]=max(dp[tr][br],calc(tr+1,br+1)+a[tr].f.s+br*a[tr].f.f); dp[tr][br]=max(dp[tr][br],calc(tr+1,br)+(k-1)*a[tr].f.f); //printf("%i %i: %i\n",tr,br,dp[tr][br]); return dp[tr][br]; } vector<int> pick, ostali; void gen(int tr,int br){ if(tr==n) return; if(br<k&&dp[tr][br]==calc(tr+1,br+1)+a[tr].f.s+br*a[tr].f.f){ pick.pb(a[tr].s); gen(tr+1,br+1); } else{ ostali.pb(a[tr].s); gen(tr+1,br); } } void test(){ memset(dp,-1,sizeof(dp)); scanf("%i %i",&n,&k); a.resize(n); for(int i=0;i<n;i++) scanf("%i %i",&a[i].f.s,&a[i].f.f),a[i].s=i; sort(all(a)); //cout << a << endl; assert(calc(0,0)>=0); pick.clear(); ostali.clear(); gen(0,0); printf("%i\n",pick.size()+ostali.size()*2); for(int i=0;i<k-1;i++) printf("%i ",pick[i]+1); for(auto p:ostali) printf("%i -%i ",p+1,p+1); //assert(!pick.empty()); if(pick.size()) printf("%i",pick[k-1]+1); printf("\n"); } int main() { int t; scanf("%i",&t); while(t--){ test(); } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int Maxn = 88; struct node { int a, b, id; } g[Maxn]; int f[Maxn][Maxn], s[Maxn]; bool flag[Maxn][Maxn], vis[Maxn]; int a[Maxn], b[Maxn], id[Maxn]; int n, m, cnt; inline bool cmp(node x, node y) { return x.b < y.b; } void init() { for (int i = 1; i <= n; ++i) for (int j = 0; j <= m; ++j) f[i][j] = flag[i][j] = 0; for (int i = 1; i <= n; ++i) s[i] = vis[i] = a[i] = b[i] = id[i] = 0; cnt = n = m = 0; } inline int read() { int s = 0, w = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') w = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar(); return s * w; } int main() { int T = read(); while (T--) { n = read(), m = read(); for (int i = 1; i <= n; ++i) g[i].a = read(), g[i].b = read(), g[i].id = i; sort(g + 1, g + 1 + n, cmp); for (int i = 1; i <= n; ++i) a[i] = g[i].a, b[i] = g[i].b, id[i] = g[i].id; for (int i = 1; i <= n; ++i) for (int j = min(i, m); j >= 0; --j) { f[i][j] = f[i - 1][j] + b[i] * (m - 1), flag[i][j] = 0; if (j && f[i - 1][j - 1] + a[i] + b[i] * (j - 1) > f[i][j]) f[i][j] = f[i - 1][j - 1] + a[i] + b[i] * (j - 1), flag[i][j] = 1; } int i = n, j = m; while (i) { if (flag[i][j]) s[++cnt] = i, vis[i] = 1, --j; --i; } printf("%d\n", (n << 1) - m); for (int i = cnt; i > 1; --i) printf("%d ", id[s[i]]); for (int i = 1; i <= n; ++i) if (!vis[i]) printf("%d %d ", id[i], -id[i]); printf("%d\n", id[s[1]]); init(); } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; void read(int &val) { int x = 0; int bz = 1; char c; for (c = getchar(); (c < '0' || c > '9') && c != '-'; c = getchar()) ; if (c == '-') { bz = -1; c = getchar(); } for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - 48; val = x * bz; } const int maxn = 1e3 + 101; const int mod = 1e9 + 7; struct Edge { int from, to, cap, flow; long long cost; }; int a[maxn], b[maxn]; bool cmp(int x, int y) { return b[x] < b[y]; } int n, k; int N; struct MCMF { int n, m; vector<Edge> edge; vector<int> G[maxn]; int inq[maxn], p[maxn], a[maxn], To[maxn]; long long d[maxn]; int tag[maxn]; void init(int _n) { n = _n; for (int i = 0; i <= n; i++) G[i].clear(); edge.clear(); } void add(int from, int to, int cap, long long cost, int flag) { edge.push_back(Edge{from, to, cap, 0, cost}); edge.push_back(Edge{to, from, 0, 0, -cost}); m = edge.size(); G[from].push_back(m - 2); G[to].push_back(m - 1); tag[m - 2] = tag[m - 1] = flag; To[m - 1] = To[m - 2] = to - N; } bool BellFord(int s, int t, int &flow, long long &cost) { for (int i = 0; i <= n; i++) d[i] = INF; memset(inq, 0, sizeof(inq)); d[s] = 0; inq[s] = 1; p[s] = 0; a[s] = INF; queue<int> q; q.push(s); while (!q.empty()) { int u = q.front(); q.pop(); inq[u] = 0; for (auto to : G[u]) { Edge &e = edge[to]; if (e.cap > e.flow && d[e.to] > d[u] + e.cost) { d[e.to] = d[u] + e.cost; p[e.to] = to; a[e.to] = min(a[u], e.cap - e.flow); if (!inq[e.to]) { q.push(e.to); inq[e.to] = 1; } } } } if (d[t] == INF) return false; flow += a[t]; cost += 1LL * d[t] * a[t]; for (int u = t; u != s; u = edge[p[u]].from) { edge[p[u]].flow += a[t]; edge[p[u] ^ 1].flow -= a[t]; } return true; } void MincostMaxflow(int s, int t, long long &cost) { int flow = 0; cost = 0; while (BellFord(s, t, flow, cost)) ; vector<int> ans1, ans2; n = (n - 4) / 3; for (int i = 1; i <= n; i++) { for (auto to : G[i]) { if (edge[to].flow == false) continue; if (tag[to] == 1) { ans1.push_back(To[to]); } else if (tag[to] == 2) { ans2.push_back(To[to]); } } } sort(ans1.begin(), ans1.end(), cmp); int sz = ans1.size(); for (int i = 0; i <= sz - 2; i++) { printf("%d ", ans1[i]); } for (auto to : ans2) { printf("%d %d ", to, -to); } printf("%d\n", ans1[sz - 1]); } } M; int main() { int t; read(t); while (t--) { read(n); read(k); N = n; M.init(3 * n + 4); int mx = 0, j = 0; for (int i = 1; i <= n; i++) { read(a[i]); read(b[i]); if (mx < a[i]) j = i; mx = max(mx, a[i]); } if (k == 1) { printf("%d\n%d\n", 1, j); continue; } printf("%d\n", k + 2 * (n - k)); int s = 0, t = 3 * n + 3; for (int i = 1; i <= n; i++) { M.add(s, i, 1, 0, -1); } for (int i = 1; i <= k - 1; i++) { for (int j = 1; j <= n; j++) { M.add(i, j + n, 1, -(1LL * (i - 1) * b[j] + a[j]), 1); } } for (int i = k; i <= n - 1; i++) { for (int j = 1; j <= n; j++) { M.add(i, j + n, 1, -(1LL * (k - 1) * b[j]), 2); } } for (int j = 1; j <= n; j++) { M.add(n, j + n, 1, -(1LL * (k - 1) * b[j] + a[j]), 1); } for (int i = n + 1; i <= n + n; i++) { M.add(i, t, 1, 0, -1); } long long cost = 0; M.MincostMaxflow(s, t, cost); } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const long long INF = (long long)1000000007 * 1000000007; const long double eps = 1e-8; const long double pi = acos(-1.0); int dx[4] = {1, -1, 0, 0}; int dy[4] = {0, 0, 1, -1}; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } template <class Cap, class Cost> struct MinCostFlow { public: MinCostFlow() {} MinCostFlow(int n) : _n(n), g(n) {} int add_edge(int from, int to, Cap cap, Cost cost) { assert(0 <= from && from < _n); assert(0 <= to && to < _n); int m = int(pos.size()); pos.push_back({from, int(g[from].size())}); g[from].push_back(_edge{to, int(g[to].size()), cap, cost}); g[to].push_back(_edge{from, int(g[from].size()) - 1, 0, -cost}); return m; } struct edge { int from, to; Cap cap, flow; Cost cost; }; edge get_edge(int i) { int m = int(pos.size()); assert(0 <= i && i < m); auto _e = g[pos[i].first][pos[i].second]; auto _re = g[_e.to][_e.rev]; return edge{ pos[i].first, _e.to, _e.cap + _re.cap, _re.cap, _e.cost, }; } std::vector<edge> edges() { int m = int(pos.size()); std::vector<edge> result(m); for (int i = 0; i < m; i++) { result[i] = get_edge(i); } return result; } std::pair<Cap, Cost> flow(int s, int t) { return flow(s, t, std::numeric_limits<Cap>::max()); } std::pair<Cap, Cost> flow(int s, int t, Cap flow_limit) { return slope(s, t, flow_limit).back(); } std::vector<std::pair<Cap, Cost>> slope(int s, int t) { return slope(s, t, std::numeric_limits<Cap>::max()); } std::vector<std::pair<Cap, Cost>> slope(int s, int t, Cap flow_limit) { assert(0 <= s && s < _n); assert(0 <= t && t < _n); assert(s != t); std::vector<Cost> dual(_n, 0), dist(_n); std::vector<int> pv(_n), pe(_n); std::vector<bool> vis(_n); auto dual_ref = [&]() { std::fill(dist.begin(), dist.end(), std::numeric_limits<Cost>::max()); std::fill(pv.begin(), pv.end(), -1); std::fill(pe.begin(), pe.end(), -1); std::fill(vis.begin(), vis.end(), false); struct Q { Cost key; int to; bool operator<(Q r) const { return key > r.key; } }; std::priority_queue<Q> que; dist[s] = 0; que.push(Q{0, s}); while (!que.empty()) { int v = que.top().to; que.pop(); if (vis[v]) continue; vis[v] = true; if (v == t) break; for (int i = 0; i < int(g[v].size()); i++) { auto e = g[v][i]; if (vis[e.to] || !e.cap) continue; Cost cost = e.cost - dual[e.to] + dual[v]; if (dist[e.to] - dist[v] > cost) { dist[e.to] = dist[v] + cost; pv[e.to] = v; pe[e.to] = i; que.push(Q{dist[e.to], e.to}); } } } if (!vis[t]) { return false; } for (int v = 0; v < _n; v++) { if (!vis[v]) continue; dual[v] -= dist[t] - dist[v]; } return true; }; Cap flow = 0; Cost cost = 0, prev_cost = -1; std::vector<std::pair<Cap, Cost>> result; result.push_back({flow, cost}); while (flow < flow_limit) { if (!dual_ref()) break; Cap c = flow_limit - flow; for (int v = t; v != s; v = pv[v]) { c = std::min(c, g[pv[v]][pe[v]].cap); } for (int v = t; v != s; v = pv[v]) { auto &e = g[pv[v]][pe[v]]; e.cap -= c; g[v][e.rev].cap += c; } Cost d = -dual[s]; flow += c; cost += c * d; if (prev_cost == d) { result.pop_back(); } result.push_back({flow, cost}); prev_cost = cost; } return result; } private: int _n; struct _edge { int to, rev; Cap cap; Cost cost; }; std::vector<std::pair<int, int>> pos; std::vector<std::vector<_edge>> g; }; using edge = MinCostFlow<int, int>::edge; void add_neg_edge(MinCostFlow<int, int> &mcf, int &ans, int S, int T, int from, int to, int cap, int cost) { mcf.add_edge(S, to, cap, 0); mcf.add_edge(from, T, cap, 0); mcf.add_edge(to, from, cap, -cost); ans += cost * cap; } int n, k; vector<int> a(n), b(n); void solve() { MinCostFlow<int, int> mcf(n + k + 5); int ans = 0; int S = n + k + 1; for (int i = 0; i < n; i++) { add_neg_edge(mcf, ans, S, S + 1, i, n + k, 1, -(k - 1) * b[i]); for (int j = 0; j < k; j++) { add_neg_edge(mcf, ans, S, S + 1, i, n + j, 1, -a[i] - j * b[i]); } } for (int i = 0; i < n; i++) { mcf.add_edge(S, i, 1, 0); } for (int j = 0; j < k; j++) { mcf.add_edge(n + j, S + 1, 1, 0); } mcf.add_edge(n + k, S + 1, n - k, 0); pair<int, int> p = mcf.flow(S, S + 1); auto es = mcf.edges(); vector<int> v(k); vector<bool> used(n); for (auto e : es) { if (e.from - n < 0 || e.from >= n + k) continue; if (e.flow == 0) { v[e.from - n] = e.to; used[e.to] = true; } } printf("%d\n", 2 * n - k); for (int i = 0; i < k - 1; i++) { printf("%d ", v[i] + 1); } for (int i = 0; i < n; i++) { if (!used[i]) { printf("%d %d ", i + 1, -i - 1); cout << i + 1 << " " << -(i + 1) << " "; } } printf("%d\n", v[k - 1] + 1); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(50); int t; scanf("%d", &t); for (int i = 0; i < t; i++) { scanf("%d%d", &n, &k); a.resize(n); b.resize(n); for (int i = 0; i < n; i++) scanf("%d%d", &a[i], &b[i]); solve(); } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1); const double eps = 1e-12; const int inf = 2000000000; const long long int infLL = (long long int)1e18; long long int MOD = 1000000007; int MOD1 = 1000000007; int MOD2 = 1000000009; inline bool checkBit(long long int n, long long int i) { return n & (1LL << i); } inline long long int setBit(long long int n, long long int i) { return n | (1LL << i); ; } inline long long int resetBit(long long int n, long long int i) { return n & (~(1LL << i)); } int dx[] = {0, 0, +1, -1}; int dy[] = {+1, -1, 0, 0}; inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } inline bool isLeapYear(long long int year) { return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); } inline void normal(long long int &a) { a %= MOD; (a < 0) && (a += MOD); } inline long long int modMul(long long int a, long long int b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a * b) % MOD; } inline long long int modAdd(long long int a, long long int b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a + b) % MOD; } inline long long int modSub(long long int a, long long int b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; } inline long long int modPow(long long int b, long long int p) { long long int r = 1LL; while (p) { if (p & 1) r = modMul(r, b); b = modMul(b, b); p >>= 1LL; } return r; } inline long long int modDiv(long long int a, long long int b) { return modMul(a, modPow(b, MOD - 2)); } bool comp(const pair<long long int, pair<long long int, long long int> > &p1, const pair<long long int, pair<long long int, long long int> > &p2) { return p1.first > p2.first; } bool comp1(const pair<long long int, long long int> &p1, const pair<long long int, long long int> &p2) { if (p1.first == p2.first) { return p1.second > p2.second; } return p1.first < p2.first; } long long int converter(string a) { long long int i, mul = 1LL, r, t, ans = 0LL; if (a.length() == 0) return 0; for (i = a.length() - 1; i >= 0; i--) { t = a[i] - '0'; r = t % 10; ans += (mul * r); mul = mul * 10; } return ans; } int msb(unsigned x) { union { double a; int b[2]; }; a = x; return (b[1] >> 20) - 1023; } const int MAX = 78; int t, n, k; pair<pair<long long int, long long int>, long long int> p[MAX]; int dp[MAX][MAX]; vector<long long int> good, bad; long long int solve(int idx, int cnt) { if (idx == n + 1) { if (cnt != k) return -infLL; return 0; } if (dp[idx][cnt] != -1) return dp[idx][cnt]; long long int ret = 0; if (cnt + 1 <= k) { ret = max(ret, cnt * p[idx].first.first + p[idx].first.second + solve(idx + 1, cnt + 1)); } ret = max(ret, ((k - 1) * p[idx].first.first) + solve(idx + 1, cnt)); return dp[idx][cnt] = ret; } void trace(int idx, int cnt) { if (idx == n + 1) return; long long int ret1 = 0LL, ret2 = 0LL; if (cnt + 1 <= k) ret1 = cnt * p[idx].first.first + p[idx].first.second + solve(idx + 1, cnt + 1); ret2 = ((k - 1) * p[idx].first.first) + solve(idx + 1, cnt); if (ret1 >= ret2) { good.push_back(p[idx].second); trace(idx + 1, cnt + 1); } else { bad.push_back(p[idx].second); trace(idx + 1, cnt); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cout.unsetf(ios::floatfield); cout.precision(20); cout.setf(ios::fixed, ios::floatfield); ; cin >> t; while (t--) { good.clear(); bad.clear(); cin >> n >> k; for (int i = 1; i <= n; ++i) { cin >> p[i].first.second >> p[i].first.first; p[i].second = i; } sort(p + 1, p + n + 1); for (int i = 1; i <= n; ++i) ; memset(dp, -1, sizeof(dp)); long long int now = solve(1, 0); trace(1, 0); cout << good.size() + (2 * bad.size()) << '\n'; for (int i = 0; i < good.size() - 1; ++i) { cout << good[i] << " "; } for (int i = 0; i < bad.size(); ++i) { cout << bad[i] << " " << -1 * bad[i] << " "; } cout << good[good.size() - 1] << '\n'; } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T> ostream &operator<<(ostream &os, vector<T> V) { os << "[ "; for (auto v : V) os << v << " "; os << "]"; return os; } template <class T> ostream &operator<<(ostream &os, set<T> S) { os << "{ "; for (auto s : S) os << s << " "; return os << "}"; } template <class L, class R> ostream &operator<<(ostream &os, map<L, R> M) { os << "{ "; for (auto m : M) os << "(" << m.first << ":" << m.second << ") "; return os << "}"; } template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) { return os << "(" << P.first << "," << P.second << ")"; } const long long N = 75; long long n, k, dp[N][N], taken[N][N]; vector<pair<pair<long long, long long>, long long>> p; vector<long long> v1, v2; bool compare(pair<pair<long long, long long>, long long> &a, pair<pair<long long, long long>, long long> &b) { if (a.first.second != b.first.second) return a.first.second < b.first.second; return a.first.first < b.first.first; } bool compare1(long long a, long long b) { return p[a].first.first < p[b].first.first; } long long rec(long long in, long long done) { if (in == n) return 0; if (dp[in][done] != -1) return dp[in][done]; long long x = (k - 1) * p[in].first.second + rec(in + 1, done); taken[in][done] = 0; if (done < k) { if (done * p[in].first.second + p[in].first.first + rec(in + 1, done + 1) > x) taken[in][done] = 1, x = done * p[in].first.second + p[in].first.first + rec(in + 1, done + 1); } return dp[in][done] = x; } void restore(long long in, long long done) { if (in == n) return; if (taken[in][done] == 1) v1.push_back(in), restore(in + 1, done + 1); else v2.push_back(in), restore(in + 1, done); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t; cin >> t; while (t--) { cin >> n >> k; p.clear(); p.resize(n); for (long long i = 0; i < n; i++) cin >> p[i].first.first >> p[i].first.second, p[i].second = i + 1; sort(p.begin(), p.end(), compare); memset(dp, -1, sizeof(dp)); v1.clear(), v2.clear(); rec(0, 0); restore(0, 0); cout << v1.size() + v2.size() * 2 << '\n'; sort(v2.begin(), v2.end(), compare1); for (long long i = 0; i < v1.size() - 1; i++) cout << p[v1[i]].second << ' '; for (long long i = 0; i < v2.size(); i++) cout << p[v2[i]].second << ' ' << -p[v2[i]].second << ' '; cout << p[v1.back()].second << '\n'; } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int inf = 2e9; struct mob { int a, b, id; }; void solve() { int n, k; cin >> n >> k; vector<mob> m(n); for (int i = 0; i < n; ++i) { cin >> m[i].a >> m[i].b; m[i].id = i; } sort(m.begin(), m.end(), [&](mob a, mob b) { return a.b < b.b; }); vector<vector<int>> dp(n + 1, vector<int>(k + 1, -inf)); dp[0][0] = 0; vector<vector<int>> p = dp; for (int i = 0; i < n; ++i) { for (int j = 0; j < k; ++j) { if (dp[i][j] >= 0) { if (dp[i + 1][j] < dp[i][j] + m[i].b * (k - 1)) { dp[i + 1][j] = dp[i][j] + m[i].b * (k - 1); p[i + 1][j] = j; } if (dp[i + 1][j + 1] < dp[i][j] + m[i].b * j + m[i].a) { dp[i + 1][j + 1] = dp[i][j] + m[i].b * j + m[i].a; p[i + 1][j + 1] = j; } } } } vector<int> g1, g2; int nn = k; for (int i = n; i; --i) { if (p[i][nn] == nn) g2.push_back(m[i - 1].id); else g1.push_back(m[i - 1].id); nn = p[i][nn]; } reverse(g1.begin(), g1.end()); reverse(g2.begin(), g2.end()); cout << g1.size() + g2.size() * 2 << '\n'; for (int i = 0; i < (int)g1.size() - 1; ++i) cout << g1[i] + 1 << ' '; for (int i = 0; i < (int)g2.size(); ++i) cout << g2[i] + 1 << ' ' << -(g2[i] + 1) << ' '; cout << g1.back() + 1 << '\n'; }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t--) solve(); }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; template <typename T1, typename T2> inline void chkmin(T1 &x, const T2 &y) { if (x > y) x = y; } template <typename T1, typename T2> inline void chkmax(T1 &x, const T2 &y) { if (x < y) x = y; } struct fa { int a, b, ind; fa() {} }; int n, k; const int MAXN = 76; fa a[MAXN]; const int INF = 1e9; int dp[MAXN][MAXN][2]; int par[MAXN][MAXN][2]; void relax(int pref, int cnt, int ind, int val, int fpar) { if (dp[pref][cnt][ind] >= val) return; dp[pref][cnt][ind] = val; par[pref][cnt][ind] = fpar; } void solve() { cin >> n >> k; for (int i = 0; i < n; i++) { cin >> a[i].a >> a[i].b; a[i].ind = i + 1; } sort(a, a + n, [&](fa i, fa j) { return i.b < j.b; }); if (k == 1) { int pos = max_element(a, a + n, [&](fa i, fa j) { return i.a < j.a; }) - a; cout << 1 << "\n" << a[pos].ind << "\n"; return; } if (k == n) { cout << n << "\n"; for (int i = 0; i < n; i++) { cout << a[i].ind << " "; } cout << "\n"; return; } for (int i = 0; i < MAXN; i++) { for (int j = 0; j < MAXN; j++) { for (int k = 0; k < 2; k++) { dp[i][j][k] = -INF; } } } cerr << "a = " << endl; for (int i = 0; i < n; i++) { cerr << a[i].a << " " << a[i].b << endl; } dp[0][0][0] = 0; for (int i = 0; i < n; i++) { for (int cnt = 0; cnt <= k - 1; cnt++) { for (int flag = 0; flag < 2; flag++) { if (dp[i][cnt][flag] == -INF) continue; if (cnt < k - 1) relax(i + 1, cnt + 1, flag, dp[i][cnt][flag] + a[i].a + a[i].b * cnt, 1); relax(i + 1, cnt, flag, dp[i][cnt][flag] + a[i].b * (k - 1), 2); if (!flag) { relax(i + 1, cnt, 1, dp[i][cnt][flag] + a[i].a + a[i].b * (k - 1), 3); } } } } int pos = 0, cnt = 0, flag = 0; for (int i = 0; i < MAXN; i++) { for (int j = 0; j < MAXN; j++) { for (int k = 0; k < 2; k++) { if (dp[i][j][k] > dp[pos][cnt][flag]) { pos = i, cnt = j, flag = k; } } } } cerr << "my_ans = " << dp[pos][cnt][flag] << endl; vector<pair<int, int>> ans; for (; pos >= 1; pos--) { ans.push_back({par[pos][cnt][flag], a[pos - 1].ind}); int ncnt = cnt; if (par[pos][cnt][flag] == 1) ncnt--; int nflag = flag; if (par[pos][cnt][flag] == 3) nflag--; cnt = ncnt; flag = nflag; } sort(ans.begin(), ans.end()); vector<int> fout; for (auto i : ans) { fout.push_back(i.second); if (i.first == 2) { fout.push_back(-i.second); } } cout << fout.size() << "\n"; for (auto i : fout) { cout << i << " "; } cout << "\n"; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { solve(); } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize(2) const double esp = 1e-6; const double pi = acos(-1.0); const int INF = 0x3f3f3f3f; const int inf = 1e9; using namespace std; long long read() { char ch = getchar(); long long x = 0, f = 1; while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } const int N = 110; int T, n, k; struct node { long long a, b; int id; bool operator<(const node temp) const { return b < temp.b; } } nd[N]; long long dp[N][N]; int pre[N][N]; int main() { T = read(); while (T--) { n = read(), k = read(); for (int i = 0; i <= n; i++) for (int j = 0; j <= k; j++) dp[i][j] = -1e18; for (int i = 1; i <= n; i++) { nd[i].a = read(), nd[i].b = read(); nd[i].id = i; } sort(nd + 1, nd + n + 1); dp[0][0] = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= k; j++) { if (j > i) break; dp[i][j] = dp[i - 1][j - 1] + nd[i].a + (j - 1) * nd[i].b; pre[i][j] = j - 1; if (dp[i - 1][j] >= 0) { long long temp = dp[i - 1][j] + (k - 1) * nd[i].b; if (temp > dp[i][j]) { dp[i][j] = temp; pre[i][j] = j; } } } } vector<int> ans; vector<int> ans2; int kk = k; for (int i = n; i >= 1; i--) { if (pre[i][kk] == kk) { ans2.push_back(-nd[i].id); ans2.push_back(nd[i].id); } else { ans.push_back(nd[i].id); kk = pre[i][kk]; } } reverse(ans.begin(), ans.end()); reverse(ans2.begin(), ans2.end()); cout << ans.size() + ans2.size() << endl; for (int i = 0; i < ans.size() - 1; i++) { printf("%d ", ans[i]); } for (auto k : ans2) printf("%d ", k); printf("%d\n", ans.back()); } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct flow { static const int N = 200005, INF = 1e9; int n, s, t; struct edge { int v, w, f, c, nxt; } e[N]; int head[N], cur[N], cnt; void addedge(int u, int v, int w, int f, int c) { cnt++; e[cnt].v = v, e[cnt].w = w, e[cnt].f = f, e[cnt].c = c; e[cnt].nxt = head[u]; head[u] = cnt; } void add(int u, int v, int w, int c = 0) { addedge(u, v, w, w, c); addedge(v, u, w, 0, -c); } void init() { cnt = 0; for (int i = 1; i <= n; i++) head[i] = 0; } int dep[N], q[N]; int bfs() { for (int i = 1; i <= n; i++) dep[i] = -1, cur[i] = head[i]; dep[s] = 0; int l = 0, r = 1; q[0] = s; while (l < r) { int u = q[l++]; for (int i = head[u]; i; i = e[i].nxt) { if (dep[e[i].v] == -1 && e[i].f > 0) { dep[e[i].v] = dep[u] + 1; q[r++] = e[i].v; } } } return dep[t] != -1; } int dfs(int u, int lim) { if (u == t || lim == 0) return lim; int flow = 0, f; for (int i = cur[u]; i; i = e[i].nxt) { cur[u] = i; if (dep[e[i].v] == dep[u] + 1 && (f = dfs(e[i].v, min(lim, e[i].f)))) { flow += f; lim -= f; e[i].f -= f; e[(i - 1 ^ 1) + 1].f += f; if (!lim) break; } } return flow; } int max_flow() { int mf = 0; while (bfs()) mf += dfs(s, INF); return mf; } int h[N], dis[N], pre[N], pu[N]; priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; int min_cost(int f) { int res = 0; for (int i = 1; i <= cnt; i += 2) e[i].f = e[i].w; for (int i = 2; i <= cnt; i += 2) e[i].f = 0; while (f > 0) { for (int i = 1; i <= n; i++) dis[i] = INF; dis[s] = 0; pq.emplace(0, s); while (!pq.empty()) { auto p = pq.top(); pq.pop(); int u = p.second; if (dis[u] < p.first) continue; for (int i = head[u]; i; i = e[i].nxt) { int v = e[i].v, c = e[i].c, f = e[i].f; if (f > 0 && dis[v] > dis[u] + c + h[u] - h[v]) { dis[v] = dis[u] + c + h[u] - h[v]; pre[v] = i, pu[v] = u; pq.emplace(dis[v], v); } } } if (dis[t] == INF) return -1; for (int i = 1; i <= n; i++) h[i] += dis[i]; int d = f; for (int u = t; u != s; u = pu[u]) d = min(d, e[pre[u]].f); f -= d; res += d * h[t]; for (int u = t; u != s; u = pu[u]) { e[pre[u]].f -= d; e[(pre[u] - 1 ^ 1) + 1].f += d; } } return res; } } g; const int N = 200; int n, k, a[N], b[N], x[N], y[N], t; vector<int> ans; int main() { scanf("%d", &t); while (t--) { scanf("%d%d", &n, &k); g.n = 2 * n + 2, g.s = 2 * n + 1, g.t = 2 * n + 2; g.init(); for (int i = 1; i <= n; i++) { g.add(g.s, i, 1, 0); g.add(i + n, g.t, 1, 0); scanf("%d%d", &a[i], &b[i]); for (int j = 1; j < k; j++) g.add(i, j + n, 1, -(a[i] + (j - 1) * b[i])); for (int j = k; j < n; j++) g.add(i, j + n, 1, -((k - 1) * b[i])); g.add(i, n + n, 1, -(a[i] + (k - 1) * b[i])); } if (k == 1) { int mx = 1; for (int i = 1; i <= n; i++) if (a[i] > a[mx]) mx = i; printf("1\n%d\n", mx); continue; } g.min_cost(g.max_flow()); for (int i = 1; i <= n; i++) { for (int j = g.head[i]; j; j = g.e[j].nxt) if (j != g.s && j % 2 && g.e[j].f == 0) x[i] = g.e[j].v - n; y[x[i]] = i; } ans.clear(); for (int i = 1; i < k; i++) ans.push_back(y[i]); for (int i = k; i < n; i++) ans.push_back(y[i]), ans.push_back(-y[i]); ans.push_back(y[n]); printf("%d\n", ans.size()); for (auto i : ans) printf("%d ", i); puts(""); } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1); const double eps = 1e-12; const int inf = 2000000000; const long long int infLL = (long long int)1e18; long long int MOD = 1000000007; int MOD1 = 1000000007; int MOD2 = 1000000009; inline bool checkBit(long long int n, long long int i) { return n & (1LL << i); } inline long long int setBit(long long int n, long long int i) { return n | (1LL << i); ; } inline long long int resetBit(long long int n, long long int i) { return n & (~(1LL << i)); } int dx[] = {0, 0, +1, -1}; int dy[] = {+1, -1, 0, 0}; inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } inline bool isLeapYear(long long int year) { return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); } inline void normal(long long int &a) { a %= MOD; (a < 0) && (a += MOD); } inline long long int modMul(long long int a, long long int b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a * b) % MOD; } inline long long int modAdd(long long int a, long long int b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a + b) % MOD; } inline long long int modSub(long long int a, long long int b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; } inline long long int modPow(long long int b, long long int p) { long long int r = 1LL; while (p) { if (p & 1) r = modMul(r, b); b = modMul(b, b); p >>= 1LL; } return r; } inline long long int modDiv(long long int a, long long int b) { return modMul(a, modPow(b, MOD - 2)); } bool comp(const pair<long long int, pair<long long int, long long int> > &p1, const pair<long long int, pair<long long int, long long int> > &p2) { return p1.first > p2.first; } bool comp1(const pair<long long int, long long int> &p1, const pair<long long int, long long int> &p2) { if (p1.first == p2.first) { return p1.second > p2.second; } return p1.first < p2.first; } long long int converter(string a) { long long int i, mul = 1LL, r, t, ans = 0LL; if (a.length() == 0) return 0; for (i = a.length() - 1; i >= 0; i--) { t = a[i] - '0'; r = t % 10; ans += (mul * r); mul = mul * 10; } return ans; } int msb(unsigned x) { union { double a; int b[2]; }; a = x; return (b[1] >> 20) - 1023; } const int MAX = 78; int t, n, k; pair<pair<long long int, long long int>, long long int> p[MAX]; long long int dp[MAX][MAX]; vector<long long int> good, bad; long long int solve(int idx, int cnt) { if (idx == n + 1) { if (cnt != k) return -infLL; return 0; } if (dp[idx][cnt] != -1) return dp[idx][cnt]; long long int ret = 0; if (cnt + 1 <= k) { ret = max(ret, cnt * p[idx].first.first + p[idx].first.second + solve(idx + 1, cnt + 1)); } ret = max(ret, ((k - 1) * p[idx].first.first) + solve(idx + 1, cnt)); return dp[idx][cnt] = ret; } void trace(int idx, int cnt) { if (idx == n + 1) return; long long int ret1 = 0LL, ret2 = 0LL; if (cnt + 1 <= k) ret1 = cnt * p[idx].first.first + p[idx].first.second + solve(idx + 1, cnt + 1); ret2 = ((k - 1) * p[idx].first.first) + solve(idx + 1, cnt); if (ret1 > ret2) { trace(idx + 1, cnt + 1); good.push_back(p[idx].second); } else { trace(idx + 1, cnt); bad.push_back(p[idx].second); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cout.unsetf(ios::floatfield); cout.precision(20); cout.setf(ios::fixed, ios::floatfield); ; cin >> t; while (t--) { good.clear(); bad.clear(); long long int maxi = 0, idx; cin >> n >> k; for (int i = 1; i <= n; ++i) { cin >> p[i].first.second >> p[i].first.first; p[i].second = i; if (p[i].first.second >= maxi) maxi = p[i].first.second, idx = i; } if (k == 1) { cout << 1 << '\n'; cout << idx << '\n'; continue; } sort(p + 1, p + n + 1); memset(dp, -1, sizeof(dp)); long long int now = solve(1, 0); ; trace(1, 0); reverse((good).begin(), (good).end()); reverse((bad).begin(), (bad).end()); if (good.size() == 0) { cout << now << '\n'; continue; } cout << good.size() + (2 * bad.size()) << '\n'; for (int i = 0; i < (int)good.size() - 1; ++i) { cout << good[i] << " "; } for (int i = 0; i < bad.size(); ++i) { cout << bad[i] << " " << -1 * bad[i] << " "; } cout << good[good.size() - 1] << '\n'; } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
python3
import sys readline = sys.stdin.readline read = sys.stdin.read ns = lambda: readline().rstrip() ni = lambda: int(readline().rstrip()) nm = lambda: map(int, readline().split()) nl = lambda: list(map(int, readline().split())) prn = lambda x: print(*x, sep='\n') def solve(): n, k = nm() mini = [tuple(nl() + [i+1]) for i in range(n)] mini.sort(key = lambda x: -x[1]) # print(mini) dp = [-1]*(k+1) dp[0] = 0 f = [[0]*(k+1) for _ in range(n)] for i in range(n): for j in range(k-1, -1, -1): if dp[j] < 0: continue if dp[j+1] < dp[j] + mini[i][0] + (k - j - 1)*mini[i][1]: dp[j+1] = dp[j] + mini[i][0] + (k - j - 1)*mini[i][1] f[i][j+1] = 1 dp[j] += (k - 1) * mini[i][1] # print(dp) cx = k a = list() b = list() for i in range(n-1, -1, -1): if f[i][cx]: a.append(mini[i][2]) cx -= 1 else: b.append(mini[i][2]) com = list() for x in a[:-1]: com.append(x) for x in b: com.append(x) com.append(-x) com.append(a[-1]) print(len(com)) print(*com) return # solve() T = ni() for _ in range(T): solve()
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; vector<pair<pair<int, int>, int>> v; long long dp[80][80]; pair<int, int> from[80][80]; set<int> s; int main() { int t; cin >> t; while (t) { t--; int i, j, n, k; cin >> n >> k; v.clear(); s.clear(); for (i = 1; i <= n; i++) { int a, b; cin >> a >> b; v.push_back({{b, a - b}, i}); s.insert(i); } sort(v.begin(), v.end()); for (i = 0; i <= n; i++) { for (j = 0; j <= k; j++) { dp[i][j] = -100000000000; from[i][j] = {-1, -1}; } } dp[0][0] = 0; for (i = 0; i < n; i++) { int ind = v[i].second; long long a = v[i].first.second, b = v[i].first.first; for (j = 0; j <= k; j++) { if (dp[i + 1][j] < dp[i][j]) { from[i + 1][j] = {ind, j}; dp[i + 1][j] = dp[i][j]; } if (j > 0 && dp[i + 1][j] < dp[i][j - 1] + (j - 1) * b + a) { from[i + 1][j] = {ind, j - 1}; dp[i + 1][j] = dp[i][j - 1] + (j - 1) * b + a; } } } vector<int> ans; int nx = n, ny = k; while (ny != 0) { pair<int, int> p = from[nx][ny]; if (p.second != ny) { ans.push_back(p.first); s.erase(p.first); } nx--; ny = p.second; } cout << 2 * n - k << endl; for (i = k - 1; i > 0; i--) { cout << ans[i] << " "; } for (int x : s) { cout << x << " " << -x << " "; } cout << ans[0] << endl; } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename T> inline bool upmin(T &x, T y) { return y < x ? x = y, 1 : 0; } template <typename T> inline bool upmax(T &x, T y) { return x < y ? x = y, 1 : 0; } const long double eps = 1e-11; const long double pi = acos(-1); const int oo = 1 << 30; const long long loo = 1ll << 62; const int mods = 1e9 + 7; const int MAXN = 5005; const int INF = 0x3f3f3f3f; inline int read() { int f = 1, x = 0; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 3) + (x << 1) + (c ^ 48); c = getchar(); } return x * f; } int a[105], b[105], f[105][105], flag[105], frm[105][105], id[105]; vector<int> V; signed main() { int Case = read(); while (Case--) { memset(f, -INF, sizeof f); memset(frm, 0, sizeof frm); memset(flag, 0, sizeof flag); int n = read(), k = read(); for (int i = 1; i <= n; i++) a[i] = read(), b[i] = read(), id[i] = i; sort(id + 1, id + n + 1, [&](int x, int y) { return b[x] < b[y]; }); V.clear(); f[0][0] = 0; for (int i = 1; i <= n; i++) for (int j = 0; j <= k; j++) { if (j && upmax(f[i][j], f[i - 1][j - 1] + a[id[i]] + b[id[i]] * (j - 1))) frm[i][j] = 1; if (upmax(f[i][j], f[i - 1][j] + b[id[i]] * (k - 1))) frm[i][j] = 2; } int nw = k; for (int i = n; i >= 1; i--) if (frm[i][nw] == 1) V.push_back(id[i]), nw--; else flag[id[i]] = 1; for (int i = 0; i < V.size() - 1; i++) printf("%d ", V[i]); for (int i = 1; i <= n; i++) if (flag[i]) printf("%d -%d ", i, i); printf("%d\n", V[V.size() - 1]); } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int T; cin >> T; while (T--) { int N, K; cin >> N >> K; vector<pair<int, pair<int, int>>> V(N); int a, b; for (int i = 0; i < N; i++) { cin >> a >> b; V[i] = {b, {a, i}}; } sort(V.begin(), V.end()); long long maxv = 0; vector<int> ans; for (int i = 0; i < N; i++) { vector<vector<long long>> dp(N, vector<long long>(K + 1, -1)); vector<vector<int>> rev(N, vector<int>(K + 1, -1)); vector<vector<int>> par(N, vector<int>(K + 1, -1)); for (int i = 0; i < N; i++) dp[i][0] = 0; int ti = 0; for (int j = 0; j < N; j++) { auto t = V[j]; if (j == i) continue; for (int k = 0; k < K; k++) { if (dp[ti][k] < 0) continue; long long temp = dp[ti][k] + t.first * (K - 1); if (dp[ti + 1][k] < 0 || dp[ti + 1][k] < temp) dp[ti + 1][k] = temp, rev[ti + 1][k] = -t.second.second - 1, par[ti + 1][k] = k; temp = dp[ti][k] + t.second.first + t.first * (k - 1); if (dp[ti + 1][k + 1] < 0 || dp[ti + 1][k + 1] < temp) dp[ti + 1][k + 1] = temp, rev[ti + 1][k + 1] = t.second.second + 1, par[ti + 1][k + 1] = k; } ti++; } vector<int> tans = {V[i].second.second + 1}; int j = K - 1; ti = N - 1; while (ti >= 0) { int t = rev[ti][j]; if (t < 0) { tans.push_back(t); tans.push_back(-t); } else tans.push_back(t); j = par[ti][j]; if (j <= 0) break; --ti; } long long v = dp[N - 1][K - 1] + V[i].first * (K - 1) + V[i].second.first; if (v > maxv) maxv = v, ans = tans; } cout << ans.size() << "\n"; for (int i = ans.size() - 1; i >= 0; i--) cout << ans[i] << " "; cout << "\n"; } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct Dinic { const static int maxn = 1000 + 10; const static int INT_INF = 1061109567; const static long long LLONG_INF = 4557430888798830399; struct Edge { int from, to; long long cap, flow, cost; }; vector<Edge> edges; vector<int> G[maxn]; bool ins[maxn]; long long d[maxn], a[maxn], pre[maxn]; long long maxFlow, minCost; void AddEdge(int from, int to, int cap, long long cost) { cost = -cost; edges.push_back((Edge){from, to, cap, 0, cost}); edges.push_back((Edge){to, from, 0, 0, -cost}); int m = edges.size(); G[from].push_back(m - 2); G[to].push_back(m - 1); } bool BellManFord(int s, int t) { memset(d, 0x3f, sizeof(d)); d[s] = 0; memset(pre, -1, sizeof(pre)); memset(ins, false, sizeof(ins)); ins[s] = true; queue<int> Q; Q.push(s); a[s] = LLONG_INF; while (!Q.empty()) { int u = Q.front(); Q.pop(); ins[u] = false; for (int i = 0; i < (int)G[u].size(); i++) { Edge& e = edges[G[u][i]]; if (e.cap > e.flow && d[e.to] > d[u] + e.cost) { d[e.to] = d[u] + e.cost; pre[e.to] = G[u][i]; a[e.to] = min(a[u], e.cap - e.flow); if (!ins[e.to]) { Q.push(e.to); ins[e.to] = true; } } } } if (d[t] == LLONG_INF) { return false; } maxFlow += a[t]; minCost += d[t] * a[t]; for (int i = t; i != s; i = edges[pre[i]].from) { edges[pre[i]].flow += a[t]; edges[pre[i] ^ 1].flow -= a[t]; } return true; } void MCMF(int s, int t) { while (BellManFord(s, t)) ; } } mcmf[100]; const int N = 1000 + 10; int a[N], b[N]; int main() { int T; scanf("%d", &T); for (int t = 1; t <= T; t++) { int n, k; scanf("%d %d", &n, &k); for (int i = 1; i <= n; i++) { scanf("%d %d", a + i, b + i); } int S = 3 * n + 1, T = 3 * n + 2; for (int i = 1; i <= n; i++) { mcmf[t].AddEdge(S, i, 1, 0); mcmf[t].AddEdge(i + n, T, 1, 1e10); mcmf[t].AddEdge(i + n + k, T, 1, 0); } for (int i = 1; i <= n; i++) { for (int j = 1; j <= k; j++) { mcmf[t].AddEdge(i, j + n, 1, a[i] + (j - 1) * b[i]); mcmf[t].AddEdge(i, j + n + k, 1, (j - 1) * b[i]); } } mcmf[t].MCMF(S, T); vector<int> vec[2][N]; int ans = 0; for (auto e : mcmf[t].edges) { if (e.flow == e.cap) { if (1 <= e.from && e.from <= n) { if (n + 1 <= e.to && e.to <= n + k) { vec[0][e.to - n].push_back(e.from); ans++; } else if (n + 1 + k <= e.to && e.to <= n + 2 * k && e.cost != 0) { vec[1][e.to - n - k].push_back(e.from); vec[1][e.to - n - k].push_back(-e.from); ans += 2; } } } } printf("%d\n", ans); for (int i = 1; i <= n; i++) { for (int j = 0; j < 2; j++) { for (auto k : vec[j][i]) { printf("%d ", k); } } } puts(""); } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long NMAX = 200001; const long long INF = (1 << 30); const long long MOD = 1000000007; const long long BLOCK = 101; const long long nr_of_bits = 18; int dp[76][76]; struct ura { ; int first, second, third; } v[76]; bool cmp(ura a, ura b) { return a.second < b.second; } vector<int> sol; vector<int> sel; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) { int n, k, i, j; cin >> n >> k; sol.clear(); sel.clear(); for (i = 0; i <= n; i++) { for (j = 0; j <= k; j++) dp[i][j] = 0; } for (i = 1; i <= n; i++) { cin >> v[i].first >> v[i].second; v[i].third = i; } sort(v + 1, v + n + 1, cmp); for (i = 1; i <= n; i++) { for (j = 0; j <= min(i, k); j++) { if (i - 1 >= j) dp[i][j] = max(dp[i][j], dp[i - 1][j] + (k - 1) * v[i].second); if (j != 0) { dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + (j - 1) * v[i].second + v[i].first); } } } i = n, j = k; while (i != 0) { if (dp[i][j] == dp[i - 1][j] + (k - 1) * v[i].second) { sol.push_back(v[i].third); sol.push_back(-v[i].third); } else if (j != 0) { if (dp[i][j] == dp[i - 1][j - 1] + (j - 1) * v[i].second + v[i].first) { sel.push_back(v[i].third); j--; } } i--; } reverse(sel.begin(), sel.end()); cout << sol.size() + sel.size() << "\n"; for (int i = 0; i < sel.size() - 1; i++) { cout << sel[i] << " "; } for (auto x : sol) { cout << x << " "; } cout << sel.back() << "\n"; } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update using namespace std; using namespace __gnu_pbds; //typedef tree< // pair<long long, int>, // change type // null_type, // less<pair<long long, int> >, // change type // rb_tree_tag, // tree_order_statistics_node_update> // ordered_set; template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; vector<string> vec_splitter(string s) { s += ','; vector<string> res; while(!s.empty()) { res.push_back(s.substr(0, s.find(','))); s = s.substr(s.find(',') + 1); } return res; } void debug_out( vector<string> __attribute__ ((unused)) args, __attribute__ ((unused)) int idx, __attribute__ ((unused)) int LINE_NUM) { cerr << endl; } template <typename Head, typename... Tail> void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) { if(idx > 0) cerr << ", "; else cerr << "Line(" << LINE_NUM << ") "; stringstream ss; ss << H; cerr << args[idx] << " = " << ss.str(); debug_out(args, idx + 1, LINE_NUM, T...); } #define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__) typedef long long ll; #define rep(i, start, end) for(int i = start; i < end; ++i) #define sz(x) (int)((x).size()) #define pb push_back #define X first #define Y second #define all(x) x.begin(), x.end() #define clr(d, v) memset(d, v, sizeof(d)) #define pii pair<int, int> //#define debug(x) cerr << #x << " : " << (x) << " " const double PI = 3.14159265358979323846; const double eps = (1e-10); int isGreater(long double x, long double y) { if (abs(x - y) < eps) return 0; if (x > y) return 1; return -1; } const int N = 80; struct S{ int a, b, idx; bool operator<(const S& other) const{ return b < other.b; } }; S v[N]; int n, k; vector<int> o, d; int lst; ll mem[N][N][2]; ll solve(int i, int c, int l) { if (i == n){ if (c == k - 1 && l) return 0; return -1e17; } ll &ret = mem[i][c][l]; if (ret != -1) return -1; ret = solve(i +1, c, l) + (k - 1) * v[i].b; if (c < k - 1) ret = max(ret, solve(i + 1, c + 1, l) + v[i].a + c * v[i].b); if (!l) ret = max(ret, solve(i + 1, c, true) + v[i].a + v[i].b * (k - 1)); return ret; } void build(int i, int c, int l) { if (i == n){ return; } ll &ret = mem[i][c][l]; if (ret == solve(i +1, c, l) + (k - 1) * v[i].b) { d.pb(v[i].idx); build(i + 1, c, l); return; } if (c < k - 1) { if (ret == solve(i + 1, c + 1, l) + v[i].a + c * v[i].b){ o.pb(v[i].idx); build(i + 1, c + 1, l); return; } } lst = v[i].idx; build(i + 1, c, true); } void SOLVE(){ cin >> n >> k; rep(i,0,n){ cin >> v[i].a >> v[i].b; } sort(v, v + n); clr(mem, -1); d.clear(); o.clear(); solve(0, 0, 0); build(0 ,0 , 0); cout << sz(o) + sz(d) * 2 + 1 << '\n'; for (int item : o) cout << item << " "; for (int item : d) cout << item << " " << -item << " "; cout << lst << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); // freopen("AC.txt", "w", stdout); #endif int tc; cin >> tc; while(tc--) SOLVE(); return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; int n, k; pair<int, int> a[80]; long long dp[80][80]; bool sel[80][80]; int id[80]; void solve() { cin >> n >> k; for (int i = 1; i <= n; ++i) { cin >> a[i].first; cin >> a[i].second; } for (int i = 1; i <= n; ++i) id[i] = i; sort(id + 1, id + 1 + n, [&](int x, int y) { return (a[x].second == a[y].second ? a[x].first > a[y].first : a[x].second < a[y].second); }); for (int i = 0; i <= n; ++i) { for (int j = 0; j <= k; ++j) { dp[i][j] = -INF; sel[i][j] = false; } } dp[0][0] = 0; for (int i = 1; i <= n; ++i) { for (int j = 0; j <= min(i, k); ++j) { if (j < k) dp[i][j] = dp[i - 1][j] + a[id[i]].second * j; else dp[i][j] = dp[i - 1][j] + a[id[i]].second * (j - 1); sel[i][j] = 0; if (j > 0) { if (dp[i - 1][j - 1] + a[id[i]].second * (j - 1) + a[id[i]].first > dp[i][j]) { sel[i][j] = true; } dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + a[id[i]].second * (j - 1) + a[id[i]].first); } } } vector<int> ans, ans2; int cur = k; for (int i = n; i >= 1; --i) { if (sel[i][cur]) { cur--; ans.push_back(id[i]); } else { ans2.push_back(id[i]); } } cout << ans.size() + ans2.size() * 2 << '\n'; for (int i = 0; i < ans.size() - 1; ++i) cout << ans[i] << ' '; for (int i = 0; i < ans2.size(); ++i) cout << ans2[i] << ' ' << -ans2[i] << ' '; cout << ans.back() << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) solve(); }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #ifndef LOCAL // #pragma GCC optimize("O3") // #pragma GCC optimize("Ofast") // #pragma GCC optimize("unroll-loops") // #pragma GCC target("avx2") #endif #define fr(i, a, b) for(int i = (a); i <= (b); ++i) #define rf(i, a, b) for(int i = (a); i >= (b); --i) #define fe(x, y) for(auto& x : y) #define fi first #define se second #define pb push_back #define sz(x) (ll)(x).size() #define pw(x) (1ll << (x)) #define all(x) (x).begin(), (x).end() using namespace std; using ll = long long; using ld = long double; #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; template<typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define fbo find_by_order #define ook order_of_key mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count()); template<typename T> bool umn(T& a, T b) { return a > b ? a = b, 1 : 0; } template<typename T> bool umx(T& a, T b) { return a < b ? a = b, 1 : 0; } const int N = 100; const int mod = 1e9 + 7; const int inf = 1e9; const ll INF = 1e18; const ld eps = 1e-9; struct PairIdx { int first, second, idx; }; int n, k; PairIdx a[N]; int dp[N][N]; int from[N][N]; vector<int> path; void trace(pair<int, int> state) { pair<int, int> next = {state.fi - 1, from[state.fi][state.se]}; if(state.fi > 0) { trace(next); } path.pb(a[state.fi].idx + 1); if(state.se == next.se) path.pb(-(a[state.fi].idx + 1)); } void solve() { cin >> n >> k; fr(i, 0, n - 1) { cin >> a[i].fi >> a[i].se; a[i].idx = i; } if(k == 1) { int mxi = 0, mx = -inf; fr(i, 0, n - 1) { if(umx(mx, a[i].fi)) { mxi = i; } } cout << 1 << "\n" << mxi + 1 << "\n"; return; } sort(a, a + n, [](PairIdx a, PairIdx b) { return a.se < b.se; }); fr(i, 0, n - 1) fr(j, 0, k) dp[i][j] = -inf; dp[0][0] = 0; dp[0][1] = a[0].fi; fr(i, 1, n - 1) { dp[i][0] = 0; fr(j, 0, k - 1) { if(umx(dp[i][j], dp[i - 1][j] + a[i].se * j)) { from[i][j] = j; } if(umx(dp[i][j + 1], dp[i - 1][j] + a[i].se * j + a[i].fi)) { from[i][j + 1] = j; } } } int best = -inf; int best_add = -1; pair<int, int> state; int cur = 0; fr(i, 1, n - 1) cur += (k - 1) * a[i].se; fr(i, 0, n - 1) { if(dp[i][k - 1] == inf) continue; fr(j, i + 1, n - 1) { if(umx(best, cur + a[j].fi + dp[i][k - 1])) { best_add = j; state = {i, k - 1}; } } if(i + 1 < n) cur -= (k - 1) * a[i + 1].se; } trace(state); fr(i, state.fi + 1, n - 1) { if(i == best_add) continue; path.pb(a[i].idx + 1); path.pb(-(a[i].idx + 1)); } path.pb(a[best_add].idx + 1); cerr << best << "\n"; cout << sz(path) << "\n"; fe(x, path) { cout << x << " "; } cout << "\n"; } int main() { #ifndef LOCAL // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); ios::sync_with_stdio(0); cin.tie(0); #endif int test; cin >> test; while(test--) { solve(); } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
// Author: Tyler Wang #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cctype> #include <chrono> #include <cmath> #include <complex> #include <cstdint> #include <cstdlib> #include <deque> #include <functional> #include <initializer_list> #include <iomanip> #include <iostream> #include <iterator> #include <limits> #include <map> #include <memory> #include <numeric> #include <optional> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <string_view> #include <tuple> #include <type_traits> #include <utility> #include <vector> using namespace std; template <class C> constexpr auto ssize(const C& c) -> std::common_type_t<std::ptrdiff_t, std::make_signed_t<decltype(c.size())>> { return (std::common_type_t<std::ptrdiff_t, std::make_signed_t<decltype(c.size())>>)c.size(); } template <class T, std::ptrdiff_t N> constexpr std::ptrdiff_t ssize(const T (&)[N]) noexcept { return N; } template <typename T> constexpr std::enable_if_t<std::is_unsigned_v<T>, bool> has_single_bit( T x) noexcept { return x != 0 && (x & (x - 1)) == 0; } template <typename T> constexpr std::enable_if_t<std::is_unsigned_v<T>, int> countl_zero( T x) noexcept { if constexpr (sizeof(T) <= sizeof(unsigned)) { return __builtin_clz(x) - (std::numeric_limits<unsigned>::digits - std::numeric_limits<T>::digits); } else if constexpr (sizeof(T) <= sizeof(unsigned long)) { return __builtin_clzl(x) - (std::numeric_limits<unsigned long>::digits - std::numeric_limits<T>::digits); } else { static_assert(sizeof(T) <= sizeof(unsigned long long)); return __builtin_clzll(x) - (std::numeric_limits<unsigned long long>::digits - std::numeric_limits<T>::digits); } } template <typename T> constexpr std::enable_if_t<std::is_unsigned_v<T>, int> countr_zero( T x) noexcept { if constexpr (sizeof(T) <= sizeof(unsigned)) { return __builtin_ctz(x); } else if constexpr (sizeof(T) <= sizeof(unsigned long)) { return __builtin_ctzl(x); } else { static_assert(sizeof(T) <= sizeof(unsigned long long)); return __builtin_ctzll(x); } } template <typename T> constexpr std::enable_if_t<std::is_unsigned_v<T>, int> popcount(T x) noexcept { if constexpr (sizeof(T) <= sizeof(unsigned)) { return __builtin_popcount(x); } else if constexpr (sizeof(T) <= sizeof(unsigned long long)) { return __builtin_popcountl(x); } else { static_assert(sizeof(T) <= sizeof(unsigned long long)); return __builtin_popcountll(x); } } template <typename T> constexpr std::enable_if_t<std::is_unsigned_v<T>, T> bit_width(T x) noexcept { return (T)(std::numeric_limits<T>::digits - countl_zero(x)); } template <typename T> constexpr std::enable_if_t<std::is_unsigned_v<T>, T> bit_ceil(T x) noexcept { return (T)(x <= 1 ? 1 : (T)1 << bit_width((T)(x - 1))); } template <typename T> constexpr std::enable_if_t<std::is_unsigned_v<T>, T> bit_floor(T x) noexcept { return (T)(x == 0 ? 0 : (T)1 << (bit_width(x) - 1)); } template <class Fun> class y_combinator_result { public: template <class T> explicit y_combinator_result(T&& fun) : fun_(std::forward<T>(fun)) {} template <class... Args> decltype(auto) operator()(Args&&... args) { return fun_(std::ref(*this), std::forward<Args>(args)...); } private: Fun fun_; }; template <class Fun> decltype(auto) y_combinator(Fun&& fun) { return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun)); } template <typename T> std::ostream& operator<<(std::ostream& os, const std::optional<T>& opt) { return opt ? os << *opt : os << "nullopt"; } template <typename T1, typename T2> std::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& p) { return os << '{' << p.first << ", " << p.second << '}'; } template <typename Container, std::enable_if_t<!std::is_convertible_v<Container, std::string_view>, typename Container::const_iterator>* = nullptr> std::ostream& operator<<(std::ostream& os, const Container& c) { os << '{'; for (auto it = c.begin(); it != c.end(); ++it) { if (it != c.begin()) { os << ", "; } os << *it; } return os << '}'; } template <typename T, typename Container> std::ostream& operator<<(std::ostream& os, std::stack<T, Container> s) { std::vector<T> v(s.size()); for (auto it = v.rbegin(); it != v.rend(); ++it) { *it = s.top(); s.pop(); } return os << v; } template <typename T, typename Container> std::ostream& operator<<(std::ostream& os, std::queue<T, Container> q) { os << '{'; if (!q.empty()) { os << q.front(); q.pop(); while (!q.empty()) { os << ", " << q.front(); q.pop(); } } return os << '}'; } template <typename T, typename Container, typename Compare> std::ostream& operator<<(std::ostream& os, std::priority_queue<T, Container, Compare> pq) { os << '{'; if (!pq.empty()) { os << pq.top(); pq.pop(); while (!pq.empty()) { os << ", " << pq.top(); pq.pop(); } } return os << '}'; } namespace detail { template <int I, typename... Ts> void tuple_print(std::ostream& os, const std::tuple<Ts...>& t) { static_assert(0 <= I && I <= sizeof...(Ts)); if constexpr (I != sizeof...(Ts)) { if constexpr (I != 0) { os << ", "; } os << std::get<I>(t); tuple_print<I + 1>(os, t); } } } // namespace detail template <typename... Ts> std::ostream& operator<<(std::ostream& os, const std::tuple<Ts...>& t) { os << '{'; detail::tuple_print<0>(os, t); return os << '}'; } namespace detail { template <typename T1, typename... Ts> void debug_print(std::string_view keys, const T1& val1, const Ts&... vals) { if constexpr (sizeof...(Ts) == 0) { std::cerr << keys << " = " << val1 << std::endl; } else { for (int i = 0, paren = 0; i < (int)keys.size(); ++i) { if (keys[i] == '(' || keys[i] == '{') { ++paren; } else if (keys[i] == ')' || keys[i] == '}') { --paren; } else if (keys[i] == ',' && paren == 0) { std::cerr << keys.substr(0, i) << " = " << val1 << ','; return debug_print(keys.substr(i + 1), vals...); } } } } template <typename... Ts> void debug_print(int line, const Ts&... keyvals) { if constexpr (sizeof...(Ts) <= 1) { std::cerr << " " << line << " |" << std::endl; } else { std::cerr << " " << line << " | "; debug_print(keyvals...); } } } // namespace detail #if defined(TYLER) && defined(__GNUC__) && __GNUC__ >= 8 #define debug(...) \ detail::debug_print(__LINE__, #__VA_ARGS__ __VA_OPT__(, ) __VA_ARGS__) #elif defined(TYLER) #define debug(...) detail::debug_print(__LINE__, #__VA_ARGS__, __VA_ARGS__) #else #define debug(...) ((void)0) #endif int k; template <typename Flow, typename Cost> class min_cost_flow { public: min_cost_flow(int n) : graph_(n) {} void add_edge(int from, int to, Flow cap, Cost cost) { graph_[from].emplace_back(to, (int)graph_[to].size(), cap, cost); graph_[to].emplace_back(from, (int)graph_[from].size() - 1, 0, -cost); } std::pair<Flow, Cost> solve( int source, int sink, Flow max_flow = std::numeric_limits<Flow>::max()) { static constexpr Cost INF_COST = std::numeric_limits<Cost>::max(); for (std::vector<edge>& edges : graph_) { for (edge& e : edges) { e.flow = 0; e.recost = e.cost; } } int n = (int)graph_.size(); std::vector<Cost> dist(n, INF_COST); dist[source] = 0; std::vector<std::pair<int, int>> pred(n); std::queue<int> q; q.push(source); std::vector<bool> active(n, false); active[source] = true; while (!q.empty()) { // Bellman-Ford shortest paths int u = q.front(); q.pop(); active[u] = false; for (int i = 0; i < (int)graph_[u].size(); ++i) { if (const edge& e = graph_[u][i]; e.flow < e.cap && dist[e.to] > dist[u] + e.cost) { dist[e.to] = dist[u] + e.cost; pred[e.to] = {u, i}; if (!active[e.to]) { q.push(e.to); active[e.to] = true; } } } } constexpr auto compare = [](const std::pair<int, Cost>& lhs, const std::pair<int, Cost>& rhs) -> bool { return lhs.second > rhs.second; }; std::priority_queue<std::pair<int, Cost>, std::vector<std::pair<int, Cost>>, decltype(compare)> pq(compare); Flow total_flow = 0; Cost total_cost = 0, curr_cost = 0; while (dist[sink] != INF_COST) { Flow delta = max_flow - total_flow; for (int v = sink; v != source;) { const auto& [u, i] = pred[v]; const edge& e = graph_[u][i]; delta = std::min(delta, e.cap - e.flow); v = u; } for (int v = sink; v != source;) { const auto& [u, i] = pred[v]; edge& e = graph_[u][i]; e.flow += delta; graph_[v][e.rev].flow -= delta; v = u; } total_cost += delta * (curr_cost += dist[sink]); if ((total_flow += delta) == max_flow) { break; } for (int u = 0; u < n; ++u) { for (edge& e : graph_[u]) { e.recost += dist[u] - dist[e.to]; } } std::fill(dist.begin(), dist.end(), INF_COST); dist[source] = 0; pq.emplace(source, 0); while (!pq.empty()) { // Dijkstra's shortest paths auto [u, d_u] = pq.top(); pq.pop(); if (d_u > dist[u]) { continue; } for (int i = 0; i < (int)graph_[u].size(); ++i) { if (const edge& e = graph_[u][i]; e.flow < e.cap && dist[e.to] > dist[u] + e.recost) { pq.emplace(e.to, dist[e.to] = dist[u] + e.recost); pred[e.to] = {u, i}; } } } } n = (n - 2) >> 1; for (int u = 0; u < k - 1; ++u) { for (edge& e : graph_[u]) { if (e.flow == e.cap) { cout << e.to - n + 1 << ' '; } } } for (int u = k - 1; u < n - 1; ++u) { for (edge& e : graph_[u]) { if (e.flow == e.cap) { cout << e.to - n + 1 << ' ' << -e.to + n - 1 << ' '; } } } for (edge& e : graph_[n - 1]) { if (e.flow == e.cap) { cout << e.to - n + 1 << '\n'; } } return std::pair(total_flow, total_cost); } private: struct edge { int to, rev; Flow cap, flow; Cost cost, recost; constexpr edge(int _to, int _rev, Flow _cap, Cost _cost) : to(_to), rev(_rev), cap(_cap), cost(_cost) {} }; std::vector<std::vector<edge>> graph_; }; // Minimum-cost assignment. O(n ^ 3 log n). // template <typename Cost> // std::pair<int, Cost> min_cost_assign( // const std::vector<std::vector<Cost>>& costs, // Cost inf = std::numeric_limits<Cost>::max()) { // int m = (int)costs.size(), n = (int)costs[0].size(); // int source = m + n, sink = m + n + 1; // min_cost_flow<int, Cost> mc_flow(m + n + 2); // for (int u = 0; u < m; ++u) { // mc_flow.add_edge(source, u, 1, 0); // } // for (int v = 0; v < n; ++v) { // mc_flow.add_edge(m + v, sink, 1, 0); // } // for (int u = 0; u < m; ++u) { // for (int v = 0; v < n; ++v) { // if (costs[u][v] != inf) { // mc_flow.add_edge(u, m + v, 1, costs[u][v]); // } // } // } // return mc_flow.solve(source, sink, std::min(m, n)); // } template <typename Cost> std::pair<Cost, std::vector<int>> min_cost_assign( const std::vector<std::vector<Cost>>& costs) { static constexpr Cost INF = std::numeric_limits<Cost>::max(); int m = (int)costs.size(), n = (int)costs[0].size(); Cost total_cost = 0; std::vector<Cost> row_pot(m), col_pot(n); std::vector<int> col_match(n, -1), pred(n); for (int i = 0; i < m; ++i) { int x = i, y = -1; std::vector<Cost> slack(n, INF); std::vector<bool> tight(n, false); do { Cost delta = INF; int yy = -1; for (int j = 0; j < n; ++j) { if (tight[j]) { continue; } Cost t = costs[x][j] - row_pot[x] - col_pot[j]; if (t < slack[j]) { slack[j] = t; pred[j] = y; } if (slack[j] < delta) { delta = slack[j]; yy = j; } } for (int j = 0; j < n; ++j) { if (tight[j]) { row_pot[col_match[j]] += delta; col_pot[j] -= delta; } else { slack[j] -= delta; } } total_cost += delta; tight[y = yy] = true; } while ((x = col_match[y]) != -1); while (pred[y] != -1) { col_match[y] = col_match[pred[y]]; y = pred[y]; } col_match[y] = i; } std::vector<int> row_match(m, -1); for (int j = 0; j < n; ++j) { if (col_match[j] != -1) { row_match[col_match[j]] = j; } } return std::pair(total_cost, row_match); } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.precision(12); std::cerr.precision(12); std::mt19937 rng((std::uint_fast32_t)std::chrono::high_resolution_clock::now() .time_since_epoch() .count()); int num_cases; cin >> num_cases; while (num_cases--) { int n; cin >> n >> k; vector<int> a(n), b(n); for (int i = 0; i < n; ++i) { cin >> a[i] >> b[i]; } vector<vector<int>> costs(n, vector<int>(n)); for (int j = 0; j < n; ++j) { for (int i = 0; i < k - 1; ++i) { costs[i][j] = -(a[j] + i * b[j]); } for (int i = k - 1; i < n - 1; ++i) { costs[i][j] = -(k - 1) * b[j]; } costs[n - 1][j] = -(a[j] + (k - 1) * b[j]); } cout << n + (n - k) << '\n'; auto match = min_cost_assign(costs).second; for (int i = 0; i < k - 1; ++i) { cout << match[i] + 1 << ' '; } for (int i = k - 1; i < n - 1; ++i) { cout << match[i] + 1 << ' ' << -match[i] - 1 << ' '; } cout << match[n - 1] + 1 << '\n'; } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; constexpr long long N = 80 + 9; constexpr long long M = 1e4 + 9; constexpr long long INF = 0x3f3f3f3f3f3f3f3f; constexpr long long INF2 = 0xcfcfcfcfcfcfcfcf; long long t, n, k; long long a[N], b[N]; long long id[N]; long long dp[N][N]; long long from[N][N]; signed main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); ; cin >> t; while (t--) { cin >> n >> k; for (long long i = 1; i <= n; i++) { cin >> a[i] >> b[i]; id[i] = i; } sort(id + 1, id + n + 1, [](const long long& i, const long long& j) { return b[i] < b[j]; }); memset(dp, 0xcf, sizeof(dp)); dp[0][0] = 0; for (long long i = 0; i < n; i++) { for (long long j = 0; j <= i && j <= k; j++) { if (j < k && dp[i][j] + a[id[i + 1]] + (k - 1) * b[id[i + 1]] > dp[i + 1][j + 1]) { dp[i + 1][j + 1] = dp[i][j] + a[id[i + 1]] + (k - 1) * b[id[i + 1]]; from[i + 1][j + 1] = 0; } if (dp[i][j] + b[id[i + 1]] * (k - 1) > dp[i + 1][j]) { dp[i + 1][j] = dp[i][j] + b[id[i + 1]] * (k - 1); from[i + 1][j] = 1; } } } vector<long long> add, del; while (n) { if (from[n][k]) { del.push_back(id[n]); } else { add.push_back(id[n]); k--; } n--; } cout << add.size() + del.size() * 2 << endl; for (long long j = 0; j < add.size() - 1; j++) { cout << add[j] << " "; } for (long long j : del) { cout << j << " " << -j << " "; } cout << add.back() << endl; } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long mod = 1000000007; long long inf = 1LL << 50; const int N = 5000; long long chooses[N + 1]; int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin.exceptions(cin.failbit); int n, m; cin >> n >> m; for (long long i = 2; i <= n; i++) { chooses[i] = (i * (i - 1)) / 2; cout << i << " " << chooses[i] << '\n'; } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const double eps = 1e-7; const int N = 100; const int M = 200010; const int P = 1000000007; const int Inf = 1000000007; const int Fni = -1000000007; inline int qr() { int r = 0, f = 1; char c = getchar(); while (c > '9' || c < '0') { if (c == '-') f = -1; c = getchar(); } while (c <= '9' && c >= '0') { r = (r << 1) + (r << 3) + c - '0', c = getchar(); } return r * f; } template <typename T> void debug(T x, char c = '\n') { cerr << x << c; } template <typename T> void debug(T *const tp, int minn, int maxn, char v = ' ', char c = '\n') { for (int i = minn; i <= maxn; ++i) debug(tp[i], v); cerr << c; } template <typename T> inline void chkmin(T &x, T y) { x = x > y ? y : x; } template <typename T> inline void chkmax(T &x, T y) { x = x < y ? y : x; } template <typename T> inline void add(T &x, long long y, int mod = P) { x += y; x = x >= mod ? x - mod : x; } template <typename T> inline void dec(T &x, long long y, int mod = P) { x -= y; x = x < 0 ? x + mod : x; } template <typename T> inline T addn(T x, long long y, int mod = P) { x += y; return (x = x > mod ? x - mod : x); } template <typename T> inline T decn(T x, long long y, int mod = P) { x -= y; return (x = x < 0 ? x + mod : x); } int T; struct minion { int a, b, id; } base[N], tmp[N]; int n, k; int cnt; int ans; int lst; int ud[N]; int qwq[N]; int f[N][N]; int pre[N][N]; bool comp(minion x, minion y) { return x.a == y.a ? x.b < y.b : x.a > y.a; } bool cop(minion x, minion y) { return x.b < y.b; } int main() { cin >> T; while (T--) { cin >> n >> k; ans = cnt = 0; fill(ud, ud + n + 1, 0); memset(f, -63, sizeof(f)); for (int i = 1; i <= n; ++i) base[i].a = qr(), base[i].id = i, base[i].b = qr(); sort(base + 1, base + n + 1, comp); f[0][0] = 1; for (int i = 1; i <= n; ++i) for (int j = 0; j <= k - 1; ++j) { int tmp = base[i].b * (j - k); if (!j) f[i][j] = f[i - 1][j], pre[i][j] = 0; else { if (f[i - 1][j] > f[i - 1][j - 1] + base[i].a + tmp) f[i][j] = f[i - 1][j], pre[i][j] = 0; else f[i][j] = f[i - 1][j - 1] + base[i].a + tmp, pre[i][j] = 1; } } int p = n, q = k - 1; while (p && q) { if (!pre[p][q]) --p; else ud[base[p].id] = 1, --q, --p; } for (int i = 1; i <= n; ++i) if (!ud[base[i].id]) qwq[++cnt] = i; for (int i = 1; i <= cnt; ++i) { int res = 0, gz = 0; for (int j = 1; j <= cnt; ++j) if (i != j) tmp[++gz] = base[qwq[j]]; sort(tmp + 1, tmp + gz + 1, cop); for (int j = 1; j <= gz; ++j) res += (k - 1) * tmp[j].b; res += base[qwq[i]].a + base[qwq[i]].b * (k - 1); if (res >= ans) ans = res, lst = qwq[i]; } cout << k - 1 + 2 * (n - k) + 1 << '\n'; for (int i = 1; i <= n; ++i) if (ud[i]) cout << i << ' '; for (int i = 1; i <= cnt; ++i) if (qwq[i] != lst) cout << base[qwq[i]].id << ' ' << -base[qwq[i]].id << ' '; cout << base[lst].id << "\n"; } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
python3
import sys input = sys.stdin.readline for f in range(int(input())): n,k=map(int,input().split()) mons=[0]*n for i in range(n): mons[i]=[0,0,0,0] a,b=map(int,input().split()) mons[i][1]=a mons[i][2]=b mons[i][0]=a-b mons[i][3]=i mons.sort() i=0 m1=[] m2=[] while i<n and i<k-1: m1.append([0,0]) m1[i][0]=mons[i][2] m1[i][1]=mons[i][3] i+=1 if k==n: m1.append([0,0]) m1[i][0]=mons[i][2] m1[i][1]=mons[i][3] i+=1 while i<n: m2.append([0,0]) m2[-1][0]=mons[i][1] m2[-1][1]=mons[i][3] i+=1 m1.sort() m2.sort() sol=[] for x in m1: sol.append(x[1]+1) for x in m2: sol.append(x[1]+1) sol.append(-x[1]-1) if k<n: sol.pop(-1) print(len(sol)) print(*sol)
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; int n, k; pair<int, int> a[80]; long long dp[80][80]; bool sel[80][80]; int id[80]; void solve() { cin >> n >> k; for (int i = 1; i <= n; ++i) { cin >> a[i].first; cin >> a[i].second; } for (int i = 1; i <= n; ++i) id[i] = i; sort(id + 1, id + 1 + n, [&](int x, int y) { return (a[x].second == a[y].second ? a[x].first > a[y].first : a[x].second < a[y].second); }); for (int i = 0; i <= n; ++i) { for (int j = 0; j <= k; ++j) { dp[i][j] = -INF; sel[i][j] = false; } } dp[0][0] = 0; for (int i = 1; i <= n; ++i) { for (int j = 0; j <= min(i, k); ++j) { if (j < k) dp[i][j] = dp[i - 1][j] + a[id[i]].second * j; else dp[i][j] = dp[i - 1][j] + a[id[i]].second * (j - 1); sel[i][j] = 0; if (j > 0) { if (dp[i - 1][j - 1] + a[id[i]].second * (j - 1) + a[id[i]].first > dp[i][j]) { sel[i][j] = true; } dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + a[id[i]].second * (j - 1) + a[id[i]].first); } } } vector<int> ans, ans2; int cur = k; for (int i = n; i >= 1; --i) { if (sel[i][cur]) { cur--; ans.push_back(id[i]); } else { ans2.push_back(id[i]); } } reverse(ans.begin(), ans.end()); cout << ans.size() + ans2.size() * 2 << '\n'; for (int i = 0; i < ans.size() - 1; ++i) cout << ans[i] << ' '; for (int i = 0; i < ans2.size(); ++i) cout << ans2[i] << ' ' << -ans2[i] << ' '; cout << ans.back() << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) solve(); }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int inf = 1012345678; vector<int> transform(int n, vector<int> arr, vector<int> perm) { vector<int> res(n); for (int i = 0; i < n; ++i) { res[i] = arr[perm[i]]; } return res; } int main() { int Q; cin >> Q; while (Q--) { int N, K; cin >> N >> K; vector<int> A(N), B(N); for (int i = 0; i < N; ++i) { cin >> A[i] >> B[i]; } vector<int> perm(N); for (int i = 0; i < N; ++i) { perm[i] = i; } sort(perm.begin(), perm.end(), [&](int i, int j) { return B[i] < B[j]; }); A = transform(N, A, perm); B = transform(N, B, perm); int bsum = 0; for (int i = 0; i < N; ++i) { bsum += B[i] * (K - 1); } vector<vector<int> > dp(N + 1, vector<int>(K, -inf)); dp[0][0] = bsum; for (int i = 0; i < N; ++i) { for (int j = 0; j < K; ++j) { dp[i + 1][j] = dp[i][j]; if (j >= 1) { dp[i + 1][j] = max(dp[i + 1][j], dp[i][j - 1] + A[i] - B[i] * (K - j - 2)); } } } int opt = -1, optval = -inf; for (int i = 0; i < N; ++i) { if (dp[i][K - 1] == -inf) continue; int val = dp[i][K - 1], mx = 0; for (int j = i; j < N; ++j) { mx = max(mx, A[j]); } val += mx; if (optval < val) { optval = val; opt = i; } } vector<int> seq; int ptr = K - 1; for (int i = opt - 1; i >= 0; --i) { if (dp[i][ptr] != dp[i + 1][ptr]) { seq.push_back(i); --ptr; } } reverse(seq.begin(), seq.end()); int last = max_element(A.begin() + opt, A.end()) - A.begin(); vector<int> ans; for (int i : seq) { ans.push_back(perm[i] + 1); } for (int i = 0; i < N; ++i) { if (!binary_search(seq.begin(), seq.end(), i) && i != last) { ans.push_back(perm[i] + 1); ans.push_back(-(perm[i] + 1)); } } ans.push_back(perm[last] + 1); cout << ans.size() << endl; for (int i = 0; i < ans.size(); ++i) { if (i) cout << ' '; cout << ans[i]; } cout << endl; } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
java
import java.util.Arrays; import java.util.Scanner; public class SummoningMinions2 { public static void main(String[] args) { Scanner fs=new Scanner(System.in); int T=fs.nextInt(); for (int tt=0; tt<T; tt++) { int n=fs.nextInt(), k=fs.nextInt(); long ans=0; int[][] cost=new int[k][n]; for (int i=0; i<n; i++) { int a=fs.nextInt(), b=fs.nextInt(); ans+=(k-1)*(long)b; for (int spot=0; spot<k; spot++) cost[spot][i]=-(-(k-1)*b+spot*b+a); } hungarian(cost); boolean[] chosen=new boolean[n]; for (int i=0; i<k; i++) chosen[rowMatchesWith[i]]=true; System.out.println(k-1 + 2*(n-k) + 1); for (int i=0; i+1<k; i++) { System.out.print(1+rowMatchesWith[i]+" "); } for (int i=0; i<n; i++) if (!chosen[i]) System.out.print((1+i)+" -"+(1+i)+" "); System.out.println(rowMatchesWith[k-1]+1); } } // finds MINIMUM total matching cost of // each row to some column, using each col at most once // **nRows must be <= nCols** //rowMatchWith[i] = the column that row i matched with static int[] rowMatchesWith; static int hungarian(int[][] a) { int n=a.length, m=a[0].length; if (n>m) throw null; int[] u=new int[n], v=new int[m+1]; //edge (i->j) >= u[i]+v[j] int[] p=new int[m+1]; //p[j] = left match for right node j Arrays.fill(p, -1); for (int i=0; i<n; i++) {//find alternating path for node i int j0=m; p[j0]=i; int[] dist=new int[m+1], from=new int[m+1]; boolean[] seen=new boolean[m+1]; Arrays.fill(dist, Integer.MAX_VALUE); Arrays.fill(from, -1); while (p[j0]!=-1) { seen[j0]=true; int i0=p[j0], delta=Integer.MAX_VALUE, j1=-1; for (int j=0; j<m; j++) {//consider edges i0 -> everything if (seen[j]) continue; int candDist=a[i0][j]-u[i0]-v[j]; if (candDist<dist[j]) {dist[j]=candDist; from[j]=j0;} if (dist[j]<delta) {dist[j]=delta; j1=j;} } //it costs at least delta to get somewhere else, //subtract that from all distances and add cost to u, v, arrays //from all done -> not done edges for (int j=0; j<=m; j++) { if (seen[j]) {u[p[j]]+=delta; v[j]-=delta;} else dist[j]-=delta; } j0=j1; } //flip alternating path while (j0!=m) { int j1=from[j0]; p[j0]=p[j1]; j0=j1; } } //sum of deltas is stored at v[m] coincidentally int ans=-v[m]; rowMatchesWith=new int[n]; for (int j=0; j<m; j++) if (p[j]!=-1) rowMatchesWith[p[j]]=j; return ans; } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1); const double eps = 1e-12; const int inf = 2000000000; const long long int infLL = (long long int)1e18; long long int MOD = 1000000007; int MOD1 = 1000000007; int MOD2 = 1000000009; inline bool checkBit(long long int n, long long int i) { return n & (1LL << i); } inline long long int setBit(long long int n, long long int i) { return n | (1LL << i); ; } inline long long int resetBit(long long int n, long long int i) { return n & (~(1LL << i)); } int dx[] = {0, 0, +1, -1}; int dy[] = {+1, -1, 0, 0}; inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } inline bool isLeapYear(long long int year) { return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); } inline void normal(long long int &a) { a %= MOD; (a < 0) && (a += MOD); } inline long long int modMul(long long int a, long long int b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a * b) % MOD; } inline long long int modAdd(long long int a, long long int b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a + b) % MOD; } inline long long int modSub(long long int a, long long int b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; } inline long long int modPow(long long int b, long long int p) { long long int r = 1LL; while (p) { if (p & 1) r = modMul(r, b); b = modMul(b, b); p >>= 1LL; } return r; } inline long long int modDiv(long long int a, long long int b) { return modMul(a, modPow(b, MOD - 2)); } bool comp(const pair<long long int, pair<long long int, long long int> > &p1, const pair<long long int, pair<long long int, long long int> > &p2) { return p1.first > p2.first; } bool comp1(const pair<long long int, long long int> &p1, const pair<long long int, long long int> &p2) { if (p1.first == p2.first) { return p1.second > p2.second; } return p1.first < p2.first; } long long int converter(string a) { long long int i, mul = 1LL, r, t, ans = 0LL; if (a.length() == 0) return 0; for (i = a.length() - 1; i >= 0; i--) { t = a[i] - '0'; r = t % 10; ans += (mul * r); mul = mul * 10; } return ans; } int msb(unsigned x) { union { double a; int b[2]; }; a = x; return (b[1] >> 20) - 1023; } const int MAX = 78; long long int t, n, k; pair<pair<long long int, long long int>, long long int> p[MAX]; long long int dp[MAX][MAX]; vector<long long int> good, bad; long long int solve(long long int idx, long long int cnt) { if (idx == n + 1) { if (cnt != k) return -infLL; return 0; } if (dp[idx][cnt] != -1) return dp[idx][cnt]; long long int ret = 0; if (cnt + 1 <= k) { ret = max(ret, cnt * p[idx].first.first + p[idx].first.second + solve(idx + 1, cnt + 1)); } ret = max(ret, ((k - 1) * p[idx].first.first) + solve(idx + 1, cnt)); return dp[idx][cnt] = ret; } void trace(long long int idx, long long int cnt) { if (idx == n + 1) return; long long int ret1 = 0LL, ret2 = 0LL; if (cnt + 1 <= k) ret1 = cnt * p[idx].first.first + p[idx].first.second + solve(idx + 1, cnt + 1); ret2 = ((k - 1) * p[idx].first.first) + solve(idx + 1, cnt); if (ret1 > ret2) { trace(idx + 1, cnt + 1); good.push_back(p[idx].second); } else { trace(idx + 1, cnt); bad.push_back(p[idx].second); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cout.unsetf(ios::floatfield); cout.precision(20); cout.setf(ios::fixed, ios::floatfield); ; cin >> t; while (t--) { good.clear(); bad.clear(); long long int maxi = 0, idx; cin >> n >> k; for (int i = 1; i <= n; ++i) { cin >> p[i].first.second >> p[i].first.first; p[i].second = i; if (p[i].first.second >= maxi) maxi = p[i].first.second, idx = i; } if (k == 1) { cout << 1 << '\n'; cout << idx << '\n'; continue; } sort(p + 1, p + n + 1); memset(dp, -1, sizeof(dp)); long long int now = solve(1, 0); ; trace(1, 0); reverse((good).begin(), (good).end()); reverse((bad).begin(), (bad).end()); if (good.size() == 0) { string ss = ""; ss += to_string(n); ss += '#'; ss += to_string(k); ss += '#'; for (int i = 1; i <= n; ++i) { ss += to_string(p[i].first.first); ss += '#'; ss += to_string(p[i].first.second); ss += '&'; } cout << ss << '\n'; continue; } cout << good.size() + (2 * bad.size()) << '\n'; for (int i = 0; i < (int)good.size() - 1; ++i) { cout << good[i] << " "; } for (int i = 0; i < bad.size(); ++i) { cout << bad[i] << " " << -1 * bad[i] << " "; } cout << good[good.size() - 1] << '\n'; } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
/* preprocessor start */ #ifdef LOCAL //* #define _GLIBCXX_DEBUG // gcc /*/ #define _LIBCPP_DEBUG 0 // clang //*/ #define __clock__ // #define __buffer_check__ #else #pragma GCC optimize("Ofast") /* #define _GLIBCXX_DEBUG // gcc /*/ // #define _LIBCPP_DEBUG 0 // clang //*/ // #define __buffer_check__ // #define NDEBUG #endif #define __precision__ 15 #define iostream_untie true #include <bits/stdc++.h> #include <ext/rope> #define __all(v) std::begin(v), std::end(v) #define __rall(v) std::rbegin(v), std::rend(v) #define __popcount(n) __builtin_popcountll(n) #define __clz32(n) __builtin_clz(n) #define __clz64(n) __builtin_clzll(n) #define __ctz32(n) __builtin_ctz(n) #define __ctz64(n) __builtin_ctzll(n) /* preprocessor end */ namespace std { // hash template <class T> size_t hash_combine(size_t seed, T const &key) { return seed ^ (hash<T>()(key) + 0x9e3779b9 + (seed << 6) + (seed >> 2)); } template <class T, class U> struct hash<pair<T, U>> { size_t operator()(pair<T, U> const &pr) const { return hash_combine(hash_combine(0, pr.first), pr.second); } }; template <class tuple_t, size_t index = tuple_size<tuple_t>::value - 1> struct tuple_hash_calc { static size_t apply(size_t seed, tuple_t const &t) { return hash_combine(tuple_hash_calc<tuple_t, index - 1>::apply(seed, t), get<index>(t)); } }; template <class tuple_t> struct tuple_hash_calc<tuple_t, 0> { static size_t apply(size_t seed, tuple_t const &t) { return hash_combine(seed, get<0>(t)); } }; template <class... T> struct hash<tuple<T...>> { size_t operator()(tuple<T...> const &t) const { return tuple_hash_calc<tuple<T...>>::apply(0, t); } }; // iostream template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) { return is >> p.first >> p.second; } template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { return os << p.first << ' ' << p.second; } template <class tuple_t, size_t index> struct tupleis { static istream &apply(istream &is, tuple_t &t) { tupleis<tuple_t, index - 1>::apply(is, t); return is >> get<index>(t); } }; template <class tuple_t> struct tupleis<tuple_t, SIZE_MAX> { static istream &apply(istream &is, tuple_t &t) { return is; } }; template <class... T> istream &operator>>(istream &is, tuple<T...> &t) { return tupleis<tuple<T...>, tuple_size<tuple<T...>>::value - 1>::apply(is, t); } template <> istream &operator>>(istream &is, tuple<> &t) { return is; } template <class tuple_t, size_t index> struct tupleos { static ostream &apply(ostream &os, const tuple_t &t) { tupleos<tuple_t, index - 1>::apply(os, t); return os << ' ' << get<index>(t); } }; template <class tuple_t> struct tupleos<tuple_t, 0> { static ostream &apply(ostream &os, const tuple_t &t) { return os << get<0>(t); } }; template <class... T> ostream &operator<<(ostream &os, const tuple<T...> &t) { return tupleos<tuple<T...>, tuple_size<tuple<T...>>::value - 1>::apply(os, t); } template <> ostream &operator<<(ostream &os, const tuple<> &t) { return os; } template <class Container, typename Value = typename Container::value_type, enable_if_t<!is_same<decay_t<Container>, string>::value, nullptr_t> = nullptr> istream& operator>>(istream& is, Container &cont) { for(auto&& e : cont) is >> e; return is; } template <class Container, typename Value = typename Container::value_type, enable_if_t<!is_same<decay_t<Container>, string>::value, nullptr_t> = nullptr> ostream& operator<<(ostream& os, const Container &cont) { bool flag = 1; for(auto&& e : cont) flag ? flag = 0 : (os << ' ', 0), os << e; return os; } } // namespace std namespace setting { using namespace std; using namespace chrono; system_clock::time_point start_time, end_time; long long get_elapsed_time() { end_time = system_clock::now(); return duration_cast<milliseconds>(end_time - start_time).count(); } void print_elapsed_time() { cerr << "\n----- Exec time : " << get_elapsed_time() << " ms -----\n\n"; } void buffer_check() { char bufc; if(cin >> bufc) cerr << "\n\033[1;35mwarning\033[0m: buffer not empty.\n"; } struct setupper { setupper() { if(iostream_untie) ios::sync_with_stdio(false), cin.tie(nullptr); cout << fixed << setprecision(__precision__); #ifdef stderr_path freopen(stderr_path, "a", stderr); #endif #ifdef LOCAL cerr << fixed << setprecision(__precision__) << boolalpha << "\n----- stderr at LOCAL -----\n\n"; #endif #ifdef __clock__ start_time = system_clock::now(); atexit(print_elapsed_time); #endif #ifdef __buffer_check__ atexit(buffer_check); #endif } } __setupper; // struct setupper } // namespace setting #ifdef __clock__ #include "clock.hpp" #else #define build_clock() ((void)0) #define set_clock() ((void)0) #define get_clock() ((void)0) #endif #ifdef LOCAL #include "dump.hpp" #else #define dump(...) ((void)0) #endif /* function utility start */ // lambda wrapper for recursive method. template <class lambda_type> class make_recursive { lambda_type func; public: make_recursive(lambda_type &&f) : func(std::move(f)) {} template <class... Args> auto operator()(Args &&... args) const { return func(*this, std::forward<Args>(args)...); } }; /* template <class T, class... types> T read(types... args) noexcept { typename std::remove_const<T>::type obj(args...); std::cin >> obj; return obj; } #define input(type, var, ...) type var{read<type>(__VA_ARGS__)} */ // substitute y for x if x > y. template <class T> inline bool chmin(T &x, const T &y) { return x > y ? x = y, true : false; } // substitute y for x if x < y. template <class T> inline bool chmax(T &x, const T &y) { return x < y ? x = y, true : false; } // binary search on discrete range. template <class iter_type, class pred_type> iter_type binary(iter_type __ok, iter_type __ng, pred_type pred) { std::ptrdiff_t dist(__ng - __ok); while(std::abs(dist) > 1) { iter_type mid(__ok + dist / 2); if(pred(mid)) __ok = mid, dist -= dist / 2; else __ng = mid, dist /= 2; } return __ok; } // binary search on real numbers. template <class pred_type> long double binary(long double __ok, long double __ng, const long double eps, pred_type pred) { while(std::abs(__ok - __ng) > eps) { long double mid{(__ok + __ng) / 2}; (pred(mid) ? __ok : __ng) = mid; } return __ok; } // size of array. template <class A, size_t N> size_t size(A (&array)[N]) { return N; } // be careful that val is type-sensitive. template <class T, class A, size_t N> void init(A (&array)[N], const T &val) { std::fill((T*)array, (T*)(array + N), val); } /* functon utility end */ /* using alias start */ using namespace std; using i32 = int_least32_t; using i64 = int_least64_t; using u32 = uint_least32_t; using u64 = uint_least64_t; using p32 = pair<i32, i32>; using p64 = pair<i64, i64>; template <class T, class Comp = less<T>> using heap = priority_queue<T, vector<T>, Comp>; template <class T> using hashset = unordered_set<T>; template <class Key, class Value> using hashmap = unordered_map<Key, Value>; using namespace __gnu_cxx; /* using alias end */ /* library start */ /* library end */ /* The main code follows. */ struct solver; template <class T = solver> void _main(); int main() { _main<>(); } template <class solver> void _main() { unsigned t = 1; #ifdef LOCAL t = 1; #endif // t = -1; // infinite loop cin >> t; // case number given while(t--) solver(); } struct solver { solver() { int n,k; cin>>n>>k; struct minio { int a,b,id; }; vector<minio> nio(n); for(int i=0; i<n; ++i) { int a,b; cin>>a>>b; nio[i]={a,b,1+i}; } sort(__all(nio),[](auto x,auto y){return x.b<y.b;}); int dp[80][80]; init(dp,-1000000000); dp[0][0]=0; int bt[80][80]={}; for(int x=0; x<n; ++x) { auto [a,b,id]=nio[x]; if(chmax(dp[x+1][k],dp[x][k]+(k-1)*b)) { bt[x][k]=0; } for(int i=0; i<k; ++i) { if(chmax(dp[x+1][i],dp[x][i]+i*b)) { bt[x][i]=0; } if(chmax(dp[x+1][i+1],dp[x][i]+i*b+a)) { bt[x][i+1]=1; } } } bool flag=true; int kk=k; for(int x=0; x<k; ++x) { if(dp[n][x]>dp[n][kk]) { flag=false; kk=x; } } k=kk; vector<int> ans; for(int x=n; x--; ) { int id=nio[x].id; if(bt[x][k]) { k--; if(flag) ans.insert(begin(ans),id); else ans.emplace_back(id); flag=false; } else { ans.emplace_back(-id); ans.emplace_back(id); } } reverse(__all(ans)); cout << ans.size() << "\n"; cout << ans << "\n"; } };
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; array<long long, 3> arr[80]; long long dp[80][80]; long long use[80][80]; int main() { int T; cin >> T; for (int i = 0; i < T; ++i) { int N, K; cin >> N >> K; for (int j = 0; j < N; ++j) { cin >> arr[j][1] >> arr[j][0]; arr[j][2] = j + 1; } memset(dp, 0, sizeof(dp)); memset(use, -1, sizeof(use)); sort(arr, arr + N); for (int j = 1; j <= N; ++j) { for (int k = 1; k <= K; ++k) { if (k > j) { dp[j][k] = -999999999999; use[j][k] = -1; continue; } dp[j][k] = max(dp[j - 1][k - 1] + arr[j - 1][0] * (k - 1) + arr[j - 1][1], dp[j - 1][k] + (K - 1) * arr[j - 1][0]); if (dp[j - 1][k - 1] + arr[j - 1][0] * (k - 1) + arr[j - 1][1] >= dp[j - 1][k] + (K - 1) * arr[j - 1][0]) { use[j][k] = arr[j - 1][2]; } else { use[j][k] = -1; } } } int keepTrack = K; vector<int> usin; set<int> used; for (int j = N; j >= 1; --j) { if (use[j][keepTrack] != -1) { usin.push_back(use[j][keepTrack]); used.insert(use[j][keepTrack]); keepTrack--; } } cout << 2 * N - K << endl; for (int j = 0; j < K - 1; ++j) { cout << usin[K - 1 - j] << " "; } for (int j = 1; j <= N; ++j) { if (used.find(j) == used.end()) { cout << j << " " << -j << " "; } } cout << usin[0] << endl; } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include<bits/stdc++.h> using namespace std; #define ll long long #define int ll #define ull unsigned long long #define pii pair<int,int> #define X first #define Y second #define mod ((ll)1e9+7) #define pb push_back #define mp make_pair #define abs(x) ((x)>0?(x):(-(x))) #define F(n) Fi(i,n) #define Fi(i,n) Fl(i,0,n) #define Fl(i,l,n) for(int i=l;i<n;i++) #define memres(a) memset(a,0,sizeof(a)) #define all(a) a.begin(),a.end() #define sz(a) ((int)a.size()) #define ceiling(a,b) (((a)+(b)-1)/(b)) #define endl '\n' //#define LOCAL #ifdef LOCAL #define debug(a) cerr<<#a<<" "<<a<<endl; #else #define debug(a) #endif int gcd(int a,int b){if(b==0)return a;return gcd(b,a%b);} pii ar[75]; bool cmp(int a,int b){return ar[a].Y<ar[b].Y;} signed main(){ ios_base::sync_with_stdio(0);cin.tie(0); int t; cin>>t; while(t--){ int n,k; cin>>n>>k; F(n)cin>>ar[i].X>>ar[i].Y; pii dp[76][76]; F(n+1)Fi(j,k+1)dp[i][j]={-1,-1}; dp[0][0]={0,0}; F(n){ Fi(j,k){ if(dp[i][j].X!=-1){ if(dp[i+1][j+1].X<dp[i][j].X+ar[i].X+ar[i].Y*i)dp[i+1][j+1]={dp[i][j].X+ar[i].X+ar[i].Y*i,i+1}; if(dp[i+1][j].X<dp[i][j].X+ar[i].Y*(k-1))dp[i+1][j]={dp[i][j].X+ar[i].Y*(k-1),dp[i][j].Y}; } } } int p=dp[n][k].Y,c=k; vector<int> v; bool us[n+1]={false}; while(p&&c){ v.pb(p); us[p]=true; p=dp[p-1][--c].Y; } cout<<n*2-k<<endl; sort(all(v),cmp); F(sz(v)-1)cout<<v[i]<<" "; for(int i=1;i<=n;i++)if(!us[i])cout<<i<<" "<<-i<<" "; cout<<v[(sz(v)-1)]<<endl; } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
java
import java.io.*; import java.util.*; public class gym{ static int n,k; static int[][]in,memo; static int inf=(int)1e8; static int dp(int i,int takenInFirstK){ if(takenInFirstK>k)return -inf; if(i>=n)return (takenInFirstK==k)?0:-inf; if(memo[i][takenInFirstK]!=-1)return memo[i][takenInFirstK]; int take=dp(i+1, takenInFirstK+1)+in[i][0]+(in[i][1]*takenInFirstK); int takeAndDelete=dp(i+1, takenInFirstK)+(in[i][1]*(k-1)); return memo[i][takenInFirstK]=Math.max(take, takeAndDelete); } static int chooseLast; static LinkedList<Integer> print,takeAndDel; static void trace(int i,int takenInFirstK){ if(takenInFirstK>k)return; if(i>=n)return; int ans=dp(i, takenInFirstK); int take=dp(i+1, takenInFirstK+1)+in[i][0]+(in[i][1]*takenInFirstK); if(ans==take){ if(takenInFirstK+1==k){ chooseLast=in[i][2]; } else{ print.add(in[i][2]); } trace(i+1, takenInFirstK+1); } else{ takeAndDel.add(in[i][2]); trace(i+1, takenInFirstK); } } static void main() throws Exception{ n=sc.nextInt();k=sc.nextInt(); in=new int[n][]; for(int i=0;i<n;i++){ in[i]=new int[] {sc.nextInt(),sc.nextInt(),i+1}; } Arrays.sort(in,(x,y)->x[1]==y[1]?x[0]-y[0]:x[1]-y[1]); memo=new int[n][k+1]; for(int i=0;i<n;i++){ for(int j=0;j<=k;j++){ memo[i][j]=-1; } } chooseLast=-1; takeAndDel=new LinkedList<>(); print=new LinkedList<>(); trace(0, 0); while(takeAndDel.size()>0){ int x=takeAndDel.pollFirst(); print.add(x); print.add(-x); } if(chooseLast!=-1){ print.add(chooseLast); } pw.println(print.size()); while(print.size()>0){ int x=print.pollFirst(); pw.print(x+" "); } pw.println(); } public static void main(String[] args) throws Exception{ pw=new PrintWriter(System.out); sc = new MScanner(System.in); int tc=sc.nextInt(); while(tc-->0)main(); pw.flush(); } static PrintWriter pw; static MScanner sc; static class MScanner { StringTokenizer st; BufferedReader br; public MScanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public MScanner(String file) throws Exception { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int[] intArr(int n) throws IOException { int[]in=new int[n];for(int i=0;i<n;i++)in[i]=nextInt(); return in; } public long[] longArr(int n) throws IOException { long[]in=new long[n];for(int i=0;i<n;i++)in[i]=nextLong(); return in; } public int[] intSortedArr(int n) throws IOException { int[]in=new int[n];for(int i=0;i<n;i++)in[i]=nextInt(); shuffle(in); Arrays.sort(in); return in; } public long[] longSortedArr(int n) throws IOException { long[]in=new long[n];for(int i=0;i<n;i++)in[i]=nextLong(); shuffle(in); Arrays.sort(in); return in; } public Integer[] IntegerArr(int n) throws IOException { Integer[]in=new Integer[n];for(int i=0;i<n;i++)in[i]=nextInt(); return in; } public Long[] LongArr(int n) throws IOException { Long[]in=new Long[n];for(int i=0;i<n;i++)in[i]=nextLong(); return in; } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public long nextLong() throws IOException { return Long.parseLong(next()); } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } } static void shuffle(int[]in) { for(int i=0;i<in.length;i++) { int idx=(int)(Math.random()*in.length); int tmp=in[i]; in[i]=in[idx]; in[idx]=tmp; } } static void shuffle(long[]in) { for(int i=0;i<in.length;i++) { int idx=(int)(Math.random()*in.length); long tmp=in[i]; in[i]=in[idx]; in[idx]=tmp; } } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; const int N = 100; array<long long, 3> a[N]; long long dp[N][N]; int anc[N][N]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { int n, k; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> a[i][1] >> a[i][0]; a[i][2] = i; } sort(a, a + n); if (k >= n) { for (int i = 0; i < n; i++) { cout << a[i][2] + 1 << " "; } cout << "\n"; } else { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { dp[i][j] = -INF; } } dp[0][0] = 0; for (int i = 0; i < n; i++) { for (int j = 0; j <= k; j++) { if (dp[i][j] != -INF) { if (dp[i + 1][j] < dp[i][j] + (k - 1) * a[i][0]) { dp[i + 1][j] = dp[i][j] + (k - 1) * a[i][0]; anc[i + 1][j] = j; } if (dp[i + 1][j + 1] < dp[i][j] + a[i][1] + a[i][0] * j) { dp[i + 1][j + 1] = dp[i][j] + a[i][1] + a[i][0] * j; anc[i + 1][j + 1] = j; } } } } vector<int> taken, killed; int x = n, y = k; while (x > 0) { if (anc[x][y] == y) killed.push_back(x - 1); else taken.push_back(x - 1); y = anc[x][y]; x--; } cout << taken.size() + killed.size() * 2 << "\n"; for (int i = 0; i + 1 < taken.size(); i++) { cout << a[taken[i]][2] + 1 << " "; } for (int i = 0; i < killed.size(); i++) { cout << (a[killed[i]][2] + 1) << " "; } for (int i = 0; i < killed.size(); i++) { cout << -(a[killed[i]][2] + 1) << " "; } cout << a[taken.back()][2] + 1 << "\n"; } } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> const int inf = 0x3f3f3f3f, Inf = 0x7fffffff; const long long INF = 0x7fffffffffffffff; __inline__ __attribute__((always_inline)) unsigned int rnd() { static unsigned int seed = 416; return seed ^= seed >> 5, seed ^= seed << 17, seed ^= seed >> 13; } template <typename _Tp> _Tp gcd(const _Tp &a, const _Tp &b) { return (!b) ? a : gcd(b, a % b); } template <typename _Tp> __inline__ __attribute__((always_inline)) _Tp abs(const _Tp &a) { return a >= 0 ? a : -a; } template <typename _Tp> __inline__ __attribute__((always_inline)) _Tp max(const _Tp &a, const _Tp &b) { return a < b ? b : a; } template <typename _Tp> __inline__ __attribute__((always_inline)) _Tp min(const _Tp &a, const _Tp &b) { return a < b ? a : b; } template <typename _Tp> __inline__ __attribute__((always_inline)) void chmax(_Tp &a, const _Tp &b) { (a < b) && (a = b); } template <typename _Tp> __inline__ __attribute__((always_inline)) void chmin(_Tp &a, const _Tp &b) { (b < a) && (a = b); } template <typename _Tp> __inline__ __attribute__((always_inline)) void read(_Tp &x) { char ch(getchar()); bool f(false); while (ch < 48 || ch > 57) f |= ch == 45, ch = getchar(); x = ch & 15, ch = getchar(); while (ch >= 48 && ch <= 57) x = (((x << 2) + x) << 1) + (ch & 15), ch = getchar(); if (f) x = -x; } template <typename _Tp, typename... Args> __inline__ __attribute__((always_inline)) void read(_Tp &t, Args &...args) { read(t); read(args...); } __inline__ __attribute__((always_inline)) int read_str(char *s) { char ch(getchar()); while (ch == ' ' || ch == '\r' || ch == '\n') ch = getchar(); char *tar = s; *tar = ch, ch = getchar(); while (ch != ' ' && ch != '\r' && ch != '\n' && ch != EOF) *(++tar) = ch, ch = getchar(); return tar - s + 1; } const int N = 85; struct node { long long a, b; int id; __inline__ __attribute__((always_inline)) bool operator<( const node &o) const { return b < o.b; } } a[N]; long long dp[N][N]; bool opt[N][N]; bool used[N]; void MAIN() { memset(used, 0, sizeof(used)); int n, k; read(n, k); for (int i = 1; i <= n; ++i) { read(a[i].a, a[i].b); a[i].id = i; } std::sort(a + 1, a + n + 1); if (k == n) { printf("%d\n", n); for (int i = 1; i <= n; ++i) printf("%d ", a[i].id); printf("\n"); return; } auto solve = [&](int id) -> long long { memset(dp, -63, sizeof(dp)); memset(opt, 0, sizeof(opt)); dp[0][0] = 0; for (int i = 1; i <= n; ++i) { if (i == id) { for (int j = 0; j <= k; ++j) { dp[i][j] = dp[i - 1][j] + a[i].a + a[i].b * (k - 1); opt[i][j] = 0; } continue; } dp[i][0] = dp[i - 1][0] + (k - 1) * a[i].b; opt[i][0] = 0; for (int j = 1; j <= k; ++j) { dp[i][j] = dp[i - 1][j] + (k - 1) * a[i].b; opt[i][j] = 0; long long tmp = dp[i - 1][j - 1] + a[i].a + (j - 1) * a[i].b; if (tmp > dp[i][j]) { dp[i][j] = tmp; opt[i][j] = 1; } } } return dp[n][k - 1]; }; long long ans = 0; int pos = 0; for (int i = 1; i <= n; ++i) { long long tmp = solve(i); if (tmp > ans) { ans = tmp; pos = i; } } pos = a[pos].id; fprintf(stderr, "%lld %d\n", ans, pos); solve(pos); std::vector<int> v; int cur = n, cnt = k - 1; while (cur) { if (opt[cur][cnt]) { --cnt; v.push_back(a[cur].id); used[a[cur].id] = true; } --cur; } std::reverse(v.begin(), v.end()); std::vector<int> res; for (auto it : v) res.push_back(it); for (int i = 1; i <= n; ++i) { if (!used[i] && i != pos) { res.push_back(i); res.push_back(-i); } } res.push_back(pos); printf("%d\n", ((int)res.size())); for (auto it : res) printf("%d ", it); printf("\n"); } int main() { int _; read(_); while (_--) MAIN(); return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 77; int T, n, k, f[N][N]; bool g[N][N], c[N]; void qr(int &x) { scanf("%d", &x); } struct node { int a, b, id; bool operator<(node t) const { return b < t.b; } } a[N]; int main() { qr(T); while (T--) { qr(n); qr(k); for (int i = 1; i <= n; i++) qr(a[i].a), qr(a[i].b), a[i].id = i; sort(a + 1, a + n + 1); memset(f, 0xcf, sizeof f); for (int i = 0; i <= n; i++) f[i][0] = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= k; j++) f[i][j] = max(f[i - 1][j], f[i - 1][j - 1] + a[i].a + (j - 1) * a[i].b), g[i][j] = f[i][j] ^ f[i - 1][j]; for (int i = n, j = k; j; i--) if (g[i][j]) c[i] = j--; else c[i] = 0; printf("%d\n", k + (n - k) * 2); for (int i = 1, j = 0; i <= n; i++) { if (j == k - 1) { for (int x = 1; x <= n; x++) if (!c[x]) printf("%d %d ", a[x].id, -a[x].id); } if (c[i]) printf("%d ", a[i].id), j++; } puts(""); } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; int t, k, n; vector<int> arr; long long A[100]; long long B[100]; int ord[100]; bool byB(int x, int y) { return B[x] < B[y]; } bool byOrd(int x, int y) { return ord[x] < ord[y]; } long long mem[100][100]; long long dp(int pos, int inK) { if (pos == n) return 0; if (mem[pos][inK] != -1) return mem[pos][inK]; long long &ret = mem[pos][inK]; if (inK == k) return ret = dp(pos + 1, inK) + B[arr[pos]] * long long(k - 1); else if (pos - n == k - inK) return dp(pos + 1, inK + 1) + A[arr[pos]] + B[arr[pos]] * long long(inK); return ret = max( dp(pos + 1, inK) + B[arr[pos]] * long long(k - 1), dp(pos + 1, inK + 1) + A[arr[pos]] + B[arr[pos]] * long long(inK)); } void solve() { int inK = 0, outK = k; for (int pos = 0; pos < n; pos++) { if (dp(pos, inK) == dp(pos + 1, inK) + B[arr[pos]] * long long(k - 1)) ord[arr[pos]] = outK++; else if (inK < k - 1) ord[arr[pos]] = ++inK; else { ord[arr[pos]] = n; inK++; } } sort(arr.begin(), arr.end(), byOrd); int last = 0; cout << n + (n - k) << endl; for (int i = 0; i < n; i++) { if (i >= k) cout << -last << " "; cout << arr[i] << " "; last = arr[i]; } cout << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> t; while (t--) { memset(mem, -1, sizeof(mem)); arr.clear(); cin >> n >> k; for (int i = 0; i < n; i++) { cin >> A[i + 1] >> B[i + 1]; arr.push_back(i + 1); } sort(arr.begin(), arr.end(), byB); solve(); } return 0; }