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> 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, 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 = 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; } for (int i = 1; i <= n; ++i) { ; } 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()); 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; 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(); ++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; mt19937 __MT(chrono::system_clock::now().time_since_epoch().count()); const int maxn = 75 + 10; long long a[maxn]; long long b[maxn]; int n, k; long long sumb; inline void rel_strategy(int mz) { priority_queue<pair<pair<long long, long long>, long long>, vector<pair<pair<long long, long long>, long long>>, less<pair<pair<long long, long long>, long long>>> q; for (auto j = (1); j <= (n); ++j) { if (mz == j) continue; q.push({{a[j], -b[j]}, j}); } vector<pair<pair<long long, long long>, long long>> c; vector<int> lt; vector<int> ans; long long tmpb = sumb; long long suma = 0; for (auto j = (0); j < (k - 1); ++j) { auto e = q.top(); q.pop(); e.first.second *= -1; swap(e.first.second, e.first.first); c.push_back(e); tmpb -= e.first.first; } while (!q.empty()) { lt.push_back(q.top().second); q.pop(); } sort((c).begin(), (c).end()); int has = 0; for (auto e : c) { suma += e.first.second; suma += e.first.first * has; has++; ans.push_back(e.second); } for (auto e : lt) { ans.push_back(e), ans.push_back(-e); } ans.push_back(mz); suma += tmpb * (k - 1); suma += a[mz]; printf("%d\n", (int)ans.size()); for (auto e : ans) { printf("%d ", e); } puts(""); } inline void solve() { long long ans = -1; int mz; for (auto i = (1); i <= (n); ++i) { priority_queue<pair<pair<long long, long long>, long long>, vector<pair<pair<long long, long long>, long long>>, less<pair<pair<long long, long long>, long long>>> q; for (auto j = (1); j <= (n); ++j) { if (i == j) continue; q.push({{a[j], -b[j]}, j}); } vector<pair<pair<long long, long long>, long long>> c; long long tmpb = sumb; long long suma = 0; for (auto j = (0); j < (k - 1); ++j) { auto e = q.top(); q.pop(); e.first.second *= -1; swap(e.first.second, e.first.first); c.push_back(e); tmpb -= e.first.first; } sort((c).begin(), (c).end()); int has = 0; for (auto e : c) { suma += e.first.second; suma += e.first.first * has; has++; } suma += tmpb * (k - 1); suma += a[i]; if (suma > ans) { ans = suma; mz = i; } } rel_strategy(mz); } int main(void) { int T; scanf("%d", &(T)); for (; T--;) { scanf("%d", &(n)), scanf("%d", &(k)); sumb = 0; for (auto i = (1); i <= (n); ++i) scanf("%lld", &(a[i])), scanf("%lld", &(b[i])), sumb += b[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> #define ld long double #define endl "\n" #define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define pb(x) push_back(x) #define mp(a,b) make_pair(a,b) #define ms(v,x) memset(v,x,sizeof(v)) #define all(v) v.begin(),v.end() #define ff first #define ss second #define rep(i, a, b) for(int i = a; i < (b); ++i) //#define per(i, a, b) for(int i = b-1; i>=a ; i--) #define trav(a, x) for(auto& a : x) #define allin(a , x) for(auto a : x) #define td(v) v.begin(),v.end() #define sz(v) (int)v.size() //#define M 1000000007 // 1e9 + 7 #define int long long using namespace std; typedef vector<int> vi; typedef long long ll; typedef pair<int,int> pii; inline ll mod(ll n, ll m ){ ll ret = n%m; if(ret < 0) ret += m; return ret; } ll gcd(ll a, ll b){return (b == 0LL ? a : gcd(b, a%b));} ll exp(ll a,ll b,ll m){ if(b==0LL) return 1LL; if(b==1LL) return mod(a,m); ll 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 int N = 80; struct coisa{ int a,b,id; coisa(){} coisa(int A,int B,int C){ a = A,b=B,id=C; } }v[N]; int dp[N][N]; void solve(){ int n,k; cin>>n>>k; for(int i=1;i<=n;i++){ int 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 << endl; cout << v[1].id << endl; return; } if(k==n){ cout << n << endl; for(int i=1;i<=n;i++){ cout << v[i].id<<" "; } cout << endl; return; } rep(i,0,N)rep(j,0,N)dp[i][j] = -1e9; dp[0][0] = 0; for(int i=1;i<=n;i++){ for(int j=0;j<=min(i-1,k);j++){ // pega ou nao dp[i][j+1] = max(dp[i][j+1],dp[i-1][j] + j*v[i].b + v[i].a); // nao pega dp[i][j] = max(dp[i][j],dp[i-1][j] + (k-1)*v[i].b); } } vector<int> tira,col; int j = k; for(int i=n;i>=1;i--){ if(i==1){ if(j==0)tira.pb(i); else {col.pb(i);j--;} break; } if(j==0)tira.pb(i); else{ if(dp[i][j] == dp[i-1][j-1] + (j-1)*v[i-1].b + v[i].a){ col.pb(i); j--; }else{ tira.pb(i); } } } reverse(col.begin(),col.end()); vector<int> ans; for(int i=0;i<sz(col) - 1;i++)ans.pb(v[col[i]].id); for(int x : tira)ans.pb(v[x].id),ans.pb(-v[x].id); ans.pb(v[col[sz(col)-1]].id); cout << ans.size() << endl; for(int x : ans )cout << x<<" "; cout << endl; } int32_t main(){ fastio; int t; cin>>t; while(t--){ solve(); } // Math -> gcd it all // Did u check N=1? Did you switch N,M? }
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; LL dp[100][100]; int path[100][100]; void update(LL &ret, LL tmp, int summoned, int idx, bool have) { if (tmp > ret) { ret = tmp; path[summoned][idx] = have; } } LL memo(int summoned, int idx) { if (idx == n) { if (summoned < k) return -(1LL << 50LL); return 0LL; } LL &ret = dp[summoned][idx]; if (ret != -1) return ret; ret = 0LL; LL tmp; if (summoned < k) update(ret, BA[idx][1] + (1LL * 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.resize(n); int x, y; FOR (i, n) { cin >> x >> y; BA[i] = {y, x, i}; } sort(all(BA)); SET(dp, 255); LL ret = memo(0, 0); // recover int summoned = 0; vector<int> takeThem; bool marked[100]; SET(marked, 0); FOR (i, n) { if (path[summoned][i]) { takeThem.pb(BA[i][2]); summoned++; marked[BA[i][2]] = true; } } 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 << " "; } cout << takeThem[SZ(takeThem) - 1]+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; using int64 = long long; using PII = pair<int, int>; const int MAXN = 100005; const int INF = 0x3f3f3f3f; vector<int> a[MAXN]; int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); #endif int t; cin >> t; while(t--) { int n,k; cin >> n >> k; int x,y; for(int i=1;i<=n;i++) { cin >> x >> y; a[i] = {x,y,i}; } sort(a+1,a+n+1,[&](auto& x, auto& y) { return x[0]==y[0]?x[1]<y[1]:x[0]>y[0]; }); sort(a+1,a+k,[&](auto& x, auto& y) { return x[1] < y[1]; }); for(int i=1;i<k;i++) cout << a[i][2] << " "; sort(a+k+1,a+n+1,[&](auto& x, auto& y) { return x[1]<y[1]; }); for(int i=k+1;i<=n;i++) cout << a[i][2] << " " << -a[i][2] << " "; cout << a[k][2] << 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
//W4P3R #include<bits/stdc++.h> #define inf 1e9 #define eps 1e-6 #define mp make_pair #define pb push_back #define re register ll #define fr first #define sd second #define pa pair<int,int> #define FOR(i,a,b) for(re i=a;i<=b;i++) #define REP(i,a,b) for(re i=a;i>=b;i--) #define lowbit(x) (x&(-x)) #define Z(x) (x>=mod?x-mod:x) #define N 210 #define M 100010 using namespace std; typedef long long ll; typedef unsigned long long ull; inline ll read() { char ch=getchar(); ll s=0,w=1; while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();} while(ch>='0'&&ch<='9'){s=s*10+ch-'0';ch=getchar();} return s*w; } int t,S,T; int n,k; struct edge { int next,to,fl,v; }e[M<<1]; int head[N],cnt=1; pa p[N];int a[N],b[N]; int minn[N],dist[N],pre[N]; int vis[N];queue<int>Q; inline void add(int x,int y,int fl,int v) { e[++cnt]=(edge){head[x],y,fl,v};head[x]=cnt; e[++cnt]=(edge){head[y],x,0,-v};head[y]=cnt; } void Update(int x,int fl) { e[pre[x]].fl-=fl; e[pre[x]^1].fl+=fl; if(e[pre[x]^1].to)Update(e[pre[x]^1].to,fl); } inline void spfa() { FOR(i,1,T)dist[i]=-inf;Q.push(S);vis[S]=1;dist[S]=0;minn[S]=inf; while(!Q.empty()) { int x=Q.front();Q.pop();vis[x]=0; for(re i=head[x];i;i=e[i].next) { if(e[i].fl&&dist[e[i].to]<dist[x]+e[i].v) { dist[e[i].to]=dist[x]+e[i].v;minn[e[i].to]=min(e[i].fl,minn[x]); pre[e[i].to]=i; if(!vis[e[i].to]){Q.push(e[i].to);vis[e[i].to]=1;} } } } if(dist[T]==-inf)return ; Update(T,minn[T]); } inline int EK() { int sum=0; while(true) { spfa(); if(dist[T]==-inf)return sum; sum+=minn[T]*dist[T]; } } int main() { //ios::sync_with_stdio(false); //freopen(".in","r",stdin); //freopen(".out","w",stdout); t=read(); while(t--) { n=read(),k=read();k=min(k,n);T=n+k+2; cnt=1;memset(head,0,sizeof(head)); FOR(i,1,n)a[i]=read(),b[i]=read(); if(n<=k) { FOR(i,1,n)p[i]=mp(b[i],i); sort(p+1,p+n+1); FOR(i,1,n)printf("%d ",p[i].sd);puts("");continue; } FOR(i,1,n)add(S,i,1,0); FOR(i,1,k)add(i+n,T,1,0); add(n+k+1,T,n-k,0); FOR(i,1,n)FOR(j,1,k) { add(i,n+j,1,a[i]+b[i]*(j-1)); } FOR(i,1,n)add(i,n+k+1,1,b[i]*(k-1)); EK(); vector<int>ans; FOR(x,n+1,n+k-1)for(re i=head[x];i;i=e[i].next) { if(e[i].fl&&e[i].to<=n&&e[i].to)ans.pb(e[i].to); } int p=n+k+1; for(re i=head[p];i;i=e[i].next) { if(e[i].fl&&e[i].to<=n&&e[i].to){ans.pb(e[i].to);ans.pb(-e[i].to);} } p=n+k; for(re i=head[p];i;i=e[i].next) { if(e[i].fl&&e[i].to<=n)ans.pb(e[i].to); } printf("%d\n",(int)ans.size()); for(int v:ans)printf("%d ",v);puts("");ans.clear(); } return 0; } //check first submit second
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 gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int n, k, tab[75][75], d, linkx[75], linky[75], tx[75], ty[75]; bool visx[75], visy[75]; bool dfs(int x) { visx[x] = true; for (int i = 0; i < n; i++) { if (!visy[i] && tx[x] + ty[i] == tab[x][i]) { visy[i] = true; if (linky[i] == -1 || dfs(linky[i])) { linkx[x] = i, linky[i] = x; return true; } } } return false; } void update() { d = 1e7; for (int i = 0; i < n; i++) { if (!visx[i]) continue; for (int j = 0; j < n; j++) { if (visy[j]) continue; d = min(d, tx[i] + ty[j] - tab[i][j]); } } for (int i = 0; i < n; i++) if (visx[i]) tx[i] -= d; for (int i = 0; i < n; i++) if (visy[i]) ty[i] += d; return; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { cin >> n >> k; pair<int, int> ar[n]; memset(linkx, -1, sizeof(linkx)); memset(linky, -1, sizeof(linky)); memset(ty, 0, sizeof(ty)); for (int i = 0; i < n; i++) cin >> ar[i].first >> ar[i].second; for (int i = 0; i < k; i++) { for (int j = 0; j < n; j++) { tab[j][i] = ar[j].first + ar[j].second * i; } } for (int i = 0; i < n; i++) tx[i] = tab[i][k - 1]; for (int i = k; i < n; i++) { for (int j = 0; j < n; j++) tab[j][i] = ar[j].second * (k - 1); } for (int i = 0; i < n; i++) { while (!dfs(i)) { update(); memset(visx, 0, sizeof(visx)); memset(visy, 0, sizeof(visy)); } } int ans[75]; for (int i = 0; i < n; i++) { ans[linkx[i]] = i; } for (int i = 0; i < k - 1; i++) cout << ans[i] + 1 << " "; for (int i = k; i < n; i++) cout << ans[i] + 1 << " " << -ans[i] - 1 << " "; cout << ans[k - 1] + 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 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); 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 pos = 0, inK = 0, outK = k; for (; 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); memset(mem, -1, sizeof(mem)); cin >> t; while (t--) { 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; }
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[75]; int b[75]; int order[75]; int main() { cin.tie(0); ios_base::sync_with_stdio(0); int t; cin >> t; while (t--) { int n, k; cin >> n >> k; for (int i = 0; i < n; ++i) cin >> a[i] >> b[i], order[i] = i; sort(order, order + n, [&](int x, int y) { if (b[x] == b[y]) return a[x] > a[y]; else return b[x] < b[y]; }); vector<vector<int>> dp(n + 1, vector<int>(n + 1, 0)); for (int ii = 1; ii <= n; ++ii) { int i = order[ii - 1]; for (int j = 0; j <= ii; ++j) { dp[ii][j] = dp[ii - 1][j] + b[i] * (k - 1); if (j) dp[ii][j] = max(dp[ii][j], dp[ii - 1][j - 1] + a[i] + b[i] * (j - 1)); } } vector<int> ans; int cur = k; int last = -1; for (int i = n; i >= 1; i--) { if (cur && (dp[i - 1][cur - 1] + a[order[i - 1]] + b[order[i - 1]] * (cur - 1) >= dp[i - 1][cur] + b[order[i - 1]] * (k - 1))) { if (last == -1) last = order[i - 1] + 1; else ans.push_back(order[i - 1] + 1); cur--; } else { ans.push_back(-order[i - 1] - 1); ans.push_back(order[i - 1] + 1); } } reverse(ans.begin(), ans.end()); ans.push_back(last); cout << ans.size() << endl; for (auto q : ans) cout << q << " "; 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; 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)); vector<vector<int>> rev(N, vector<int>(K, -1)); vector<vector<int>> par(N, vector<int>(K, -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; if (k + 1 < K) { temp = dp[ti][k] - t.second.first + t.first * k; 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; vector<int> sub; int j = K - 1; ti = N - 1; while (ti >= 0) { int t = rev[ti][j]; if (t < 0) sub.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; reverse(tans.begin(), tans.end()); for (auto &i : sub) tans.push_back(i), tans.push_back(-i); tans.push_back(V[i].second.second + 1); ans = tans; } } cout << ans.size() << "\n"; for (auto &i : ans) cout << i << " "; cout << 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; struct pt { int a, b, ind; pt() {} pt(int a, int b, int ind) : a(a), b(b), ind(ind) {} }; const int inf = 1e9; int n, k; void solve(int tc) { scanf("%d", &n); scanf("%d", &k); vector<pt> ar(n); for (int i = 0; i < n; ++i) { int a, b; scanf("%d", &a); scanf("%d", &b); ar[i] = pt(a, b, i + 1); } sort(ar.begin(), ar.end(), [](pt &x, pt &y) -> bool { if (x.b == y.b) return x.a < y.a; return x.b < y.b; }); vector<vector<int> > dp(n + 1, vector<int>(k + 1, -inf)); vector<vector<int> > trace(n + 1, vector<int>(k + 1, -1)); dp[0][0] = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < k; ++j) { if (dp[i + 1][j + 1] < dp[i][j] + ar[i].a + (j)*ar[i].b) { dp[i + 1][j + 1] = dp[i][j] + ar[i].a + (j)*ar[i].b; trace[i + 1][j + 1] = 1; } if (dp[i + 1][j] < dp[i][j] + (k - 1) * ar[i].b) { dp[i + 1][j] = dp[i][j] + (k - 1) * ar[i].b; trace[i + 1][j] = 0; } } } int moves = 2 * n - k; cout << moves << "\n"; vector<int> ans[2]; while (n > 0) { assert(trace[n][k] != -1); int p = trace[n][k]; ans[p].emplace_back(ar[n - 1].ind); k -= p; --n; } reverse(ans[1].begin(), ans[1].end()); reverse(ans[0].begin(), ans[0].end()); for (int i = 0; i < (int)ans[1].size() - 1; ++i) { cout << ans[1][i] << " "; } for (int it : ans[0]) { cout << it << " " << -it << " "; } cout << ans[1].back() << "\n"; } int main() { int tt = 1; scanf("%d", &tt); for (int i = 1; i <= tt; ++i) { solve(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; 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); 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 pos = 0, inK = 0, outK = k; for (; 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; } 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); memset(mem, -1, sizeof(mem)); cin >> t; while (t--) { 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); dp(0, 0); 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
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(reverse=True) 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
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/2); } 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]+mnn[i].b*(k-1), dp[i-1][j-1]+mnn[i].a+mnn[i].b*(j-1)); poss[i][j]=(dp[i][j]^dp[i-1][j]+mnn[i].b*(k-1))==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 <bits/stdc++.h> using namespace std; int a[75]; int b[75]; int order[75]; int main() { cin.tie(0); ios_base::sync_with_stdio(0); int t; cin >> t; while (t--) { int n, k; cin >> n >> k; for (int i = 0; i < n; ++i) cin >> a[i] >> b[i], order[i] = i; sort(order, order + n, [&](int x, int y) { if (b[x] == b[y]) return a[x] > a[y]; else return b[x] < b[y]; }); vector<vector<int>> dp(n + 1, vector<int>(n + 1, 0)); for (int ii = 1; ii <= n; ++ii) { int i = order[ii - 1]; for (int j = 0; j <= ii; ++j) { dp[ii][j] = dp[ii - 1][j] + b[i] * (k - 1); if (j) dp[ii][j] = max(dp[ii][j], dp[ii - 1][j - 1] + a[i] + b[i] * (j - 1)); } } vector<int> ans; int cur = k; int last = -1; for (int i = n; i >= 1; i--) { if (cur && (dp[i - 1][cur - 1] + a[order[i - 1]] + b[order[i - 1]] * (cur - 1) >= dp[i - 1][cur] + b[order[i - 1]] * (k - 1))) { if (last == -1) last = order[i - 1] + 1; else ans.push_back(order[i - 1] + 1); cur--; } else { ans.push_back(-order[i - 1] - 1); } } reverse(ans.begin(), ans.end()); vector<int> ans2; for (auto q : ans) if (q > 0) ans2.push_back(q); for (auto q : ans) if (q < 0) ans2.push_back(-q), ans2.push_back(q); ans2.push_back(last); cout << ans2.size() << endl; for (auto q : ans2) cout << q << " "; 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; const int maxn = 100; const int inf = 2147483647; const double pi = 3.1415926576; const long long INF = 9223372036854775807ll; int t, n, k, path[maxn][maxn], gr1[maxn], gr2[maxn], tot1, tot2; long long dp[maxn][maxn]; struct Node { long long a, b; int id; } st[maxn]; bool cmp(Node a, Node b) { return a.b < b.b || a.b == b.b && a.a < b.a; } int main() { scanf("%d", &t); for (register int i = 1; i < (maxn); ++i) dp[0][i] = -1; while (t--) { scanf("%d%d", &n, &k); for (register int i = 1; i < (n + 1); ++i) { scanf("%lld%lld", &st[i].a, &st[i].b); st[i].id = i; } sort(st + 1, st + 1 + n, cmp); for (register int i = 1; i < (n + 1); ++i) for (register int j = 0; j < (min(k, i) + 1); ++j) { if (dp[i - 1][j] != -1) dp[i][j] = dp[i - 1][j] + ((long long)k - 1ll) * st[i].b, path[i][j] = j; else dp[i][j] = -INF; if (j && dp[i - 1][j - 1] != -1) { if (dp[i][j] < dp[i - 1][j - 1] + st[i].a + ((long long)j - 1ll) * st[i].b) { dp[i][j] = dp[i - 1][j - 1] + st[i].a + ((long long)j - 1ll) * st[i].b; path[i][j] = j - 1; } } if (dp[i][j] == -INF) dp[i][j] = -1; } int jj = path[n][k], pre = k, cnt = n; tot1 = tot2 = 0; while (cnt) { if (jj == pre) gr2[tot2++] = st[cnt].id; else gr1[tot1++] = st[cnt].id; cnt--, pre = jj; jj = path[cnt][jj]; } printf("%d\n", tot1 + tot2 * 2); for (register int i = tot1 - 1; i >= (1); --i) printf("%d ", gr1[i]); for (register int i = tot2 - 1; i >= (0); --i) printf("%d %d ", gr2[i], -gr2[i]); printf("%d\n", gr1[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 = 0x3f3f3f3f3f3f3f3f; const int N = 80; int id[N], a[N], b[N]; int res[N][N]; long long dp[N][N]; bool cmp(int x, int y) { return b[x] < b[y]; } void solve() { int n, k; cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i] >> b[i], id[i] = i; sort(id + 1, id + 1 + n, cmp); for (int i = 0; i <= n; i++) for (int j = 0; j <= i; j++) dp[i][j] = -inf, res[i][j] = 0; dp[0][0] = 0; for (int i = 0; i < n; i++) for (int j = 0; j <= i; j++) { if (dp[i][j] >= 0) { long long w = dp[i][j] + j * b[id[i + 1]] + a[id[i + 1]]; if (w > dp[i + 1][j + 1]) { dp[i + 1][j + 1] = w; res[i + 1][j + 1] = id[i + 1]; } w = dp[i][j] + (k - 1) * b[id[i + 1]]; if (w > dp[i + 1][j]) { dp[i + 1][j] = w; res[i + 1][j] = -id[i + 1]; } } } vector<int> A, B, ans; for (int i = n; i; i--) { if (res[i][k] > 0) { A.push_back(res[i][k]); --k; } else { B.push_back(-res[i][k]); } } reverse(B.begin(), B.end()); for (int i = 0; i < (int)A.size() - 1; i++) ans.push_back(A[i]); for (auto it : B) { ans.push_back(it); ans.push_back(-it); } ans.push_back(A.back()); cout << (int)ans.size() << '\n'; for (auto it : ans) cout << it << ' '; cout << '\n'; } int main() { std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); int _; cin >> _; while (_--) 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; struct pt { int a, b, ind; pt() {} pt(int a, int b, int ind) : a(a), b(b), ind(ind) {} }; const int inf = 1e9; int n, k; void solve(int tc) { scanf("%d", &n); scanf("%d", &k); vector<pt> ar(n); for (int i = 0; i < n; ++i) { int a, b; scanf("%d", &a); scanf("%d", &b); ar[i] = pt(a, b, i + 1); } sort(ar.begin(), ar.end(), [](pt &x, pt &y) -> bool { if (x.b == y.b) return x.a < y.a; return x.b < y.b; }); vector<vector<int> > dp(n + 1, vector<int>(k + 1, -inf)); vector<vector<int> > trace(n + 1, vector<int>(k + 1, 0)); dp[0][0] = 0; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= k; ++j) { if (dp[i][j] < dp[i - 1][j - 1] + ar[i - 1].a + (j - 1) * ar[i - 1].b) { dp[i][j] = dp[i - 1][j - 1] + ar[i - 1].a + (j - 1) * ar[i - 1].b; trace[i][j] = 1; } if (dp[i][j] < dp[i - 1][j] + (k - 1) * ar[i - 1].b) { dp[i][j] = dp[i - 1][j] + (k - 1) * ar[i - 1].b; trace[i][j] = 0; } } } int moves = 2 * n - k; cout << moves << "\n"; vector<int> ans[2]; while (n > 0) { int p = trace[n][k]; ans[p].emplace_back(ar[n - 1].ind); k -= p; --n; } reverse(ans[1].begin(), ans[1].end()); reverse(ans[0].begin(), ans[0].end()); for (int i = 0; i < (int)ans[1].size() - 1; ++i) { cout << ans[1][i] << " "; } for (int it : ans[0]) { cout << it << " " << -it << " "; } cout << ans[1].back() << "\n"; } int main() { int tt = 1; scanf("%d", &tt); for (int i = 1; i <= tt; ++i) { solve(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; const int inf = 1e9; const long long inf_ll = 1e18; const long long N = 100; array<long long, 3> a[N]; long long dp[N][N], res[2 * N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t; cin >> t; while (t--) { long long n, k; cin >> n >> k; for (long long i = 0; i < n; i++) { cin >> a[i][1] >> a[i][0]; a[i][2] = i + 1; } sort(a, a + n); long long ans = 0; for (long long r = 0; r < n; r++) { fill(dp[0] + 1, dp[0] + k, -inf_ll); dp[0][0] = 0; for (long long i = 1; i <= n; i++) for (long long j = 0; j < k; j++) { if (i - 1 == r) { dp[i][j] = dp[i - 1][j]; continue; } dp[i][j] = dp[i - 1][j] + a[i - 1][0] * (k - 1); if (j > 0) dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + a[i - 1][1] + a[i - 1][0] * (j - 1)); } long long c = dp[n][k - 1] + a[r][1] + a[r][0] * (k - 1); if (c > ans) { ans = c; res[2 * n - k - 1] = a[r][2]; int p = k - 2, q = 2 * n - k - 2, j = k - 1; for (int i = n; i > 0; i--) { if (i - 1 == r) continue; if (j == 0 || dp[i - 1][j] + a[i - 1][0] * (k - 1) >= dp[i - 1][j - 1] + a[i - 1][1] + a[i - 1][0] * (j - 1)) { res[q--] = -a[i - 1][2]; res[q--] = a[i - 1][2]; } else { res[p--] = a[i - 1][2]; j--; } } } } for (int i = 0; i < 2 * n - k; i++) cout << res[i] << " \n"[i == 2 * n - k - 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 <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++) { if (dp[i][k - 1][1] > dp[pos][cnt][flag]) { pos = i, cnt = k - 1, flag = 1; } } 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; 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 (i = 1; i <= n; ++i) ans1[i] = ans2[i] = 0; 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 + (i - 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
cpp
#include <bits/stdc++.h> using namespace std; struct minion { int a, b, id; }; bool sortb(minion x, minion y) { return x.b < y.b; } int n, k; minion ar[100]; int dp[100][100]; void calc() { sort(ar, ar + n, sortb); for (int i = 0; i <= n; ++i) for (int j = 0; j <= k; ++j) dp[i][j] = -1e9; dp[0][0] = ar[0].b * (k - 1); dp[0][1] = ar[0].a; for (int i = 1; i < n; ++i) { dp[i][0] = dp[i - 1][0] + ar[i].b * (k - 1); for (int j = 1; j <= k; ++j) dp[i][j] = max(dp[i - 1][j - 1] + ar[i].a + (j - 1) * ar[i].b, dp[i - 1][j] + ar[i].b * (k - 1)); } int place = k; vector<minion> used, notused; for (int i = n - 1; i > 0; --i) { if (place == 0 || dp[i][place] == dp[i - 1][place] + ar[i].b * (k - 1)) notused.push_back(ar[i]); else { --place; used.push_back(ar[i]); } } if (place == 1) used.push_back(ar[0]); else notused.push_back(ar[0]); for (int i = used.size() - 1; i > 0; --i) cout << used[i].id << " "; for (auto u : notused) cout << u.id << " -" << u.id << ' '; cout << used[0].id << '\n'; } 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 + 1; } calc(); } }
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; } sort(p + 1, p + n + 1); if (k == 1) { cout << 1 << '\n'; cout << idx << '\n'; continue; } if (k == n) { cout << n << '\n'; for (int i = 1; i <= n; ++i) { cout << p[i].second << " "; } cout << '\n'; continue; } 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()); 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; 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] + (k - 1) * b) { from[i + 1][j] = {ind, j}; dp[i + 1][j] = dp[i][j] + (k - 1) * b; } 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> void read(T &x) { x = 0; int f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); } x *= f; } inline void write(int x) { if (x > 9) write(x / 10); putchar(x % 10 + '0'); } const int N = 80; const long long INF = 1ll << 60; long long a[N], b[N], id[N], n, m; inline bool upd(long long &x, long long v) { if (v <= x) return 0; x = v; return 1; } long long dp[N][N], lst[N][N]; int ans[N], lans; int a2[N], l2; int tp[N]; inline void solve() { int i, j; read(n), read(m); for (i = 1; i <= n; ++i) tp[i] = 0, read(a[i]), read(b[i]), id[i] = i; for (i = 1; i <= n; ++i) for (j = i + 1; j <= n; ++j) if (b[i] < b[j]) swap(a[i], a[j]), swap(b[i], b[j]), swap(id[i], id[j]); for (i = 0; i <= n; ++i) for (j = 0; j <= n; ++j) dp[i][j] = -INF; dp[0][0] = 0; for (i = 1; i <= n; ++i) for (j = 0; j <= n; ++j) { if (upd(dp[i][j], dp[i - 1][j] + b[i] * (m - 1))) lst[i][j] = 0; if (j && upd(dp[i][j], dp[i - 1][j - 1] + b[i] * (m - j) + a[i])) lst[i][j] = 1; } lans = 0; int nown = n, nowm = m; while (nown) { if (lst[nown][nowm]) ans[++lans] = nown, --nowm, tp[nown] = 1; --nown; } l2 = 0; for (i = 1; i < lans; ++i) a2[++l2] = id[ans[i]]; for (i = 1; i <= n; ++i) if (!tp[i]) { a2[++l2] = id[i]; a2[++l2] = -id[i]; } a2[++l2] = id[ans[lans]]; cout << l2 << '\n'; for (i = 1; i <= l2; ++i) cout << a2[i] << (i < l2 ? ' ' : '\n'); } int main() { int T; read(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; 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]; } struct MCMF { int n, m; vector<Edge> edge; vector<int> G[maxn]; int inq[maxn], d[maxn], p[maxn], a[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; } 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(i); else if (tag[to] == 2) ans2.push_back(i); } } 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--) { int n, k; read(n); read(k); 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 <= n; i++) { for (int j = 1; j <= n; j++) { M.add(i, i + n, 1, -(1LL * (i - 1) * b[i] + a[i]), 1); M.add(i, i + n + n, 1, -1LL * (k - 1) * b[i], 2); } } for (int i = n + 1; i <= n + n; i++) { M.add(i, 3 * n + 1, 1, 0, -1); M.add(i + n, 3 * n + 2, 1, 0, -1); } M.add(3 * n + 1, 3 * n + 3, k, 0, -1); M.add(3 * n + 2, 3 * n + 3, n - k, 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; template <class A> ostream& operator<<(ostream& out, const vector<A>& v) { out << "["; for (int ind = 0; ind < int(int(v.size())); ++ind) { if (ind) out << ", "; out << v[ind]; } return out << "]"; } template <class A, class B> ostream& operator<<(ostream& out, const pair<A, B>& p) { return out << "(" << p.x << ", " << p.y << ")"; } const int INF = int(1e9); const long long int INF64 = long long int(1e18); const int MOD = int(1e9) + 7; const long double EPS = 1e-9; const long double PI = acos(-1.0); const int N = 75 + 1; int n, k; pair<pair<int, int>, int> minions[N]; bool read() { if (scanf("%d %d", &n, &k) != 2) return false; for (int id = 0; id < int(n); ++id) { scanf("%d%d", &minions[id].first.first, &minions[id].first.first); minions[id].second = id; } return true; } int dp[N][N]; int pos[N][N]; void solve() { sort( minions, minions + n, [](pair<pair<int, int>, int> minion1, pair<pair<int, int>, int> minion2) { return minion1.first.second != minion2.first.second ? minion1.first.second < minion2.first.second : minion1.first.first < minion2.first.first; }); memset(dp, -INF, sizeof(dp)); dp[0][0] = 0; for (int m = 0; m < int(n); ++m) { for (int fgCnt = 0; fgCnt < int(m); ++fgCnt) { int a = minions[m].first.first; int b = minions[m].first.second; if (dp[m][fgCnt] >= 0) { if (dp[m + 1][fgCnt + 1] < dp[m][fgCnt] + a + fgCnt * b) { dp[m + 1][fgCnt + 1] = dp[m][fgCnt] + a + fgCnt * b; pos[m + 1][fgCnt + 1] = fgCnt; } if (dp[m + 1][fgCnt] < dp[m][fgCnt] + (k - 1) * b) { dp[m + 1][fgCnt] = dp[m][fgCnt] + (k - 1) * b; pos[m + 1][fgCnt] = fgCnt; } } } } vector<int> fg, sg; int fgCnt = k; for (int m = n; m > 0; --m) { if (pos[m][fgCnt] == fgCnt) { sg.push_back(minions[m - 1].second + 1); } else { fg.push_back(minions[m - 1].second + 1); } fgCnt = pos[m][fgCnt]; } reverse(begin(fg), end(fg)); reverse(begin(sg), end(sg)); printf("%d\n", int(fg.size()) + 2 * int(sg.size())); for (int ind = 0; ind < int(int(fg.size()) - 1); ++ind) printf("%d ", fg[ind]); for (int m : sg) printf("%d %d ", m, -m); printf("%d\n", fg.back()); } int main() { cerr.precision(15); cout.precision(15); cerr << fixed; cout << fixed; int t; scanf("%d", &t); while (read()) 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; 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] * j); 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; x.push_back(p + 1); 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 { x.push_back(-e[i - 1].second - 1); x.push_back(e[i - 1].second + 1); i--; } } else { x.push_back(-e[i - 1].second - 1); x.push_back(e[i - 1].second + 1); i--; } } reverse(x.begin(), x.end()); 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 Maxn = 88, inf = 0x3f3f3f3f; 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 = 0; i <= n; ++i) for (int j = 0; j <= m; ++j) f[i][j] = flag[i][j] = -inf; for (int i = 1; i <= n; ++i) s[i] = vis[i] = 0; cnt = 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); init(); for (int i = 1; i <= n; ++i) a[i] = g[i].a, b[i] = g[i].b, id[i] = g[i].id; f[0][0] = 0; 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); 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]]); } 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 = 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); 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
#pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") //#pragma GCC optimize(3) //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //#pragma GCC target("sse3","sse2","sse") //#pragma GCC target("avx","sse4","sse4.1","sse4.2","ssse3") //#pragma GCC target("f16c") //#pragma GCC optimize("inline","fast-math","unroll-loops","no-stack-protector") //#pragma GCC diagnostic error "-fwhole-program" //#pragma GCC diagnostic error "-fcse-skip-blocks" //#pragma GCC diagnostic error "-funsafe-loop-optimizations" //#pragma GCC diagnostic error "-std=c++14" #include "bits/stdc++.h" #include "ext/pb_ds/tree_policy.hpp" #include "ext/pb_ds/assoc_container.hpp" #define PB push_back #define PF push_front #define LB lower_bound #define UB upper_bound #define fr(x) freopen(x,"r",stdin) #define fw(x) freopen(x,"w",stdout) #define REP(x, l, u) for(ll x = l;x<u;x++) #define RREP(x, l, u) for(ll x = l;x>=u;x--) #define complete_unique(a) a.erase(unique(begin(a),end(a)),end(a)) #define mst(x, a) memset(x,a,sizeof(x)) #define all(a) begin(a),end(a) #define rall(a) rbegin(a),rend(a) #define PII pair<int,int> #define PLL pair<ll,ll> #define MP make_pair #define lowbit(x) ((x)&(-(x))) #define bitcnt(x) (__builtin_popcountll(x)) #define lson (ind<<1) #define rson (ind<<1|1) #define se second #define fi first #define sz(x) ((int)x.size()) #define EX0 exit(0); typedef long long ll; typedef unsigned long long ull; typedef double db; typedef long double ld; using namespace __gnu_pbds; //required using namespace std; template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; typedef vector<ll> VLL; typedef vector<int> VI; const ll mod = 1e9 + 7; string to_string (string s) { return '"' + s + '"'; } string to_string (const char *s) { return to_string ((string) s); } string to_string (bool b) { return (b ? "true" : "false"); } template<typename A, typename B> string to_string (pair<A, B> p) { return "(" + to_string (p.first) + ", " + to_string (p.second) + ")"; } template<typename A> string to_string (A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string (x); } res += "}"; return res; } void debug_out () { cerr<<endl; } template<typename Head, typename... Tail> void debug_out (Head H, Tail... T) { cerr<<" "<<to_string (H); debug_out (T...); } #ifdef LOCAL #define dbg(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define dbg(...) {} #endif template<typename T, typename S> inline bool upmin (T &a, const S &b) { return a > b ? a = b, 1 : 0; } template<typename T, typename S> inline bool upmax (T &a, const S &b) { return a < b ? a = b, 1 : 0; } ull twop (ll x) { return 1ULL<<x; } ll MOD (ll a, ll m) { a %= m; if (a < 0)a += m; return a; } ll inverse (ll a, ll m) { a = MOD (a, m); if (a <= 1)return a; return MOD ((1 - inverse (m, a) * m) / a, m); } template<typename T> T sqr (T x) { return x * x; } ll gcd (ll a, ll b) { a = abs (a), b = abs (b); while (b != 0) { a %= b; swap (a, b); } return a; } ll fast (ll a, ll b, ll mod) { if (b < 0)a = inverse (a, mod), b = -b; ll ans = 1; while (b) { if (b & 1)ans = ans * a % mod; a = a * a % mod; b /= 2; } return ans % mod; } namespace SOLVE { pair<ll, VLL> ans; ll a[1010], b[1010], n, k; ll dp[100][100]; void gao (VLL v) { mst(dp, 0xcf); dp[0][0] = 0; REP(i, 1, n + 1) { int id = v[i - 1]; dp[i][0] = 0; if (i == n) { dp[i][k] = dp[i - 1][k - 1] + a[id] + b[id] * (k - 1); } else { REP(j, 1, k + 1) { dp[i][j] = max (dp[i - 1][j] + b[id] * j, dp[i - 1][j - 1] + a[id] + b[id] * (j - 1)); } } } dbg(dp[1][0], v); upmax (ans, MP (dp[n][k], v)); } void main () { cin>>n>>k; REP(i, 1, n + 1)cin>>a[i]>>b[i]; ans.fi = LLONG_MIN; REP(i, 1, n + 1) { VLL v; REP(j, 1, n + 1) { if (j != i) { v.PB (j); } } sort (all(v), [] (const ll &x, const ll &y) { return b[x] < b[y]; }); v.PB (i); gao (v); } gao (ans.se); int i = n, j = k; VLL res; while (j > 0) { int id = ans.se[i - 1]; if (dp[i][j] == dp[i - 1][j] + b[id] * j){ res.PB(id); res.PB(-id); i--; }else{ res.PB(id); i--; j--; } } cout<<sz(res)<<endl; reverse(all(res)); for(auto i:res)cout<<i<<" "; cout<<endl; } } signed main () { #ifdef LOCAL fr("/Users/zhangqingchuan/Desktop/cp/cp/input.txt"); fw("/Users/zhangqingchuan/Desktop/cp/cp/output.txt"); #endif int t = 1; cin>>t; for (int i = 1; i <= t; i++) { // cout<<"Case #"<<i<<": "; SOLVE::main (); } // clock_t st = clock(); // while(clock() - st < 3.0 * CLOCKS_PER_SEC){ // // } 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; } } } memset(vis, 0, sizeof(vis)); 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; 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) { cout << 1000000000000 + k << '\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; bool cmp(array<int, 3> a, array<int, 3> b) { if (a[1] != b[1]) return a[1] < b[1]; return a[0] < b[0]; } int main() { ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0); int q; cin >> q; for (; q > 0; q--) { int n, k; cin >> n >> k; vector<array<int, 3> > g(n); for (int i = 0; i < n; i++) { cin >> g[i][0] >> g[i][1]; g[i][2] = i; } sort(g.begin(), g.end(), cmp); pair<long long, vector<int> > f[n + 1][k + 1]; for (int i = 0; i <= n; i++) for (int j = 0; j <= k; j++) f[i][j].first = -1; vector<int> nl; nl.clear(); f[0][0] = {0, nl}; nl.push_back(g[0][2] + 1); f[0][1] = {g[0][0], nl}; for (int i = 0; i < n - 1; i++) for (int j = 0; j < k; j++) { if (f[i][j].first == -1) continue; long long sum = f[i][j].first; vector<int> gr = f[i][j].second; if (sum + g[i + 1][1] * (k - 1) > f[i + 1][j].first) f[i + 1][j] = {sum + g[i + 1][1] * (k - 1), gr}; sum += g[i + 1][0] + j * g[i + 1][1]; gr.push_back(g[i + 1][2] + 1); if (sum > f[i + 1][j + 1].first) f[i + 1][j + 1] = {sum, gr}; } vector<int> event = f[n - 1][k].second; int last = event.back(); bool mk[n + 1]; memset(mk, 0, sizeof(mk)); for (auto it : event) mk[it] = 1; event.pop_back(); for (int i = 0; i < n; i++) { int nm = g[i][2] + 1; if (mk[nm]) continue; event.push_back(nm); event.push_back(-nm); } event.push_back(last); cout << int(event.size()) << '\n'; for (auto it : event) cout << it << " "; 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
#define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #pragma comment (linker, "/STACK:526000000") #include "bits/stdc++.h" using namespace std; typedef string::const_iterator State; #define eps 1e-8L #define MAX_MOD 1000000007LL #define GYAKU 500000004LL #define MOD 998244353LL #define pb push_back #define mp make_pair typedef long long ll; typedef long double ld; #define REP(a,b) for(long long (a) = 0;(a) < (b);++(a)) #define ALL(x) (x).begin(),(x).end() // geometry library typedef complex<long double> Point; typedef pair<complex<long double>, complex<long double>> Line; typedef struct Circle { complex<long double> center; long double r; }Circle; long double dot(Point a, Point b) { return (a.real() * b.real() + a.imag() * b.imag()); } long double cross(Point a, Point b) { return (a.real() * b.imag() - a.imag() * b.real()); } long double Dist_Line_Point(Line a, Point b) { if (dot(a.second - a.first, b - a.first) < eps) return abs(b - a.first); if (dot(a.first - a.second, b - a.second) < eps) return abs(b - a.second); return abs(cross(a.second - a.first, b - a.first)) / abs(a.second - a.first); } int is_intersected_ls(Line a, Line b) { return (cross(a.second - a.first, b.first - a.first) * cross(a.second - a.first, b.second - a.first) < eps) && (cross(b.second - b.first, a.first - b.first) * cross(b.second - b.first, a.second - b.first) < eps); } Point intersection_l(Line a, Line b) { Point da = a.second - a.first; Point db = b.second - b.first; return a.first + da * cross(db, b.first - a.first) / cross(db, da); } long double Dist_Line_Line(Line a, Line b) { if (is_intersected_ls(a, b) == 1) { return 0; } return min({ Dist_Line_Point(a,b.first), Dist_Line_Point(a,b.second),Dist_Line_Point(b,a.first),Dist_Line_Point(b,a.second) }); } pair<Point, Point> intersection_Circle_Circle(Circle a, Circle b) { long double dist = abs(a.center - b.center); assert(dist <= eps + a.r + b.r); assert(dist + eps >= abs(a.r - b.r)); Point target = b.center - a.center; long double pointer = target.real() * target.real() + target.imag() * target.imag(); long double aa = pointer + a.r * a.r - b.r * b.r; aa /= 2.0L; Point l{ (aa * target.real() + target.imag() * sqrt(pointer * a.r * a.r - aa * aa)) / pointer, (aa * target.imag() - target.real() * sqrt(pointer * a.r * a.r - aa * aa)) / pointer }; Point r{ (aa * target.real() - target.imag() * sqrt(pointer * a.r * a.r - aa * aa)) / pointer, (aa * target.imag() + target.real() * sqrt(pointer * a.r * a.r - aa * aa)) / pointer }; r = r + a.center; l = l + a.center; return mp(l, r); } //end of geometry ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } template<typename A> A pows(A val, ll b) { assert(b >= 1); A ans = val; b--; while (b) { if (b % 2) { ans *= val; } val *= val; b /= 2LL; } return ans; } template<typename A> class Compressor { public: bool is_zipped = false; map<A, ll> zipper; map<ll, A> unzipper; queue<A> fetcher; Compressor() { is_zipped = false; zipper.clear(); unzipper.clear(); } void add(A now) { assert(is_zipped == false); zipper[now] = 1; fetcher.push(now); } void exec() { assert(is_zipped == false); int cnt = 0; for (auto i = zipper.begin(); i != zipper.end(); ++i) { i->second = cnt; unzipper[cnt] = i->first; cnt++; } is_zipped = true; } ll fetch() { assert(is_zipped == true); A hoge = fetcher.front(); fetcher.pop(); return zipper[hoge]; } ll zip(A now) { assert(is_zipped == true); assert(zipper.find(now) != zipper.end()); return zipper[now]; } A unzip(ll a) { assert(is_zipped == true); assert(a < unzipper.size()); return unzipper[a]; } ll next(A now) { auto x = zipper.upper_bound(now); if (x == zipper.end()) return zipper.size(); return (ll)((*x).second); } ll back(A now) { auto x = zipper.lower_bound(now); if (x == zipper.begin()) return -1; x--; return (ll)((*x).second); } }; template<typename A> class Matrix { public: vector<vector<A>> data; Matrix(vector<vector<A>> a) :data(a) { } Matrix operator + (const Matrix obj) { vector<vector<A>> ans; assert(obj.data.size() == this->data.size()); assert(obj.data[0].size() == this->data[0].size()); REP(i, obj.data.size()) { ans.push_back(vector<A>()); REP(q, obj.data[i].size()) { A hoge = obj.data[i][q] + (this->data[i][q]); ans.back().push_back(hoge); } } return Matrix(ans); } Matrix operator - (const Matrix obj) { vector<vector<A>> ans; assert(obj.data.size() == this->data.size()); assert(obj.data[0].size() == this->data[0].size()); REP(i, obj.data.size()) { ans.push_back(vector<A>()); REP(q, obj.data[i].size()) { A hoge = this->data[i][q] - obj.data[i][q]; ans.back().push_back(hoge); } } return Matrix(ans); } Matrix operator * (const Matrix obj) { vector<vector<A>> ans; assert(obj.data.size() == this->data[0].size()); REP(i, this -> data.size()) { ans.push_back(vector<A>()); REP(q, obj.data[0].size()) { A hoge = ((this->data[i][0]) * (obj.data[0][q])); for (int t = 1; t < obj.data.size(); ++t) { hoge += ((this->data[i][t]) * obj.data[t][q]); } ans.back().push_back(hoge); } } return Matrix(ans); } Matrix& operator *= (const Matrix obj) { *this = (*this * obj); return *this; } Matrix& operator += (const Matrix obj) { *this = (*this + obj); return *this; } Matrix& operator -= (const Matrix obj) { *this = (*this - obj); return *this; } }; struct Fraction { ll a; ll b; Fraction() :a(0LL), b(1LL) { } Fraction(ll c, ll d) { int hoge = gcd(llabs(c), llabs(d)); c /= hoge; d /= hoge; if (d < 0) { d *= -1; c *= -1; } a = c; b = d; } bool operator <(Fraction rhs) const { return a * rhs.b < rhs.a * b; } }; template <std::uint_fast64_t mod> class modint { public: using u64 = std::uint_fast64_t; u64 value = 0; modint() :value(0LL) { } modint(ll a) : value(((a% mod) + 2 * mod) % mod) { } constexpr modint operator+(const modint rhs) const { return modint(*this) += rhs; } constexpr modint operator-(const modint rhs) const { return modint(*this) -= rhs; } constexpr modint operator*(const modint rhs) const { return modint(*this) *= rhs; } constexpr modint operator/(const modint rhs) const { return modint(*this) /= rhs; } constexpr modint& operator+=(const modint rhs) { value += rhs.value; if (value >= mod) { value -= mod; } return *this; } constexpr modint& operator-=(const modint rhs) { if (value < rhs.value) { value += mod; } value -= rhs.value; return *this; } constexpr modint& operator*=(const modint rhs) { value = (value * rhs.value) % mod; return *this; } constexpr modint& operator/=(modint rhs) { ll rem = mod - 2; while (rem) { if (rem % 2) { *this *= rhs; } rhs *= rhs; rem /= 2LL; } return *this; } bool operator <(modint rhs) const { return value < rhs.value; } friend ostream& operator<<(ostream& os, modint& p) { os << p.value; return (os); } }; class Dice { public: vector<ll> vertexs; //Up: 0,Left: 1,Center: 2,Right: 3,Adj: 4, Down: 5 Dice(vector<ll> init) :vertexs(init) { } //Look from Center void RtoL() { for (int q = 1; q < 4; ++q) { swap(vertexs[q], vertexs[q + 1]); } } void LtoR() { for (int q = 3; q >= 1; --q) { swap(vertexs[q], vertexs[q + 1]); } } void UtoD() { swap(vertexs[5], vertexs[4]); swap(vertexs[2], vertexs[5]); swap(vertexs[0], vertexs[2]); } void DtoU() { swap(vertexs[0], vertexs[2]); swap(vertexs[2], vertexs[5]); swap(vertexs[5], vertexs[4]); } bool ReachAble(Dice now) { set<Dice> hoge; queue<Dice> next; next.push(now); hoge.insert(now); while (next.empty() == false) { Dice seeing = next.front(); next.pop(); if (seeing == *this) return true; seeing.RtoL(); if (hoge.count(seeing) == 0) { hoge.insert(seeing); next.push(seeing); } seeing.LtoR(); seeing.LtoR(); if (hoge.count(seeing) == 0) { hoge.insert(seeing); next.push(seeing); } seeing.RtoL(); seeing.UtoD(); if (hoge.count(seeing) == 0) { hoge.insert(seeing); next.push(seeing); } seeing.DtoU(); seeing.DtoU(); if (hoge.count(seeing) == 0) { hoge.insert(seeing); next.push(seeing); } } return false; } bool operator ==(const Dice& a) { for (int q = 0; q < 6; ++q) { if (a.vertexs[q] != (*this).vertexs[q]) { return false; } } return true; } bool operator <(const Dice& a) const { return (*this).vertexs < a.vertexs; } }; pair<Dice, Dice> TwoDimDice(int center, int up) { int target = 1; while (true) { if (center != target && 7 - center != target && up != target && 7 - up != target) { break; } target++; } return mp(Dice(vector<ll>{up, target, center, 7 - target, 7 - center, 7 - up}), Dice(vector<ll>{up, 7 - target, center, target, 7 - center, 7 - up})); } tuple<Dice, Dice, Dice, Dice> OneDimDice(int center) { int bo = min(center, 7 - center); pair<int, int> goa; if (bo == 1) { goa = mp(2, 3); } else if (bo == 2) { goa = mp(1, 3); } else if (bo == 3) { goa = mp(1, 2); } tuple<Dice, Dice, Dice, Dice> now = make_tuple(Dice(vector<ll>{goa.first, goa.second, center, 7 - goa.second, 7 - center, 7 - goa.first}), Dice(vector<ll>{goa.first, 7 - goa.second, center, goa.second, 7 - center, 7 - goa.first}), Dice(vector<ll>{7 - goa.first, goa.second, center, 7 - goa.second, 7 - center, goa.first}), Dice(vector<ll>{7 - goa.first, 7 - goa.second, center, goa.second, 7 - center, goa.first})); return now; } template<typename A, typename B> class Dijkstra { public: vector<vector<pair<int, A>>> vertexs; B Cost_Function; Dijkstra(int n, B cost) : Cost_Function(cost) { vertexs = vector<vector<pair<int, A>>>(n, vector<pair<int, A>>{}); } ~Dijkstra() { vertexs.clear(); } void add_edge(int a, int b, A c) { vertexs[a].push_back(mp(b, c)); } vector<ll> build_result(int StartPoint) { vector<ll> dist(vertexs.size(), 2e18); dist[StartPoint] = 0; priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> next; next.push(make_pair(0, StartPoint)); while (next.empty() == false) { pair<ll, int> now = next.top(); next.pop(); if (dist[now.second] != now.first) continue; for (auto x : vertexs[now.second]) { ll now_cost = now.first + Cost_Function(x.second); if (dist[x.first] > now_cost) { dist[x.first] = now_cost; next.push(mp(now_cost, x.first)); } } } return dist; } }; class Dinic { public: struct edge { int to; int cap; int rev; }; vector<vector<edge>> Graph; vector<int> level; vector<int> itr; Dinic(int n) { Graph = vector<vector<edge>>(n, vector<edge>()); } void add_edge(int a, int b, int cap) { Graph[a].push_back(edge{ b, cap ,(int)Graph[b].size() }); Graph[b].push_back(edge{ a,0,(int)Graph[a].size() - 1 }); } void bfs(int s) { level = vector<int>(Graph.size(), -1); level[s] = 0; queue<int> next; next.push(s); while (next.empty() == false) { int now = next.front(); next.pop(); for (auto x : Graph[now]) { if (x.cap == 0) continue; if (level[x.to] == -1) { level[x.to] = level[now] + 1; next.push(x.to); } } } } int dfs(int now, int goal, int val) { if (goal == now) return val; for (int& i = itr[now]; i < (int)Graph[now].size(); ++i) { edge& target = Graph[now][i]; if (target.cap > 0 && level[now] < level[target.to]) { int d = dfs(target.to, goal, min(val, target.cap)); if (d > 0) { target.cap -= d; Graph[target.to][target.rev].cap += d; return d; } } } return 0; } int run(int s, int t) { int ans = 0; int f = 0; while (bfs(s), level[t] >= 0) { itr = vector<int>(Graph.size(), 0); while ((f = dfs(s, t, 1e9)) > 0) { ans += f; } } return ans; } }; class HLDecomposition { public: vector<vector<int>> vertexs; vector<int> depth; vector<int> backs; vector<int> connections; vector<int> zip, unzip; HLDecomposition(int n) { vertexs = vector<vector<int>>(n, vector<int>()); depth = vector<int>(n); zip = vector<int>(n); unzip = zip; } void add_edge(int a, int b) { vertexs[a].push_back(b); vertexs[b].push_back(a); } int depth_dfs(int now, int back) { depth[now] = 0; for (auto x : vertexs[now]) { if (x == back) continue; depth[now] = max(depth[now], 1 + depth_dfs(x, now)); } return depth[now]; } void dfs(int now, int backing) { zip[now] = backs.size(); unzip[backs.size()] = now; backs.push_back(backing); int now_max = -1; int itr = -1; for (auto x : vertexs[now]) { if (depth[x] > depth[now]) continue; if (now_max < depth[x]) { now_max = depth[x]; itr = x; } } if (itr == -1) return; connections.push_back(connections.back()); dfs(itr, backing); for (auto x : vertexs[now]) { if (depth[x] > depth[now]) continue; if (x == itr) continue; connections.push_back(zip[now]); dfs(x, backs.size()); } return; } void build() { depth_dfs(0, -1); connections.push_back(-1); dfs(0, -1); } vector<pair<int, int>> query(int a, int b) { a = zip[a]; b = zip[b]; vector<pair<int, int>> ans; while (backs[a] != backs[b]) { if (a < b) swap(a, b); ans.push_back(mp(backs[a], a + 1)); a = connections[a]; } if (a > b) swap(a, b); ans.push_back(mp(a, b + 1)); return ans; } int lca(int a, int b) { a = zip[a]; b = zip[b]; while (backs[a] != backs[b]) { if (a < b) swap(a, b); a = connections[a]; } return unzip[min(a, b)]; } }; //by ei1333 //https://ei1333.github.io/luzhiled/snippets/structure/segment-tree.html template< typename Monoid > struct SegmentTree { using F = function< Monoid(Monoid, Monoid) >; int sz; vector< Monoid > seg; const F f; const Monoid M1; SegmentTree(int n, const F f, const Monoid& M1) : f(f), M1(M1) { sz = 1; while (sz < n) sz <<= 1; seg.assign(2 * sz + 1, M1); } void set(int k, const Monoid& x) { seg[k + sz] = x; } void build() { for (int k = sz - 1; k > 0; k--) { seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]); } } void update(int k, const Monoid& x) { k += sz; seg[k] = x; while (k >>= 1) { seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]); } } Monoid query(int a, int b) { Monoid L = M1, R = M1; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) L = f(L, seg[a++]); if (b & 1) R = f(seg[--b], R); } return f(L, R); } Monoid operator[](const int& k) const { return seg[k + sz]; } template< typename C > int find_subtree(int a, const C& check, Monoid& M, bool type) { while (a < sz) { Monoid nxt = type ? f(seg[2 * a + type], M) : f(M, seg[2 * a + type]); if (check(nxt)) a = 2 * a + type; else M = nxt, a = 2 * a + 1 - type; } return a - sz; } template< typename C > int find_first(int a, const C& check) { Monoid L = M1; if (a <= 0) { if (check(f(L, seg[1]))) return find_subtree(1, check, L, false); return -1; } int b = sz; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) { Monoid nxt = f(L, seg[a]); if (check(nxt)) return find_subtree(a, check, L, false); L = nxt; ++a; } } return -1; } template< typename C > int find_last(int b, const C& check) { Monoid R = M1; if (b >= sz) { if (check(f(seg[1], R))) return find_subtree(1, check, R, true); return -1; } int a = sz; for (b += sz; a < b; a >>= 1, b >>= 1) { if (b & 1) { Monoid nxt = f(seg[--b], R); if (check(nxt)) return find_subtree(b, check, R, true); R = nxt; } } return -1; } }; template< typename Monoid, typename OperatorMonoid = Monoid > struct LazySegmentTree { using F = function< Monoid(Monoid, Monoid) >; using G = function< Monoid(Monoid, OperatorMonoid) >; using H = function< OperatorMonoid(OperatorMonoid, OperatorMonoid) >; int sz, height; vector< Monoid > data; vector< OperatorMonoid > lazy; const F f; const G g; const H h; const Monoid M1; const OperatorMonoid OM0; LazySegmentTree(int n, const F f, const G g, const H h, const Monoid& M1, const OperatorMonoid OM0) : f(f), g(g), h(h), M1(M1), OM0(OM0) { sz = 1; height = 0; while (sz < n) sz <<= 1, height++; data.assign(2 * sz, M1); lazy.assign(2 * sz, OM0); } void set(int k, const Monoid& x) { data[k + sz] = x; } void build() { for (int k = sz - 1; k > 0; k--) { data[k] = f(data[2 * k + 0], data[2 * k + 1]); } } inline void propagate(int k) { if (lazy[k] != OM0) { lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]); lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]); data[k] = reflect(k); lazy[k] = OM0; } } inline Monoid reflect(int k) { return lazy[k] == OM0 ? data[k] : g(data[k], lazy[k]); } inline void recalc(int k) { while (k >>= 1) data[k] = f(reflect(2 * k + 0), reflect(2 * k + 1)); } inline void thrust(int k) { for (int i = height; i > 0; i--) propagate(k >> i); } void update(int a, int b, const OperatorMonoid& x) { thrust(a += sz); thrust(b += sz - 1); for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) { if (l & 1) lazy[l] = h(lazy[l], x), ++l; if (r & 1) --r, lazy[r] = h(lazy[r], x); } recalc(a); recalc(b); } Monoid query(int a, int b) { thrust(a += sz); thrust(b += sz - 1); Monoid L = M1, R = M1; for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) { if (l & 1) L = f(L, reflect(l++)); if (r & 1) R = f(reflect(--r), R); } return f(L, R); } Monoid operator[](const int& k) { return query(k, k + 1); } template< typename C > int find_subtree(int a, const C& check, Monoid& M, bool type) { while (a < sz) { propagate(a); Monoid nxt = type ? f(reflect(2 * a + type), M) : f(M, reflect(2 * a + type)); if (check(nxt)) a = 2 * a + type; else M = nxt, a = 2 * a + 1 - type; } return a - sz; } template< typename C > int find_first(int a, const C& check) { Monoid L = M1; if (a <= 0) { if (check(f(L, reflect(1)))) return find_subtree(1, check, L, false); return -1; } thrust(a + sz); int b = sz; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) { Monoid nxt = f(L, reflect(a)); if (check(nxt)) return find_subtree(a, check, L, false); L = nxt; ++a; } } return -1; } template< typename C > int find_last(int b, const C& check) { Monoid R = M1; if (b >= sz) { if (check(f(reflect(1), R))) return find_subtree(1, check, R, true); return -1; } thrust(b + sz - 1); int a = sz; for (b += sz; a < b; a >>= 1, b >>= 1) { if (b & 1) { Monoid nxt = f(reflect(--b), R); if (check(nxt)) return find_subtree(b, check, R, true); R = nxt; } } return -1; } }; class KMP { public: vector<ll> table; vector<ll> Pattern; KMP(vector<ll> a) { build(a); } void build(vector<ll> a) { Pattern = a; table = vector<ll>(a.size() + 1, -1); int j = -1; for (int i = 0; i < a.size(); ++i) { while (j >= 0 && Pattern[i] != Pattern[j]) { j = table[j]; } table[i + 1] = ++j; } return; } vector<ll> search(vector<ll> a) { vector<ll> ans; for (int i = 0, k = 0; i < a.size(); ++i) { while (k >= 0 && a[i] != Pattern[k]) k = table[k]; ++k; if (k >= Pattern.size()) { ans.push_back(i - Pattern.size() + 1); k = table[k]; } } return ans; } }; unsigned long xor128() { static unsigned long x = 123456789, y = 362436069, z = 521288629, w = 88675123; unsigned long t = (x ^ (x << 11)); x = y; y = z; z = w; return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8))); } void init() { iostream::sync_with_stdio(false); cout << fixed << setprecision(20); } #define int ll void solve(){ int n, k; cin >> n >> k; vector<tuple<int, int,int>> inputs; REP(i, n) { int a, b; cin >> a >> b; inputs.push_back(make_tuple(b, a, i + 1)); } sort(ALL(inputs)); int ans = 0; vector<int> AnsStep; REP(i, n) { vector<tuple<int, int, int>> evals; REP(q, n) { if (i == q) continue; evals.push_back(inputs[q]); } vector<int> tmps; int cost = get<1>(inputs[i]); int gos[80] = {}; vector<pair<int, int>> nexts; int hoge = get<0>(inputs[i]); for (int q = (int)evals.size() - 1; q >= 0; --q) { nexts.push_back(mp(hoge + get<1>(evals[q]), get<2>(evals[q]))); hoge += get<0>(evals[q]); } sort(ALL(nexts)); reverse(ALL(nexts)); REP(q, k-1) { cost += nexts[q].first; gos[nexts[q].second] = 1; } REP(q, evals.size()) { tmps.push_back(get<2>(evals[q])); if (gos[get<2>(evals[q])] == 0) { tmps.push_back(-get<2>(evals[q])); } } tmps.push_back(get<2>(inputs[i])); if (cost > ans) { ans = cost; AnsStep = tmps; } } cout << AnsStep.size() << endl; REP(i, AnsStep.size()) { cout << AnsStep[i] << " "; } cout << endl; } #undef int int main() { init(); int t; cin >> t; REP(tea,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 namespace std; const int maxn = 1048576; const int mod = 998244353; const double pi = acos(-1); int dp[77][77], from[77][77]; bool update(int &x, int v) { if (x < v) { x = v; return 1; } return 0; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> dp[0][0]; int n, k; while (cin >> n >> k) { vector<pair<pair<int, int>, int>> pp; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; pp.push_back({{y, x}, i + 1}); } sort(pp.begin(), pp.end()); memset(dp, -1, sizeof(dp)); for (int i = 0; i < 76; i++) { for (int j = 0; j < 76; j++) { from[i][j] = 1; } } dp[0][0] = 0; for (int i = 0; i < (int)pp.size(); i++) { for (int j = 0; j <= min(k, i); j++) { if (j < k) { if (update(dp[j + 1][i - j], dp[j][i - j] + pp[i].first.second + pp[i].first.first * j)) { from[j + 1][i - j] = 0; } } if (update(dp[j][i - j + 1], dp[j][i - j] + pp[i].first.first * (k - 1))) { from[j][i - j + 1] = 1; } } } int now = k; vector<int> res[2]; for (int i = (int)pp.size(); i > 0; i--) { cout << now << ' ' << i << endl; res[from[now][i - now]].push_back(pp[i - 1].second); if (!from[now][i - now]) { now--; } } reverse(res[0].begin(), res[0].end()); vector<int> ans; for (int i = 0; i < (int)res[0].size() - 1; i++) { ans.push_back(res[0][i]); } for (auto &i : res[1]) { ans.push_back(i); ans.push_back(-i); } ans.push_back(res[0].back()); cout << (int)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; 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[cnt]]); 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 long long N = 1111; long long te, n, k, x, y; struct item { long long a, b, p; } a[N], b[N]; bool cmp(item a, item b) { return a.a < b.a; } bool cmp1(item a, item b) { return a.b < b.b; } long long f() { long long ans = 0, mx = 0; for (long long i = 1; i <= n; i++) b[i] = a[i]; for (long long i = 2; i < k; i++) if (cmp1(b[i], b[i - 1])) swap(b[i], b[i - 1]); for (long long i = k - 1; i >= 2; i--) if (cmp1(b[i], b[i - 1])) swap(b[i], b[i - 1]); for (long long i = 1; i < k; i++) ans += b[i].a + b[i].b * (i - 1); for (long long i = k; i <= n; i++) ans += b[i].b * (k - 1), mx = max(mx, b[i].a); return ans + mx; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> te; srand(time(0)); for (; te--;) { cin >> n >> k; for (long long i = 1; i <= n; i++) cin >> a[i].a >> a[i].b, a[i].p = i; if (k == 1) { cout << 1 << endl << a[n].p << endl; continue; } sort(a + 1, a + k, &cmp1); for (long long ch = 1; ch;) { ch = 0; x = f(); for (long long i = 1; i < k; i++) for (long long j = k; j <= n; j++) { swap(a[i], a[j]); y = f(); if (x < y) { ch = 1; x = y; for (long long ii = 2; ii < k; ii++) if (cmp1(a[ii], a[ii - 1])) swap(a[ii], a[ii - 1]); for (long long ii = k - 1; ii >= 2; ii--) if (cmp1(a[ii], a[ii - 1])) swap(a[ii], a[ii - 1]); break; } swap(a[i], a[j]); } } cout << k + (n - k) * 2 << endl; sort(a + k, a + n + 1, &cmp); for (long long i = 1; i < k; i++) cout << a[i].p << " "; for (long long i = k; i < n; i++) cout << a[i].p << " " << -a[i].p << " "; cout << a[n].p << 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
python3
from operator import itemgetter import sys p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def main(): inf = 10 ** 9 for _ in range(II()): n, k = MI() ab = [(a, b, i) for i, (a, b) in enumerate(LLI(n))] dp = [[inf] * n for _ in range(k)] log = [[-1 for _ in range(n)] for _ in range(k)] for e in range(n): dp[0][e] = -ab[e][0] ab.sort(key=itemgetter(1)) for a, b, i in ab: for j in range(k - 2, -1, -1): for e in range(n): if i==e:continue pre = dp[j][e] cur = pre + b * (k - 1 - j) - a if cur < dp[j + 1][e]: dp[j + 1][e] = cur log[j+1][e]=i #print(ab) #p2D(dp) #p2D(log) #print() mn=mne=inf for e in range(n): cur=dp[-1][e] if cur<mn: mn=cur mne=e use=[False]*n use[mne]=True ans=[] for t in range(1,k): i=log[t][mne] ans.append(i+1) use[i]=True for i in range(n): if use[i]:continue ans.append(i+1) ans.append(-i-1) ans.append(mne+1) print(len(ans)) print(*ans) main()
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()) arr=[] for i in range(n): a,b=map(int,input().split()) arr.append([a,b,i+1]) arr.sort(key=lambda x:x[1]) if(k==1): arr.sort(key=lambda x:x[0],reverse=True) print(1) print(arr[0][2]) continue print(k+(n-k)*2) for i in range(k-1): print(arr[i][2],end=" ") for i in range(k-1,n-1): print(arr[i][2],-arr[i][2],end=" ") print(arr[n-1][2])
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; struct Edge { int to, val, next, cost; } edge[25086]; int s, t; int cnt = -1; int head[25086]; queue<int> q; int d[25086]; int cur[25086]; int inq[25086]; int vis[25086]; int costsum; void addEdge(int u, int v, int w, int f) { cnt++; edge[cnt].to = v; edge[cnt].val = w; edge[cnt].next = head[u]; edge[cnt].cost = f; head[u] = cnt; cnt++; edge[cnt].to = u; edge[cnt].val = 0; edge[cnt].next = head[v]; edge[cnt].cost = -f; head[v] = cnt; } bool bfs() { memset(d, 0x3f, sizeof(d)); memset(vis, 0, sizeof(vis)); d[s] = 0; q.push(s); inq[s] = 1; while (!q.empty()) { int u = q.front(); q.pop(); inq[u] = 0; for (int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to; cur[v] = head[v]; int f = edge[i].cost; if (d[v] > d[u] + f && edge[i].val > 0) { d[v] = d[u] + f; if (!inq[v]) { q.push(v); inq[v] = 1; } } } } return d[t] != inf; } int dfs(int u, int flow) { vis[u] = 1; int nowflow = 0; if (u == t) return flow; for (int i = cur[u]; i != -1; i = edge[i].next) { cur[u] = i; int v = edge[i].to; int w = edge[i].val; int f = edge[i].cost; if (d[v] == d[u] + f && w > 0 && !vis[v]) { int k = dfs(v, min(flow - nowflow, w)); if (k > 0) { edge[i].val -= k; edge[i ^ 1].val += k; nowflow += k; costsum += f * k; if (nowflow == flow) break; } } } vis[u] = 0; if (!nowflow) d[u] = inf; return nowflow; } int dinic() { int ans = 0; while (bfs()) { ans += dfs(s, inf); } return ans; } int T; int n, k, a, b; int opt[25086]; int main() { scanf("%d", &T); while (T--) { memset(head, -1, sizeof(head)); scanf("%d%d", &n, &k); s = 0, t = 2 * n + 1, costsum = 0; for (int i = 1; i <= n; i++) { scanf("%d%d", &a, &b); addEdge(s, i, 1, 0), addEdge(i + n, t, 1, 0); for (int j = 1; j <= n; j++) { if (j < k) addEdge(i, j + n, 1, a + (i - 1) + b); else if (j < n) addEdge(i, j + n, 1, (k - 1) * b); else addEdge(i, j + n, 1, a + (k - 1) + b); } } dinic(); for (int i = 1; i <= n; i++) { for (int j = head[i]; j != 1; j = edge[j].next) { if (!edge[j].val) { opt[edge[j].to - n] = i; break; } } } printf("%d\n", k - 1 + (n - k) * 2 + 1); for (int i = 1; i <= n; i++) { if (i < k || i == n) printf("%d ", opt[i]); else printf("%d %d ", opt[i], -opt[i]); } 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 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); 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 pos = 0, inK = 0, outK = k; for (; 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; }
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--) { memset(f, 0, sizeof(f)); memset(g, 0, sizeof(g)); 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; } } } memset(vis, 0, sizeof(vis)); 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
python3
T = int(input().strip()) for t in range(T): n, k = list(map(int, input().split())) s = [] for i in range(n): a, b = list(map(int, input().split())) s.append(((a,b, i+1))) s = sorted(s, key=lambda x:x[0]) army = sorted(s[-k:], key=lambda x:x[1]) res = [] for i in range(k - 1): res.append(army[i][2]) for i in range(n-k): res.append(s[i][2]) res.append(-s[i][2]) res.append(army[k-1][2]) print(len(res)) print(*res)
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 custom_hash { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; const int N = 1e2 + 1; const int mod = 998244353; long long int a[N], b[N], c[N]; class HungarianAlgorithm { public: HungarianAlgorithm(); ~HungarianAlgorithm(); double Solve(vector<vector<double> > &DistMatrix, vector<int> &Assignment); private: void assignmentoptimal(int *assignment, double *cost, double *distMatrix, int nOfRows, int nOfColumns); void buildassignmentvector(int *assignment, bool *starMatrix, int nOfRows, int nOfColumns); void computeassignmentcost(int *assignment, double *cost, double *distMatrix, int nOfRows); void step2a(int *assignment, double *distMatrix, bool *starMatrix, bool *newStarMatrix, bool *primeMatrix, bool *coveredColumns, bool *coveredRows, int nOfRows, int nOfColumns, int minDim); void step2b(int *assignment, double *distMatrix, bool *starMatrix, bool *newStarMatrix, bool *primeMatrix, bool *coveredColumns, bool *coveredRows, int nOfRows, int nOfColumns, int minDim); void step3(int *assignment, double *distMatrix, bool *starMatrix, bool *newStarMatrix, bool *primeMatrix, bool *coveredColumns, bool *coveredRows, int nOfRows, int nOfColumns, int minDim); void step4(int *assignment, double *distMatrix, bool *starMatrix, bool *newStarMatrix, bool *primeMatrix, bool *coveredColumns, bool *coveredRows, int nOfRows, int nOfColumns, int minDim, int row, int col); void step5(int *assignment, double *distMatrix, bool *starMatrix, bool *newStarMatrix, bool *primeMatrix, bool *coveredColumns, bool *coveredRows, int nOfRows, int nOfColumns, int minDim); }; HungarianAlgorithm::HungarianAlgorithm() {} HungarianAlgorithm::~HungarianAlgorithm() {} double HungarianAlgorithm::Solve(vector<vector<double> > &DistMatrix, vector<int> &Assignment) { unsigned int nRows = DistMatrix.size(); unsigned int nCols = DistMatrix[0].size(); double *distMatrixIn = new double[nRows * nCols]; int *assignment = new int[nRows]; double cost = 0.0; for (unsigned int i = 0; i < nRows; i++) for (unsigned int j = 0; j < nCols; j++) distMatrixIn[i + nRows * j] = DistMatrix[i][j]; assignmentoptimal(assignment, &cost, distMatrixIn, nRows, nCols); Assignment.clear(); for (unsigned int r = 0; r < nRows; r++) Assignment.push_back(assignment[r]); delete[] distMatrixIn; delete[] assignment; return cost; } void HungarianAlgorithm::assignmentoptimal(int *assignment, double *cost, double *distMatrixIn, int nOfRows, int nOfColumns) { double *distMatrix, *distMatrixTemp, *distMatrixEnd, *columnEnd, value, minValue; bool *coveredColumns, *coveredRows, *starMatrix, *newStarMatrix, *primeMatrix; int nOfElements, minDim, row, col; *cost = 0; for (row = 0; row < nOfRows; row++) assignment[row] = -1; nOfElements = nOfRows * nOfColumns; distMatrix = (double *)malloc(nOfElements * sizeof(double)); distMatrixEnd = distMatrix + nOfElements; for (row = 0; row < nOfElements; row++) { value = distMatrixIn[row]; if (value < 0) cerr << "All matrix elements have to be non-negative." << endl; distMatrix[row] = value; } coveredColumns = (bool *)calloc(nOfColumns, sizeof(bool)); coveredRows = (bool *)calloc(nOfRows, sizeof(bool)); starMatrix = (bool *)calloc(nOfElements, sizeof(bool)); primeMatrix = (bool *)calloc(nOfElements, sizeof(bool)); newStarMatrix = (bool *)calloc(nOfElements, sizeof(bool)); if (nOfRows <= nOfColumns) { minDim = nOfRows; for (row = 0; row < nOfRows; row++) { distMatrixTemp = distMatrix + row; minValue = *distMatrixTemp; distMatrixTemp += nOfRows; while (distMatrixTemp < distMatrixEnd) { value = *distMatrixTemp; if (value < minValue) minValue = value; distMatrixTemp += nOfRows; } distMatrixTemp = distMatrix + row; while (distMatrixTemp < distMatrixEnd) { *distMatrixTemp -= minValue; distMatrixTemp += nOfRows; } } for (row = 0; row < nOfRows; row++) for (col = 0; col < nOfColumns; col++) if (fabs(distMatrix[row + nOfRows * col]) < DBL_EPSILON) if (!coveredColumns[col]) { starMatrix[row + nOfRows * col] = true; coveredColumns[col] = true; break; } } else { minDim = nOfColumns; for (col = 0; col < nOfColumns; col++) { distMatrixTemp = distMatrix + nOfRows * col; columnEnd = distMatrixTemp + nOfRows; minValue = *distMatrixTemp++; while (distMatrixTemp < columnEnd) { value = *distMatrixTemp++; if (value < minValue) minValue = value; } distMatrixTemp = distMatrix + nOfRows * col; while (distMatrixTemp < columnEnd) *distMatrixTemp++ -= minValue; } for (col = 0; col < nOfColumns; col++) for (row = 0; row < nOfRows; row++) if (fabs(distMatrix[row + nOfRows * col]) < DBL_EPSILON) if (!coveredRows[row]) { starMatrix[row + nOfRows * col] = true; coveredColumns[col] = true; coveredRows[row] = true; break; } for (row = 0; row < nOfRows; row++) coveredRows[row] = false; } step2b(assignment, distMatrix, starMatrix, newStarMatrix, primeMatrix, coveredColumns, coveredRows, nOfRows, nOfColumns, minDim); computeassignmentcost(assignment, cost, distMatrixIn, nOfRows); free(distMatrix); free(coveredColumns); free(coveredRows); free(starMatrix); free(primeMatrix); free(newStarMatrix); return; } void HungarianAlgorithm::buildassignmentvector(int *assignment, bool *starMatrix, int nOfRows, int nOfColumns) { int row, col; for (row = 0; row < nOfRows; row++) for (col = 0; col < nOfColumns; col++) if (starMatrix[row + nOfRows * col]) { assignment[row] = col; break; } } void HungarianAlgorithm::computeassignmentcost(int *assignment, double *cost, double *distMatrix, int nOfRows) { int row, col; for (row = 0; row < nOfRows; row++) { col = assignment[row]; if (col >= 0) *cost += distMatrix[row + nOfRows * col]; } } void HungarianAlgorithm::step2a(int *assignment, double *distMatrix, bool *starMatrix, bool *newStarMatrix, bool *primeMatrix, bool *coveredColumns, bool *coveredRows, int nOfRows, int nOfColumns, int minDim) { bool *starMatrixTemp, *columnEnd; int col; for (col = 0; col < nOfColumns; col++) { starMatrixTemp = starMatrix + nOfRows * col; columnEnd = starMatrixTemp + nOfRows; while (starMatrixTemp < columnEnd) { if (*starMatrixTemp++) { coveredColumns[col] = true; break; } } } step2b(assignment, distMatrix, starMatrix, newStarMatrix, primeMatrix, coveredColumns, coveredRows, nOfRows, nOfColumns, minDim); } void HungarianAlgorithm::step2b(int *assignment, double *distMatrix, bool *starMatrix, bool *newStarMatrix, bool *primeMatrix, bool *coveredColumns, bool *coveredRows, int nOfRows, int nOfColumns, int minDim) { int col, nOfCoveredColumns; nOfCoveredColumns = 0; for (col = 0; col < nOfColumns; col++) if (coveredColumns[col]) nOfCoveredColumns++; if (nOfCoveredColumns == minDim) { buildassignmentvector(assignment, starMatrix, nOfRows, nOfColumns); } else { step3(assignment, distMatrix, starMatrix, newStarMatrix, primeMatrix, coveredColumns, coveredRows, nOfRows, nOfColumns, minDim); } } void HungarianAlgorithm::step3(int *assignment, double *distMatrix, bool *starMatrix, bool *newStarMatrix, bool *primeMatrix, bool *coveredColumns, bool *coveredRows, int nOfRows, int nOfColumns, int minDim) { bool zerosFound; int row, col, starCol; zerosFound = true; while (zerosFound) { zerosFound = false; for (col = 0; col < nOfColumns; col++) if (!coveredColumns[col]) for (row = 0; row < nOfRows; row++) if ((!coveredRows[row]) && (fabs(distMatrix[row + nOfRows * col]) < DBL_EPSILON)) { primeMatrix[row + nOfRows * col] = true; for (starCol = 0; starCol < nOfColumns; starCol++) if (starMatrix[row + nOfRows * starCol]) break; if (starCol == nOfColumns) { step4(assignment, distMatrix, starMatrix, newStarMatrix, primeMatrix, coveredColumns, coveredRows, nOfRows, nOfColumns, minDim, row, col); return; } else { coveredRows[row] = true; coveredColumns[starCol] = false; zerosFound = true; break; } } } step5(assignment, distMatrix, starMatrix, newStarMatrix, primeMatrix, coveredColumns, coveredRows, nOfRows, nOfColumns, minDim); } void HungarianAlgorithm::step4(int *assignment, double *distMatrix, bool *starMatrix, bool *newStarMatrix, bool *primeMatrix, bool *coveredColumns, bool *coveredRows, int nOfRows, int nOfColumns, int minDim, int row, int col) { int n, starRow, starCol, primeRow, primeCol; int nOfElements = nOfRows * nOfColumns; for (n = 0; n < nOfElements; n++) newStarMatrix[n] = starMatrix[n]; newStarMatrix[row + nOfRows * col] = true; starCol = col; for (starRow = 0; starRow < nOfRows; starRow++) if (starMatrix[starRow + nOfRows * starCol]) break; while (starRow < nOfRows) { newStarMatrix[starRow + nOfRows * starCol] = false; primeRow = starRow; for (primeCol = 0; primeCol < nOfColumns; primeCol++) if (primeMatrix[primeRow + nOfRows * primeCol]) break; newStarMatrix[primeRow + nOfRows * primeCol] = true; starCol = primeCol; for (starRow = 0; starRow < nOfRows; starRow++) if (starMatrix[starRow + nOfRows * starCol]) break; } for (n = 0; n < nOfElements; n++) { primeMatrix[n] = false; starMatrix[n] = newStarMatrix[n]; } for (n = 0; n < nOfRows; n++) coveredRows[n] = false; step2a(assignment, distMatrix, starMatrix, newStarMatrix, primeMatrix, coveredColumns, coveredRows, nOfRows, nOfColumns, minDim); } void HungarianAlgorithm::step5(int *assignment, double *distMatrix, bool *starMatrix, bool *newStarMatrix, bool *primeMatrix, bool *coveredColumns, bool *coveredRows, int nOfRows, int nOfColumns, int minDim) { double h, value; int row, col; h = DBL_MAX; for (row = 0; row < nOfRows; row++) if (!coveredRows[row]) for (col = 0; col < nOfColumns; col++) if (!coveredColumns[col]) { value = distMatrix[row + nOfRows * col]; if (value < h) h = value; } for (row = 0; row < nOfRows; row++) if (coveredRows[row]) for (col = 0; col < nOfColumns; col++) distMatrix[row + nOfRows * col] += h; for (col = 0; col < nOfColumns; col++) if (!coveredColumns[col]) for (row = 0; row < nOfRows; row++) distMatrix[row + nOfRows * col] -= h; step3(assignment, distMatrix, starMatrix, newStarMatrix, primeMatrix, coveredColumns, coveredRows, nOfRows, nOfColumns, minDim); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { int n, k; cin >> n >> k; int xyz = -1ll, idx = -1; for (int i = 1; i <= n; ++i) { cin >> a[i] >> b[i]; if (a[i] > xyz) { xyz = a[i]; idx = i; } } vector<vector<double> > costMatrix(n, vector<double>(n)); double mx = 1e6; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { if (j == 1) { costMatrix[i - 1][j - 1] = mx - a[i]; } else if (j <= k) { costMatrix[i - 1][j - 1] = mx - (a[i] + (j - 1) * b[i]); } else { costMatrix[i - 1][j - 1] = mx - (j - 1) * b[i]; } } } HungarianAlgorithm algo; vector<int> v, ans(k - 1); double cost = mx * n - algo.Solve(costMatrix, v); for (int i = 0; i < v.size(); ++i) { if (v[i] + 1 < k) { ans[v[i]] = i + 1; } } for (int i = 0; i < v.size(); ++i) { if (v[i] + 1 > k) { ans.push_back(i + 1); ans.push_back(-i - 1); } } for (int i = 0; i < v.size(); ++i) { if (v[i] + 1 == k) { ans.push_back(i + 1); break; } } cout << ans.size() << endl; for (int x : ans) { cout << x << ' '; } cout << 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
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 < n; ++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
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]) dp = [[-10**9]*(k+1) for _ in range(n+1)] # dp[i][j]: i 番目まで見て j 匹最後まで残す(採用する) dp[0][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[i+1][j+1] < dp[i+1][j] + mini[i][0] + (k - j - 1)*mini[i][1]: dp[i+1][j+1] = dp[i+1][j] + mini[i][0] + (k - j - 1)*mini[i][1] # 自身を採用 f[i+1][j+1] = 1 if dp[i+1][j] < dp[i+1][j] + (k - 1) * mini[i][1]: dp[i+1][j] = dp[i+1][j] + (k - 1) * mini[i][1] # サポート役(消す) cx = k a = list() b = list() # 復元 採用フラグが立っていればa それ以外はb 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
python3
for _ in range(int(input())): n,k=map(int,input().split()) arr=[] for i in range(n): a,b=map(int,input().split()) arr.append([a,b,i+1]) arr.sort(key=lambda x:x[1]) if(k==1): arr.sort(key=lambda x:x[0],reverse=True) print(arr[0][2]) continue for i in range(k-1): print(arr[i][2],end=" ") for i in range(k,n-1): print(arr[i][2],-arr[i][2],end=" ") print(arr[n-1][2])
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); vector<long long> ans; sort(v2.begin(), v2.end(), compare1); for (long long i = 0; i < v1.size() - 1; i++) ans.push_back(p[v1[i]].second); if (v1.size() == 1) ans.push_back(p[v1[0]].second); if (v1.size() > 1 || (v1.size() == 1 && k > 1)) for (long long i = 0; i < v2.size(); i++) ans.push_back(p[v2[i]].second), ans.push_back(-p[v2[i]].second); if (v1.size() > 1) ans.push_back(p[v1.back()].second); cout << ans.size() << '\n'; for (auto it : ans) cout << it << ' '; 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()] l = [] def givetotsum(l1): global l su = 0 for i in l1: if i > 0: su += l[i-1][0] else:su += (k - 1)*l[-i-1][1] return su def givearray(l): ans = [] 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) return ans 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]) ans1 = givearray(l[:]) l.sort(key = lambda x:(x[0] + s - x[1],-x[1]),reverse = 1) l[:k] = sorted(l[:k],key = lambda x:x[1]) ans2 = givearray(l[:]) l.sort(key = lambda x:x[-1]) ans = ans1 if givetotsum(ans1[:]) > givetotsum(ans2[:]) else ans2 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 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]; } struct MCMF { int n, m; vector<Edge> edge; vector<int> G[maxn]; int inq[maxn], d[maxn], p[maxn], a[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) { 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); } 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 (to < 2 * n) continue; if (edge[to].flow == false) continue; if (to < 4 * n) { ans1.push_back(i); } else ans2.push_back(i); } } 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--) { int n, k; read(n); read(k); 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); } for (int i = 1; i <= n; i++) { M.add(i, i + n, 1, -a[i]); } for (int i = 1; i <= n; i++) M.add(i, i + n + n, 1, -1LL * (k - 1) * b[i]); for (int i = n + 1; i <= n + n; i++) { M.add(i, 3 * n + 1, 1, 0); M.add(i + n, 3 * n + 2, 1, 0); } M.add(3 * n + 1, 3 * n + 3, k, 0); M.add(3 * n + 2, 3 * n + 3, n - k, 0); 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 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) { cout << n << "\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) << " "; 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> using namespace std; template <typename T> void read(T &x) { x = 0; int f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); } x *= f; } inline void write(int x) { if (x > 9) write(x / 10); putchar(x % 10 + '0'); } const int N = 80; const long long INF = 1ll << 62; long long a[N], b[N], id[N], n, m; inline bool upd(long long &x, long long v) { if (v <= x) return 0; x = v; return 1; } long long dp[N][N], lst[N][N]; int ans[N], lans; int a2[N], l2; int tp[N]; inline void solve() { int i, j; read(n), read(m); for (i = 1; i <= n; ++i) tp[i] = 0, read(a[i]), read(b[i]), id[i] = i; for (i = 1; i <= n; ++i) for (j = i + 1; j <= n; ++j) if (b[i] < b[j]) swap(a[i], a[j]), swap(b[i], b[j]), swap(id[i], id[j]); for (i = 0; i <= n; ++i) for (j = 0; j <= n; ++j) dp[i][j] = -INF; dp[0][0] = 0; for (i = 1; i <= n; ++i) for (j = 0; j <= m; ++j) { if (dp[i - 1][j] >= 0 && upd(dp[i][j], dp[i - 1][j] + b[i] * (m - 1))) lst[i][j] = 0; if (j && dp[i - 1][j - 1] >= 0 && upd(dp[i][j], dp[i - 1][j - 1] + b[i] * (m - j) + a[i])) lst[i][j] = 1; } lans = 0; int nown = n, nowm = m; while (nown) { if (lst[nown][nowm]) ans[++lans] = nown, --nowm, tp[nown] = 1; --nown; } l2 = 0; for (i = 1; i < lans; ++i) a2[++l2] = id[ans[i]]; for (i = 1; i <= n; ++i) if (!tp[i]) { a2[++l2] = id[i]; a2[++l2] = -id[i]; } a2[++l2] = id[ans[lans]]; cout << l2 << '\n'; for (i = 1; i <= l2; ++i) cout << a2[i] << (i < l2 ? ' ' : '\n'); } int main() { int T; read(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; 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; using ll = long long; using ull = uint64_t; using i32 = int32_t; using u32 = uint32_t; using i64 = int64_t; using u64 = uint64_t; using pii = pair<int, int>; using pll = pair<ll, ll>; using ld = double; namespace io { auto unistd_read = read; auto unistd_write = write; const int BUFSIZE = 1 << 20; int isize, osize; char ibuf[BUFSIZE + 10], obuf[BUFSIZE + 10]; char *is, *it, *os = obuf, *ot = obuf + BUFSIZE; char getchar() { if (is == it) { is = ibuf; it = ibuf + unistd_read(STDIN_FILENO, ibuf, BUFSIZE); if (is == it) return EOF; } return *is++; } char getalpha() { char c = getchar(); while (!isalpha(c)) c = getchar(); return c; } void putchar(char c) { *os++ = c; if (os == ot) { unistd_write(STDOUT_FILENO, obuf, BUFSIZE); os = obuf; } } int inp() { int x = 0, f = 0; char ch; for (ch = getchar(); !isdigit(ch); ch = getchar()) { if (ch == EOF) return -1; if (ch == '-') f = 1; } for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar()) ; return f ? -x : x; } ll inp_ll() { ll x = 0; int f = 0; char ch; for (ch = getchar(); !isdigit(ch); ch = getchar()) if (ch == '-') f = 1; for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar()) ; return f ? -x : x; } template <class T> bool read(T& x) { x = 0; int f = 0; char ch; for (ch = getchar(); !isdigit(ch); ch = getchar()) { if (ch == EOF) return 0; if (ch == '-') f = 1; } for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar()) ; if (f) x = -x; return 1; } bool read(char* s) { char* t = s; char ch = getchar(); for (; ch == ' ' || ch == '\n'; ch = getchar()) ; for (; ch != ' ' && ch != '\n' && ch != EOF; ch = getchar()) *t++ = ch; *t = 0; return s != t; } template <class T, class... Args> bool read(T& x, Args&... args) { return read(x) && read(args...); } template <class T> bool readln(T& x) { x = 0; int f = 0; char ch; for (ch = getchar(); !isdigit(ch); ch = getchar()) { if (ch == EOF) return 0; if (ch == '-') f = 1; } for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar()) ; if (f) x = -x; for (; ch != '\n' && ch != EOF; ch = getchar()) ; return 1; } bool readln(char* s) { char* t = s; while (1) { char ch = getchar(); if (ch == '\n' || ch == EOF) break; *t++ = ch; } *t = 0; return s != t; } template <class T, class... Args> bool readln(T& x, Args&... args) { return read(x) && readln(args...); } template <class T> void write(T x) { static char s[22]; static char* it = s + 20; static char* end = s + 20; if (x < 0) { putchar('-'); x = -x; } do { *--it = x % 10 + '0'; x /= 10; } while (x); for (; it < end; ++it) putchar(*it); } void write(const char* s) { for (; *s; ++s) putchar(*s); } template <> void write(char* s) { write((const char*)s); } template <> void write(char c) { putchar(c); } template <class T, class... Args> void write(T x, Args... args) { write(x); write(args...); } void writeln() { putchar('\n'); } template <class T, class... Args> void writeln(T x, Args... args) { write(x); writeln(args...); } template <class Iterator> bool input(Iterator st, Iterator ed) { for (; st != ed; ++st) { if (!read(*st)) return false; } return true; } template <class T> bool input(T& a) { input(a.begin(), a.end()); } template <class Iterator> void print(Iterator st, Iterator ed, const char* c = " ") { int flag = 0; for (; st != ed; ++st) { if (flag) write(c); flag = 1; write(*st); } writeln(); } template <class T> void print(const T& a, const char* c = " ") { print(a.begin(), a.end(), c); } struct ender { ~ender() { if (os != obuf) unistd_write(STDOUT_FILENO, obuf, os - obuf); } } __ender; } // namespace io int64_t power(int64_t a, int64_t b, int64_t p) { if (!b) return 1; int64_t t = power(a, b >> 1, p); t = t * t % p; if (b & 1) t = t * a % p; return t; } mt19937 rd(chrono::steady_clock::now().time_since_epoch().count()); using namespace io; template <class T> inline void freshmin(T& a, const T& b) { if (a > b) a = b; } template <class T> inline void freshmax(T& a, const T& b) { if (a < b) a = b; } const ll B = 31; const ll MOD = 1000000007; const int INF = 1000000010; const ll INFll = 4000000000000000000LL; const int MAXN = 5010; int dx[] = {-1, 1, 0, 0, -1, -1, 1, 1}; int dy[] = {0, 0, -1, 1, -1, 1, -1, 1}; ld det(ld x1, ld y1, ld x2, ld y2, ld x3, ld y3) { return x1 * y2 - x2 * y1 + x2 * y3 - x3 * y2 + x3 * y1 - x1 * y3; } namespace KM { using T = int; const int MAXN = 110; const T INF = 1000000000; int nx, ny; int times; T v[MAXN][MAXN]; T ex[MAXN], ey[MAXN]; T slack[MAXN]; int ux[MAXN], uy[MAXN], mx[MAXN], my[MAXN], pre[MAXN]; void match(int y) { while (y) { my[y] = pre[y]; swap(y, mx[pre[y]]); } } void bfs(int sx) { for (int y = 1; y <= ny; ++y) slack[y] = INF; queue<int> Q; Q.push(sx); ++times; while (1) { while (!Q.empty()) { int x = Q.front(); Q.pop(); ux[x] = times; T gap; for (int y = 1; y <= ny; ++y) if (uy[y] != times && (gap = ex[x] + ey[y] - v[x][y]) <= slack[y]) { pre[y] = x; if (!gap) { uy[y] = times; if (!my[y]) return match(y); else Q.push(my[y]); } else slack[y] = gap; } } T d = INF; for (int y = 1; y <= ny; ++y) if (uy[y] != times) freshmin(d, slack[y]); for (int x = 1; x <= nx; ++x) if (ux[x] == times) ex[x] -= d; for (int y = 1; y <= ny; ++y) if (uy[y] == times) ey[y] += d; else slack[y] -= d; for (int y = 1; y <= ny; ++y) if (uy[y] != times && !slack[y]) { uy[y] = times; if (!my[y]) return match(y); else Q.push(my[y]); } } } int KM() { for (int y = 1; y <= ny; ++y) { my[y] = 0; ey[y] = 0; uy[y] = 0; } for (int x = 1; x <= nx; ++x) { mx[x] = 0; ex[x] = *max_element(v[x] + 1, v[x] + ny + 1); ux[x] = 0; } for (int x = 1; x <= nx; ++x) bfs(x); T ans = 0; for (int y = 1; y <= ny; ++y) ans += v[my[y]][y]; return ans; } void init(int nx_, int ny_) { nx = nx_; ny = ny_; for (int i = 1; i <= nx; ++i) for (int j = 1; j <= ny; ++j) v[i][j] = 0; } void addedge(int x, int y, T z) { v[x][y] = z; } } // namespace KM int n, k; void solve() { read(n, k); KM::init(n, n); for (int i = 1; i <= n; ++i) { int a, b; read(a, b); for (int j = 1; j <= n; ++j) if (j < k) KM::addedge(i, j, a + b * (j - 1)); else if (j == n) KM::addedge(i, j, a + b * k); else KM::addedge(i, j, b * k); } int res = KM::KM(); vector<int> ans; for (int j = 1; j < k; ++j) ans.push_back(KM::my[j]); for (int j = k; j < n; ++j) ans.push_back(KM::my[j]), ans.push_back(-KM::my[j]); ans.push_back(KM::my[n]); writeln<int>(ans.size()); print(ans); } int main() { int T = 1; read(T); for (int test = 1; test <= T; ++test) { 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("O3") #pragma GCC optimize("unroll-loops") const long long INF = 0x3f3f3f3f3f3f3f3f; const long long llinf = (1LL << 62); const int inf = (1 << 30); const int nmax = 1e2 + 50; const long long mod = 1e9 + 7; using namespace std; long long ts, i, j, n, f[nmax][nmax][2], k, k1, rs; pair<pair<long long, long long>, long long> a[nmax]; vector<long long> del, add; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cerr.tie(0); cout.tie(0); cin >> ts; while (ts--) { cin >> n >> k; for (i = 1; i <= n; i++) { cin >> a[i].first.second >> a[i].first.first; a[i].second = i; } sort(a + 1, a + n + 1); for (i = 0; i <= n; i++) for (j = 1; j <= k; j++) f[i][j][0] = f[i][j][1] = -inf; for (i = 1; i <= n; i++) { for (j = 1; j <= min(k, i); j++) { if (max(f[i - 1][j][0], f[i - 1][j][1]) != -inf) f[i][j][0] = max(f[i - 1][j][0], f[i - 1][j][1]) + (k - 1) * a[i].first.first; if (max(f[i - 1][j - 1][0], f[i - 1][j - 1][1]) != -inf) f[i][j][1] = max(f[i][j][1], max(f[i - 1][j - 1][0], f[i - 1][j - 1][1]) + a[i].first.second + (j - 1) * a[i].first.first); } } rs = max(f[n][k][0], f[n][k][1]); del.clear(), add.clear(), k1 = k; for (i = n; i >= 1; i--) { if (k && rs == f[i][k][1]) { add.push_back(a[i].second); rs -= a[i].first.second + (k - 1) * a[i].first.first; k--; } else { del.push_back(a[i].second); rs -= (k1 - 1) * a[i].first.first; } } reverse(add.begin(), add.end()); cout << 2 * n - k1 << endl; for (i = 0; i < (int)add.size() - 1; i++) cout << add[i] << " "; for (i = 0; i < (int)del.size(); i++) cout << del[i] << " " << -del[i] << " "; 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
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(); } if(asdf == 10) { out = new PrintWriter(System.out); out.println(n + " " + k); out.println(Arrays.toString(a)); out.println(Arrays.toString(b)); out.flush(); return; } 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]; else temp[2] = 2147483647; if(al.size() >= 2) temp[3] = al.get(1).a+b[i]+b[min]; else temp[3] = 2147483647; 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]; else temp[2] = 2147483647; if(al.size() >= 2) temp[3] = al.get(1).a+b[i]+b[min]; else temp[3] = 2147483647; 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; 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; printf("%d\n", n * 2 - k); for (int i = 0; i < k - 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[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
cpp
#include <bits/stdc++.h> using namespace std; struct Edge { int from, to, capacity, cost; Edge(int from, int to, int capacity, int cost) : from(from), to(to), capacity(capacity), cost(cost){}; }; vector<vector<int>> adj, cost, capacity; const int INF = 2e9; void shortest_paths(int n, int v0, vector<int>& d, vector<int>& p) { d.assign(n, INF); d[v0] = 0; vector<bool> inq(n, false); queue<int> q; q.push(v0); p.assign(n, -1); while (!q.empty()) { int u = q.front(); q.pop(); inq[u] = false; for (int v : adj[u]) { if (capacity[u][v] > 0 && d[v] > d[u] + cost[u][v]) { d[v] = d[u] + cost[u][v]; p[v] = u; if (!inq[v]) { inq[v] = true; q.push(v); } } } } } int min_cost_flow(int N, vector<Edge>& edges, int s, int t) { adj.assign(N, vector<int>()); cost.assign(N, vector<int>(N, 0)); capacity.assign(N, vector<int>(N, 0)); for (Edge e : edges) { adj[e.from].push_back(e.to); adj[e.to].push_back(e.from); cost[e.from][e.to] = e.cost; cost[e.to][e.from] = -e.cost; capacity[e.from][e.to] = e.capacity; } int flow = 0; int cost = 0; vector<int> d, p; while (1) { shortest_paths(N, s, d, p); if (d[t] == INF) break; int f = 2000000000; int cur = t; while (cur != s) { f = min(f, capacity[p[cur]][cur]); cur = p[cur]; } flow += f; cost += f * d[t]; cur = t; while (cur != s) { capacity[p[cur]][cur] -= f; capacity[cur][p[cur]] += f; cur = p[cur]; } } return cost; } int ab[76][2]; int main() { int T; int i, j; int n, k; int s, t; scanf("%d", &T); while (T--) { vector<Edge> edges; vector<int> res; scanf("%d %d", &n, &k); s = n + n, t = n + n + 1; for (i = 0; i < n; i++) { scanf("%d %d", &ab[i][0], &ab[i][1]); } for (i = 0; i < n; i++) { edges.emplace_back(s, i, 1, 0); edges.emplace_back(i + n, t, 1, 0); } for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (i >= k - 1 && i < n - 1) edges.emplace_back(j, i + n, 1, -(k - 1) * ab[j][1]); else if (i == n - 1) edges.emplace_back(j, i + n, 1, -(ab[j][0] + (k - 1) * ab[j][1])); else edges.emplace_back(j, i + n, 1, -(ab[j][0] + i * ab[j][1])); } } cout << -min_cost_flow(n + n + 2, edges, s, t) << endl; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (!capacity[j][i + n]) { if (i >= k - 1 && i < n - 1) { res.push_back(j + 1); res.push_back(-j - 1); } else res.push_back(j + 1); } } } printf("%d\n", 2 * n - k); for (auto o : res) printf("%d ", o); 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.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]; else temp[2] = 2147483647; if(al.size() >= 2) temp[3] = al.get(1).a+b[i]+b[min]; else temp[3] = 2147483647; 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]; else temp[2] = 2147483647; if(al.size() >= 2) temp[3] = al.get(1).a+b[i]+b[min]; else temp[3] = 2147483647; 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; int main() { ios_base::sync_with_stdio(0), cin.tie(0); int t; cin >> t; while (t--) { int n, k; cin >> n >> k; vector<pair<int, int>> a(n); for (auto &i : a) cin >> i.first >> i.second; sort(a.begin(), a.end(), [](pair<int, int> x, pair<int, int> y) { return x.second < y.second; }); vector<vector<int>> dp(n + 1, vector<int>(k + 2, -2e9)); dp[0][0] = 0; for (int p = 0; p < n; ++p) for (int i = k; i >= 0; --i) { dp[p + 1][i + 1] = max(dp[p + 1][i + 1], dp[p][i] + a[p].first + i * a[p].second); dp[p + 1][i] = dp[p][i] + (k - 1) * a[p].second; } deque<int> d; int p = n, i = k; for (; p > 0; --p) { int c1 = -2e9, c0 = c1; if (i > 0) c1 = dp[p - 1][i - 1] + a[p - 1].first + (i - 1) * a[p - 1].second; c0 = dp[p - 1][i] + (k - 1) * a[p - 1].second; if (c1 == dp[p][i]) { d.push_front(p); --i; continue; } d.push_back(p); d.push_back(-p); } vector<int> ans; while (!d.empty()) ans.push_back(d.front()), d.pop_front(); if (k != n) rotate(ans.begin() + k - 1, ans.begin() + k, ans.end()); cout << ans.size() << "\n"; for (auto &i : ans) cout << i << " \n"[&i == &ans.back()]; } 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,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; long long int const mod = 998244353; std::mt19937 rng( (int)std::chrono::steady_clock::now().time_since_epoch().count()); int rand_rng(int l, int r) { uniform_int_distribution<int> p(l, r); return p(rng); } long long int arr1[1000009]; vector<long long int> sieve; void sievefun(long long int n) { for (long long int i = 2; i <= n; i++) { if (arr1[i] == 0) { for (long long int j = i * i; j <= n; j += i) { arr1[j] = 1; } } } for (int i = 2; i <= n; i++) { if (arr1[i] == 0) sieve.push_back(i); } } long long int power(long long int x, long long int y, long long int m) { long long int temp; if (y == 0 || x == 1) return 1; temp = power(x, y / 2, m) % m; if (y % 2 == 0) return ((temp) * (temp)) % m; else return (((x) % m) * ((temp * temp) % m)) % m; } long long int dp[109][109]; pair<pair<long long int, long long int>, int> a[109]; long long int p[109][109]; int main() { long long int T; cin >> T; for (int _ = 0; _ < (int)T; _++) { long long int n, k; cin >> n >> k; for (int i = 0; i < (int)n; i++) { cin >> a[i + 1].first.second >> a[i + 1].first.first; a[i + 1].second = i + 1; } sort(a + 1, a + n + 1); dp[0][0] = 0; for (int i = 0; i < (int)n + 1; i++) for (int j = 0; j < (int)k + 1; j++) dp[i][j] = -1, p[i][j] = 0; dp[0][0] = 0; for (int i = 1; i <= n; i++) { for (int j = 0; j <= k; j++) { if (dp[i - 1][j] != -1) dp[i][j] = dp[i - 1][j] + (k - 1) * a[i].first.first; if (j) { if (dp[i - 1][j - 1] != -1) { if (dp[i - 1][j - 1] + a[i].first.second + a[i].first.first * (j - 1) > dp[i][j]) p[i][j] = 1; dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + a[i].first.second + a[i].first.first * (j - 1)); } } } } set<long long int> ans; int ind = n; int pos = k; vector<long long int> out; while (ind) { if (p[ind][pos]) { ans.insert(a[ind].second); pos--; } ind--; } ind = *ans.rbegin(); out.push_back(ind); if (out.size()) { for (int i = 1; i <= n; i++) { if (ans.find(i) == ans.end()) { out.push_back(-1 * i); out.push_back(i); } } } ind = 1; for (auto i = ans.rbegin(); i != ans.rend(); i++) { if (i == ans.rbegin()) continue; out.push_back(*i); } cout << out.size() << "\n"; for (auto i : out) 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; const int le = 76; struct mon { int i = 0; long long power = 0; long long group = 0; bool operator<(const mon& other) const { if (this->group == other.group) { return this->power > other.power; } return this->group < other.group; } }; long long dp[le][le]; mon a[le]; int role[le]; int main() { iostream::sync_with_stdio(0); cin.tie(0); cout.tie(0); int q; cin >> q; while (q--) { int n, k; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> a[i].power >> a[i].group; a[i].i = i; } for (int i = 0; i <= n; i++) { for (int j = 0; j <= k; j++) { dp[i][j] = -1; } } fill(role, role + n, 0); sort(a, a + n); dp[0][0] = 0; for (int i = 0; i < n; i++) { for (int j = 0; j <= k; j++) { if (dp[i][j] == -1) continue; if (j < k) { dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + a[i].group * j + a[i].power); } dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + a[i].group * (k - 1)); } } int x, y; x = n; y = k; while (x > 0) { if (dp[x - 1][y] + a[x - 1].group * (k - 1) == dp[x][y]) { role[x - 1] = 1; } else { y--; } x--; } cout << 2 * n - k << endl; for (int i = 0; i < n && k > 1; i++) { if (role[i] == 0) { cout << a[i].i + 1 << ' '; k--; } } for (int i = 0; i < n; i++) { if (role[i] == 1) { cout << a[i].i + 1 << ' ' << -(a[i].i + 1) << ' '; } } for (int i = n - 1; i >= 0; i--) { if (role[i] == 0) { cout << a[i].i + 1 << endl; break; } } } 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 = 2e2 + 5; int n, k; struct node { int id, a, b; node(int i = 0, int _a = 0, int _b = 0) { id = i, a = _a, b = _b; } bool operator<(const node& a) const { return b < a.b; } } p[N]; long long dp[N][N]; int pre[N][N]; int main() { int TTT; cin >> TTT; while (TTT--) { cin >> n >> k; long long ans = 0; for (int i = (1); i < (n + 1); i++) { cin >> p[i].a >> p[i].b; p[i].id = i; } sort(p + 1, p + n + 1); memset(dp, 0, sizeof(dp)); for (int i = (1); i < (n + 1); i++) { dp[i][0] = dp[i - 1][0] + (long long)(k - 1) * p[i].b; for (int j = (1); j < (i + 1); j++) { long long x = dp[i - 1][j - 1] + (j - 1) * p[i].b + p[i].a; long long y = dp[i - 1][j] + (k - 1) * p[i].b; dp[i][j] = max(x, y); if (x > y) pre[i][j] = j - 1; else pre[i][j] = j; } } int u = k; vector<int> p1, p2; for (int i = n; i >= 1; --i) { if (pre[i][u] == u) { p2.push_back(p[i].id); } else u--, p1.push_back(p[i].id); } reverse(p1.begin(), p1.end()); reverse(p2.begin(), p2.end()); cout << (int)p1.size() + 2 * (int)p2.size() << "\n"; for (int i = (0); i < (k - 1); i++) cout << p1[i] << " "; for (auto it : p2) cout << it << " " << -it << " "; cout << p1[k - 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; const int Maxn = 88, inf = 0x3f3f3f3f; 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] = -inf; 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--) { init(); 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; f[0][0] = 0; 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); 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]]); } 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; bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) { return a.first.first > b.first.first; } int Main() { int n, k; cin >> n >> k; vector<pair<pair<int, int>, int> > a(n); for (int i = 0; i < n; i++) { cin >> a[i].first.second >> a[i].first.first; a[i].second = i; } sort(a.begin(), a.end(), cmp); vector<vector<int> > DP(k + 1, vector<int>(n + 1)); vector<vector<bool> > op(k + 1, vector<bool>(n + 1)); for (int i = 1; i <= k; i++) { for (int j = 1; j <= n; j++) { DP[i][j] = max(DP[i - 1][j - 1] + a[j - 1].first.second + (i - 1) * a[j - 1].first.first, DP[i][j - 1] + a[j - 1].first.first * (k - 1)); if (DP[i][j] == DP[i - 1][j - 1] + a[j - 1].first.second + (i - 1) * a[j - 1].first.first) op[i][j] = 1; } } vector<int> steps; int i = k; int j = n; while (i != 0 && j != 0) { if (op[i][j]) { steps.push_back(j - 1); j--; i--; } else { j--; } } reverse(steps.begin(), steps.end()); vector<int> rez; vector<bool> Added(n); for (int i = 0; i < steps.size() - 1; i++) { rez.push_back(a[steps[i]].second + 1); Added[steps[i]] = 1; } Added[steps[steps.size() - 1]] = 1; for (int i = 0; i < n; i++) { if (Added[i]) continue; rez.push_back(a[i].second + 1); rez.push_back(-(a[i].second + 1)); } rez.push_back(a[steps[steps.size() - 1]].second + 1); cout << rez.size() << endl; for (int i = 0; i < rez.size(); i++) { cout << rez[i] << " "; } cout << endl; } int main() { int t; cin >> t; for (int i = 0; i < t; i++) Main(); }
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 = (1e2) + 10; const int M = (1e5) + 10; const int INF = (1e9) + 10; const int maxn = 5e6 + 100; int dp[N][N]; int p[N][N]; void solve() { int n, k; cin >> n >> k; vector<pair<int, int> > arr(n); vector<int> ans[2]; map<pair<int, int>, int> poz; for (int i = int(0); i < int(n); i++) { int a, b; cin >> a >> b; arr[i] = {b, a}; poz[{b, a}] = i + 1; } sort(arr.begin(), arr.end()); memset(dp, -INF, sizeof(dp)); dp[0][0] = 0; for (int i = int(0); i < int(n); i++) { for (int j = int(0); j < int(k + 1); j++) { if (dp[i + 1][j + 1] < dp[i][j] + arr[i].first * j + arr[i].second) { dp[i + 1][j + 1] = dp[i][j] + arr[i].first * j + arr[i].second; p[i + 1][j + 1] = 1; } if (dp[i + 1][j] < dp[i][j] + arr[i].first * (k - 1)) { dp[i + 1][j] = dp[i][j] + arr[i].first * (k - 1); p[i + 1][j] = 0; } } } int pos = k; for (int i = n; i >= 1; i--) { ans[p[i][pos]].push_back(poz[arr[i - 1]]); pos -= p[i][pos]; } reverse(ans[0].begin(), ans[0].end()); reverse(ans[1].begin(), ans[1].end()); cout << ans[0].size() * 2 + ans[1].size() << endl; for (int i = int(0); i < int(ans[1].size() - 1); i++) cout << ans[1][i] << ' '; for (int i = int(0); i < int(ans[0].size()); i++) cout << ans[0][i] << ' ' << -ans[0][i] << ' '; cout << ans[1][ans[1].size() - 1]; cout << endl; } int main() { int t = 1; while (t) { solve(); t--; } 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
/* 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]; long[][] dp=new long[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, Long.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]+mnn[i].b*(k-1), dp[i-1][j-1]+mnn[i].a+mnn[i].b*(j-1)); poss[i][j]=(dp[i][j]^dp[i-1][j]+mnn[i].b*(k-1))==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 <bits/stdc++.h> using namespace std; int dp[80][80], dir[80][80]; struct minion { int pow, add; } a[80]; int ind[80]; static inline void refresh() { for (int i = 0; i < 80; ++i) { ind[i] = a[i].pow = a[i].add = 0; for (int j = 0; j < 80; ++j) { dp[i][j] = dir[i][j] = 0; } } } struct cmp { inline bool operator()(const int &x, const int &y) { return a[x].add < a[y].add; } }; int dx[] = {-1, -1}; int dy[] = {0, -1}; vector<int> mvs; void mkMoves(int i, int j) { mvs.clear(); while (i) { int q = dir[i][j] - 1; if (q) { mvs.push_back(ind[i]); } else { mvs.push_back(-ind[i]); mvs.push_back(ind[i]); } i += dx[q]; j += dy[q]; } reverse(mvs.begin(), mvs.end()); cout << mvs.size() << "\n"; for (int i = 0; i < mvs.size(); ++i) { cout << mvs[i] << " "; } cout << "\n\n"; } int main() { int t, n, k; cin >> t; while (t--) { cin >> n >> k; refresh(); for (int i = 1; i <= n; ++i) { cin >> a[i].pow >> a[i].add; ind[i] = i; } sort(ind, ind + n, cmp()); for (int i = 1; i <= n; ++i) { for (int j = 0; j <= k && j <= i; ++j) { int l = 0, r = 0; if (j >= 1) { l = dp[i - 1][j - 1] + a[ind[i]].pow + (j - 1) * a[ind[i]].add; } if (j < i) { r = dp[i - 1][j] + (k - 1) * a[ind[i]].add; } dp[i][j] = max(l, r); if (l < r) { dir[i][j] = 1; } else { dir[i][j] = 2; } } } mkMoves(n, k); } 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> class Hungarian { const T FINF = numeric_limits<T>::max() / 2.0; public: int n, m; vector<T> u, v, p, way; Hungarian(){}; Hungarian(vector<vector<T> > a) { n = a.size() - 1; m = a[0].size() - 1; u.resize(n + 2); v.resize(m + 2); p.resize(m + 2); way.resize(m + 2); for (int i = 1; i <= n; ++i) { p[0] = i; int j0 = 0; vector<T> minv(m + 1, FINF); vector<bool> used(m + 1, false); do { used[j0] = true; int i0 = p[j0], j1; T delta = FINF; for (int j = 1; j <= m; ++j) if (!used[j]) { int cur = a[i0][j] - u[i0] - v[j]; if (cur < minv[j]) minv[j] = cur, way[j] = j0; if (minv[j] < delta) delta = minv[j], j1 = j; } for (int j = 0; j <= m; ++j) if (used[j]) u[p[j]] += delta, v[j] -= delta; else minv[j] -= delta; j0 = j1; } while (p[j0] != 0); do { int j1 = way[j0]; p[j0] = p[j1]; j0 = j1; } while (j0); } } T getCost() { return -v[0]; } vector<int> getAns() { vector<int> ans(n + 1); for (int j = 1; j <= m; ++j) ans[p[j]] = j; return ans; } }; int main() { int test; scanf("%d", &test); while (test--) { int n, k; scanf("%d %d", &n, &k); vector<int> a(n), b(n); for (int i = 0; i < n; ++i) { scanf("%d %d", &a[i], &b[i]); } vector<vector<int> > cost(n, vector<int>(n)); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cost[i][j] = (j < k) ? -(a[i] + j * b[i]) : -((k - 1) * b[i]); } } Hungarian<int> g(cost); vector<int> out; for (int i = 1; i < k; ++i) { out.emplace_back(g.p[i] + 1); } for (int i = k + 1; i <= n; ++i) { out.emplace_back(g.p[i] + 1); out.emplace_back(-g.p[i] - 1); } out.emplace_back(g.p[k] + 1); for (int i = 0; i < out.size(); ++i) { printf("%d%c", out[i], " \n"[i + 1 == out.size()]); } } 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
/* 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]+mnn[i].b*(k-1), dp[i-1][j-1]+mnn[i].a+mnn[i].b*(j-1)); poss[i][j]=(dp[i][j]^dp[i-1][j]+mnn[i].b*(k-1))==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
python3
5
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 a[x] < a[y]; } struct MCMF { int n, m; vector<Edge> edge; vector<int> G[maxn]; int inq[maxn], d[maxn], p[maxn], a[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) { 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); } 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 (to < 2 * n) continue; if (edge[to].flow == false) continue; if (to < 4 * n) { ans1.push_back(i); } else ans2.push_back(i); } } 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--) { int n, k; read(n); read(k); 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]); } printf("%d\n", k + 2 * (n - k)); if (k == 1) { printf("%d\n%d\n", 1, j); continue; } int s = 0, t = 3 * n + 3; for (int i = 1; i <= n; i++) { M.add(s, i, 1, 0); } for (int i = 1; i <= n; i++) { M.add(i, i + n, 1, -a[i]); } for (int i = 1; i <= n; i++) M.add(i, i + n + n, 1, -1LL * (k - 1) * b[i]); for (int i = n + 1; i <= n + n; i++) { M.add(i, 3 * n + 1, 1, 0); M.add(i + n, 3 * n + 2, 1, 0); } M.add(3 * n + 1, 3 * n + 3, k, 0); M.add(3 * n + 2, 3 * n + 3, n - k, 0); 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 int maxn = 100; const int inf = 2147483647; const double pi = 3.1415926576; const long long INF = 9223372036854775807ll; long long t, n, k, dp[maxn][maxn], path[maxn][maxn], gr1[maxn], gr2[maxn], tot1, tot2; struct Node { long long a, b, id; bool operator<(const Node A) const { return b < A.b; } } st[maxn]; int main() { scanf("%lld", &t); memset(dp[0] + 1, -1, sizeof(dp[0] + 1)); while (t--) { scanf("%lld%lld", &n, &k); for (register int i = 1; i < (n + 1); ++i) { scanf("%lld%lld", &st[i].a, &st[i].b); st[i].id = i; } sort(st + 1, st + 1 + n); for (register int i = 1; i < (n + 1); ++i) for (register int j = 0; j < (min((int)k, i) + 1); ++j) { if (dp[i - 1][j] != -1) dp[i][j] = dp[i - 1][j] + (k - 1ll) * st[i].b, path[i][j] = j; else dp[i][j] = -inf; if (j && dp[i - 1][j - 1] != -1) { if (dp[i][j] < dp[i - 1][j - 1] + st[i].a + (j - 1ll) * st[i].b) { dp[i][j] = dp[i - 1][j - 1] + st[i].a + (j - 1ll) * st[i].b; path[i][j] = j - 1; } } if (dp[i][j] == -inf) dp[i][j] = -1; } long long jj = path[n][k], pre = k, cnt = n; tot1 = tot2 = 0; while (cnt) { if (jj == pre) gr2[tot2++] = st[cnt].id; else gr1[tot1++] = st[cnt].id; cnt--, pre = jj; jj = path[cnt][jj]; } printf("%lld\n", tot1 + tot2 * 2ll); for (register int i = tot1 - 1; i >= (1); --i) printf("%lld ", gr1[i]); for (register int i = tot2 - 1; i >= (0); --i) printf("%lld %lld ", gr2[i], -gr2[i]); printf("%lld\n", gr1[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 n, k; int dp[80][80]; bool included[80]; pair<pair<int, int>, int> A[80]; vector<int> ans; bool byfise(pair<pair<int, int>, int> i, pair<pair<int, int>, int> j) { return i.first.second < j.first.second; } void backtrack() { int x = n, y = k; while (x) { if (dp[x - 1][y - 1] + A[x].first.first + (y - 1) * A[x].first.second == dp[x][y]) { y--; included[x] = 1; } x--; } } int main() { int t; scanf("%d", &t); while (t--) { memset(included, 0, sizeof included); memset(dp, -1, sizeof dp); ans.clear(); scanf("%d %d", &n, &k); for (int i = 1; i <= n; i++) { scanf("%d %d", &A[i].first.first, &A[i].first.second); A[i].second = i; } sort(A + 1, A + 1 + n, byfise); dp[0][0] = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (dp[i][j] == -1) continue; dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + (k - 1) * A[i + 1].first.second); dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + A[i + 1].first.first + j * A[i + 1].first.second); } } backtrack(); for (int i = 1; i <= n; i++) if (included[i]) { ans.push_back(A[i].second); } int temp = ans.back(); ans.pop_back(); for (int i = 1; i <= n; i++) { if (!included[i]) { ans.push_back(A[i].second); ans.push_back(-A[i].second); } } ans.push_back(temp); printf("%lu\n", ans.size()); for (int i = 0; i < ans.size(); i++) printf("%d%c", ans[i], (i == ans.size() - 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
#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=1; w<=min(q,k); w++){ ll go = dp[q-1][w-1] + w*m[w].F.S + m[w].F.F; ll ne_go = w==q ? 0 : dp[q-1][w] + (k-1)*m[w].S; 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(; ord.size()<k;){ if(take[cp][ck]){ ord.pb(cp); ck--; } else loxi.pb(cp); cp--; } 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 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); 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 - 1; i; --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[cnt]]); 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; 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 (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
cpp
#include <bits/stdc++.h> using namespace std; const int N = 77, inf = -1e9; int n, k, t, dp[N][N], par[N][N], mark[N]; pair<int, pair<int, int> > a[N]; int main() { cin >> t; for (int q = 0; q < t; q++) { fill(mark, mark + N, 0); cin >> n >> k; for (int i = 1; i < n + 1; i++) { cin >> a[i].second.first >> a[i].first; a[i].second.second = i; }; sort(a + 1, a + n + 1); for (int i = 1; i < n + 1; i++) dp[0][i] = inf; for (int i = 1; i < n + 1; i++) { dp[i][0] = dp[i - 1][0] + a[i].first * (k - 1); for (int j = 1; j < k + 1; j++) { dp[i][j] = dp[i - 1][j] + a[i].first * (k - 1); if (dp[i][j] < dp[i - 1][j - 1] + a[i].second.first + a[i].first * (j - 1)) dp[i][j] = dp[i - 1][j - 1] + a[i].second.first + a[i].first * (j - 1), par[i][j] = 1; } } cout << n * 2 - k << endl; int now = k; for (int i = n; i >= 1; i--) { if (par[i][now]) mark[i] = 1, now--; } for (int i = 1; i < n + 1; i++) { if (mark[i] && k > 1) { k--; cout << a[i].second.second << " "; } } for (int i = 1; i < n + 1; i++) if (!mark[i]) cout << a[i].second.second << " " << -a[i].second.second << " "; for (int i = n; i >= 1; i--) if (mark[i]) { cout << a[i].second.second << endl; break; } } }
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; #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization("unroll-loops") const long long int N = 5001; const long long int p = 31; const long long int INF = 1e9; const long long int mod = 1e9; long long int powm(long long int a, long long int b) { a = a % mod; long long int res = 1; while (b) { if (b & 1) res = (res * a) % mod; a = (a * a) % mod; b >>= 1; } return (res % mod); } long long int a[N], b[N]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int t = 1; cin >> t; while (t--) { long long int n, k; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> a[i] >> b[i]; } long long int arr[n + 1][n + 1]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (j < k - 1) { arr[i][j] = INF - (a[i] + (j)*b[i]); } else if (j == n - 1) { arr[i][j] = INF - (a[i] + (k - 1) * b[i]); } else { arr[i][j] = INF - ((k - 1) * b[i]); } } } long long int m = n; vector<int64_t> u(n + 1), v(m + 1); vector<int> p(m + 1), way(m + 1); for (int i = 1; i <= n; i++) { vector<int64_t> min_v(m + 1, INF); vector<bool> used(m + 1, false); p[0] = i; int j0 = 0; do { used[j0] = true; int i0 = p[j0], j1 = 0; int64_t delta = INF; for (int j = 1; j <= m; j++) if (!used[j]) { int64_t cur = arr[i0 - 1][j - 1] - u[i0] - v[j]; if (cur < min_v[j]) { min_v[j] = cur; way[j] = j0; } if (min_v[j] < delta) { delta = min_v[j]; j1 = j; } } for (int j = 0; j <= m; j++) if (used[j]) { u[p[j]] += delta; v[j] -= delta; } else { min_v[j] -= delta; } j0 = j1; } while (p[j0] != 0); do { int j1 = way[j0]; p[j0] = p[j1]; j0 = j1; } while (j0 != 0); } vector<long long int> ans(n + 1); for (long long int j = 1; j <= m; ++j) ans[p[j]] = j; long long int ct = 0; cout << n + (n - k) << '\n'; for (long long int i = 1; i <= n; i++) { cout << p[i] << " "; ct++; if (ct == k && i != n) { cout << -p[i] << " "; ct--; } } 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; const int maxn = 100; const int inf = 2147483647; const double pi = 3.1415926576; const long long INF = 9223372036854775807ll; int t, n, k, path[maxn][maxn], gr1[maxn], gr2[maxn], tot1, tot2; int dp[maxn][maxn]; struct Node { long long a, b; int id; } st[maxn]; bool cmp(Node a, Node b) { return a.b < b.b || a.b == b.b && a.a < b.a; } int main() { scanf("%d", &t); while (t--) { scanf("%d%d", &n, &k); for (register int i = 1; i < (n + 1); ++i) { scanf("%d%d", &st[i].a, &st[i].b); st[i].id = i; } sort(st + 1, st + 1 + n, cmp); for (register int i = 0; i < (n + 1); ++i) for (register int j = 0; j < (n + 1); ++j) dp[i][j] = -inf; dp[0][0] = 0; for (register int i = 1; i < (n + 1); ++i) for (register int j = 0; j < (n + 1); ++j) { if (dp[i - 1][j] >= 0 && dp[i - 1][j] + (k - 1) * st[i].b > dp[i][j]) { dp[i][j] = dp[i - 1][j] + (k - 1) * st[i].b > dp[i][j]; path[i][j] = j; } if (j && dp[i - 1][j - 1] >= 0 && dp[i][j] < dp[i - 1][j - 1] + st[i].a + (j - 1) * st[i].b) { dp[i][j] = dp[i - 1][j - 1] + st[i].a + (j - 1) * st[i].b; path[i][j] = j - 1; } } int cur = k; tot1 = tot2 = 0; for (register int i = n; i >= (1); --i) { if (path[i][cur] == cur) gr2[tot2++] = st[i].id; else gr1[tot1++] = st[i].id; cur = path[i][cur]; } printf("%d\n", tot1 + tot2 * 2); for (register int i = tot1 - 1; i >= (1); --i) printf("%d ", gr1[i]); for (register int i = tot2 - 1; i >= (0); --i) printf("%d %d ", gr2[i], -gr2[i]); printf("%d\n", gr1[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 = 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; } 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; const int N = (1e2) + 10; const int M = (1e5) + 10; const int INF = (1e9) + 10; const int maxn = 5e6 + 100; int dp[N][N]; int p[N][N]; void solve() { int n, k; cin >> n >> k; vector<pair<int, int> > arr(n); vector<int> ans[2]; map<pair<int, int>, int> poz; for (int i = int(0); i < int(n); i++) { int a, b; cin >> a >> b; arr[i] = {b, a}; poz[{b, a}] = i + 1; } sort(arr.begin(), arr.end()); memset(dp, -INF, sizeof(dp)); dp[0][0] = 0; for (int i = int(0); i < int(n); i++) { for (int j = int(0); j < int(k + 1); j++) { if (dp[i + 1][j + 1] < dp[i][j] + arr[i].first * j + arr[i].second) { dp[i + 1][j + 1] = dp[i][j] + arr[i].first * j + arr[i].second; p[i + 1][j + 1] = 1; } if (dp[i + 1][j] < dp[i][j] + arr[i].first * (k - 1)) { dp[i + 1][j] = dp[i][j] + arr[i].first * (k - 1); p[i + 1][j] = 0; } } } int pos = k; for (int i = n; i >= 1; i--) { ans[p[i][pos]].push_back(poz[arr[i - 1]]); pos -= p[i][pos]; } reverse(ans[0].begin(), ans[0].end()); reverse(ans[1].begin(), ans[1].end()); cout << ans[0].size() * 2 + ans[1].size() << endl; for (int i = int(0); i < int(ans[1].size() - 1); i++) cout << ans[1][i] << ' '; for (int i = int(0); i < int(ans[0].size()); i++) cout << ans[0][i] << ' ' << -ans[0][i] << ' '; cout << ans[1][ans[1].size() - 1]; cout << endl; } int main() { int t = 1; cin >> t; while (t) { solve(); t--; } 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 dp[75][75]; bool is[75][75]; bool act[75][75]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int q; cin >> q; while (q--) { int n, k; cin >> n >> k; vector<pair<int, int> > mini; vector<pair<int, int> > stats; long long a, b; long long ki = 1LL * k; for (int i = 0; i < n; i++) { cin >> a >> b; mini.push_back(make_pair(b, i)); stats.push_back(make_pair(a, b)); } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { dp[i][j] = 0; is[i][j] = false; act[i][j] = false; } } sort(mini.begin(), mini.end()); dp[0][0] = (ki - 1LL) * (stats[mini[0].second].second); dp[0][1] = stats[mini[0].second].first; is[0][1] = true; is[0][0] = true; act[0][1] = true; for (int i = 0; i < n; i++) for (int i = 0; i < n - 1; i++) { a = stats[mini[i + 1].second].first; b = stats[mini[i + 1].second].second; for (int j = 0; j <= k; j++) { if (is[i][j]) { long long dead = b * (ki - 1LL); long long alive = a + b * (1LL * (j)); is[i + 1][j] = true; if (dp[i + 1][j] < dp[i][j] + dead) { dp[i + 1][j] = dp[i][j] + dead; act[i + 1][j] = false; } is[i + 1][j + 1] = true; if (dp[i + 1][j + 1] < dp[i][j] + alive) { act[i + 1][j + 1] = true; dp[i + 1][j + 1] = dp[i][j] + alive; } } } } vector<int> vivos; vector<int> muertos; int current = k; for (int i = n - 1; i >= 0; i--) { if (act[i][current]) { vivos.push_back(i); current--; } else { muertos.push_back(i); } } cout << (vivos.size() + 2 * muertos.size()) << '\n'; for (int i = 0; i < vivos.size() - 1; i++) { cout << (mini[vivos[i]].second + 1) << " "; } for (int i = 0; i < muertos.size(); i++) { cout << (mini[muertos[i]].second + 1) << " " << (-1 * (mini[muertos[i]].second + 1)) << " "; } cout << (mini[vivos[vivos.size() - 1]].second + 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> #pragma GCC optimize("O2") using namespace std; const int MAX = 5e3 + 5; const long long MAX2 = 11; const long long MOD = 1000000007; const long long MOD2 = 1000005329; const long long INF = 2e18; const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1, 0}; const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1, 0}; const double pi = acos(-1); const double EPS = 1e-9; const int block = 2000; int tc, n, k, res, nw; pair<int, pair<int, int> > x[MAX]; vector<int> ans, tmp; vector<pair<int, pair<int, int> > > v; bool cmp(pair<int, pair<int, int> > a, pair<int, pair<int, int> > b) { return a.first - a.second.first > b.first - b.second.first; } bool cmp2(pair<int, pair<int, int> > a, pair<int, pair<int, int> > b) { return a.second.first < b.second.first; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> tc; for (long long ntc = 1; ntc <= tc; ++ntc) { cin >> n >> k; for (long long i = 1; i <= n; ++i) cin >> x[i].first >> x[i].second.first, x[i].second.second = i; res = 0; for (long long o = 1; o <= n; ++o) { v.clear(), tmp.clear(); for (long long i = 1; i <= n; ++i) if (i != o) v.push_back(x[i]); sort(v.begin(), v.end(), cmp); sort(v.begin(), v.begin() + k, cmp2); nw = 0; for (long long i = 0; i <= k - 2; ++i) nw += v[i].first + i * v[i].second.first, tmp.push_back(v[i].second.second); for (long long i = k - 1; i <= n - 2; ++i) nw += (k - 1) * v[i].second.first, tmp.push_back(v[i].second.second), tmp.push_back(-v[i].second.second); nw += x[o].first + x[o].second.first * (k - 1), tmp.push_back(x[o].second.second); if (nw > res) res = nw, ans = tmp; } cout << ans.size() << '\n'; for (auto i : ans) cout << i << ' '; 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
cpp
#include <bits/stdc++.h> using namespace std; struct EdmondsKarp { struct edge { int from, to, f, cap, cost; }; vector<vector<long long> > G; vector<edge> E; int source, sink; const int inf = 1e9; EdmondsKarp(int s, int t) : source(s), sink(t), G(t + 1){}; void add_edge(int from, int to, int cap, int cost) { G[from].push_back(E.size()); E.push_back({from, to, cap, cap, cost}); G[to].push_back(E.size()); E.push_back({to, from, 0, cap, -cost}); } int solve() { auto bfs = [&]() { vector<int> dad(sink + 1, -1), e(sink + 1); vector<int> cost(sink + 1); dad[source] = -2; queue<int> q; q.push(source); while (!q.empty()) { int v = q.front(); q.pop(); if (v == sink) continue; for (int i = 0; i < G[v].size(); ++i) { int idx = G[v][i]; int next = E[idx].to; if (E[idx].f) { if (dad[next] == -1) { dad[next] = v; e[next] = idx; cost[next] = cost[v] + E[idx].cost; q.push(next); } else { if (cost[v] + E[idx].cost > cost[next]) { dad[next] = v; e[next] = idx; cost[next] = cost[v] + E[idx].cost; } } } } } if (dad[sink] == -1) return 0; int cur = sink, flow = inf; while (cur != source) { int idx = e[cur]; flow = min(flow, E[idx].f); cur = dad[cur]; } cur = sink; while (cur != source) { int idx = e[cur]; E[idx].f -= flow; E[idx ^ 1].f += flow; cur = dad[cur]; } return flow; }; int flow = 0, f; while (f = bfs()) flow += f; return flow; } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t, n, m; cin >> t; while (t--) { cin >> n >> m; vector<int> data(n), cost(n); for (int i = 0; i < n; ++i) cin >> data[i] >> cost[i]; int s = 0, t = 2 * n + 1; EdmondsKarp flow(s, t); for (int i = 1; i <= n; ++i) { flow.add_edge(s, i, 1, 0); flow.add_edge(i + n, t, 1, 0); for (int j = 1; j < m; ++j) flow.add_edge(i, j + n, 1, data[i - 1] + cost[i - 1] * (j - 1)); for (int j = m; j < n; ++j) flow.add_edge(i, j + n, 1, cost[i - 1] * (m - 1)); flow.add_edge(i, n + n, 1, data[i - 1] + cost[i - 1] * (m - 1)); } flow.solve(); vector<int> pos(n + 1); for (auto &e : flow.E) { if (e.from <= n && e.to > n && !e.f) pos[e.to - n] = e.from; } cout << m + (n - m) * 2 << "\n"; for (int i = 1; i < m; ++i) cout << pos[i] << " "; for (int i = m; i < n; ++i) cout << pos[i] << " " << -pos[i] << " "; cout << pos[n] << "\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 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) << " "; 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> using namespace std; const int maxn = 80; int n, dp[maxn], old[maxn], k; bool cer[maxn][maxn][maxn]; struct minion { int a, b, i; } m[maxn]; int main() { ios::sync_with_stdio(0), cin.tie(0); int t; cin >> t; while (t--) { cin >> n >> k; for (int i = 0; i < n; i++) cin >> m[i].a >> m[i].b, m[i].i = i; sort(m, m + n, [](minion a, minion b) { return a.b < b.b; }); int ans = 0, cerl; for (int last = 0; last < n; last++) { memset(dp, -63, sizeof dp); dp[0] = 0; for (int i = 0; i < n; i++) if (i != last) { for (int j = k - 1; j > 0; j--) { cer[last][i][j] = dp[j] + m[i].b * j < dp[j - 1] + m[i].b * (j - 1) + m[i].a; dp[j] = max(dp[j] + m[i].b * j, dp[j - 1] + m[i].b * (j - 1) + m[i].a); } } if (ans < m[last].a + m[last].b * (k - 1) + dp[k - 1]) cerl = last, ans = m[last].a + m[last].b * (k - 1) + dp[k - 1]; } vector<int> come, app; for (int i = n - 1, j = k - 1; i >= 0; i--) if (i != cerl) if (cer[cerl][i][j]) come.push_back(i), j--; else app.push_back(i); reverse(come.begin(), come.end()); cout << come.size() + app.size() * 2 + 1 << '\n'; for (auto i : come) cout << m[i].i + 1 << ' '; for (auto i : app) cout << m[i].i + 1 << ' ' << -m[i].i - 1 << ' '; cout << m[cerl].i + 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
#pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") //#pragma GCC optimize(3) //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //#pragma GCC target("sse3","sse2","sse") //#pragma GCC target("avx","sse4","sse4.1","sse4.2","ssse3") //#pragma GCC target("f16c") //#pragma GCC optimize("inline","fast-math","unroll-loops","no-stack-protector") //#pragma GCC diagnostic error "-fwhole-program" //#pragma GCC diagnostic error "-fcse-skip-blocks" //#pragma GCC diagnostic error "-funsafe-loop-optimizations" //#pragma GCC diagnostic error "-std=c++14" #include "bits/stdc++.h" #include "ext/pb_ds/tree_policy.hpp" #include "ext/pb_ds/assoc_container.hpp" #define PB push_back #define PF push_front #define LB lower_bound #define UB upper_bound #define fr(x) freopen(x,"r",stdin) #define fw(x) freopen(x,"w",stdout) #define REP(x, l, u) for(ll x = l;x<u;x++) #define RREP(x, l, u) for(ll x = l;x>=u;x--) #define complete_unique(a) a.erase(unique(begin(a),end(a)),end(a)) #define mst(x, a) memset(x,a,sizeof(x)) #define all(a) begin(a),end(a) #define rall(a) rbegin(a),rend(a) #define PII pair<int,int> #define PLL pair<ll,ll> #define MP make_pair #define lowbit(x) ((x)&(-(x))) #define bitcnt(x) (__builtin_popcountll(x)) #define lson (ind<<1) #define rson (ind<<1|1) #define se second #define fi first #define sz(x) ((int)x.size()) #define EX0 exit(0); typedef long long ll; typedef unsigned long long ull; typedef double db; typedef long double ld; using namespace __gnu_pbds; //required using namespace std; template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; typedef vector<ll> VLL; typedef vector<int> VI; const ll mod = 1e9 + 7; string to_string (string s) { return '"' + s + '"'; } string to_string (const char *s) { return to_string ((string) s); } string to_string (bool b) { return (b ? "true" : "false"); } template<typename A, typename B> string to_string (pair<A, B> p) { return "(" + to_string (p.first) + ", " + to_string (p.second) + ")"; } template<typename A> string to_string (A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string (x); } res += "}"; return res; } void debug_out () { cerr<<endl; } template<typename Head, typename... Tail> void debug_out (Head H, Tail... T) { cerr<<" "<<to_string (H); debug_out (T...); } #ifdef LOCAL #define dbg(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define dbg(...) {} #endif template<typename T, typename S> inline bool upmin (T &a, const S &b) { return a > b ? a = b, 1 : 0; } template<typename T, typename S> inline bool upmax (T &a, const S &b) { return a < b ? a = b, 1 : 0; } ull twop (ll x) { return 1ULL<<x; } ll MOD (ll a, ll m) { a %= m; if (a < 0)a += m; return a; } ll inverse (ll a, ll m) { a = MOD (a, m); if (a <= 1)return a; return MOD ((1 - inverse (m, a) * m) / a, m); } template<typename T> T sqr (T x) { return x * x; } ll gcd (ll a, ll b) { a = abs (a), b = abs (b); while (b != 0) { a %= b; swap (a, b); } return a; } ll fast (ll a, ll b, ll mod) { if (b < 0)a = inverse (a, mod), b = -b; ll ans = 1; while (b) { if (b & 1)ans = ans * a % mod; a = a * a % mod; b /= 2; } return ans % mod; } namespace SOLVE { pair<ll, VLL> ans; ll a[1010], b[1010], n, k; ll dp[100][100]; void gao (VLL v) { mst(dp, 0xcf); dp[0][0] = 0; REP(i, 1, n + 1) { int id = v[i - 1]; dp[i][0] = 0; if (i == n) { dp[i][k] = dp[i - 1][k - 1] + a[id] + b[id] * (k - 1); } else { REP(j, 1, k + 1) { dp[i][j] = max (dp[i - 1][j] + b[id] * (k-1), dp[i - 1][j - 1] + a[id] + b[id] * (j - 1)); } } } dbg(dp[1][0], v); upmax (ans, MP (dp[n][k], v)); } void main () { cin>>n>>k; REP(i, 1, n + 1)cin>>a[i]>>b[i]; ans.fi = LLONG_MIN; REP(i, 1, n + 1) { VLL v; REP(j, 1, n + 1) { if (j != i) { v.PB (j); } } dbg(v); sort (all(v), [] (const ll &x, const ll &y) { return b[x] < b[y]; }); v.PB (i); gao (v); } gao (ans.se); int i = n, j = k-1; VLL res;res.PB(ans.se.back()); while (j > 0) { int id = ans.se[i - 1]; if (dp[i][j] == dp[i - 1][j] + b[id] * (k-1)){ res.PB(-id); res.PB(id); i--; }else{ res.PB(id); i--; j--; } } dbg(ans.fi); cout<<sz(res)<<endl; reverse(all(res)); for(auto i:res)cout<<i<<" "; cout<<endl; } } signed main () { #ifdef LOCAL fr("/Users/zhangqingchuan/Desktop/cp/cp/input.txt"); fw("/Users/zhangqingchuan/Desktop/cp/cp/output.txt"); #endif int t = 1; cin>>t; for (int i = 1; i <= t; i++) { // cout<<"Case #"<<i<<": "; SOLVE::main (); } // clock_t st = clock(); // while(clock() - st < 3.0 * CLOCKS_PER_SEC){ // // } 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 mxN = 80; int N, K; tuple<int, int, int> v[mxN]; int dp[mxN][mxN], pa[mxN][mxN]; int main() { ios::sync_with_stdio(false); cin.tie(0); int T; cin >> T; while (T--) { cin >> N >> K; for (int i = (1); i <= (N); ++i) { int A, B; cin >> A >> B; v[i] = {B, A, i}; } sort(v + 1, v + 1 + N); memset(pa, -1, sizeof pa); for (int i = (N); i >= (1); --i) { auto& [B, A, id] = v[i]; for (int x = (0); x <= (K); ++x) { int y = i - 1 - x; dp[i][x] = 0; if (x < K) { int a = dp[i + 1][x + 1] + B * x + A; if (a > dp[i][x]) { dp[i][x] = a; pa[i][x] = x + 1; } } if (y < N - K) { int a = dp[i + 1][x] + B * (K - 1); if (a > dp[i][x]) { dp[i][x] = a; pa[i][x] = x; } } } } vector<int> p, q; cout << N + (N - K) << '\n'; for (int i = 1, x = 0; i <= N; x = pa[i++][x]) { auto [B, A, id] = v[i]; if (pa[i][x] == x) { q.push_back(id); } else { p.push_back(id); } } for (int i = (0); i <= ((int)(p).size() - 2); ++i) { cout << p[i] << ' '; } for (int i = (0); i <= ((int)(q).size() - 1); ++i) { cout << q[i] << ' ' << -q[i] << ' '; } cout << p.back() << '\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 maxn = 1e2 + 5; int a[maxn], b[maxn], dp[maxn][maxn][maxn], vis[maxn]; int main() { int T; scanf("%d", &T); while (T--) { int n, kk; scanf("%d%d", &n, &kk); for (int i = 1; i <= n; i++) { scanf("%d%d", a + i, b + i); b[i] *= kk - 1; } memset(dp, -1, sizeof(dp)); dp[0][0][0] = 0; for (int i = 1; i <= n; i++) for (int j = 0; j <= kk; j++) for (int k = 0; k <= n - kk; k++) { if (j && dp[i - 1][j - 1][k] != -1) dp[i][j][k] = dp[i - 1][j - 1][k] + a[i]; if (k && dp[i - 1][j][k - 1] != -1) dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j][k - 1] + b[i]); } int j = kk, k = n - kk; memset(vis, -1, sizeof(vis)); for (int i = n; i; i--) { if (j && dp[i][j][k] == dp[i - 1][j - 1][k] + a[i]) { j--; vis[i] = 0; } else if (k && dp[i][j][k] == dp[i - 1][j][k - 1] + b[i]) { k--; vis[i] = 1; } } printf("%d\n", kk + (n - kk) * 2); for (int i = 1; i <= n && kk > 1; i++) if (!vis[i]) { printf("%d ", i); kk--; } for (int i = 1; i <= n; i++) if (vis[i] == 1) { printf("%d %d ", i, -i); } for (int i = n; i; i--) if (!vis[i]) { printf("%d\n", i); break; } } 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 = 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", m - 1 + ((n - m) << 1) + 1); 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 a[75]; int b[75]; int order[75]; int main() { cin.tie(0); ios_base::sync_with_stdio(0); int t; cin >> t; while (t--) { int n, k; cin >> n >> k; for (int i = 0; i < n; ++i) cin >> a[i] >> b[i], order[i] = i; sort(order, order + n, [&](int x, int y) { if (b[x] == b[y]) return a[x] > a[y]; else return b[x] < b[y]; }); vector<vector<int>> dp(n + 1, vector<int>(n + 1, 0)); for (int ii = 1; ii <= n; ++ii) { int i = order[ii - 1]; for (int j = 0; j <= min(ii, k); ++j) { if (j + (n - ii) >= k) dp[ii][j] = dp[ii - 1][j] + b[i] * (k - 1); if (j) dp[ii][j] = max(dp[ii][j], dp[ii - 1][j - 1] + a[i] + b[i] * (j - 1)); } } vector<int> ans; int cur = k; int last = -1; for (int i = n; i >= 1; i--) { if (cur && (dp[i - 1][cur - 1] + a[order[i - 1]] + b[order[i - 1]] * (cur - 1) >= dp[i - 1][cur] + b[order[i - 1]] * (k - 1))) { if (last == -1) last = order[i - 1] + 1; else ans.push_back(order[i - 1] + 1); cur--; } else { ans.push_back(-order[i - 1] - 1); } } reverse(ans.begin(), ans.end()); vector<int> ans2; for (auto q : ans) if (q > 0) ans2.push_back(q); for (auto q : ans) if (q < 0) ans2.push_back(-q), ans2.push_back(q); ans2.push_back(last); cout << ans2.size() << endl; for (auto q : ans2) cout << q << " "; 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; const int INF = 0x3f3f3f3f; const int maxn = 76 * 78; const int maxm = maxn * 2; int cnt; struct Edge { int from, to, cap, flow, cost; Edge() {} Edge(int u, int v, int ca, int fl, int co) : from(u), to(v), cap(ca), flow(fl), cost(co) {} }; struct MCFC { int n, m, s, t; Edge edges[maxm]; int first[maxn]; int next[maxm]; int inq[maxn]; int d[maxm]; int p[maxn]; int a[maxn]; int Q[maxn]; MCFC() {} void init(int n) { this->n = n; memset(first, -1, sizeof(first)); m = 0; } void addEdge(int u, int v, int cap, int cost) { edges[m] = Edge(u, v, cap, 0, cost); next[m] = first[u]; first[u] = m++; edges[m] = Edge(v, u, 0, 0, -cost); next[m] = first[v]; first[v] = m++; } bool bellmanFord(int s, int t, int &flow, long long &cost) { for (int i = 0; i < n; i++) d[i] = INF; memset(inq, false, sizeof(inq)); d[s] = 0; inq[s] = true; p[s] = 0; a[s] = INF; int front, rear; Q[rear = front = 0] = s; while (front <= rear) { int u = Q[front++]; inq[u] = false; for (int i = first[u]; i != -1; i = next[i]) { Edge &e = edges[i]; if (e.cap > e.flow && d[e.to] > d[u] + e.cost) { d[e.to] = d[u] + e.cost; p[e.to] = i; a[e.to] = min(a[u], e.cap - e.flow); if (!inq[e.to]) { Q[++rear] = e.to; inq[e.to] = true; } } } } if (d[t] == INF) return false; flow += a[t]; cost += (long long)d[t] * (long long)a[t]; int u = t; while (u != s) { edges[p[u]].flow += a[t]; edges[p[u] ^ 1].flow -= a[t]; u = edges[p[u]].from; } return true; } int mincostMaxflow(int s, int t, long long &cost) { int flow = 0; cost = 0; while (bellmanFord(s, t, flow, cost)) ; return flow; } } g; int res[120], pos; int A[120], B[120]; int main() { int T, N, K, s, t, u, v, cap, cost, totFlow = 0; long long totCost = 0; scanf("%d", &T); while (T--) { scanf("%d%d", &N, &K); for (int i = 1; i <= N; ++i) scanf("%d %d", &A[i], &B[i]); if (N == K) { printf("%d\n", N); for (int i = 1; i <= N; ++i) { int minp = 1; for (int j = 1; j <= N; ++j) if (B[j] < B[minp]) minp = j; B[minp] = INF; printf("%d ", minp); } printf("\n"); continue; } for (int i = 1; i <= N; ++i) res[i] = 0; pos = K; cnt = 0; s = 0; t = N * K + N + K + 9; g.init(t + 1); for (int i = 1; i <= N; ++i) { for (int j = 1; j <= K; ++j) { u = i, v = N + (i - 1) * K + j, cap = 1, cost = -(A[i] + (j - 1) * B[i]); g.addEdge(u, v, cap, cost); } u = i, v = t, cap = 1, cost = -((K - 1) * B[i]); g.addEdge(u, v, cap, cost); } for (int i = 1; i <= N; ++i) { for (int j = 1; j <= K; ++j) { u = N + (i - 1) * K + j, v = N * K + N + j, cap = 1, cost = 0; g.addEdge(u, v, cap, cost); } } for (int i = 1; i <= N; ++i) { u = s, v = i, cap = 1, cost = 0; g.addEdge(u, v, cap, cost); } for (int j = 1; j <= K; j++) { u = N * K + N + j, v = t, cap = 1, cost = 0; g.addEdge(u, v, cap, cost); } totFlow = g.mincostMaxflow(s, t, totCost); for (int i = 0; i < N * K + N; ++i) { if (g.edges[i << 1].flow == 1) { if (i % (K + 1) + 1 <= K) res[i % (K + 1) + 1] = i / (K + 1) + 1; else res[++pos] = i / (K + 1) + 1; } } printf("%d\n", K + 2 * (N - K)); for (int i = 1; i <= K - 1; ++i) printf("%d ", res[i]); for (int i = K + 1; i <= N; ++i) printf("%d %d ", res[i], -res[i]); printf("%d\n", res[K]); } 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 = 75; int T, n, m, dp[N][N], from[N][N], id[N], flag[N]; struct Ask { int x, y, from; } A[N]; int cmp(Ask x, Ask y) { return x.y < y.y; } int main() { scanf("%d", &T); while (T--) { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) scanf("%d%d", &A[i].x, &A[i].y), A[i].from = i + 1; sort(A, A + n, cmp); memset(dp, 0xcf, sizeof dp); dp[0][0] = 0; for (int i = 0; i < n; i++) for (int j = 0; j <= m; j++) { if (j <= m && dp[i + 1][j + 1] < dp[i][j] + A[i].x + A[i].y * j) { dp[i + 1][j + 1] = dp[i][j] + A[i].x + A[i].y * j; from[i + 1][j + 1] = 0; } if (dp[i + 1][j] < dp[i][j] + A[i].y * (m - 1)) dp[i + 1][j] = dp[i][j], from[i + 1][j] = 1; } int now = m; for (int i = n; i; i--) { if (!from[i][now]) id[now] = A[i - 1].from, flag[i - 1] = 0, now--; else flag[i - 1] = 1; } printf("%d\n", n * 2 - m); for (int i = 1; i < m; i++) printf("%d ", id[i]); for (int i = 0; i < n; i++) if (flag[i]) printf("%d %d ", A[i].from, -A[i].from); printf("%d\n", id[m]); } }
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, T; pair<pair<long long, long long>, int> arr[80]; long long dp[80][80]; int sol[80][80]; vector<int> s1, s2; void rellenar() { s1.clear(); s2.clear(); for (int i = 0; i < 80; i++) { for (int j = 0; j < 80; j++) { dp[i][j] = -(1ll << 60); sol[i][j] = 0; } } dp[0][0] = 0; } bool compa(const pair<pair<long long, long long>, int> &A, const pair<pair<long long, long long>, int> &B) { if (A.first.first == B.first.first) return A.first.second > B.first.second; return A.first.first < B.first.first; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> T; while (T--) { cin >> N >> K; for (int i = 1; i <= N; i++) { cin >> arr[i].first.second >> arr[i].first.first; arr[i].second = i; } sort(arr + 1, arr + (N + 1), compa); rellenar(); for (int i = 1; i <= N; i++) for (int j = 0; j <= K; j++) { if (dp[i - 1][j] >= 0 && dp[i][j] < dp[i - 1][j] + arr[i].first.first * long long(K - 1)) { dp[i][j] = dp[i - 1][j] + arr[i].first.first * long long(j - 1); sol[i][j] = 2; } if (j && dp[i - 1][j - 1] >= 0 && dp[i][j] < dp[i - 1][j - 1] + arr[i].first.first * long long(j - 1) + arr[i].first.second) { dp[i][j] = dp[i - 1][j - 1] + arr[i].first.first * long long(j - 1) + arr[i].first.second; sol[i][j] = 1; } } int k = K; for (int i = N; i > 0; i--) if (sol[i][k] == 1) { s1.push_back(arr[i].second); k--; } else s2.push_back(arr[i].second); reverse(s1.begin(), s1.end()); reverse(s2.begin(), s2.end()); cout << K + 2 * (N - K) << "\n"; for (int i = 0; i < K - 1; i++) cout << s1[i] << " "; for (auto &v : s2) cout << v << " " << -v << " "; cout << s1[K - 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; ifstream in("input.txt"); ofstream out("output.txt"); vector<pair<int, int>> mobs; int n, k; void input() { cin >> n >> k; mobs.resize(n); for (pair<int, int>& x : mobs) cin >> x.first >> x.second; sort(mobs.begin(), mobs.end()); } vector<vector<pair<int, int>>> anss; int best = 0; void solve(vector<pair<int, int>>& cur) { vector<int> table; for (pair<int, int> x : cur) { int p = table.size(); if (table.size() == k) { sort(table.begin(), table.end()); p = 0; } else table.push_back(0); for (int i = 0; i < table.size(); i++) table[i] += x.second; table[p] = x.first; } int sum = 0; for (int x : table) sum += x; if (sum > best) { anss.clear(); anss.push_back(cur); best = sum; } else if (sum == best) anss.push_back(cur); } void calc() { for (int l = k; l <= n;) { vector<pair<int, int>> cur; for (int i = 0; i < l; i++) cur.push_back(mobs[i]); solve(cur); if (!next_permutation(mobs.begin(), mobs.end())) l++; } } int main() { cin.tie(0); cout.tie(0); cin.sync_with_stdio(0); cout.sync_with_stdio(0); ; input(); calc(); cout << best; return 0; cout << "Best value = " << best << '\n'; for (vector<pair<int, int>>& x : anss) { for (pair<int, int> c : x) cout << c.first << '|' << c.second << " "; 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; #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(); 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] = 0; } 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] = 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; %>