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" ] }
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[i] = 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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; class Debugger { public: template <typename T> Debugger &operator<<(const T &v) { cerr << v; return *this; } ~Debugger() { cerr << endl; } }; template <typename T1, typename T2> inline ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << "(" << p.first << ", " << p.second << ")"; } template <typename T, size_t N> inline ostream &operator<<(ostream &os, const array<T, N> &a) { bool first = true; os << "["; for (auto &v : a) { if (!first) os << ", "; os << v; first = false; } os << "]"; return os; } template <typename T> inline ostream &operator<<(ostream &os, const vector<T> &v) { bool first = true; os << "["; for (unsigned int i = 0; i < v.size(); i++) { if (!first) os << ", "; os << v[i]; first = false; } return os << "]"; } template <typename T> inline ostream &operator<<(ostream &os, const set<T> &v) { bool first = true; os << "{"; for (typename set<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii) { if (!first) os << ", "; os << *ii; first = false; } return os << "}"; } template <typename T1, typename T2> inline ostream &operator<<(ostream &os, const map<T1, T2> &v) { bool first = true; os << "["; for (typename map<T1, T2>::const_iterator ii = v.begin(); ii != v.end(); ++ii) { if (!first) os << ", "; os << *ii; first = false; } return os << "]"; } const long long MOD = 1000000000 + 7; const int dx[] = {-1, 1, 0, 0}; const int dy[] = {0, 0, 1, -1}; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long power_mod(long long x, long long y, long long p) { long long ans = 1; x %= p; while (y > 0) { if (y & 1) ans = (ans * x) % p; y >>= 1; x = (x * x) % p; } return ans; } inline long long modInverse(long long n, long long p) { return power_mod(n, p - 2, p); } long long nCrModP(vector<long long> &fac, long long n, long long r, long long p) { if (r == 0) return 1; return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; } template <typename T> void print_vector(const vector<T> &v) { for (int i = (0); i < (((int)(v).size())); i++) { if (i > 0) cout << " "; cout << v[i]; } cout << endl; } int n, m; long long dp[76][76]; bool state[76][76]; pair<pair<int, int>, int> minions[76]; bool update(long long &a, long long b) { if (a == -1 || b > a) { a = b; return true; } return false; } long long calculate(int lastOne) { memset(dp, -1, sizeof(dp)); memset(state, 0, sizeof(state)); dp[0][0] = 0; long long a, b; for (int i = (1); i < (n + 1); i++) { if (i == lastOne) { for (int j = (0); j < (m); j++) dp[i][j] = dp[i - 1][j]; continue; } a = minions[i].first.second; b = minions[i].first.first; for (int j = (0); j < (m); j++) { if (dp[i - 1][j] != -1) { if (update(dp[i][j], dp[i - 1][j] + b * (m - 1))) state[i][j] = false; } if (j > 0 && dp[i - 1][j - 1] != -1) { if (update(dp[i][j], dp[i - 1][j - 1] + a + b * (j - 1))) state[i][j] = true; } } } a = minions[lastOne].first.second; b = minions[lastOne].first.first; return dp[n][m - 1] + a + b * (m - 1); } void solve() { cin >> n >> m; for (int i = (1); i < (n + 1); i++) { cin >> minions[i].first.second >> minions[i].first.first; minions[i].second = i; } sort(minions + 1, minions + n + 1); long long mx = 0; int lastOne = -1; for (int i = (1); i < (n + 1); i++) { long long t = calculate(i); if (t > mx) { mx = t; lastOne = i; } } assert(lastOne != -1); calculate(lastOne); vector<int> vis(n + 1, false); int x = n, y = m - 1; while (x > 0) { if (state[x][y]) { vis[x] = true; y--; } x--; } vector<int> ans; for (int i = (1); i < (n + 1); i++) { if (vis[i] && i != lastOne) ans.push_back(minions[i].second); } for (int i = (1); i < (n + 1); i++) { if (!vis[i] && i != lastOne) { ans.push_back(minions[i].second); ans.push_back(-minions[i].second); } } ans.push_back(minions[lastOne].second); cout << ((int)(ans).size()) << endl; print_vector(ans); } int main() { ios_base::sync_with_stdio(false); srand(time(NULL)); cin.tie(NULL); cout.tie(NULL); int cas; cin >> cas; for (int i = (0); i < (cas); i++) { solve(); } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC target("avx2,avx") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") using namespace std; using ll = long long; using ull = unsigned long long; using i128 = __int128_t; using pii = pair<int, int>; using pll = pair<long long, long long>; constexpr char ln = '\n'; istream& operator>>(istream& is, __int128_t& x) { x = 0; string s; is >> s; int n = int(s.size()), it = 0; if (s[0] == '-') it++; for (; it < n; it++) x = (x * 10 + s[it] - '0'); if (s[0] == '-') x = -x; return is; } ostream& operator<<(ostream& os, __int128_t x) { if (x == 0) return os << 0; if (x < 0) os << '-', x = -x; deque<int> deq; while (x) deq.emplace_front(x % 10), x /= 10; for (int e : deq) os << e; return os; } template <class T1, class T2> ostream& operator<<(ostream& os, const pair<T1, T2>& p) { return os << "(" << p.first << ", " << p.second << ")"; } template <class T> ostream& operator<<(ostream& os, const vector<T>& v) { os << "{"; for (int i = 0; i < int(v.size()); i++) { if (i) os << ", "; os << v[i]; } return os << "}"; } template <class Container> inline int SZ(Container& v) { return int(v.size()); } template <class T> inline void UNIQUE(vector<T>& v) { v.erase(unique(v.begin(), v.end()), v.end()); } template <class T1, class T2> inline bool chmax(T1& a, T2 b) { if (a < b) { a = b; return true; } return false; } template <class T1, class T2> inline bool chmin(T1& a, T2 b) { if (a > b) { a = b; return true; } return false; } inline int topbit(ull x) { return x == 0 ? -1 : 63 - __builtin_clzll(x); } inline int botbit(ull x) { return x == 0 ? 64 : __builtin_ctzll(x); } inline int popcount(ull x) { return __builtin_popcountll(x); } inline int kthbit(ull x, int k) { return (x >> k) & 1; } inline constexpr long long TEN(int x) { return x == 0 ? 1 : TEN(x - 1) * 10; } inline void print() { cout << "\n"; } template <class T> inline void print(const vector<T>& v) { for (int i = 0; i < int(v.size()); i++) { if (i) cout << " "; cout << v[i]; } print(); } template <class T, class... Args> inline void print(const T& x, const Args&... args) { cout << x << " "; print(args...); } struct fast_ios { fast_ios() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; constexpr ll INF = 1e18; void solve() { int N, K; cin >> N >> K; vector<ll> A(N), B(N); for (int i = 0; i < (N); i++) cin >> A[i] >> B[i]; vector<int> idx(N); iota((idx).begin(), (idx).end(), 0); sort((idx).begin(), (idx).end(), [&](int i, int j) { return B[i] < B[j]; }); vector<vector<ll>> dp(N + 1, vector<ll>(K + 1, -INF)); vector<vector<int>> pre(N + 1, vector<int>(K + 1)); dp[0][0] = 0; for (int i = 0; i < (N); i++) for (int j = 0; j < (K + 1); j++) { int u = idx[i]; if (chmax(dp[i + 1][j], dp[i][j] + B[u] * (K - 1))) { pre[i + 1][j] = j; } if (j < K) { if (chmax(dp[i + 1][j + 1], dp[i][j] + A[u] + B[u] * j)) { pre[i + 1][j + 1] = j; } } } vector<int> C, D; { int k = K; for (int n = N; n > 0; n--) { int pk = pre[n][k]; if (pk == k) { D.emplace_back(idx[n - 1]); } else { C.emplace_back(idx[n - 1]); } k = pk; } } reverse((C).begin(), (C).end()); cout << SZ(C) + SZ(D) * 2 << ln; for (int i = 0; i < (SZ(C) - 1); i++) cout << C[i] + 1 << " "; for (auto e : D) cout << e + 1 << " " << -(e + 1) << " "; cout << C.back() + 1 << ln; } int main() { 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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using li = long long; using ld = long double; using pii = pair<int, int>; using vi = vector<int>; using matr = vector<vi>; const int INF = 1e9 + 13; vector<int> hungary(vector<vector<int>> a) { int n = a.size() - 1; int m = n; vector<int> u(n + 1), v(m + 1), p(m + 1), way(m + 1); for (int i = 1; i <= n; ++i) { p[0] = i; int j0 = 0; vector<int> minv(m + 1, INF); vector<char> used(m + 1, false); do { used[j0] = true; int i0 = p[j0], delta = INF, j1; 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); } vector<int> ans(n + 1); for (int j = 1; j <= m; ++j) ans[j] = p[j]; return ans; } void solve() { int n, k; cin >> n >> k; vector<int> x(n), y(n); for (int i = 0; i < n; i++) { cin >> x[i] >> y[i]; } vector<vector<int>> a(n + 1, vector<int>(n + 1, 0)); for (int i = 0; i < n; i++) { for (int j = 0; j < k - 1; j++) { a[i + 1][j + 1] = x[i] + y[i] * j; } for (int j = k - 1; j < n - 1; j++) { a[i + 1][j + 1] = y[i] * (k - 1); } a[i + 1][n] = x[i] + y[i] * (k - 1); } for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) a[i][j] = -a[i][j]; } vector<int> res = hungary(a); vector<int> ans; for (int i = 0; i < k - 1; i++) { ans.push_back(res[i + 1]); } for (int i = k - 1; i < n - 1; i++) { ans.push_back(res[i + 1]); ans.push_back(-res[i + 1]); } ans.push_back(res[n]); cout << ans.size() << endl; for (auto x : ans) cout << x << ' '; cout << endl; } int main() { int t = 1; cin >> t; while (t--) solve(); }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; void read(long long &x) { char ch = getchar(); x = 0; while (!isdigit(ch)) ch = getchar(); while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar(); } const long long N = 77; long long n, k, f[N][N], g[N][N]; pair<long long, pair<long long, long long> > a[N]; vector<long long> res, ans; signed main() { long long T; read(T); while (T--) { read(n), read(k); k = min(k, n); for (long long i = 1; i <= n; ++i) read(a[i].second.first), read(a[i].first), a[i].second.second = i; if (k == 1) { puts("1"); long long w = 0; for (long long i = 1; i <= n; ++i) if (a[i].second.first > a[w].second.first) w = i; printf("%lld\n", w); continue; } sort(a + 1, a + n + 1); memset(f, 0xcf, sizeof(f)); f[0][0] = 0; for (long long i = 1; i <= n; ++i) for (long long j = 0; j <= i && j <= k; ++j) { if (j && f[i][j] < f[i - 1][j - 1] + a[i].second.first + a[i].first * (k - 1)) f[i][j] = f[i - 1][j - 1] + a[i].second.first + a[i].first * (j - 1), g[i][j] = 1; if (f[i][j] < f[i - 1][j] + a[i].first * (k - 1)) f[i][j] = f[i - 1][j] + a[i].first * (k - 1), g[i][j] = 0; } res.clear(); ans.clear(); for (long long i = n, j = k; i; --i) { if (g[i][j]) res.push_back(a[i].second.second), --j; else ans.push_back(a[i].second.second); } reverse(res.begin(), res.end()); printf("%lld\n", (long long)res.size() + ans.size() * 2); for (long long i = 0; i < k - 1; ++i) printf("%lld ", res[i]); for (long long i : ans) printf("%lld %lld ", i, -i); printf("%lld\n", res[k - 1]); } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
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 = 160; 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 << 1], 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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename T, typename S> inline void chkmin(T &a, const S &b) { a = a < b ? a : b; } template <typename T, typename S> inline void chkmax(T &a, const S &b) { a = a > b ? a : b; } const int MAXN = 80; struct Data { int a, b, id; } dat[MAXN]; int f[MAXN][MAXN], g[MAXN][MAXN], ans[MAXN], T, n, K; bool cmp(const Data &a, const Data &b) { return a.b < b.b; } int main() { for (scanf("%d", &T); T--;) { scanf("%d%d", &n, &K); for (int i = 1; i <= n; i++) { scanf("%d%d", &dat[i].a, &dat[i].b); dat[i].id = i; } sort(dat + 1, dat + 1 + n, cmp); memset(f, 0xc0, sizeof(f)); f[0][0] = 0; memset(g, 0, sizeof(g)); memset(ans, 0, sizeof(ans)); for (int i = 1; i <= n; i++) { int a = dat[i].a, b = dat[i].b; for (int j = 1; j <= i && j <= K; j++) { int x = f[i - 1][j - 1] + a + b * (j - 1), y = f[i - 1][j] + b * (K - 1); f[i][j] = max(x, y); g[i][j] = x >= y ? 0 : 1; } f[i][0] = f[i - 1][0] + b * (K - 1); g[i][0] = 1; } for (int i = n, j = K; i > 0; i--) { if (g[i][j]) ans[i] = 1; else --j; } printf("%d\n", n + n - K); int cnt = 0; for (int i = 1; i <= n; i++) if (!ans[i] && ++cnt < K) printf("%d ", dat[i].id); assert(cnt == K); for (int i = 1; i <= n; i++) if (ans[i]) printf("%d %d ", dat[i].id, -dat[i].id); for (int i = n; i > 0; i--) if (!ans[i]) { printf("%d\n", dat[i].id); 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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T> using vc = vector<T>; template <class T> using vvc = vc<vc<T>>; template <class T> void mkuni(vector<T>& v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); } template <class T> void print(T x, int suc = 1) { cout << x; if (suc == 1) cout << '\n'; else cout << ' '; } template <class T> void print(const vector<T>& v, int suc = 1) { for (int i = 0; i < v.size(); i++) print(v[i], i == (int)(v.size()) - 1 ? suc : 2); } const int INF = 0x3f3f3f3f; const int maxn = 80; int n; int cost[maxn][maxn]; int lx[maxn], ly[maxn], match[maxn], slack[maxn]; int pprev[maxn]; bool vy[maxn]; void augment(int root) { memset(vy, 0, sizeof(vy)); memset(slack, 0x3f, sizeof(slack)); int py; match[py = 0] = root; do { vy[py] = true; int x = match[py], yy; int delta = INF; for (int y = 1; y <= n; y++) { if (!vy[y]) { if (lx[x] + ly[y] - cost[x][y] < slack[y]) slack[y] = lx[x] + ly[y] - cost[x][y], pprev[y] = py; if (slack[y] < delta) delta = slack[y], yy = y; } } for (int y = 0; y <= n; y++) { if (vy[y]) lx[match[y]] -= delta, ly[y] += delta; else slack[y] -= delta; } py = yy; } while (match[py] != -1); do { int pre = pprev[py]; match[py] = match[pre], py = pre; } while (py); } int KM() { for (int i = 1; i <= n; i++) { lx[i] = ly[i] = 0; match[i] = -1; for (int j = 1; j <= n; j++) lx[i] = max(lx[i], cost[i][j]); } int ans = 0; for (int root = 1; root <= n; root++) augment(root); for (int i = 1; i <= n; i++) ans += lx[i], ans += ly[i]; return ans; } int main() { int T; cin >> T; while (T--) { int k; cin >> n >> k; for (int i = 1; i <= n; i++) { int a, b; cin >> a >> b; for (int j = 1; j <= n; j++) { if (j <= k) cost[i][j] = (j - 1) * b + a; else cost[i][j] = (k - 1) * b; } } KM(); vector<int> ans; for (int i = 1; i < k; i++) ans.push_back(match[i]); for (int i = k + 1; i <= n; i++) { ans.push_back(match[i]); ans.push_back(-match[i]); } ans.push_back(match[k]); print(ans.size()); 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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; 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 f == 1 ? x : -x; } inline void print(int x) { if (x < 0) putchar('-'), x = ~x + 1; if (x > 10) print(x / 10); putchar((x % 10) | 48); } int dp[80][80][2]; int T; int n, k; struct node { pair<int, int> s; int id; bool operator<(const node &x) const { return s > x.s; } } a[100]; int m; vector<int> op1, op2, op3; void dfs(int x, int j, int op) { if (x == n + 1) return; if (op) { if (dp[x][j][op] == dp[x + 1][j][op] + a[x].s.first * (k - 1)) { op2.push_back(x); dfs(x + 1, j, op); } else if (dp[x][j][op] == dp[x + 1][j][0] + a[x].s.first * (k - 1) + a[x].s.second) { op3.push_back(x); dfs(x + 1, j, 0); } else if (j && dp[x][j][op] == dp[x + 1][j - 1][op] + a[x].s.first * (j - 1) + a[x].s.second) { dfs(x + 1, j - 1, op); op1.push_back(x); } } else { if (dp[x][j][0] == dp[x + 1][j][0] + a[x].s.first * (k - 1)) { op2.push_back(x); dfs(x + 1, j, 0); } else if (j && dp[x][j][0] == dp[x + 1][j - 1][0] + a[x].s.first * (j - 1) + a[x].s.second) { dfs(x + 1, j - 1, 0); op1.push_back(x); } } } vector<int> ans; int main() { T = read(); while (T--) { op1.clear(), op2.clear(), op3.clear(); n = read(), k = read(); for (int i = 1; i <= n; ++i) a[i].s.second = read(), a[i].s.first = read(), a[i].id = i; sort(a + 1, a + n + 1); memset(dp, -0x3f, sizeof(dp)); dp[n + 1][0][0] = 0; int ans = 0; for (int i = n; i >= 1; --i) { for (int j = 0; j <= (n - i + 1) && j < k; ++j) { dp[i][j][0] = dp[i + 1][j][0] + a[i].s.first * (k - 1); dp[i][j][1] = max(dp[i + 1][j][1] + a[i].s.first * (k - 1), dp[i + 1][j][0] + a[i].s.first * (k - 1) + a[i].s.second); if (j) { dp[i][j][0] = max(dp[i][j][0], dp[i + 1][j - 1][0] + a[i].s.first * (j - 1) + a[i].s.second); dp[i][j][1] = max(dp[i][j][1], dp[i + 1][j - 1][1] + a[i].s.first * (j - 1) + a[i].s.second); } } } dfs(1, k - 1, 1); printf("%d\n", op1.size() + op2.size() * 2 + 1); for (int i = 0; i < op1.size(); ++i) { printf("%d ", a[op1[i]].id); } for (int i = 0; i < op2.size(); ++i) printf("%d %d ", a[op2[i]].id, -a[op2[i]].id); printf("%d\n", a[op3[0]].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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; pair<pair<int, int>, int> ba[102]; int g[102][102], dp[102][102]; int ans[102]; vector<int> ansvec; bool compba(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) { if (a.first.first != b.first.first) return a.first.first < b.first.first; return a.first.second < b.first.second; } int main() { int t; scanf("%d", &t); while (t--) { int n, k; scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) { int ai, bi; scanf("%d%d", &ai, &bi); ba[i] = {{bi, ai}, i}; } sort(ba + 1, ba + n + 1, compba); for (int i = 0; i <= n; i++) for (int j = 0; j <= k; j++) dp[i][j] = -1; for (int i = 0; i <= n; i++) for (int j = 0; j <= k; j++) g[i][j] = -1; dp[0][0] = 0; for (int i = 1; i <= n; i++) { for (int j = 0; j <= min(i - 1, k); j++) { int a0 = (k - 1) * ba[i].first.first; int a1 = min(k - 1, j) * ba[i].first.first + ba[i].first.second; if (dp[i][j] < dp[i - 1][j] + a0) { dp[i][j] = dp[i - 1][j] + a0; g[i][j] = 0; } if (j + 1 <= k) { if (dp[i][j + 1] < dp[i - 1][j] + a1) { dp[i][j + 1] = dp[i - 1][j] + a1; g[i][j + 1] = 1; } } } } int fi = n, fj = k; int lk = -1; while (fi) { int ai = ba[fi].second; if (g[fi][fj]) { ans[ai] = fj; if (fj == k) lk = ai; fj--; } else ans[ai] = 0; fi--; } ansvec.clear(); ansvec.assign(k - 1, 0); for (int i = 1; i <= n; i++) { if (ans[i]) { if (ans[i] < k) ansvec[ans[i] - 1] = i; } } for (int i = 1; i <= n; i++) { if (!ans[i]) { ansvec.push_back(i); ansvec.push_back(-i); } } ansvec.push_back(lk); printf("%d\n", ansvec.size()); for (auto it : ansvec) printf("%d ", it); printf("\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" ] }
CORRECT
cpp
#include <bits/stdc++.h> template <typename T> bool ckmax(T& a, T b) { return a < b ? a = b, 1 : 0; } template <typename T> bool ckmin(T& a, T b) { return b < a ? a = b, 1 : 0; } const int MN = 80; int N, RK, T, dp[MN][MN], ans; bool u[MN], pr[MN][MN]; struct minion { public: int a, b, id; void in() { scanf("%d%d", &a, &b); } bool operator<(minion o) const { return b < o.b; } } a[MN]; std::vector<int> seq; int main(void) { scanf("%d", &T); for (; T--;) { scanf("%d%d", &N, &RK); for (int i = 0; i < N; ++i) a[i].in(), a[i].id = i + 1; std::sort(a, a + N); memset(dp, -1, sizeof dp); dp[0][0] = 0; for (int i = 0; i < N; ++i) for (int j = 0; j <= RK; ++j) if (~dp[i][j]) { if (ckmax(dp[i + 1][j], dp[i][j] + (RK - 1) * a[i].b)) pr[i + 1][j] = 0; if (j + 1 <= RK && ckmax(dp[i + 1][j + 1], dp[i][j] + a[i].a + j * a[i].b)) pr[i + 1][j + 1] = 1; } { seq.clear(); ans = dp[N][RK]; memset(u, 0, N * sizeof u[0]); int k = RK; for (int i = N; i > 0; --i) { if (pr[i][k]) u[i - 1] = 1, seq.push_back(a[i - 1].id), --k; } assert(!k); std::reverse(seq.begin(), seq.end()); int v = seq.back(); seq.pop_back(); for (int i = 0; i < N; ++i) if (!u[i]) seq.push_back(a[i].id), seq.push_back(-a[i].id); seq.push_back(v); } for (int K = 1; K < RK; ++K) { memset(dp, -1, sizeof dp); dp[0][0] = 0; for (int i = 0; i < N; ++i) for (int j = 0; j <= K; ++j) if (~dp[i][j]) { if (ckmax(dp[i + 1][j], dp[i][j] + K * a[i].b)) pr[i + 1][j] = 0; if (j + 1 <= K && ckmax(dp[i + 1][j + 1], dp[i][j] + a[i].a + j * a[i].b)) pr[i + 1][j] = 1; } if (dp[N][K] <= ans) continue; seq.clear(); ans = dp[N][K]; memset(u, 0, N * sizeof u[0]); int k = K; for (int i = N; i >= 0; --i) { if (pr[i][k]) u[i - 1] = 1, seq.push_back(a[i - 1].id), --k; } assert(!k); std::reverse(seq.begin(), seq.end()); for (int i = 0; i < N; ++i) if (!u[i]) seq.push_back(a[i].id), seq.push_back(-a[i].id); } printf("%d\n", seq.size()); for (int i = 0; i < seq.size(); ++i) printf("%d%c", seq[i], " \n"[i + 1 == seq.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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline int add(int _a, int _b) { if (_a < 0) { _a += 1000000007; } if (_b < 0) { _b += 1000000007; } if (_a + _b >= 1000000007) { return _a + _b - 1000000007; } return _a + _b; } inline int mul(int _a, int _b) { if (_a < 0) { _a += 1000000007; } if (_b < 0) { _b += 1000000007; } return ((long long int)((long long int)_a * (long long int)_b)) % 1000000007; } const int N = 75; namespace wm { bool vis[N + 5]; int U[N + 5], V[N + 5], P[N + 5], way[N + 5], minv[N + 5], match[N + 5], ar[N + 5][N + 5]; int hungarian(int n, int m, int mat[N + 5][N + 5], int flag) { memset(U, 0, sizeof(U)), memset(V, 0, sizeof(V)), memset(P, 0, sizeof(P)), memset(ar, 0, sizeof(ar)), memset(way, 0, sizeof(way)); int i, j; for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { ar[i][j] = mat[i][j]; if (flag == +1) ar[i][j] = -ar[i][j]; } } if (n > m) m = n; int a, b, c, d, r, w; for (i = 1; i <= n; i++) { P[0] = i, b = 0; for (j = 0; j < m + 1; j++) minv[j] = INT_MAX, vis[j] = 0; do { vis[b] = 1; a = P[b], d = 0, w = INT_MAX; for (j = 1; j <= m; j++) { if (!vis[j]) { r = ar[a][j] - U[a] - V[j]; if (r < minv[j]) minv[j] = r, way[j] = b; if (minv[j] < w) w = minv[j], d = j; } } for (j = 0; j < m + 1; j++) { if (vis[j]) U[P[j]] += w, V[j] -= w; else minv[j] -= w; } b = d; } while (P[b] != 0); do { d = way[b]; P[b] = P[d], b = d; } while (b != 0); } for (j = 1; j <= m; j++) match[j] = P[j]; return (flag == +1) ? V[0] : -V[0]; } } // namespace wm int n, A[N + 5], B[N + 5], choice, mat[N + 5][N + 5]; void solve() { int i, j; for (i = 0; i < n; i++) for (j = 0; j < n; j++) { if (j < choice - 1) mat[i + 1][j + 1] = A[i] + j * B[i]; else if (j >= choice - 1 && j < n - 1) mat[i + 1][j + 1] = (choice - 1) * B[i]; else mat[i + 1][j + 1] = A[i] + (choice - 1) * B[i]; } int sol = wm ::hungarian(n, n, mat, +1); printf("%d", choice - 1 + 2 * (n - choice) + 1), puts(""); vector<int> vec; for (i = 1; i <= choice - 1; i++) vec.push_back(wm ::match[i]); for (i = choice; i <= n - 1; ++i) vec.push_back(wm ::match[i]), vec.push_back(-wm ::match[i]); vec.push_back(wm ::match[n]); for (i = 0; i < vec.size(); i++) { if (i) printf(" "); printf("%d", vec[i]); } puts(""); } int main() { int cs, ts; scanf("%d", &ts); for (cs = 0; cs < ts; cs++) { int i, j; scanf("%d %d", &n, &choice); for (i = 0; i < n; i++) scanf("%d %d", &A[i], &B[i]); solve(); } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; void solve() { int n, k; cin >> n >> k; vector<int> a(n), b(n); for (int i = 0; i < n; ++i) { cin >> a[i] >> b[i]; } vector<vector<long long>> dp(n + 1, vector<long long>(k + 2, 0)); vector<vector<int>> p(n + 1, vector<int>(k + 2)); vector<int> id(n); iota(id.begin(), id.end(), 0); sort(id.begin(), id.end(), [&](int i, int j) { return b[i] < b[j]; }); auto rlx = [&](int i, int j, long long val, int tk) { if (dp[i][j] < val) { dp[i][j] = val; p[i][j] = tk; } }; for (int i = 0; i < n; ++i) { int ii = id[i]; for (int j = 0; j <= min(i, k); ++j) { rlx(i + 1, j, dp[i][j] + (k - 1) * b[ii], 0); rlx(i + 1, j + 1, dp[i][j] + a[ii] + j * b[ii], 1); } } vector<int> u, v; for (int i = n, j = k; i >= 1; j -= p[i][j], --i) { if (p[i][j]) { u.push_back(id[i - 1]); } else { v.push_back(id[i - 1]); } } reverse(u.begin(), u.end()); cout << k + 2 * (n - k) << '\n'; for (int i = 0; i < k - 1; ++i) { cout << u[i] + 1 << ' '; } for (int i = 0; i < n - k; ++i) { cout << v[i] + 1 << ' ' << -(v[i] + 1) << ' '; } cout << u[k - 1] + 1; } int main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); int t; cin >> t; while (t--) { solve(); 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" ] }
CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC target("avx2") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") using namespace std; template <typename T> T sqr(T a) { return a * a; } template <typename T> int sign(T a) { if (a == 0) return 0; return (a > 0 ? 1 : -1); } template <typename T> bool uax(T& a, const T b) { if (a < b) { a = b; return true; } return false; } template <typename T> bool uin(T& a, const T b) { if (a > b) { a = b; return true; } return false; } template <typename T1, typename T2> istream& operator>>(istream& in, pair<T1, T2>& p) { in >> p.first >> p.second; return in; } template <typename T1, typename T2> ostream& operator<<(ostream& out, pair<T1, T2>& p) { out << p.first << ' ' << p.second; return out; } mt19937_64 rnd(0); const int LOG = 64; const int N = (int)1e5 + 7; const int MAXN = (int)4e5 + 7; const int MOD = (int)998244353; const int INF = (int)2e9 + 7; const int CONST = 450; const long long LINF = (int64_t)1e15; const long double PI = 3.1415926535897932384626433832795; const long double EPS = 1; struct Monster { int a, b, ind; Monster(){}; Monster(int a, int b, int ind) : a(a), b(b), ind(ind){}; }; bool operator<(const Monster& a, const Monster& b) { return a.b < b.b; } long long dp[100][100]; int par[100][100]; int n, k; Monster mon[N]; void solve() { cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> mon[i].a >> mon[i].b; mon[i].ind = i; } sort(mon + 1, mon + n + 1); for (int i = 0; i < 100; i++) { fill(dp[i], dp[i] + 100, -LINF); fill(par[i], par[i] + 100, -1); } dp[0][0] = 0; for (int i = 1; i <= n; i++) { par[i][0] = 0; dp[i][0] = dp[i - 1][0] + (k - 1) * mon[i].b; for (int j = 1; j <= k; j++) { if (dp[i - 1][j - 1] == -LINF && dp[i - 1][j] == -LINF) continue; if (dp[i - 1][j] == -LINF) { dp[i][j] = dp[i - 1][j - 1] + (j - 1) * mon[i].b + mon[i].a; par[i][j] = 1; continue; } if (dp[i - 1][j - 1] == -LINF) { dp[i][j] = dp[i - 1][j] + (k - 1) * mon[i].b; par[i][j] = 0; continue; } long long val1 = dp[i - 1][j - 1] + (j - 1) * mon[i].b + mon[i].a; long long val2 = dp[i - 1][j] + (k - 1) * mon[i].b; dp[i][j] = max(val1, val2); par[i][j] = (val1 >= val2 ? 1 : 0); } } int pos = n, cnt = k; vector<int> stay, rem; while (pos) { if (par[pos][cnt] == 0) { rem.push_back(mon[pos].ind); pos--; } else { stay.push_back(mon[pos].ind); pos--, cnt--; } } reverse(stay.begin(), stay.end()), reverse(rem.begin(), rem.end()); cout << (int)stay.size() + 2 * (int)rem.size() << "\n"; for (int i = 0; i < (int)stay.size() - 1; i++) cout << stay[i] << ' '; for (int x : rem) cout << x << ' ' << -x << ' '; cout << stay.back() << "\n"; } int main() { double start = clock(); cout << fixed << setprecision(20); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); rnd.seed(time(0)); int t = 1; cin >> t; for (int i = 0; i < t; i++) { solve(); } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename A, typename B> ostream& operator<<(ostream& os, const pair<A, B>& x) { return os << "(" << x.first << "," << x.second << ")"; } int main() { int T; scanf("%d", &T); while (T--) { int n, k; scanf("%d%d", &n, &k); vector<pair<pair<int, int>, int>> M(n); long long offset = 0; for (int i = 0; i < n; ++i) { scanf("%d%d", &M[i].first.second, &M[i].first.first); offset += (k - 1) * M[i].first.second; M[i].second = i + 1; } sort(M.begin(), M.end()); const long long INF = 1LL << 60; vector<vector<vector<pair<long long, int>>>> dp( n + 1, vector<vector<pair<long long, int>>>( k + 1, vector<pair<long long, int>>(2, make_pair(-INF, -1)))); dp[0][0][0] = make_pair(0, 0); for (int i = 0; i < n; ++i) { for (int j = 0; j < k; ++j) { dp[i + 1][j][0] = max(dp[i + 1][j][0], make_pair(dp[i][j][0].first, 0)); dp[i + 1][j][1] = max(dp[i + 1][j][0], make_pair(dp[i][j][1].first, 0)); dp[i + 1][j][1] = max(dp[i + 1][j][1], make_pair(dp[i][j][0].first + M[i].first.second, 1)); if (j + 1 < k) { for (int v = 0; v < 2; ++v) { dp[i + 1][j + 1][v] = max(dp[i + 1][j + 1][v], make_pair(dp[i][j][v].first + M[i].first.second - (k - 1 - j) * M[i].first.first, 2)); } } } } if (false) cerr << "dp[n][k - 1][1]" << "=" << dp[n][k - 1][1] << endl; int j = k - 1, c = 1; int las = -1; vector<int> pick; for (int i = n; i >= 1; --i) { int act = dp[i][j][c].second; assert(act != -1); if (act == 0) { } else if (act == 1) { assert(c == 1); las = i - 1; c = 0; } else { pick.push_back(i - 1); --j; } } assert(j == 0); assert(c == 0); reverse(pick.begin(), pick.end()); assert(las != -1); vector<bool> used(n + 1); vector<int> ans; for (int x : pick) { ans.push_back(M[x].second); used[M[x].second] = true; } used[M[las].second] = true; for (int i = 1; i <= n; ++i) { if (used[i]) continue; ans.push_back(i); ans.push_back(-i); } ans.push_back(M[las].second); printf("%d\n", ans.size()); for (int x : ans) printf("%d ", x); 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" ] }
CORRECT
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.util.stream.IntStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Arrays; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.IOException; import java.util.stream.Collectors; import java.util.ArrayList; import java.util.List; import java.util.stream.Stream; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.Comparator; import java.util.Collections; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author out_of_the_box */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); FSummoningMinions solver = new FSummoningMinions(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class FSummoningMinions { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); int k = in.nextInt(); int[] a = new int[n]; int[] b = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); b[i] = in.nextInt(); } int[][] dp = new int[n][k + 1]; int[] indexes = new int[n]; for (int i = 0; i < n; i++) { indexes[i] = i; } indexes = Arrays.stream(indexes).boxed().sorted(Comparator.comparingInt(ind -> b[ind])).mapToInt(i -> i) .toArray(); boolean[][] select = new boolean[n][k + 1]; dp[0][0] = (k - 1) * b[indexes[0]]; dp[0][1] = a[indexes[0]]; select[0][1] = true; for (int i = 1; i < n; i++) { dp[i][0] = dp[i - 1][0] + (k - 1) * (b[indexes[i]]); } for (int i = 1; i < n; i++) { int maxJ = Math.min(i + 1, k); for (int j = 1; j <= maxJ; j++) { int first = dp[i - 1][j - 1] + a[indexes[i]] + (j - 1) * b[indexes[i]]; int second = (j <= i) ? dp[i - 1][j] + (k - 1) * b[indexes[i]] : (-1); if (first >= second) { dp[i][j] = first; select[i][j] = true; } else { dp[i][j] = second; } } } List<Integer> selected = new ArrayList<>(); boolean[] selectedFlag = new boolean[n]; int tbs = k; for (int i = n - 1; i >= 0; i--) { if (tbs == 0) break; if (select[i][tbs]) { selected.add(indexes[i]); selectedFlag[indexes[i]] = true; tbs--; } } List<Integer> listA = Arrays.stream(a).boxed().collect(Collectors.toList()); List<Integer> listB = Arrays.stream(b).boxed().collect(Collectors.toList()); String message = String.format("Selected size mismatch. size = %d, k = %d, n = %d, a = %s, b = %s", selected.size(), k, n, listA, listB); MiscUtility.assertion(selected.size() == k, message); Collections.reverse(selected); int m = k + (n - k) * 2; out.println(m); for (int i = 0; i < (k - 1); i++) { out.print((selected.get(i) + 1) + " "); } for (int i = 0; i < n; i++) { if (!selectedFlag[i]) { out.print((i + 1) + " "); out.print(-(i + 1) + " "); } } out.print(selected.get(k - 1) + 1); out.println(); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void println() { writer.println(); } public void close() { writer.close(); } public void print(int i) { writer.print(i); } public void println(int i) { writer.println(i); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String nextString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return nextString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class MiscUtility { public static void assertion(boolean condition, String message) { if (!condition) { throw new RuntimeException("Assertion failed. " + message); } } } }
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" ] }
CORRECT
java
import java.io.*; import java.util.*; public class Main extends PrintWriter { private void solve() { int T = sc.nextInt(); for(int tc = 1; tc <= T; tc++) { n = sc.nextInt(); k = sc.nextInt(); minions = new long[n][]; for(int i = 0; i < n; i++) { minions[i] = new long[] {sc.nextLong(), sc.nextLong(), i+1}; } Arrays.sort(minions, Comparator.comparing(arr->arr[1])); memo = new Long[n][k+1]; long[][] dp = new long[n+1][k+1]; int[][] choice = new int[n+1][k+1]; for(int i = 0; i <= n; i++) Arrays.fill(dp[i], -1L<<60); dp[n][k] = 0L; for(int i = n-1; i >= 0; i--) { for(int j = k; j >= 0; j--) { if(j+1 <= k) { long cost = minions[i][0] + j*minions[i][1] + dp[i+1][j+1]; if(cost > dp[i][j]) { dp[i][j] = cost; choice[i][j] = 0; } } long cost = (k-1)*minions[i][1] + dp[i+1][j]; if(cost > dp[i][j]) { dp[i][j] = cost; choice[i][j] = 1; } } } int idx = 0; int sz = 0; ArrayDeque<Integer> ans1 = new ArrayDeque<Integer>(); ArrayDeque<Integer> ans2 = new ArrayDeque<Integer>(); int last = -1; while(idx < n) { int num = (int)minions[idx][2]; if(choice[idx][sz] == 0) { sz++; if(sz == k) { last = num; } else { ans1.add(num); } } else { ans2.add(num); ans2.add(-num); } idx++; } println(ans1.size() + ans2.size() + 1); for(int x : ans1) { print(x + " "); } for(int x : ans2) { print(x + " "); } println(last); } } int n; int k; long[][] minions; Long[][] memo; long doit(int i, int j) { if(i == n && j == k) return 0L; else if(j > k || i == n) return -1L<<60; else if(memo[i][j] == null) { long res = 0L; res = Math.max(res, minions[i][0] + j*minions[i][1] + doit(i+1,j+1)); res = Math.max(res, (k-1)*minions[i][1] + doit(i+1,j)); memo[i][j] = res; } return memo[i][j]; } // Main() throws FileNotFoundException { super(new File("output.txt")); } // InputReader sc = new InputReader(new FileInputStream("test_input.txt")); Main() { super(System.out); } InputReader sc = new InputReader(System.in); static class InputReader { InputReader(InputStream in) { this.in = in; } InputStream in; private byte[] buf = new byte[16384]; private int curChar; private int numChars; public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = in.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } public static void main(String[] $) { new Thread(null, new Runnable() { public void run() { long start = System.nanoTime(); try {Main solution = new Main(); solution.solve(); solution.close();} catch (Exception e) {e.printStackTrace(); System.exit(1);} System.err.println((System.nanoTime()-start)/1E9); } }, "1", 1 << 27).start(); } }
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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline int read() { register int t = 0; register char v = getchar(); while (v < '0') v = getchar(); while (v >= '0') t = (t << 3) + (t << 1) + v - 48, v = getchar(); return t; } int f[76][76], n, m, t, v[76][76], ans[76], cnt, pos; struct node { int x, y, id; bool operator<(const node a) const { return y < a.y; }; } p[76]; inline void dfs(register int x, register int y) { if (!x) return; if (v[x][y]) ans[x] = 1, --y; dfs(x - 1, y); } int main() { t = read(); while (t--) { n = read(), m = read(); for (register int i = 1; i <= n; ++i) p[i].x = read(), p[i].y = read(), p[i].id = i; sort(p + 1, p + n + 1), memset(v, 0, sizeof(v)), memset(f, -0x3f, sizeof(f)); f[0][0] = 0; for (register int i = 1; i <= n; ++i) for (register int j = 0; j <= min(i, m); ++j) { f[i][j] = f[i - 1][j] + p[i].y * (m - 1); if (j && f[i - 1][j - 1] + p[i].x + p[i].y * (j - 1) > f[i][j]) f[i][j] = f[i - 1][j - 1] + p[i].x + p[i].y * (j - 1), v[i][j] = 1; } printf("%d\n", m + (n - m) * 2); cnt = 0; memset(ans, 0, sizeof(ans)), dfs(n, m); for (register int i = 1; i <= n; ++i) if (ans[i] && (++cnt != m)) printf("%d ", p[i].id); else if (ans[i]) pos = i; for (register int i = 1; i <= n; ++i) if (!ans[i]) printf("%d %d ", p[i].id, -p[i].id); printf("%d\n", p[pos].id); } }
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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 7; const int inf = INT_MAX; const long long inff = 1e18; const long long mod = 1e9 + 7; int dp[77][77]; int a[77], b[77]; int used[77]; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { int n, k; cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i] >> b[i]; if (k == 1) { int att = 0, mx = 0; for (int i = 1; i <= n; i++) if (a[i] > mx) mx = a[i], att = i; cout << 1 << '\n' << att << '\n'; } else { memset(dp, 0, sizeof(dp)); memset(used, 0, sizeof(used)); for (int i = 0; i <= n; i++) for (int j = 1; j <= k; j++) dp[i][j] = -inf; dp[0][0] = 0; vector<int> v; for (int i = 1; i <= n; i++) v.push_back(i); sort((v).begin(), (v).end(), [&](int x, int y) { return b[x] < b[y]; }); for (int i = 1; i <= n; i++) for (int j = 1; j <= min(i, k); j++) { if (j - 1 <= i) dp[i][j] = dp[i - 1][j]; dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + (a[v[i - 1]] - (k - j) * b[v[i - 1]])); } vector<int> vv; int nowk = k; for (int i = n; i >= 1; i--) { if (nowk > 0 && dp[i - 1][nowk - 1] + (a[v[i - 1]] - (k - nowk) * b[v[i - 1]]) == dp[i][nowk]) { used[v[i - 1]] = 1; vv.push_back(v[i - 1]); nowk--; } } reverse((vv).begin(), (vv).end()); cout << 2 * n - k << '\n'; for (int i = 0; i < k - 1; i++) cout << vv[i] << ' '; for (int i = 1; i <= n; i++) if (!used[i]) cout << i << ' ' << -i << ' '; cout << vv[k - 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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { int n, k; cin >> n >> k; vector<int> a(n); vector<int> b(n); for (int i = 0; i < n; i++) cin >> a[i] >> b[i]; if (n == k) { vector<array<int, 2>> v(n); for (int i = 0; i < n; i++) v[i] = {b[i], i}; sort(v.begin(), v.end()); cout << n << "\n"; for (int i = 0; i < n; i++) cout << v[i][1] + 1 << " \n"[i == n - 1]; continue; } vector<vector<int>> dp(n + 1, vector<int>(n + 1, -1)); vector<vector<bool>> p(n + 1, vector<bool>(n + 1, 0)); dp[0][0] = 0; vector<array<int, 2>> v(n); for (int i = 0; i < n; i++) v[i] = {b[i], i}; sort(v.begin(), v.end()); for (int i = 0; i < n; i++) { int x = v[i][1]; for (int j = 0; j <= n; j++) { if (dp[i][j] == -1) continue; if (dp[i][j] + a[x] + j * b[x] > dp[i + 1][j + 1]) { dp[i + 1][j + 1] = dp[i][j] + a[x] + j * b[x]; p[i + 1][j + 1] = 0; } if (dp[i][j] + (k - 1) * b[x] > dp[i + 1][j]) { dp[i + 1][j] = dp[i][j] + (k - 1) * b[x]; p[i + 1][j] = 1; } } } vector<int> one; vector<int> two; int now = k; for (int i = n; i >= 1; i--) { int x = v[i - 1][1]; if (p[i][now] == 0) { one.push_back(x); now--; } else two.push_back(x); } reverse(one.begin(), one.end()); vector<int> res; for (int i = 0; i < k - 1; i++) res.push_back(one[i] + 1); for (int x : two) { res.push_back(x + 1); res.push_back(-(x + 1)); } res.push_back(one[k - 1] + 1); cout << res.size() << "\n"; for (int x : res) cout << x << " "; 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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int T, n, k; struct card { int a, b, id; } A[105]; int f[105][105], fr[105][105]; int use[105], used[105], cnt, pos, num; int cmp(card u, card v) { return u.b < v.b; } int main() { scanf("%d", &T); while (T--) { scanf("%d %d", &n, &k); for (int i = 1; i <= n; i++) scanf("%d %d", &A[i].a, &A[i].b), A[i].id = i; sort(A + 1, A + n + 1, cmp); memset(f, 0, sizeof(f)); memset(used, 0, sizeof(used)); for (int i = 1; i <= n; i++) { for (int j = 0; j < i; j++) { if (f[i][j] < f[i - 1][j] + (k - 1) * A[i].b) f[i][j] = f[i - 1][j] + (k - 1) * A[i].b, fr[i][j] = j; if (f[i][j + 1] < f[i - 1][j] + A[i].a + j * A[i].b) f[i][j + 1] = f[i - 1][j] + A[i].a + j * A[i].b, fr[i][j + 1] = j; } } cnt = 0, pos = n, num = k; while (pos) { if (fr[pos][num] == num - 1) use[++cnt] = A[pos].id, used[A[pos].id] = 1; num = fr[pos][num], pos--; } reverse(use + 1, use + cnt + 1); printf("%d\n", 2 * n - k); for (int i = 1; i < cnt; i++) printf("%d ", use[i]); for (int i = 1; i <= n; i++) if (!used[i]) printf("%d %d ", i, -i); printf("%d\n", use[cnt]); } 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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long N = 105; long long n, k; struct P { long long x, y, id; } a[N]; long long f[N][N], g[N][N]; bool maximize(long long &x, long long y) { if (x < y) { x = y; return true; } return false; } void Main() { cin >> n >> k; for (long long i = 1, __c = n; i <= __c; ++i) cin >> a[i].x >> a[i].y, a[i].id = i; memset(f, -1, sizeof(f)); memset(g, 0, sizeof(g)); sort(a + 1, a + n + 1, [](P x, P y) { return x.y < y.y; }); f[0][0] = 0; for (long long i = 0, __c = n - 1; i <= __c; ++i) { for (long long j = 0, __c = k; j <= __c; ++j) if (f[i][j] != -1) { if (maximize(f[i + 1][j], f[i][j] + (k - 1) * a[i + 1].y)) g[i + 1][j] = 1; if (j < k) if (maximize(f[i + 1][j + 1], f[i][j] + j * a[i + 1].y + a[i + 1].x)) g[i + 1][j + 1] = 2; } } vector<long long> pa; vector<long long> nope; long long now = k; for (long long i = n, __c = 1; i >= __c; --i) { if (g[i][now] == 2) pa.push_back(a[i].id), now--; else nope.push_back(a[i].id); } reverse(begin(pa), end(pa)); now = pa.back(); pa.pop_back(); for (auto &i : nope) pa.push_back(i), pa.push_back(-i); pa.push_back(now); cout << pa.size() << '\n'; for (auto &v : pa) cout << v << ' '; cout << '\n'; } int32_t main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); if (fopen("TASK" ".inp", "r")) freopen( "TASK" ".inp", "r", stdin); long long T; cin >> T; for (long long _ = 1, __c = T; _ <= __c; ++_) { 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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long mod = 998244353; long long powmod(long long a, long long b) { long long res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } double PI = 3.14159265358979323846; long double e = 2.71828182845904523536028747135266249775724709369995; long long fill(vector<pair<pair<long long, long long>, long long> > &v, vector<vector<long long> > &dp, long long idx, long long k, long long n, long long realn) { assert(idx >= 0 && k >= 0); if (dp[idx][k] != -1) return dp[idx][k]; if (idx + 1 == k) { if (idx == 0) { dp[idx][k] = v[idx].first.second; return dp[idx][k]; } dp[idx][k] = v[idx].first.second + v[idx].first.first * (n) + fill(v, dp, idx - 1, k - 1, n - 1, realn); return dp[idx][k]; } if (k == 0) { if (idx == 0) { dp[idx][k] = v[idx].first.first * (realn - 1); return dp[idx][k]; } long long a = fill(v, dp, idx - 1, k, n, realn); dp[idx][k] = a + v[idx].first.first * (realn - 1); return dp[idx][k]; } long long a = fill(v, dp, idx - 1, k, n, realn) + v[idx].first.first * (realn - 1); long long b = fill(v, dp, idx - 1, k - 1, n - 1, realn) + v[idx].first.second + v[idx].first.first * (n); dp[idx][k] = max(a, b); return dp[idx][k]; } void solve() { long long k, n; cin >> n >> k; vector<pair<pair<long long, long long>, long long> > v(n); for (long long i = 0; i < n; i++) { cin >> v[i].first.second >> v[i].first.first; v[i].second = i + 1; } sort(v.begin(), v.end()); vector<vector<long long> > dp(n, vector<long long>(k + 1, -1)); fill(v, dp, n - 1, k, k - 1, k); vector<long long> choose; long long cnt = 0; for (long long i = n - 1; i >= 0; i--) { if (cnt == k) break; if (i == 0) { choose.push_back(i); cnt++; break; } if (dp[i][k - cnt] == dp[i - 1][k - cnt - 1] + (k - 1 - cnt) * v[i].first.first + v[i].first.second) { cnt++; choose.push_back(i); } else { continue; } } assert(cnt == k); cout << 2 * n - k << endl; set<long long> s; for (long long i = cnt - 1; i >= 1; i--) { cout << v[choose[i]].second << " "; s.insert(v[choose[i]].second); } for (long long i = 1; i <= n; i++) { if (s.find(i) == s.end() && i != v[choose[0]].second) cout << i << " " << -i << " "; } cout << v[choose[0]].second << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.precision(20); int t = 1; cin >> t; for (int i = 0; i < t; i++) { solve(); } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int inf = 1023456789; struct minion { int a, b, i; }; void solve_case() { int N, K; cin >> N >> K; vector<minion> minions(N); for (int i = 0; i < N; ++i) { cin >> minions[i].a >> minions[i].b; minions[i].i = i; } sort(minions.begin(), minions.end(), [](const minion &x, const minion &y) { return x.b < y.b; }); vector<vector<int>> dp_score(N, vector<int>(K + 1, -inf)); vector<vector<bool>> dp_keep(N, vector<bool>(K + 1)); for (int i = 0; i < N; ++i) { for (int j = 0; j <= min(i + 1, K); ++j) { int score_keep_minion = j == 0 ? -inf : ((i == 0 ? 0 : dp_score[i - 1][j - 1]) + minions[i].a + (j - 1) * minions[i].b); int score_destroy_minion = j == i + 1 ? -inf : (i == 0 ? 0 : dp_score[i - 1][j]) + minions[i].b * (K - 1); if (score_keep_minion > score_destroy_minion) { dp_score[i][j] = score_keep_minion; dp_keep[i][j] = true; } else { dp_score[i][j] = score_destroy_minion; dp_keep[i][j] = false; }; } } vector<int> keep; vector<int> destroy; int j = K; for (int i = N - 1; i >= 0; --i) { if (dp_keep[i][j]) { keep.push_back(minions[i].i); j--; } else { destroy.push_back(minions[i].i); } } assert(j == 0); assert(keep.size() == K); reverse(keep.begin(), keep.end()); cout << (keep.size() + 2 * destroy.size()) << "\n"; for (int i = 0; i < K - 1; ++i) { cout << (1 + keep[i]) << " "; } for (int i = 0; i < destroy.size(); ++i) { cout << (1 + destroy[i]) << " "; cout << -(1 + destroy[i]) << " "; } cout << (1 + keep[K - 1]) << "\n"; } int main() { ios::sync_with_stdio(false); cin.tie(0); int T; cin >> T; for (int t = 0; t < T; ++t) { solve_case(); } 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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long N = 80; const long long INF = 1e18 + 239; long long dp[N][N]; long long wr[N][N]; void init() { for (long long i = 0; i < N; i++) { for (long long j = 0; j < N; j++) { dp[i][j] = -INF; wr[i][j] = -1; } } dp[0][0] = 0; } void solve(long long n, long long k, vector<long long> a, vector<long long> b, vector<long long> tind, long long &ans, vector<long long> &res) { vector<long long> ind(n); iota(ind.begin(), ind.end(), 0); sort(ind.begin(), ind.end(), [&](long long i, long long j) { return b[i] < b[j]; }); init(); for (long long i = 0; i < n; i++) { for (long long j = 0; j < n; j++) { if (dp[i][j] == -INF) continue; if (dp[i + 1][j] < dp[i][j] + (k)*b[ind[i]]) { dp[i + 1][j] = dp[i][j] + (k)*b[ind[i]]; wr[i + 1][j] = 0; } if (j + 1 <= k) { if (dp[i + 1][j + 1] < dp[i][j] + j * b[ind[i]] + a[ind[i]]) { dp[i + 1][j + 1] = dp[i][j] + j * b[ind[i]] + a[ind[i]]; wr[i + 1][j + 1] = 1; } } } } long long cans = dp[n][k]; long long wans = k; ans = cans; res.clear(); vector<long long> rv(n); for (long long i = n; i > 0; i--) { rv.push_back(wr[i][wans]); if (wr[i][wans] == 1) wans--; } reverse(rv.begin(), rv.end()); for (long long i = 0; i < n; i++) { if (rv[i] == 1) { res.push_back((tind[ind[i]] + 1)); } } for (long long i = 0; i < n; i++) { if (rv[i] != 1) { res.push_back((tind[ind[i]] + 1)); res.push_back(-(tind[ind[i]] + 1)); } } } void solve() { long long n, k; cin >> n >> k; vector<long long> a(n); vector<long long> b(n); for (long long i = 0; i < n; i++) { cin >> a[i] >> b[i]; } vector<long long> ind(n); iota(ind.begin(), ind.end(), 0); sort(ind.begin(), ind.end(), [&](long long i, long long j) { return b[i] > b[j]; }); long long ans = -INF; vector<long long> res; { for (long long rs = 0; rs < n; rs++) { vector<long long> na, nb; vector<long long> tind; for (long long i = 0; i < n; i++) { if (i != rs) { na.push_back(a[ind[i]]); nb.push_back(b[ind[i]]); tind.push_back(ind[i]); } } long long tans = -INF; vector<long long> tv; solve(n - 1, k - 1, na, nb, tind, tans, tv); tv.push_back((ind[rs] + 1)); tans += (k - 1) * b[ind[rs]]; tans += a[ind[rs]]; if (tans > ans) { ans = tans; res = tv; } } } cout << res.size() << endl; for (auto t : res) { cout << t << ' '; } cout << endl; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); long long 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" ] }
CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize("-O3") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") using namespace std; const long long MOD = 1e9 + 7; const long long INF = 1e18; const double E = 1e-8; const double pi = acos(-1); mt19937_64 rnd(time(0)); long long n, m, k, h, T; long long dp[80][80]; bool tt[80][80]; pair<pair<long long, long long>, long long> a[1001]; long long binpow(long long h, long long r, long long md = MOD) { long long l = 1; while (r) { if (r & 1) l *= h, l %= md; h *= h; h %= md; r /= 2; } return l; } long long gcd(long long x, long long y) { if (x < y) swap(x, y); while (x && y) { x %= y; swap(x, y); } return x + y; } bool comp(pair<pair<long long, long long>, long long> x, pair<pair<long long, long long>, long long> y) { return x.first.second < y.first.second; } void solve() { cin >> n >> m; m = min(n, m); for (int i = 1; i <= n; i++) { cin >> a[i].first.first >> a[i].first.second; a[i].second = i; } sort(a + 1, a + n + 1, comp); for (int i = 0; i <= n; i++) for (int j = 0; j <= m; j++) dp[i][j] = -1e18; dp[0][0] = 0; for (int i = 0; i < n; i++) for (int j = 0; j <= m; j++) { if (dp[i + 1][j] < dp[i][j] + a[i + 1].first.second * (m - 1)) { dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + a[i + 1].first.second * (m - 1)); tt[i + 1][j] = 0; } if (j < m && dp[i + 1][j + 1] < dp[i][j] + a[i + 1].first.first + a[i + 1].first.second * j) { dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + a[i + 1].first.first + a[i + 1].first.second * j); tt[i + 1][j + 1] = 1; } } long long p = m; vector<long long> v, v2; for (int i = n; i > 0; i--) { if (tt[i][p] == 0) { v2.push_back(a[i].second); } else { v.push_back(a[i].second); p--; } } reverse(v.begin(), v.end()); cout << v.size() + v2.size() * 2 << "\n"; for (int i = 0; i < v.size() - 1; i++) cout << v[i] << " "; for (int i = 0; i < v2.size(); i++) cout << v2[i] << " -" << v2[i] << " "; cout << v.back(); cout << "\n"; } int main() { ios::sync_with_stdio(0); cin.tie(0); long long 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" ] }
CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; struct node { long long id, add, val; }; node p[100]; long long n, dp[100][100]; bool f[100]; bool cmp(node x, node y) { return x.add < y.add; } deque<long long> ans; void print(long long x, long long y) { if (!y) return; if (x && dp[x][y] == dp[x - 1][y - 1] + (x - 1) * p[y].add + p[y].val) { ans.emplace_front(y); print(x - 1, y - 1); } else { f[y] = 1; print(x, y - 1); } } void solve() { long long k; cin >> n >> k; for (long long i = 1; i <= n; i++) cin >> p[i].val >> p[i].add, p[i].id = i; memset(dp, 0, sizeof(dp)); sort(p + 1, p + n + 1, cmp); for (long long i = 1; i <= k; i++) { for (long long j = 0; j <= n; j++) dp[i][j] = -1e17; } dp[0][0] = 0; for (long long i = 1; i <= n; i++) dp[0][i] = dp[0][i - 1] + (k - 1) * p[i].add; for (long long i = 1; i <= k; i++) { for (long long j = i; j <= n; j++) { dp[i][j] = max(dp[i - 1][j - 1] + (i - 1) * p[j].add + p[j].val, dp[i][j - 1] + (k - 1) * p[j].add); } } ans.clear(); memset(f, 0, sizeof(f)); print(k, n); cout << n * 2 - k << '\n'; long long t = ans.back(); ans.pop_back(); for (auto u : ans) cout << p[u].id << ' '; for (long long i = 1; i <= n; i++) { if (f[i]) { cout << p[i].id << ' ' << -p[i].id << ' '; } } cout << p[t].id << ' '; cout << '\n'; } signed main() { ios::sync_with_stdio(0); cin.tie(0); long long 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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long buf_size = 1e5 + 5; char buf[buf_size], *S, *T; inline char read_char() { if (S == T) T = (S = buf) + fread(buf, 1, buf_size, stdin); return S != T ? *(S++) : EOF; } inline long long read_int() { long long x = 0, f = 1; char c = read_char(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = read_char(); } while (c >= '0' && c <= '9') x = x * 10 + c - 48, c = read_char(); return x * f; } const long long N = 80; long long t, n, k; struct node { long long a, b, id; bool operator<(const node& x) const { return b < x.b; } } a[N]; long long f[N][N]; bool g[N][N]; bool mark[N]; signed main() { t = read_int(); while (t--) { n = read_int(), k = read_int(); for (long long i = 1; i <= n; i++) a[i].a = read_int(), a[i].b = read_int(), a[i].id = i; sort(a + 1, a + n + 1); for (long long i = 0; i <= n; i++) for (long long j = 0; j <= k; j++) f[i][j] = -10000000000, g[i][j] = 0; f[0][0] = 0; for (long long i = 1; i <= n; i++) { for (long long j = 0; j <= min(i, k); j++) { if (f[i - 1][j] + a[i].b * (k - 1) > f[i][j]) f[i][j] = f[i - 1][j] + a[i].b * (k - 1); if (j > 0 && f[i - 1][j - 1] + a[i].a + a[i].b * (j - 1) > f[i][j]) f[i][j] = f[i - 1][j - 1] + a[i].a + a[i].b * (j - 1), g[i][j] = 1; } } for (long long i = n, j = k; i >= 1; i--) { mark[a[i].id] = g[i][j]; j -= g[i][j]; } printf("%lld\n", 2 * n - k); long long p = 1; for (long long i = 1; i <= n; i++) { if (mark[a[i].id] && p < k) { p++; printf("%lld ", a[i].id); } } for (long long i = 1; i <= n; i++) { if (!mark[a[i].id]) printf("%lld %lld ", a[i].id, -a[i].id); } for (long long i = n; i >= 1; i--) if (mark[a[i].id]) { printf("%lld\n", a[i].id); 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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline int read() { register int x = 0, f = 1; register char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = 0; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + (ch ^ '0'); ch = getchar(); } return f ? x : -x; } int n, k, f[80][80], w[80][80], c[80][80], ans, vis[80]; struct node { int a, b, id; } p[80]; vector<int> C[80][80], ansp; int main() { for (int T = (1), _ed = (read()); T <= _ed; ++T) { memset(vis, 0, sizeof vis); n = read(), k = read(); ans = 0; ansp.clear(); for (int i = (1), _ed = (n); i <= _ed; ++i) p[i] = (node){read(), read(), i}; sort(p + 1, p + n + 1, [&](node x, node y) { return x.b < y.b; }); for (int i = (1), _ed = (n); i <= _ed; ++i) for (int j = (1), _ed = (k - 1); j <= _ed; ++j) w[i][j] = -(k - j) * p[i].b + p[i].a; for (int l = (1), _ed = (n); l <= _ed; ++l) { memset(f, -0x3f, sizeof f); f[0][0] = 0, C[0][0].clear(); int t = 0; for (int i = (1), _ed = (n); i <= _ed; ++i) { if (i == l) continue; ++t; for (int j = (0), _ed = (min(t, k - 1)); j <= _ed; ++j) { f[t][j] = f[t - 1][j]; C[t][j] = C[t - 1][j]; if (j) { int tmp = f[t - 1][j - 1] + w[i][j]; if (f[t][j] < tmp) { f[t][j] = tmp; C[t][j] = C[t - 1][j - 1]; C[t][j].push_back(p[i].id); } } } } int res = f[t][k - 1]; for (int i = (1), _ed = (n); i <= _ed; ++i) res += (k - 1) * p[i].b; res += p[l].a; C[t][k - 1].push_back(p[l].id); if (ans < res) { ans = res; ansp = C[t][k - 1]; } } int m = (int)(ansp).size(); printf("%d\n", n + n - m); for (int i = (0), _ed = (m - 2); i <= _ed; ++i) printf("%d ", ansp[i]), vis[ansp[i]] = 1; vis[ansp.back()] = 1; for (int i = (1), _ed = (n); i <= _ed; ++i) if (!vis[i]) printf("%d %d ", i, -i); printf("%d\n", ansp.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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct Edge { long long to, dis, next, cost; } edge[24050]; long long num = -1; bool vis[10010]; long long mincost; long long pre[10010], head[10010], cost[10010], last[10010], flow[10010], n, k, a[110], b[110], s, t, maxflow; long long to[110]; void add(long long f, long long t, long long dis, long long cost) { edge[++num].to = t; edge[num].dis = dis; edge[num].next = head[f]; edge[num].cost = cost; head[f] = num; edge[++num].to = f; edge[num].dis = 0; edge[num].cost = -cost; edge[num].next = head[t]; head[t] = num; } queue<long long> q; bool spfa(long long s, long long t) { memset(cost, 0x3f3f3f3f, sizeof cost); memset(flow, 0x3f3f3f3f, sizeof flow); memset(vis, 0, sizeof vis); q.push(s); vis[s] = 1; cost[s] = 0; pre[t] = -1; while (!q.empty()) { long long nowp = q.front(); q.pop(); vis[nowp] = 0; for (long long i = head[nowp]; i != -1; i = edge[i].next) { if (edge[i].dis > 0 && cost[edge[i].to] > cost[nowp] + edge[i].cost) { cost[edge[i].to] = cost[nowp] + edge[i].cost; pre[edge[i].to] = nowp; last[edge[i].to] = i; flow[edge[i].to] = min(flow[nowp], edge[i].dis); if (!vis[edge[i].to]) { vis[edge[i].to] = 1; q.push(edge[i].to); } } } } return pre[t] != -1; } void MCMF(long long s, long long t) { while (spfa(s, t)) { long long now = t; maxflow += flow[t]; mincost += flow[t] * cost[t]; while (now != s) { edge[last[now]].dis -= flow[t]; edge[last[now] ^ 1].dis += flow[t]; now = pre[now]; } } } signed main() { long long T; cin >> T; while (T--) { num = -1; memset(to, -1, sizeof to); memset(head, -1, sizeof head); cin >> n >> k; for (long long i = 1; i <= n; i++) cin >> a[i] >> b[i]; maxflow = mincost = 0; s = 0; t = 2 * n + 1; for (long long i = 1; i <= n; i++) add(s, i, 1, 0), add(i + n, t, 1, 0); for (long long i = 1; i <= n; i++) { for (long long j = 1; j <= n; j++) { long long nc = 0; if (j <= k - 1) nc = a[i] + b[i] * (j - 1ll); else if (j != n) nc = b[i] * (k - 1ll); else nc = a[i] + b[i] * (k - 1ll); nc = 0x3f3f3f3f - nc; add(i, j + n, 1, nc); } } MCMF(s, t); long long nowi = -1; for (long long i = n * 4; i <= num; i += 2) { nowi++; if (edge[i].dis == 0) to[1 + nowi % n] = 1 + nowi / n; } cout << 2 * n - k << endl; for (long long i = 1; i <= k - 1; i++) cout << to[i] << " "; for (long long i = k; i < n; i++) cout << to[i] << " " << -to[i] << " "; cout << to[n]; cout << endl; } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
python3
from sys import stdin, gettrace from heapq import nlargest if not gettrace(): def input(): return next(stdin)[:-1] # def input(): # return stdin.buffer.readline() INF = int(10E10) def main(): def solve(): n, k = map(int, input().split()) mm = [] for i in range(1,n+1): a,b = map(int, input().split()) mm.append((b, a, i)) mm.sort() dp = [[(-1000000, False)] * min(i+1,k+1) for i in range(n+1)] dp[1][0] = (mm[0][0] * (k-1), False) dp[1][1] = (mm[0][1], True) for j,(b,a,_) in enumerate(mm[1:],2): dp[j][0] = (dp[j-1][0][0] + b * (k-1), False) for l in range(1, min(j, k+1)): v1 = dp[j-1][l][0] + b * (k-1) v2 = dp[j-1][l-1][0] + a + b * (l-1) if v1 > v2: dp[j][l] = (v1, False) else: dp[j][l] = (v2, True) if j <= k: dp[j][j] = (dp[j-1][j-1][0] + a + b * (j-1), True) g1 = [] g2 = [] l = k for j in range(n, 0, -1): _, _, i = mm[j-1] if dp[j][l][1]: g1.append(i) l -= 1 else: g2.append(i) g2.append(-i) g1.reverse() res = g1[:k-1] + g2 + g1[k-1:] print(len(res)) print(' '.join(map(str, res))) q = int(input()) for _ in range(q): solve() if __name__ == "__main__": 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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; const int N = 6000, M = 3e4; struct edge { int to, nxt; int w, c; } e[M]; int TT, n, m, x, y, k, a[80], b[80]; int ans, S, T, i, tot = 1, head[N], d[N], path[N]; queue<int> Q; bool v[N]; void add(int x, int y, int w, int c) { e[++tot] = {y, head[x], w, c}, head[x] = tot; e[++tot] = {x, head[y], 0, -c}, head[y] = tot; } bool spfa() { for (int i = (0); i <= (N - 1); ++i) d[i] = INF; memset(v, 0, sizeof v); d[S] = 0, Q.push(S), v[S] = 1; while (!Q.empty()) { x = Q.front(), Q.pop(), v[x] = 0; for (i = head[x]; i; i = e[i].nxt) if (e[i].w && d[y = e[i].to] > d[x] + e[i].c) { d[y] = d[x] + e[i].c, path[y] = i; if (!v[y]) Q.push(y), v[y] = 1; } } if (d[T] == INF) return false; for (y = T; y != S; y = x) { i = path[y], x = e[i ^ 1].to; --e[i].w, ++e[i ^ 1].w; } ans += d[T]; return true; } int main() { cin >> TT; while (TT--) { scanf("%d%d", &n, &k); for (int i = (1); i <= (n); ++i) scanf("%d%d", a + i, b + i); tot = 1, memset(head, 0, sizeof head); S = 0, T = (k + 2) * n + 1; add(S, 1, n - k, 0); for (int i = (1); i <= (n); ++i) add(1, n + i, 1, a[i]); for (int i = (2); i <= (k); ++i) { add(S, i, 1, 0); for (int j = (1); j <= (n); ++j) add(i, n + j, 1, (i - 1) * b[j]); } for (int j = (1); j <= (n); ++j) add(n + j, T, 1, 0); while (spfa()) ; printf("%d\n", n * 2 - k); memset(v, 0, sizeof v); for (int q = (k); q >= (2); --q) { for (i = head[q]; i; i = e[i].nxt) if (e[i].to > n && !e[i].w) { x = e[i].to - n; printf("%d ", x), v[x] = true; break; } } for (i = head[1]; i; i = e[i].nxt) if (e[i].to > n && !e[i].w) { x = e[i].to - n; printf("%d -%d ", x, x), v[x] = true; } for (int i = (1); i <= (n); ++i) if (!v[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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) { for (ll i = 0; i < v.size(); ++i) os << v[i] << " "; return os; } template <typename T> ostream& operator<<(ostream& os, const set<T>& v) { for (auto it : v) os << it << " "; return os; } template <typename T, typename S> ostream& operator<<(ostream& os, const pair<T, S>& v) { os << v.first << " " << v.second; return os; } const ll mod = 1e9 + 7; const ll inf = 2e18; const ll ninf = -2e18; ll takemod(ll a) { return ((a % mod) + mod) % mod; } ll pow(ll a, ll b, ll m) { ll ans = 1; a %= m; while (b) { if (b & 1) ans = (ans * a) % m; b /= 2; a = (a * a) % m; } return ans; } ll modinv(ll a) { return takemod(pow(takemod(a), mod - 2, mod)); } const ll N = 100; ll dp[N][N]; ll n, k; struct minion { ll a, b, ind; minion(ll ad, ll bd, ll cd) { a = ad; b = bd; ind = cd; } }; bool compa(minion m1, minion m2) { return m1.b < m2.b; } void do_case() { cin >> n >> k; vector<minion> arr; arr.push_back(minion(0, 0, 0)); for (ll i = 1; i <= n; i++) { ll a, b; cin >> a >> b; arr.push_back(minion(a, b, i)); } sort(arr.begin() + 1, arr.end(), compa); memset(dp, -1, sizeof(dp)); dp[0][0] = 0; for (ll i = 1; i <= n; i++) { for (ll j = 0; j <= k; j++) { if (j && dp[i - 1][j - 1] >= 0) { dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + arr[i].b * (j - 1) + arr[i].a); } if (dp[i - 1][j] >= 0) dp[i][j] = max(dp[i][j], dp[i - 1][j] + (k - 1) * arr[i].b); } } ll ans = dp[n][k]; ll it = k; vector<ll> arr1; vector<ll> arr2; for (ll i = n; i >= 1; i--) { if (it && dp[i - 1][it - 1] >= 0) { if (dp[i][it] == dp[i - 1][it - 1] + arr[i].b * (it - 1) + arr[i].a) { it--; arr1.push_back(arr[i].ind); continue; } } if (dp[i - 1][it] >= 0 && dp[i][it] == dp[i - 1][it] + (k - 1) * arr[i].b) arr2.push_back(arr[i].ind); } reverse(arr1.begin(), arr1.end()); cout << arr1.size() + 2 * arr2.size() << '\n'; for (ll i = 0; i < (ll)(arr1.size() - 1); i++) cout << arr1[i] << " "; for (ll i = 0; i < (ll)arr2.size(); i++) { cout << arr2[i] << " " << -arr2[i] << " "; } cout << arr1.back() << '\n'; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); time_t t1, t2; t1 = clock(); ll t; cin >> t; while (t--) do_case(); t2 = clock(); cerr << '\n' << t2 - t1 << '\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" ] }
CORRECT
java
/** * @author egaeus * @mail sebegaeusprogram@gmail.com * @veredict * @url * @category * @date **/ import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.*; import java.util.stream.Collectors; import java.util.stream.IntStream; import static java.lang.Integer.parseInt; public class CFF { static TreeSet<Integer> lAdy[]; static int[] vis; static ArrayList<int[]> list; public static void main(String args[]) throws Throwable { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int T = parseInt(in.readLine()); for (int t = 0; t++ < T; ) { StringTokenizer st = new StringTokenizer(in.readLine()); int N = parseInt(st.nextToken()), K = parseInt(st.nextToken()); int[][] arr = new int[N][]; for (int i = 0; i < N; i++) { st = new StringTokenizer(in.readLine()); arr[i] = new int[]{parseInt(st.nextToken()), parseInt(st.nextToken())}; } int S = 2 * N, D = 2 * N + 1; MaxFlow_MinCost maxFlow_minCost = new MaxFlow_MinCost(2 * N + 2); for (int i = 0; i < N; i++) maxFlow_minCost.addEdge(S, i, 1, 0); for (int i = 0; i < N; i++) maxFlow_minCost.addEdge(N + i, D, 1, 0); for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) maxFlow_minCost.addEdge(i, N + j, 1, -(arr[i][1] * Math.min(j, K - 1) + (j < K - 1 || j == N - 1 ? arr[i][0] : 0))); int[] maxFlow = maxFlow_minCost.getMinCostFlow(S, D); ArrayList<Integer> solution = new ArrayList<>(); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) if (maxFlow[j] == i) { solution.add(j + 1); if (i >= K - 1 && i < N - 1) solution.add(-(j + 1)); } } System.out.println(solution.size()); System.out.println(solution.stream().map(a -> a+"").collect(Collectors.joining(" "))); } } static class MaxFlow_MinCost { static final long INF = 1000000000; private int n; private List<Edge>[] edges; @SuppressWarnings("unchecked") public MaxFlow_MinCost(int n) { this.n = n; edges = new List[n]; for (int i = 0; i < n; i++) edges[i] = new ArrayList<Edge>(); } public void addEdge(int s, int t, int cap, int cost) { edges[s].add(new Edge(s, t, edges[t].size(), cap, cost)); edges[t].add(new Edge(t, s, edges[s].size() - 1, 0, -cost)); } public int[] getMinCostFlow(int source, int sink) { long flow = 0, cost = 0; long[] potential = new long[n]; // allows Dijkstra to work with // negative edge weights while (true) { Edge[] parent = new Edge[n]; // used to store an augmenting path long[] dist = new long[n]; // minimal cost to vertex Arrays.fill(dist, INF); dist[source] = 0; // Dijkstra on cost PriorityQueue<Item> que = new PriorityQueue<Item>(); que.add(new Item(0, source)); while (!que.isEmpty()) { Item item = que.poll(); if (item.dist != dist[item.v]) continue; for (Edge e : edges[item.v]) { long temp = dist[item.v] + e.cost + potential[item.v] - potential[e.t]; // if can push some flow, and new cost is cheaper than // push if (e.cap > e.flow && dist[e.t] > temp) { dist[e.t] = temp; parent[e.t] = e; que.add(new Item(temp, e.t)); } } } // couldn't reach sink if (parent[sink] == null) break; // update potentials for Dijkstra for (int i = 0; i < n; i++) if (parent[i] != null) potential[i] += dist[i]; // maximum flow that can be pushed long augFlow = INF; for (int i = sink; i != source; i = parent[i].s) augFlow = Math.min(augFlow, parent[i].cap - parent[i].flow); // push the flow for (int i = sink; i != source; i = parent[i].s) { Edge e = parent[i]; e.flow += augFlow; edges[e.t].get(e.rev).flow -= augFlow; cost += augFlow * e.cost; } flow += augFlow; } int[] result = new int[n / 2 - 1]; for (int i = 0; i < result.length; i++) { for (int j = 0; j < edges[i].size(); j++) if (edges[i].get(j).flow == 1) result[i] = edges[i].get(j).t - result.length; } return result; } public class Item implements Comparable<Item> { long dist; int v; public Item(long dist, int v) { this.dist = dist; this.v = v; } public int compareTo(Item that) { if (this.dist < that.dist) return -1; if (this.dist > that.dist) return 1; return 0; } } public class Edge { public int s, t, rev, cap, flow, cost; public Edge(int s, int t, int reverse, int cap, int cost) { this.s = s; this.t = t; this.rev = reverse; this.cap = cap; this.cost = cost; } } } }
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" ] }
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] = -1; 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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") #pragma GCC target("avx") bool RELAXED = false; template <int, typename T> struct MINMAX { T val; MINMAX(T val) : val(val) {} }; template <typename T> MINMAX<1, T> MAX(T val) { return MINMAX<1, T>(val); }; template <typename T> MINMAX<2, T> MIN(T val) { return MINMAX<2, T>(val); }; template <typename T, typename U> inline T& operator|=(T& lhs, MINMAX<1, U> rhs) { return lhs = (rhs.val > lhs ? (RELAXED = true, rhs.val) : (RELAXED = false, lhs)); } template <typename T, typename U> inline T& operator|=(T& lhs, MINMAX<2, U> rhs) { return lhs = (rhs.val < lhs ? (RELAXED = true, rhs.val) : (RELAXED = false, lhs)); } template <typename T, typename U> istream& operator>>(istream& in, pair<T, U>& p) { in >> p.first >> p.second; return in; } template <typename T> inline vector<T> READ(int n) { vector<T> vec(n); for (int i = 0; i < int(n); i++) cin >> vec[i]; return vec; } template <typename T> using min_heap = priority_queue<T, vector<T>, greater<T> >; const int MAXN = 1e5 + 5; signed long long int dp[MAXN][80]; signed long long int bt[MAXN][80]; vector<pair<int, int> > minion; int n, k; map<pair<int, int>, vector<int> > ids; int get_id(pair<int, int> minion) { int id = ids[minion].back(); ids[minion].pop_back(); return id; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cout << fixed << setprecision(12); int T; cin >> T; while (T--) { cin >> n >> k; minion.clear(); minion.resize(n); memset(dp, 0x80, sizeof dp); memset(bt, 0, sizeof bt); ids.clear(); for (int i = 0; i < int(n); i++) { int a, b; cin >> a >> b; minion[i] = {a, b}; ids[{a, b}].push_back(i + 1); } sort((minion).begin(), (minion).end(), [&](pair<int, int> a, pair<int, int> b) { return a.second < b.second; }); 42; dp[0][0] = 0; for (int i = 0; i < int(n); i++) { for (int j = 0; j < int(k + 1); j++) { dp[i + 1][j] = dp[i][j] + (k - 1) * minion[i].second; bt[i + 1][j] = false; } for (int j = 0; j < int(k); j++) { dp[i + 1][j + 1] |= MAX(dp[i][j] + minion[i].first + j * minion[i].second); if (RELAXED) bt[i + 1][j + 1] = true; } } vector<int> keep; int lk = k; for (int i = int(n); i >= int(0); --i) { if (bt[i][lk]) { keep.push_back(i - 1); lk--; } if (lk < 0) break; } reverse((keep).begin(), (keep).end()); vector<int> actions; for (int i = 0; i < int(k - 1); i++) { pair<int, int> mi = minion[keep[i]]; actions.push_back(get_id(mi)); } for (int i = 0; i < int(n); i++) { pair<int, int> mi = minion[i]; if (count((keep).begin(), (keep).end(), i)) continue; int id = get_id(mi); actions.push_back(id); actions.push_back(-id); } { pair<int, int> mi = minion[keep.back()]; actions.push_back(get_id(mi)); } 42; cout << actions.size() << endl; for (auto x : actions) 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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename T> void read(T &n) { n = 0; T f = 1; char c = getchar(); while (!isdigit(c) && c != '-') c = getchar(); if (c == '-') f = -1, c = getchar(); while (isdigit(c)) n = n * 10 + c - '0', c = getchar(); n *= f; } template <typename T> void write(T n) { if (n < 0) putchar('-'), n = -n; if (n > 9) write(n / 10); putchar(n % 10 + '0'); } int n, m, vis[105]; long long f[105][105], p[105][105]; struct node { long long a, b; int id; friend bool operator<(node a, node b) { return a.b < b.b; } } v[105]; int main() { int T; cin >> T; while (T--) { read(n), read(m); for (int i = 1; i <= n; i++) { read(v[i].a), read(v[i].b); v[i].id = i; } sort(v + 1, v + n + 1); memset(f, -0x3f, sizeof(f)); memset(p, 0, sizeof(p)); memset(vis, 0, sizeof(vis)); 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] + v[i].b * (m - 1); if (j && f[i - 1][j - 1] + v[i].a + v[i].b * (j - 1) > f[i][j]) { f[i][j] = f[i - 1][j - 1] + v[i].a + v[i].b * (j - 1); p[i][j] = 1; } } } int t = m; for (int i = n; i >= 1; i--) { if (p[i][t]) { vis[i] = 1; t--; } } printf("%d\n", m + (n - m) * 2); int cnt = 0, x; for (int i = 1; i <= n; i++) { if (!vis[i]) continue; cnt++; if (cnt == m) x = v[i].id; else printf("%d ", v[i].id); } for (int i = 1; i <= n; i++) { if (!vis[i]) printf("%d %d ", v[i].id, -v[i].id); } printf("%d\n", x); } 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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>; template <class T> T poll(pqg<T> &x) { T y = x.top(); x.pop(); return y; } template <int SZ> struct mcmf { struct Edge { int to, rev; long long flow, cap; long long cost; }; vector<Edge> adj[SZ]; void addEdge(int u, int v, long long cap, long long cost) { assert(cap >= 0); Edge a{v, (int)adj[v].size(), 0, cap, cost}, b{u, (int)adj[u].size(), 0, 0, -cost}; adj[u].push_back(a), adj[v].push_back(b); } int N, second, t; long long INF = 1e18; pair<int, int> pre[SZ]; pair<long long, long long> cost[SZ]; long long totCost, curCost; long long totFlow; void reweight() { for (int i = 0; i < N; i++) for (auto &p : adj[i]) p.cost += cost[i].first - cost[p.to].first; } bool spfa() { for (int i = 0; i < N; i++) cost[i] = make_pair(INF, 0); cost[second] = make_pair(0, INF); pqg<pair<long long, int>> todo; todo.push({0, second}); while ((int)todo.size()) { auto x = poll(todo); if (x.first > cost[x.second].first) continue; for (auto &a : adj[x.second]) if (x.first + a.cost < cost[a.to].first && a.flow < a.cap) { pre[a.to] = {x.second, a.rev}; cost[a.to] = {x.first + a.cost, min(a.cap - a.flow, cost[x.second].second)}; todo.push({cost[a.to].first, a.to}); } } curCost += cost[t].first; return cost[t].second; } void backtrack() { long long df = cost[t].second; totFlow += df, totCost += curCost * df; for (int x = t; x != second; x = pre[x].first) { adj[x][pre[x].second].flow -= df; adj[pre[x].first][adj[x][pre[x].second].rev].flow += df; } } pair<long long, long long> calc(int _N, int _s, int _t) { N = _N; second = _s, t = _t; totFlow = totCost = curCost = 0; while (spfa()) { reweight(); backtrack(); } return {totFlow, totCost}; } }; void solve() { int n, k; cin >> n >> k; vector<pair<long long, long long>> minion; for (int i = 0; i < n; i++) { long long a, b; cin >> a >> b; minion.push_back(make_pair(a, b)); } mcmf<160> G; int second = n + k + 1; int t = n + k + 2; for (int i = 0; i < n; i++) G.addEdge(second, i, 1, 0); for (int i = n; i < n + k; i++) G.addEdge(i, t, 1, 0); G.addEdge(n + k, t, n - k, 0); for (int i = 0; i < n; i++) { for (int j = 0; j < k; j++) { G.addEdge(i, j + n, 1, -(minion[i].first + j * minion[i].second)); } } for (int i = 0; i < n; i++) G.addEdge(i, n + k, 1, -(k - 1) * minion[i].second); pair<long long, long long> sol = G.calc(t + 1, second, t); assert(sol.first == n); vector<int> ans; for (int i = n; i < n + k - 1; i++) { for (auto &e : G.adj[i]) { if (e.flow == -1) { ans.push_back(e.to + 1); } } } for (auto &e : G.adj[n + k]) { if (e.flow == -1) { ans.push_back(e.to + 1); ans.push_back(-(e.to + 1)); } } for (auto &e : G.adj[n + k - 1]) { if (e.flow == -1) { ans.push_back(e.to + 1); } } cout << ans.size() << endl; for (int i = 0; i < ans.size(); i++) cout << ans[i] << ' '; cout << endl; } int main() { ios::sync_with_stdio(0); cin.tie(0); mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int t; cin >> t; for (int i = 0; i < t; i++) solve(); }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
java
import java.io.*; import java.util.*; public class Main { static final long MOD = 1000000007L; static final int INF = 50000000; static final int NINF = -500000000; static ArrayDeque<int[]>[] graph; public static void main(String[] args) { FastScanner sc = new FastScanner(); PrintWriter pw = new PrintWriter(System.out); int Q = sc.ni(); for (int q = 0; q < Q; q++) { int N = sc.ni(); int K = sc.ni(); int[][] nums = new int[N+1][3]; //A,B,i Arrays.fill(nums[0],-1); for (int i = 1; i <= N; i++) nums[i] = new int[] {sc.ni(),sc.ni(),i}; sort(nums); int[][] dp = new int[N+1][K+1]; //put minion i in position j for (int i = 0; i <= N; i++) Arrays.fill(dp[i], NINF); dp[0][0] = 0; int[][] from = new int[N+1][K+1]; //equal to k or k-1 for (int i = 1; i <= N; i++) { for (int j = 0; j <= Math.min(i, K); j++) { int v1 = 0; if (j > 0) v1 = dp[i-1][j-1]+nums[i][0]+(j-1)*nums[i][1]; //put this minion in the final roster int v2 = dp[i-1][j]+(K-1)*nums[i][1]; //this is a throwaway minion if (v1 > v2) { dp[i][j] = v1; from[i][j] = j-1; } else { dp[i][j] = v2; from[i][j] = j; } } } //pw.println(Arrays.deepToString(nums)); //pw.println(Arrays.deepToString(dp)); //pw.println(Arrays.deepToString(from)); ArrayList<Integer> survived = new ArrayList<Integer>(); ArrayList<Integer> killed = new ArrayList<Integer>(); int pos = K; for (int i = N; i >= 1; i--) { if (from[i][pos]==pos) { killed.add(nums[i][2]); } else { survived.add(nums[i][2]); } pos = from[i][pos]; } Collections.reverse(survived); ArrayList<Integer> ans = new ArrayList<Integer>(); for (int i = 0; i < survived.size()-1; i++) { ans.add(survived.get(i)); } for (int i = 0; i < killed.size(); i++) { int idx = killed.get(i); ans.add(idx); ans.add(0-idx); } ans.add(survived.get(survived.size()-1)); pw.println(ans.size()); for (int a: ans) pw.print(a + " "); pw.println(); } pw.close(); } public static void sort(int[][] arr) { //Sort an array (immune to quicksort TLE) Random rgen = new Random(); for (int i = 0; i < arr.length; i++) { int randomPosition = rgen.nextInt(arr.length); int[] temp = arr[i]; arr[i] = arr[randomPosition]; arr[randomPosition] = temp; } Arrays.sort(arr, new Comparator<int[]>() { @Override public int compare(int[] arr1, int[] arr2) { return arr1[1]-arr2[1]; //Ascending order. } }); } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline char gc() { static char buf[1 << 16], *p1 = buf, *p2 = buf; if (p1 == p2) { p2 = (p1 = buf) + fread(buf, 1, 1 << 16, stdin); if (p2 == p1) return EOF; } return *p1++; } template <class t> inline t read(t &x) { char c = gc(); bool f = 0; x = 0; while (!isdigit(c)) f |= c == '-', c = gc(); while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48), c = gc(); if (f) x = -x; return x; } template <class t> inline void write(t x) { if (x < 0) putchar('-'), write(-x); else { if (x > 9) write(x / 10); putchar('0' + x % 10); } } const int N = 205, M = 1e4 + 5, P = 80; int en = 1, mc, mf, h[N], dis[N], n, k, ans[P]; bool v[N]; struct edge { int n, v, f, w; } e[M << 1]; struct fafafa { int fa, id; } pre[N]; void add(int x, int y, int f, int w) { e[++en] = (edge){h[x], y, f, w}; h[x] = en; } bool spfa(int s, int t) { memset(v, 0, sizeof v); memset(pre, 0, sizeof pre); memset(dis, 0x3f, sizeof dis); queue<int> q; q.push(s); v[s] = 1; dis[s] = 0; while (!q.empty()) { int x = q.front(); q.pop(); for (int i = h[x]; i; i = e[i].n) { int y = e[i].v; if (e[i].f && dis[x] + e[i].w < dis[y]) { dis[y] = dis[x] + e[i].w; pre[y] = (fafafa){x, i}; if (!v[y]) { v[y] = 1; q.push(y); } } } v[x] = 0; } return dis[t] ^ 0x3f3f3f3f; } void mcmf(int s, int t) { while (spfa(s, t)) { int flow = INT_MAX; for (int i = t; i ^ s; i = pre[i].fa) flow = min(flow, e[pre[i].id].f); for (int i = t; i ^ s; i = pre[i].fa) { e[pre[i].id].f -= flow; e[pre[i].id ^ 1].f += flow; } mf += flow; mc += flow * dis[t]; } } void exadd(int x, int y, int f, int w) { add(x, y, f, w); add(y, x, 0, -w); } void doit() { read(n); read(k); for (int i = 1; i <= n; i++) exadd(0, i, 1, 0), exadd(i + n, n * 2 + 1, 1, 0); for (int i = 1, a, b; i <= n; i++) { read(a); read(b); for (int j = 1, val; j <= n; j++) { if (j < k) val = a + (j - 1) * b; else if (j < n) val = (k - 1) * b; else val = a + (k - 1) * b; exadd(i, j + n, 1, -val); } } mcmf(0, n * 2 + 1); write(k + (n - k) * 2); puts(""); for (int x = 1; x <= n; x++) for (int i = h[x]; i; i = e[i].n) { int y = e[i].v; if (y <= n) continue; if (!e[i].f) ans[y - n] = x; } for (int i = 1; i <= n; i++) { write(ans[i]); putchar(' '); if (i >= k && i < n) write(-ans[i]), putchar(' '); } puts(""); en = 1; mc = mf = 0; for (int i = 0; i <= n * 2 + 1; i++) h[i] = 0; } signed main() { int t; read(t); while (t--) doit(); }
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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int t, n, k; long long dp[80][80]; vector<pair<pair<int, int>, int> > v; long long solve(int i, int rem) { if (i == n) { if (rem) return -1e15; return 0; } long long &ans = dp[i][rem]; if (ans != -1) return ans; if (rem) ans = solve(i + 1, rem - 1) + (k - rem) * v[i].first.first + v[i].first.second; ans = max(ans, solve(i + 1, rem) + v[i].first.first * (k - 1)); return ans; } vector<int> v1, v2; void build(int i, int rem) { if (i == n) return; long long ans1 = solve(i + 1, rem - 1) + (k - rem) * v[i].first.first + v[i].first.second; long long ans2 = solve(i + 1, rem) + v[i].first.first * (k - 1); if (ans1 > ans2) { v1.push_back(v[i].second); build(i + 1, rem - 1); return; } v2.push_back(v[i].second); build(i + 1, rem); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> t; while (t--) { cin >> n >> k; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; v.push_back({{y, x}, i + 1}); } sort(v.begin(), v.end()); memset(dp, -1, sizeof dp); solve(0, k); build(0, k); cout << v1.size() + v2.size() * 2 << "\n"; v2.push_back(v1.back()); v1.pop_back(); for (auto i : v1) cout << i << " "; for (int i = 0; i < v2.size(); i++) { if (i == v2.size() - 1) cout << v2[i]; else cout << v2[i] << " " << -v2[i] << " "; } cout << "\n"; v.clear(); v1.clear(); v2.clear(); } }
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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long inf = 1e9 + 7; const long long INF = 1LL << 60; const long long mod = 1e9 + 7; const long double eps = 1e-8; const long double pi = acos(-1.0); template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } void solve() { long long n, k; cin >> n >> k; vector<long long> a(n), b(n); vector<pair<pair<long long, long long>, long long> > c(n); for (long long i = 0; i < n; i++) { cin >> a[i] >> b[i]; c[i] = {{a[i], b[i]}, i}; } sort(c.begin(), c.end(), [&](pair<pair<long long, long long>, long long> x, pair<pair<long long, long long>, long long> y) { return x.first.second < y.first.second; }); static long long dp[80][80]; for (long long i = 0; i < 80; i++) { for (long long j = 0; j < 80; j++) { dp[i][j] = -INF; } } dp[0][0] = 0; for (long long i = 0; i < n; i++) { for (long long j = 0; j <= i; ++j) { if (dp[i][j] != -INF) chmax(dp[i + 1][j + 1], dp[i][j] + c[i].first.first + j * c[i].first.second); if (dp[i][j] != -INF) chmax(dp[i + 1][j], dp[i][j] + (k - 1) * c[i].first.second); } } long long cur = dp[n][k]; long long p = n, q = k; vector<long long> s(n, -1); for (long long i = n - 1; i >= 0; --i) { if (p - 1 >= 0 && q - 1 >= 0) { if (dp[p - 1][q - 1] + c[i].first.first + (q - 1) * c[i].first.second == cur) { s[i] = 0; p--; q--; cur = dp[p][q]; continue; } } if (p - 1 >= 0) { if (dp[p - 1][q] + (k - 1) * c[i].first.second == cur) { s[i] = 1; --p; cur = dp[p][q]; } } } vector<long long> ans, ba; long long last = -1; for (long long i = 0; i < n; i++) { if (s[i] == 0) { if ((long long)ans.size() < k - 1) ans.push_back(c[i].second + 1); else last = c[i].second; } else { ba.push_back(c[i].second + 1); } } for (long long x : ba) { ans.push_back(x); ans.push_back(-x); } ans.push_back(last + 1); cout << (long long)ans.size() << endl; for (long long i = 0; i < (long long)ans.size(); i++) { if (i) cout << " "; cout << ans[i]; } cout << endl; } signed main() { ios::sync_with_stdio(false); cin.tie(0); long long t; cin >> t; for (long long i = 0; i < t; i++) solve(); return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using Vi = vector<int>; using Pii = pair<int, int>; int uplg(int n) { return 32 - __builtin_clz(n); } int uplg(ll n) { return 64 - __builtin_clzll(n); } void run(); int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(18); run(); return 0; } ll dp[80][80]; bool take[80][80]; ll check(int k, const vector<Pii>& elems) { for (int i = (0); i < (80); i++) for (int j = (0); j < (80); j++) dp[i][j] = -1e12; dp[0][0] = 0; for (int pref = (1); pref < (int((elems).size()) + 1); pref++) { auto e = elems[pref - 1]; for (int taken = (0); taken < (k + 1); taken++) { dp[pref][taken] = dp[pref - 1][taken] + k * e.second; take[pref][taken] = 0; if (taken > 0) { ll alt = dp[pref - 1][taken - 1] + e.first + (taken - 1) * e.second; if (alt > dp[pref][taken]) { dp[pref][taken] = alt; take[pref][taken] = 1; } } if (dp[pref][taken] < 0) { dp[pref][taken] = -1e12; } } } return dp[int((elems).size())][k]; } void solve() { int n, k; cin >> n >> k; vector<pair<Pii, int>> elems(n); for (int i = (0); i < (n); i++) elems[i].second = i; for (auto& e : (elems)) cin >> e.first.first >> e.first.second; sort((elems).begin(), (elems).end(), [](pair<Pii, int> l, pair<Pii, int> r) { return l.first.second < r.first.second; }); vector<Pii> tmp; int bestI = -1; ll ans = -1; for (int i = (0); i < (n); i++) { tmp.clear(); for (int j = (0); j < (n); j++) if (j != i) tmp.push_back(elems[j].first); ll alt = check(k - 1, tmp); alt += elems[i].first.first + elems[i].first.second * (k - 1); if (alt > ans) { ans = alt; bestI = i; } } Vi ind; tmp.clear(); for (int j = (0); j < (n); j++) if (j != bestI) tmp.push_back(elems[j].first), ind.push_back(elems[j].second); ll alt = check(k - 1, tmp); alt += elems[bestI].first.first + elems[bestI].first.second * (k - 1); assert(alt == ans); Vi toTake, toDel; int pref = int((tmp).size()), taken = k - 1; while (pref > 0) { if (take[pref][taken]) { toTake.push_back(ind[pref - 1]); taken--; } else { toDel.push_back(ind[pref - 1]); } pref--; } reverse((toTake).begin(), (toTake).end()); reverse((toDel).begin(), (toDel).end()); Vi moves; for (auto& t : (toTake)) moves.push_back(t + 1); for (auto& t : (toDel)) { moves.push_back(t + 1); moves.push_back(-t - 1); } moves.push_back(elems[bestI].second + 1); cout << int((moves).size()) << '\n'; for (auto& e : (moves)) cout << e << ' '; cout << '\n'; } void run() { int t; cin >> t; while (t--) solve(); }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
python3
from operator import itemgetter import sys int1 = lambda x: int(x) - 1 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 SI(): return sys.stdin.readline()[:-1] 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 = [[[] 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] if pre==inf:continue cur = pre + b * (k - 1 - j) - a if cur < dp[j + 1][e]: dp[j + 1][e] = cur log[j+1][e]=log[j][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 first=log[-1][mne] use=[False]*n use[mne]=True ans=[] for i in first: 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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int inf = 2000000000; char buffer[2000020], *p1, *p2; template <class T> void read(T& x) { char ch = ((p1 == p2 && (p2 = (p1 = buffer) + fread(buffer, 1, 2000020, stdin), p1 == p2)) ? EOF : *p1++); x = 0; bool f = 1; while (!('0' <= ch && ch <= '9') && ch != '-') ch = ((p1 == p2 && (p2 = (p1 = buffer) + fread(buffer, 1, 2000020, stdin), p1 == p2)) ? EOF : *p1++); if (ch == '-') f = 0, ch = ((p1 == p2 && (p2 = (p1 = buffer) + fread(buffer, 1, 2000020, stdin), p1 == p2)) ? EOF : *p1++); while ('0' <= ch && ch <= '9') x = (x << 1) + (x << 3) + ch - '0', ch = ((p1 == p2 && (p2 = (p1 = buffer) + fread(buffer, 1, 2000020, stdin), p1 == p2)) ? EOF : *p1++); x = (f) ? x : -x; } int _num[20]; template <class T> void write(T x) { if (!x) { putchar('0'), putchar('\n'); return; } if (x < 0) putchar('-'), x = -x; int c = 0; while (x) _num[++c] = x % 10, x /= 10; while (c) putchar('0' + _num[c--]); putchar('\n'); } int n, k, ti; struct ele { int b, a, id; ele(int b, int a, int id) : b(b), a(a), id(id) {} ele() {} bool operator<(const ele y) const { return b < y.b; } } p[85]; int vis[85][85]; int dp[85][85]; int DP(int pos, int rest) { if (pos > n) { if (rest == 0) return 0; else return -inf; } if (vis[pos][rest] == ti) return dp[pos][rest]; vis[pos][rest] = ti; int& ans = dp[pos][rest]; if (rest) return ans = max(DP(pos + 1, rest - 1) + p[pos].a + p[pos].b * (k - rest), DP(pos + 1, rest) + p[pos].b * (k - 1)); else return ans = DP(pos + 1, rest) + p[pos].b * (k - 1); } int ans[85]; void find_ans(int pos, int rest) { if (pos > n) return; if (rest) { int A = DP(pos + 1, rest - 1) + p[pos].a + p[pos].b * (k - rest); int B = DP(pos + 1, rest) + p[pos].b * (k - 1); if (A > B) find_ans(pos + 1, rest - 1), ans[pos] = p[pos].id; else find_ans(pos + 1, rest), ans[pos] = -p[pos].id; } else { find_ans(pos + 1, rest); ans[pos] = -p[pos].id; } } int main() { int T, x, y; scanf("%d", &T); while (T--) { ti++; scanf("%d%d", &n, &k); for (register int i = 1; i <= n; i++) scanf("%d%d", &x, &y), p[i] = ele(y, x, i); sort(p + 1, p + 1 + n); DP(1, k), find_ans(1, k); int num = n; for (register int i = 1; i <= n; i++) if (ans[i] < 0) num++; printf("%d\n", num); for (register int i = 1; i <= n; i++) { if (k > 1 && ans[i] > 0) printf("%d ", ans[i]), k--; } for (register int i = 1; i <= n; i++) { if (ans[i] < 0) printf("%d %d ", -ans[i], ans[i]); } for (register int i = n; i >= 1; i--) { if (ans[i] > 0) { printf("%d\n", ans[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" ] }
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 = 0; j <= k; j++) f[i][j][0] = f[i][j][1] = -inf; f[0][0][0] = 0; for (i = 1; i <= n; i++) { for (j = 0; 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 (j && 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" ] }
CORRECT
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.StringTokenizer; import java.io.BufferedReader; import java.util.Comparator; import java.util.ArrayDeque; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); SummoningMinions solver = new SummoningMinions(); solver.solve(1, in, out); out.close(); } static class SummoningMinions { public void solve(int testNumber, InputReader in, PrintWriter out) { int T = in.nextInt(); while (T-- > 0) { int N = in.nextInt(); int K = in.nextInt(); int[][] arr = new int[N][3]; for (int i = 0; i < N; i++) { arr[i][0] = in.nextInt(); arr[i][1] = in.nextInt(); arr[i][2] = i + 1; } Arrays.sort(arr, new Comparator<int[]>() { public int compare(int[] ints, int[] t1) { return Integer.compare(ints[1], t1[1]); } }); long[][] dp = new long[N][K + 1]; boolean[][] add = new boolean[N][K + 1]; Arrays.fill(dp[0], Long.MIN_VALUE); dp[0][0] = (K - 1) * arr[0][1]; dp[0][1] = (arr[0][0]); add[0][1] = true; for (int i = 1; i < N; i++) { for (int j = 0; j <= K; j++) { dp[i][j] = dp[i - 1][j] + (K - 1) * arr[i][1]; if (j > 0) { if (dp[i - 1][j - 1] + arr[i][0] + arr[i][1] * (j - 1) > dp[i][j]) { add[i][j] = true; dp[i][j] = dp[i - 1][j - 1] + arr[i][0] + arr[i][1] * (j - 1); } } } } int u = K; ArrayDeque<Integer> order = new ArrayDeque<>(); ArrayList<Integer> not_added = new ArrayList<>(); for (int i = N - 1; i >= 0; i--) { if (add[i][u]) { order.addFirst(arr[i][2]); u--; } else { not_added.add(arr[i][2]); } } StringBuilder str = new StringBuilder(); str.append((2 * N - K) + "\n"); // assert false; while (order.size() > 1) { str.append(order.removeFirst() + " "); } for (int i : not_added) { str.append(i + " "); str.append((-i) + " "); } str.append(order.removeFirst() + " "); out.println(str.toString()); } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; template <class T> using pq = priority_queue<T>; template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>; int scan() { return getchar(); } void scan(int& a) { cin >> a; } void scan(long long& a) { cin >> a; } void scan(char& a) { cin >> a; } void scan(double& a) { cin >> a; } void scan(long double& a) { cin >> a; } void scan(char a[]) { scanf("%s", a); } void scan(string& a) { cin >> a; } template <class T> void scan(vector<T>&); template <class T, size_t size> void scan(array<T, size>&); template <class T, class L> void scan(pair<T, L>&); template <class T, size_t size> void scan(T (&)[size]); template <class T> void scan(vector<T>& a) { for (auto& i : a) scan(i); } template <class T> void scan(deque<T>& a) { for (auto& i : a) scan(i); } template <class T, size_t size> void scan(array<T, size>& a) { for (auto& i : a) scan(i); } template <class T, class L> void scan(pair<T, L>& p) { scan(p.first); scan(p.second); } template <class T, size_t size> void scan(T (&a)[size]) { for (auto& i : a) scan(i); } template <class T> void scan(T& a) { cin >> a; } void IN() {} template <class Head, class... Tail> void IN(Head& head, Tail&... tail) { scan(head); IN(tail...); } string stin() { string s; cin >> s; return s; } template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } vector<int> iota(int n) { vector<int> a(n); iota(begin(a), end(a), 0); return a; } template <class T> void UNIQUE(vector<T>& x) { sort(begin(x), end(x)); x.erase(unique(begin(x), end(x)), x.end()); } int in() { int x; cin >> x; return x; } long long lin() { unsigned long long x; cin >> x; return x; } void print() { putchar(' '); } void print(bool a) { cout << a; } void print(int a) { cout << a; } void print(long long a) { cout << a; } void print(char a) { cout << a; } void print(string& a) { cout << a; } void print(double a) { cout << a; } template <class T> void print(const vector<T>&); template <class T, size_t size> void print(const array<T, size>&); template <class T, class L> void print(const pair<T, L>& p); template <class T, size_t size> void print(const T (&)[size]); template <class T> void print(const vector<T>& a) { if (a.empty()) return; print(a[0]); for (auto i = a.begin(); ++i != a.end();) { cout << " "; print(*i); } cout << endl; } template <class T> void print(const deque<T>& a) { if (a.empty()) return; print(a[0]); for (auto i = a.begin(); ++i != a.end();) { cout << " "; print(*i); } } template <class T, size_t size> void print(const array<T, size>& a) { print(a[0]); for (auto i = a.begin(); ++i != a.end();) { cout << " "; print(*i); } } template <class T, class L> void print(const pair<T, L>& p) { cout << '('; print(p.first); cout << ","; print(p.second); cout << ')'; } template <class T, size_t size> void print(const T (&a)[size]) { print(a[0]); for (auto i = a; ++i != end(a);) { cout << " "; print(*i); } } template <class T> void print(const T& a) { cout << a; } int out() { putchar('\n'); return 0; } template <class T> int out(const T& t) { print(t); putchar('\n'); return 0; } template <class Head, class... Tail> int out(const Head& head, const Tail&... tail) { print(head); putchar(' '); out(tail...); return 0; } long long gcd(long long a, long long b) { while (b) { long long c = b; b = a % b; a = c; } return a; } long long lcm(long long a, long long b) { if (!a || !b) return 0; return a * b / gcd(a, b); } vector<pair<long long, long long>> factor(long long x) { vector<pair<long long, long long>> ans; for (long long i = 2; i * i <= x; i++) if (x % i == 0) { ans.push_back({i, 1}); while ((x /= i) % i == 0) ans.back().second++; } if (x != 1) ans.push_back({x, 1}); return ans; } vector<int> divisor(int x) { vector<int> ans; for (int i = 1; i * i <= x; i++) if (x % i == 0) { ans.push_back(i); if (i * i != x) ans.push_back(x / i); } return ans; } int popcount(long long x) { return __builtin_popcountll(x); } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int rnd(int n) { return uniform_int_distribution<int>(0, n)(rng); } template <class... T> void err(const T&...) {} 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, 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; } void print(int n) { for (int i = 0; i < n; i++) { cerr << seg[i + sz] << " "; } cerr << '\n'; } }; signed main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cout << fixed << setprecision(15); int T = in(); while (T--) { int n = in(), k = in(); auto id = iota(n); vector<long long> a, b; for (long long i = 0; i < n; ++i) { a.push_back(in()), b.push_back(in()); } sort(begin(id), end(id), [&](int x, int y) { return b[x] < b[y]; }); long long dp[100][100] = {}; for (long long i = 0; i < n + 1; ++i) for (long long j = 0; j < n + 1; ++j) dp[i][j] = -1e15; dp[0][0] = 0; int pre[100][100] = {}; int mem[100][100]; for (long long i = 0; i < n; ++i) { int now = id[i]; for (long long j = 0; j < k + 1; ++j) { if (chmax(dp[i + 1][j], dp[i][j] + b[now] * (k - 1))) pre[i + 1][j] = j; if (chmax(dp[i + 1][j + 1], dp[i][j] + a[now] + b[now] * j)) pre[i + 1][j + 1] = j; } } vector<int> ope; int now = k; for (long long i = n; i >= 1; --i) { if (pre[i][now] != now) { ope.emplace_back(id[i - 1]); now--; } } reverse(begin(ope), end(ope)); cout << n + (n - k) << '\n'; for (long long i = 0; i < k - 1; ++i) cout << ope[i] + 1 << " "; vector<bool> used(n); for (long long i = 0; i < k; ++i) used[ope[i]] = 1; for (long long i = 0; i < n; ++i) if (!used[i]) { cout << i + 1 << " " << -(i + 1) << " "; } cout << ope[k - 1] + 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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct edge { int toi, toj; long long cost; long long label; }; int main() { int T; cin >> T; for (int _ = 0; _ < T; _++) { int n, k; cin >> n >> k; vector<vector<long long>> a; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; a.push_back({y, x, i}); } sort(a.begin(), a.end()); vector<vector<long long>> dp(n + 1, vector<long long>(k + 1, -10000000000000000)); dp[0][0] = 0; vector<vector<vector<edge>>> E(n + 1, vector<vector<edge>>(k + 1, vector<edge>())); for (int i = 0; i < n; i++) { for (int j = 0; j <= k; j++) { if (dp[i][j] == -10000000000000000) continue; dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + a[i][0] * (k - 1)); E[i + 1][j].push_back({i, j, a[i][0] * (k - 1), -(a[i][2] + 1)}); if (j != k) { dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + a[i][1] + a[i][0] * j); E[i + 1][j + 1].push_back({i, j, a[i][1] + a[i][0] * j, a[i][2] + 1}); } } } vector<int> ans_plus, ans_minus; int ni = n, nj = k; while (ni != 0 || nj != 0) { for (int i = 0; i < E[ni][nj].size(); i++) { auto &e = E[ni][nj][i]; if (dp[e.toi][e.toj] + e.cost == dp[ni][nj]) { if (e.label > 0) ans_plus.push_back(e.label); else ans_minus.push_back(-e.label); ni = e.toi; nj = e.toj; break; } } } vector<int> ans; while (ans_plus.size() != 1) { ans.push_back(ans_plus.back()); ans_plus.pop_back(); } for (int i = 0; i < ans_minus.size(); i++) { int t = ans_minus[i]; ans.push_back(t); ans.push_back(-t); } ans.push_back(ans_plus.back()); cout << ans.size() << endl; for (int i = 0; i < ans.size(); i++) { if (i != 0) cout << ' '; cout << ans[i]; } cout << endl; } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, k; struct pairab { int a, b, idx; }; bool compare(pairab i1, pairab i2) { return (i1.b == i2.b) ? (i1.a < i2.a) : (i1.b < i2.b); } pairab in[80]; int inf = (int)1e9; int memo[80][80]; int dp(int i, int takenInFirstK) { if (takenInFirstK > k) return -inf; if (i >= n) return (takenInFirstK == k) ? 0 : -inf; if (memo[i][takenInFirstK] + 1) return memo[i][takenInFirstK]; int take = dp(i + 1, takenInFirstK + 1) + in[i].a + (in[i].b * takenInFirstK); int takeAndDelete = dp(i + 1, takenInFirstK) + (in[i].b * (k - 1)); return memo[i][takenInFirstK] = max(take, takeAndDelete); } int chooseLast; list<int> print, takeAndDel; void trace(int i, int takenInFirstK) { if (takenInFirstK > k) return; if (i >= n) return; int ans = dp(i, takenInFirstK); int take = dp(i + 1, takenInFirstK + 1) + in[i].a + (in[i].b * takenInFirstK); if (ans == take) { if (takenInFirstK + 1 == k) { chooseLast = in[i].idx; } else { print.push_back(in[i].idx); } trace(i + 1, takenInFirstK + 1); } else { takeAndDel.push_back(in[i].idx); trace(i + 1, takenInFirstK); } } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); ; int tc; cin >> tc; while (tc--) { cin >> n >> k; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; in[i].a = a; in[i].b = b; in[i].idx = i + 1; } sort(in, in + n, compare); for (int i = 0; i < n; i++) { for (int j = 0; j <= k; j++) { memo[i][j] = -1; } } chooseLast = -1; trace(0, 0); while (takeAndDel.size()) { int x = takeAndDel.front(); takeAndDel.pop_front(); print.push_back(x); print.push_back(-x); } if (chooseLast + 1) { print.push_back(chooseLast); } cout << print.size() << endl; while (print.size()) { int x = print.front(); print.pop_front(); cout << x << " "; } 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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, k; long long aa[100], bb[100], ww[100]; int tt; int dv[100][100][100]; long long dd[100][100][100]; int dt[100][100][100]; long long calc(int a, int b, int c) { if (a == 0 && b == 0) return 0; if (a == 0 || b < 0) return -999999999999999LL; if (dv[a][b][c] == tt) return dd[a][b][c]; if (a == c) { long long d = calc(a - 1, b, c) + aa[a] + (k - 1) * bb[a]; dd[a][b][c] = d; dt[a][b][c] = 3; } else { long long d1 = calc(a - 1, b, c) + (k - 1) * bb[a]; long long d2 = calc(a - 1, b - 1, c) + aa[a] + (b - 1) * bb[a]; if (d1 >= d2) { dd[a][b][c] = d1; dt[a][b][c] = 1; } else { dd[a][b][c] = d2; dt[a][b][c] = 2; } } dv[a][b][c] = tt; return dd[a][b][c]; } void lol() { cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> aa[i] >> bb[i]; ww[i] = i; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n - 1; j++) { if (bb[j] > bb[j + 1]) { swap(aa[j], aa[j + 1]); swap(bb[j], bb[j + 1]); swap(ww[j], ww[j + 1]); } } } long long pp = 0; int pi = 0; for (int i = 1; i <= n; i++) { long long uu = calc(n, k - 1, i); if (uu >= pp) { pp = uu; pi = i; } } vector<int> v1, v2; int ak = k; k--; while (n >= 1) { if (dt[n][k][pi] == 1) { if (ak != 1) v1.push_back(ww[n]); n--; } else if (dt[n][k][pi] == 2) { v2.push_back(ww[n]); n--; k--; } else if (dt[n][k][pi] == 3) { n--; } } reverse(v2.begin(), v2.end()); cout << v1.size() * 2 + v2.size() + 1 << "\n"; for (auto x : v2) cout << x << " "; for (auto x : v1) cout << x << " " << -x << " "; cout << ww[pi] << "\n"; } int main() { int t; cin >> t; for (int i = 1; i <= t; i++) { tt = i; lol(); } }
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" ] }
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 = -infLL; if (cnt + 1 <= k) { ret = max(ret, cnt * p[idx].first.first + p[idx].first.second + solve(idx + 1, cnt + 1)); } ret = max(ret, ((k - 1) * p[idx].first.first) + solve(idx + 1, cnt)); return dp[idx][cnt] = ret; } void trace(int idx, int cnt) { if (idx == n + 1) return; long long int ret1 = -infLL, ret2 = -infLL; if (cnt + 1 <= k) ret1 = cnt * p[idx].first.first + p[idx].first.second + solve(idx + 1, cnt + 1); ret2 = ((k - 1) * p[idx].first.first) + solve(idx + 1, cnt); if (ret1 >= ret2) { good.push_back(p[idx].second); trace(idx + 1, cnt + 1); } else { bad.push_back(p[idx].second); trace(idx + 1, cnt); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cout.unsetf(ios::floatfield); cout.precision(20); cout.setf(ios::fixed, ios::floatfield); ; cin >> t; while (t--) { good.clear(); bad.clear(); 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); cout << good.size() + (2 * bad.size()) << '\n'; for (int i = 0; i < good.size() - 1; ++i) { cout << good[i] << " "; } for (int i = 0; i < bad.size(); ++i) { cout << bad[i] << " " << -1 * bad[i] << " "; } cout << good[good.size() - 1] << '\n'; } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
java
import java.io.*; import java.util.*; public class gym{ static int n,k; static int[][]in; static long[][]memo; static long inf=(long)1e12; static long dp(int i,int takenInFirstK){ if(takenInFirstK>k)return -inf; if(i>=n)return (takenInFirstK==k)?0:-inf; if(memo[i][takenInFirstK]!=-1)return memo[i][takenInFirstK]; long take=dp(i+1, takenInFirstK+1)+in[i][0]+(in[i][1]*takenInFirstK); long takeAndDelete=dp(i+1, takenInFirstK)+(in[i][1]*(k-1)); return memo[i][takenInFirstK]=Math.max(take, takeAndDelete); } static int chooseLast; static LinkedList<Integer> print,takeAndDel; static void trace(int i,int takenInFirstK){ if(takenInFirstK>k)return; if(i>=n)return; long ans=dp(i, takenInFirstK); long take=dp(i+1, takenInFirstK+1)+in[i][0]+(in[i][1]*takenInFirstK); if(ans==take){ if(takenInFirstK+1==k){ chooseLast=in[i][2]; } else{ print.add(in[i][2]); } trace(i+1, takenInFirstK+1); } else{ takeAndDel.add(in[i][2]); trace(i+1, takenInFirstK); } } static void main() throws Exception{ n=sc.nextInt();k=sc.nextInt(); in=new int[n][]; for(int i=0;i<n;i++){ in[i]=new int[] {sc.nextInt(),sc.nextInt(),i+1}; } Arrays.sort(in,(x,y)->x[1]==y[1]?x[0]-y[0]:x[1]-y[1]); memo=new long[n][k+1]; for(int i=0;i<n;i++){ for(int j=0;j<=k;j++){ memo[i][j]=-1; } } chooseLast=-1; takeAndDel=new LinkedList<>(); print=new LinkedList<>(); trace(0, 0); while(takeAndDel.size()>0){ int x=takeAndDel.pollFirst(); print.add(x); print.add(-x); } if(chooseLast!=-1){ print.add(chooseLast); } pw.println(print.size()); while(print.size()>0){ int x=print.pollFirst(); pw.print(x+" "); } pw.println(); } public static void main(String[] args) throws Exception{ pw=new PrintWriter(System.out); sc = new MScanner(System.in); int tc=sc.nextInt(); while(tc-->0)main(); pw.flush(); } static PrintWriter pw; static MScanner sc; static class MScanner { StringTokenizer st; BufferedReader br; public MScanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public MScanner(String file) throws Exception { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int[] intArr(int n) throws IOException { int[]in=new int[n];for(int i=0;i<n;i++)in[i]=nextInt(); return in; } public long[] longArr(int n) throws IOException { long[]in=new long[n];for(int i=0;i<n;i++)in[i]=nextLong(); return in; } public int[] intSortedArr(int n) throws IOException { int[]in=new int[n];for(int i=0;i<n;i++)in[i]=nextInt(); shuffle(in); Arrays.sort(in); return in; } public long[] longSortedArr(int n) throws IOException { long[]in=new long[n];for(int i=0;i<n;i++)in[i]=nextLong(); shuffle(in); Arrays.sort(in); return in; } public Integer[] IntegerArr(int n) throws IOException { Integer[]in=new Integer[n];for(int i=0;i<n;i++)in[i]=nextInt(); return in; } public Long[] LongArr(int n) throws IOException { Long[]in=new Long[n];for(int i=0;i<n;i++)in[i]=nextLong(); return in; } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public long nextLong() throws IOException { return Long.parseLong(next()); } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } } static void shuffle(int[]in) { for(int i=0;i<in.length;i++) { int idx=(int)(Math.random()*in.length); int tmp=in[i]; in[i]=in[idx]; in[idx]=tmp; } } static void shuffle(long[]in) { for(int i=0;i<in.length;i++) { int idx=(int)(Math.random()*in.length); long tmp=in[i]; in[i]=in[idx]; in[idx]=tmp; } } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long INFLL = 1e18; const int MOD = 1e9 + 7; const int MAXN = 2e5 + 5; tuple<int, int, int> minions[111]; bool picked[111]; long long dp[111][111]; int n, k; vector<int> ans; void mxze(long long &a, long long b) { a = max(a, b); } long long solve(int i, int j) { if (i == n) return 0; if (dp[i][j] != -1) return dp[i][j]; long long ret = 0; auto [b, a, id] = minions[i]; if (j < k) mxze(ret, b * j + a + solve(i + 1, j + 1)); if (n - i > k - j) mxze(ret, b * (k - 1) + solve(i + 1, j)); return dp[i][j] = ret; } int main() { int t; scanf("%d", &t); while (t--) { memset(dp, -1, sizeof(dp)); memset(picked, false, sizeof(picked)); ans.clear(); scanf("%d", &n), scanf("%d", &k); for (int i = 0; i < (int)n; i++) { int a, b; scanf("%d", &a), scanf("%d", &b); minions[i] = {b, a, i + 1}; } sort(minions, minions + n); int i = 0, j = 0; while (i < n) { auto [b, a, id] = minions[i]; if (j < k && solve(i, j) == b * j + a + solve(i + 1, j + 1)) { picked[id] = true; ans.push_back(id); i++, j++; } else i++; } int lst = ans.back(); ans.pop_back(); for (int i = 1; i < (int)n + 1; i++) if (!picked[i]) ans.push_back(i), ans.push_back(-i); ans.push_back(lst); printf("%zu\n", ans.size()); for (auto id : ans) printf("%d ", id); printf("\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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int inf = 1012345678; vector<int> transform(int n, vector<int> arr, vector<int> perm) { vector<int> res(n); for (int i = 0; i < n; ++i) { res[i] = arr[perm[i]]; } return res; } int main() { int Q; cin >> Q; while (Q--) { int N, K; cin >> N >> K; vector<int> A(N), B(N); for (int i = 0; i < N; ++i) { cin >> A[i] >> B[i]; } vector<int> perm(N); for (int i = 0; i < N; ++i) { perm[i] = i; } sort(perm.begin(), perm.end(), [&](int i, int j) { return B[i] < B[j]; }); A = transform(N, A, perm); B = transform(N, B, perm); int bsum = 0; for (int i = 0; i < N; ++i) { bsum += B[i] * (K - 1); } vector<vector<int> > dp(N + 1, vector<int>(K, -inf)); dp[0][0] = bsum; for (int i = 0; i < N; ++i) { for (int j = 0; j < K; ++j) { dp[i + 1][j] = dp[i][j]; if (j >= 1) { dp[i + 1][j] = max(dp[i + 1][j], dp[i][j - 1] + A[i] - B[i] * (K - j)); } } } int opt = -1, optval = -inf; for (int i = 0; i < N; ++i) { if (dp[i][K - 1] == -inf) continue; int val = dp[i][K - 1]; int mx = *max_element(A.begin() + i, A.end()); val += mx; if (optval < val) { optval = val; opt = i; } } vector<int> seq; int ptr = K - 1; for (int i = opt - 1; i >= 0; --i) { if (dp[i][ptr] != dp[i + 1][ptr]) { seq.push_back(i); --ptr; } } reverse(seq.begin(), seq.end()); int last = max_element(A.begin() + opt, A.end()) - A.begin(); vector<int> ans; for (int i : seq) { ans.push_back(perm[i] + 1); } for (int i = 0; i < N; ++i) { if (!binary_search(seq.begin(), seq.end(), i) && i != last) { ans.push_back(perm[i] + 1); ans.push_back(-(perm[i] + 1)); } } ans.push_back(perm[last] + 1); cout << ans.size() << endl; for (int i = 0; i < ans.size(); ++i) { if (i) cout << ' '; cout << ans[i]; } cout << endl; } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<ll>; const int maxn = 80; ll n, dp[maxn][maxn], p[maxn][maxn]; vector<pair<ll, ll>> a; vi used; map<pair<ll, ll>, vi> idx; vi ans; void track(int n, int k) { if (!n) return; if (p[n][k] == 2) { track(n - 1, k - 1); used[idx[a[n - 1]].back()] = 1; ans.push_back(idx[a[n - 1]].back()); idx[a[n - 1]].pop_back(); } else track(n - 1, k); } void solve() { int n, k; cin >> n >> k; memset(dp, -1, sizeof dp); a = vector<pair<ll, ll>>(n); dp[0][0] = 0; for (auto &i : a) cin >> i.second >> i.first; for (int i = 0; i < n; i++) idx[{a[i].second, a[i].first}].push_back(i + 1); sort(a.begin(), a.end()); for (auto &i : a) swap(i.first, i.second); for (int i = 0; i < n; i++) { for (int j = 0; j <= k; j++) { if (dp[i][j] < 0) continue; dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + (k - 1) * a[i].second); if (dp[i + 1][j] == dp[i][j] + (k - 1) * a[i].second) p[i + 1][j] = 1; if (j < k) { dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + j * a[i].second + a[i].first); if (dp[i + 1][j + 1] == dp[i][j] + j * a[i].second + a[i].first) p[i + 1][j + 1] = 2; } } } used = vi(n + 1, 0); ans.clear(); track(n, k); int ab = ans.back(); ans.pop_back(); for (int i = 1; i <= n; i++) if (!used[i]) ans.push_back(i), ans.push_back(-i); ans.push_back(ab); cout << ans.size() << '\n'; for (auto i : ans) cout << i << " "; cout << '\n'; } int main() { cin.tie(0)->sync_with_stdio(0); int t; cin >> t; while (t--) solve(); }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> const int inf = 0x3f3f3f3f, Inf = 0x7fffffff; const long long INF = 0x7fffffffffffffff; __inline__ __attribute__((always_inline)) unsigned int rnd() { static unsigned int seed = 416; return seed ^= seed >> 5, seed ^= seed << 17, seed ^= seed >> 13; } template <typename _Tp> _Tp gcd(const _Tp &a, const _Tp &b) { return (!b) ? a : gcd(b, a % b); } template <typename _Tp> __inline__ __attribute__((always_inline)) _Tp abs(const _Tp &a) { return a >= 0 ? a : -a; } template <typename _Tp> __inline__ __attribute__((always_inline)) _Tp max(const _Tp &a, const _Tp &b) { return a < b ? b : a; } template <typename _Tp> __inline__ __attribute__((always_inline)) _Tp min(const _Tp &a, const _Tp &b) { return a < b ? a : b; } template <typename _Tp> __inline__ __attribute__((always_inline)) void chmax(_Tp &a, const _Tp &b) { (a < b) && (a = b); } template <typename _Tp> __inline__ __attribute__((always_inline)) void chmin(_Tp &a, const _Tp &b) { (b < a) && (a = b); } template <typename _Tp> __inline__ __attribute__((always_inline)) void read(_Tp &x) { char ch(getchar()); bool f(false); while (ch < 48 || ch > 57) f |= ch == 45, ch = getchar(); x = ch & 15, ch = getchar(); while (ch >= 48 && ch <= 57) x = (((x << 2) + x) << 1) + (ch & 15), ch = getchar(); if (f) x = -x; } template <typename _Tp, typename... Args> __inline__ __attribute__((always_inline)) void read(_Tp &t, Args &...args) { read(t); read(args...); } __inline__ __attribute__((always_inline)) int read_str(char *s) { char ch(getchar()); while (ch == ' ' || ch == '\r' || ch == '\n') ch = getchar(); char *tar = s; *tar = ch, ch = getchar(); while (ch != ' ' && ch != '\r' && ch != '\n' && ch != EOF) *(++tar) = ch, ch = getchar(); return tar - s + 1; } const int N = 85; struct node { long long a, b; int id; __inline__ __attribute__((always_inline)) bool operator<( const node &o) const { return b < o.b; } } a[N]; long long dp[N][N]; bool opt[N][N]; bool used[N]; void MAIN() { memset(used, 0, sizeof(used)); int n, k; read(n, k); for (int i = 1; i <= n; ++i) { read(a[i].a, a[i].b); a[i].id = i; } std::sort(a + 1, a + n + 1); if (k == n) { printf("%d\n", n); for (int i = 1; i <= n; ++i) printf("%d ", a[i].id); printf("\n"); return; } auto solve = [&](int id) -> long long { memset(dp, -63, sizeof(dp)); memset(opt, 0, sizeof(opt)); dp[0][0] = 0; for (int i = 1; i <= n; ++i) { if (i == id) { for (int j = 0; j <= k; ++j) { dp[i][j] = dp[i - 1][j] + a[i].a + a[i].b * (k - 1); opt[i][j] = 0; } continue; } dp[i][0] = dp[i - 1][0] + (k - 1) * a[i].b; opt[i][0] = 0; for (int j = 1; j <= k; ++j) { dp[i][j] = dp[i - 1][j] + (k - 1) * a[i].b; opt[i][j] = 0; long long tmp = dp[i - 1][j - 1] + a[i].a + (j - 1) * a[i].b; if (tmp > dp[i][j]) { dp[i][j] = tmp; opt[i][j] = 1; } } } return dp[n][k - 1]; }; long long ans = 0; int pos = 0; for (int i = 1; i <= n; ++i) { long long tmp = solve(i); if (tmp > ans) { ans = tmp; pos = i; } } solve(pos); fprintf(stderr, "%lld %d\n", ans, pos); pos = a[pos].id; std::vector<int> v; int cur = n, cnt = k - 1; while (cur) { if (opt[cur][cnt]) { --cnt; v.push_back(a[cur].id); used[a[cur].id] = true; } --cur; } std::reverse(v.begin(), v.end()); std::vector<int> res; for (auto it : v) res.push_back(it); for (int i = 1; i <= n; ++i) { if (!used[i] && i != pos) { res.push_back(i); res.push_back(-i); } } res.push_back(pos); printf("%d\n", ((int)res.size())); for (auto it : res) printf("%d ", it); printf("\n"); } int main() { int _; read(_); while (_--) MAIN(); return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N_MAX = 77; int t; int n, k; struct Minion { int a, b; int index; }; bool operator<(const Minion &x, const Minion &y) { return x.b < y.b; } Minion v[N_MAX]; int dp[N_MAX][N_MAX]; bool step[N_MAX][N_MAX]; bool lastk[N_MAX]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> t; while (t--) { cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> v[i].a >> v[i].b; v[i].index = i; } sort(v + 1, v + n + 1); for (int j = 1; j <= k; j++) dp[0][j] = INT_MIN; for (int i = 1; i <= n; i++) { dp[i][0] = dp[i - 1][0] + (k - 1) * v[i].b; step[i][0] = false; for (int j = 1; j <= k; j++) { dp[i][j] = max(dp[i - 1][j] + (k - 1) * v[i].b, dp[i - 1][j - 1] + (j - 1) * v[i].b + v[i].a); if (dp[i][j] == dp[i - 1][j] + (k - 1) * v[i].b) step[i][j] = false; else step[i][j] = true; } } for (int i = n, j = k; i > 0; i--) if (step[i][j] == true) { lastk[i] = true; j--; } else lastk[i] = false; cout << n + (n - k) << "\n"; int imaxb = -1; for (int i = n; i >= 1; i--) if (lastk[i] == true) { imaxb = i; break; } for (int i = 1; i <= n; i++) if (lastk[i] == true && i != imaxb) cout << v[i].index << " "; for (int i = 1; i <= n; i++) if (lastk[i] == false) cout << v[i].index << " " << -v[i].index << " "; cout << v[imaxb].index << "\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" ] }
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--; } } } } cout << 2 * n - k << "\n"; 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" ] }
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 = 1; i <= n; ++i) { for (int j = 0; j <= min(k, n); ++j) { if (j > 0 and 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) { 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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 100 + 5; struct node { long long x, y; int id; bool operator<(const node &ope) const { return y < ope.y; } } a[maxn]; bool vis[maxn][maxn]; long long dp[maxn][maxn]; bool vis2[maxn]; inline void init() { memset(dp, -1, sizeof(dp)); memset(vis, false, sizeof(vis)); memset(vis2, false, sizeof(vis2)); } void solve(int n, int k) { init(); dp[0][0] = 0; sort(a + 1, a + 1 + n); for (int i = 1; i <= n; ++i) { int t = ((i) < (k) ? (i) : (k)); for (int j = 0; j <= t; ++j) { if (dp[i - 1][j] >= 0) dp[i][j] = dp[i - 1][j] + (k - 1) * 1ll * a[i].y; if (j > 0 && dp[i - 1][j - 1] >= 0) { if (dp[i][j] >= dp[i - 1][j - 1] + a[i].x + (j - 1) * 1ll * a[i].y) continue; dp[i][j] = dp[i - 1][j - 1] + a[i].x + (j - 1) * 1ll * a[i].y; vis[i][j] = true; } } } } void print(int n, int k) { printf("%d\n", k + (n - k) * 2); for (int i = n, j = k; i > 0 && j >= 0; --i) { if (vis[i][j]) vis2[i] = true, --j; } int t = 0; for (int i = 1; i <= n; ++i) { if (!vis2[i]) continue; if (++t == k) { t = a[i].id; break; } printf("%d ", a[i].id); } for (int i = 1; i <= n; ++i) { if (!vis2[i]) printf("%d %d ", a[i].id, -a[i].id); } printf("%d\n", t); } int main() { int T; scanf("%d", &T); while (T--) { int n, k; scanf("%d%d", &n, &k); for (int i = 1; i <= n; ++i) { scanf("%lld%lld", &a[i].x, &a[i].y); a[i].id = i; } solve(n, k); print(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" ] }
CORRECT
cpp
#include <bits/stdc++.h> template <typename T> inline void chkmin(T &x, T y) { x = min(x, y); } template <typename T> inline void chkmax(T &x, T y) { x = max(x, y); } template <typename T> inline void read(T &x) { T f = 1; x = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0'; x *= f; } const int N = 80; int n, k; struct Info { int first, second; int home; bool operator<(const Info &o) const { if (first != o.first) return first < o.first; else return second < o.second; } } a[N]; int memo[N][N]; inline int dp(int on, int used) { if (used > k) return -1e9; int &ans = memo[on][used]; if (ans != -1) return ans; if (on == n) { return ans = used == k ? 0 : int(-1e9); } else { return ans = std::max( dp(on + 1, used) + (k - 1) * a[on].first, dp(on + 1, used + 1) + a[on].second + used * a[on].first); } } int main() { int T; read(T); while (T--) { read(n); read(k); memset(memo, -1, sizeof(memo)); for (int i = 0; i < n; ++i) { read(a[i].second); read(a[i].first); a[i].second = -a[i].second; a[i].home = i + 1; } std ::sort(a, a + n); for (int i = 0; i < n; ++i) a[i].second = -a[i].second; std ::vector<int> bas; std ::vector<bool> got(n + 1, false); for (int i = 0, j = 0; i < n;) { if (dp(i, j) == dp(i + 1, j) + (k - 1) * a[i].first) { ++i; } else { bas.push_back(a[i].home); got[a[i].home] = true; ++i; ++j; } } std ::vector<int> ans; assert((int)bas.size() == k); for (int i = 0; i + 1 < k; ++i) ans.push_back(bas[i]); for (int i = 1; i <= n; ++i) { if (!got[i]) { ans.push_back(i); ans.push_back(-i); } } ans.push_back(bas.back()); printf("%d\n", (int)ans.size()); for (auto x : ans) printf("%d ", x); printf("\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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; constexpr long double m_pi = 3.1415926535897932L; constexpr long long MOD = 1000000007; constexpr long long INF = 1LL << 61; constexpr long double EPS = 1e-10; template <typename T> using vector2 = vector<vector<T>>; template <typename T> using vector3 = vector<vector2<T>>; string operator*(const string& s, int k) { if (k == 0) return ""; string p = (s + s) * (k / 2); if (k % 2 == 1) p += s; return p; } template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } struct Edge { int to, rev; long long cap; Edge(int _to, long long _cap, int _rev) { to = _to; cap = _cap; rev = _rev; } }; void add_edge(vector<vector<Edge>>& G, int from, int to, long long cap, bool revFlag, long long revCap) { G[from].push_back(Edge(to, cap, (long long)G[to].size())); if (revFlag) G[to].push_back(Edge(from, revCap, (long long)G[from].size() - 1)); } long long max_flow_dfs(vector<vector<Edge>>& G, long long v, long long t, long long f, vector<bool>& used) { if (v == t) return f; used[v] = true; for (int i = 0; i < G[v].size(); ++i) { Edge& e = G[v][i]; if (!used[e.to] && e.cap > 0) { long long d = max_flow_dfs(G, e.to, t, min(f, e.cap), used); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } long long max_flow(vector<vector<Edge>>& G, long long s, long long t) { long long flow = 0; for (;;) { vector<bool> used(G.size()); for (long long(i) = (long long)(0); i < (long long)(used.size()); i++) used[i] = false; long long f = max_flow_dfs(G, s, t, INF, used); if (f == 0) { return flow; } flow += f; } } void BellmanFord(vector<vector<Edge>>& G, long long s, vector<long long>& d, vector<long long>& negative) { d.resize(G.size()); negative.resize(G.size()); for (long long(i) = (long long)(0); i < (long long)(d.size()); i++) d[i] = INF; for (long long(i) = (long long)(0); i < (long long)(d.size()); i++) negative[i] = false; d[s] = 0; for (long long(k) = (long long)(0); k < (long long)(G.size() - 1); k++) { for (long long(i) = (long long)(0); i < (long long)(G.size()); i++) { for (long long(j) = (long long)(0); j < (long long)(G[i].size()); j++) { if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) { d[G[i][j].to] = d[i] + G[i][j].cap; } } } } for (long long(k) = (long long)(0); k < (long long)(G.size() - 1); k++) { for (long long(i) = (long long)(0); i < (long long)(G.size()); i++) { for (long long(j) = (long long)(0); j < (long long)(G[i].size()); j++) { if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) { d[G[i][j].to] = d[i] + G[i][j].cap; negative[G[i][j].to] = true; } if (negative[i] == true) negative[G[i][j].to] = true; } } } } void Dijkstra(vector<vector<Edge>>& G, long long s, vector<long long>& d) { d.resize(G.size()); for (long long(i) = (long long)(0); i < (long long)(d.size()); i++) d[i] = INF; d[s] = 0; priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, greater<pair<long long, long long>>> q; q.push(make_pair(0, s)); while (!q.empty()) { pair<long long, long long> a = q.top(); q.pop(); if (d[a.second] < a.first) continue; for (long long(i) = (long long)(0); i < (long long)(G[a.second].size()); i++) { Edge e = G[a.second][i]; if (d[e.to] > d[a.second] + e.cap) { d[e.to] = d[a.second] + e.cap; q.push(make_pair(d[e.to], e.to)); } } } } void WarshallFloyd(vector<vector<Edge>>& G, vector<vector<long long>>& d) { d.resize(G.size()); for (long long(i) = (long long)(0); i < (long long)(d.size()); i++) d[i].resize(G.size()); for (long long(i) = (long long)(0); i < (long long)(d.size()); i++) { for (long long(j) = (long long)(0); j < (long long)(d[i].size()); j++) { d[i][j] = ((i != j) ? INF : 0); } } for (long long(i) = (long long)(0); i < (long long)(G.size()); i++) { for (long long(j) = (long long)(0); j < (long long)(G[i].size()); j++) { chmin(d[i][G[i][j].to], G[i][j].cap); } } for (long long(i) = (long long)(0); i < (long long)(G.size()); i++) { for (long long(j) = (long long)(0); j < (long long)(G.size()); j++) { for (long long(k) = (long long)(0); k < (long long)(G.size()); k++) { chmin(d[j][k], d[j][i] + d[i][k]); } } } } bool tsort(vector<vector<Edge>>& graph, vector<long long>& order) { int n = graph.size(), k = 0; vector<long long> in(n); for (auto& es : graph) for (auto& e : es) in[e.to]++; priority_queue<long long, vector<long long>, greater<long long>> que; for (long long(i) = (long long)(0); i < (long long)(n); i++) if (in[i] == 0) que.push(i); while (que.size()) { int v = que.top(); que.pop(); order.push_back(v); for (auto& e : graph[v]) if (--in[e.to] == 0) que.push(e.to); } if (order.size() != n) return false; else return true; } class Lca { public: const int n = 0; const int log2_n = 0; std::vector<std::vector<int>> parent; std::vector<int> depth; Lca() {} Lca(const vector<vector<Edge>>& g, int root) : n(g.size()), log2_n(log2(n) + 1), parent(log2_n, std::vector<int>(n)), depth(n) { dfs(g, root, -1, 0); for (int k = 0; k + 1 < log2_n; k++) { for (int v = 0; v < (int)g.size(); v++) { if (parent[k][v] < 0) parent[k + 1][v] = -1; else parent[k + 1][v] = parent[k][parent[k][v]]; } } } void dfs(const vector<vector<Edge>>& g, int v, int p, int d) { parent[0][v] = p; depth[v] = d; for (auto& e : g[v]) { if (e.to != p) dfs(g, e.to, v, d + 1); } } int get(int u, int v) { if (depth[u] > depth[v]) std::swap(u, v); for (int k = 0; k < log2_n; k++) { if ((depth[v] - depth[u]) >> k & 1) { v = parent[k][v]; } } if (u == v) return u; for (int k = log2_n - 1; k >= 0; k--) { if (parent[k][u] != parent[k][v]) { u = parent[k][u]; v = parent[k][v]; } } return parent[0][u]; } }; void visit(const vector<vector<Edge>>& g, int v, vector<vector<int>>& scc, stack<int>& S, vector<int>& inS, vector<int>& low, vector<int>& num, int& time) { low[v] = num[v] = ++time; S.push(v); inS[v] = true; for (decltype((g[v]).begin()) e = (g[v]).begin(); e != (g[v]).end(); ++e) { int w = e->to; if (num[w] == 0) { visit(g, w, scc, S, inS, low, num, time); low[v] = min(low[v], low[w]); } else if (inS[w]) low[v] = min(low[v], num[w]); } if (low[v] == num[v]) { scc.push_back(vector<int>()); while (1) { int w = S.top(); S.pop(); inS[w] = false; scc.back().push_back(w); if (v == w) break; } } } void stronglyConnectedComponents(const vector<vector<Edge>>& g, vector<vector<int>>& scc) { const int n = g.size(); vector<int> num(n), low(n); stack<int> S; vector<int> inS(n); int time = 0; for (long long(u) = (long long)(0); u < (long long)(n); u++) if (num[u] == 0) visit(g, u, scc, S, inS, low, num, time); } class UnionFind { vector<int> data; long long num; public: UnionFind(int size) : data(size, -1), num(size) {} bool unite(int x, int y) { x = root(x); y = root(y); if (x != y) { if (data[y] < data[x]) swap(x, y); data[x] += data[y]; data[y] = x; } num -= (x != y); return x != y; } bool findSet(int x, int y) { return root(x) == root(y); } int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); } long long size(int x) { return -data[root(x)]; } long long numSet() { return num; } }; template <typename T, typename F> class SegmentTree { private: T identity; F merge; long long n; vector<T> dat; public: SegmentTree(F f, T id, vector<T> v) : merge(f), identity(id) { int _n = v.size(); n = 1; while (n < _n) n *= 2; dat.resize(2 * n - 1, identity); for (long long(i) = (long long)(0); i < (long long)(_n); i++) dat[n + i - 1] = v[i]; for (int i = n - 2; i >= 0; i--) dat[i] = merge(dat[i * 2 + 1], dat[i * 2 + 2]); } SegmentTree(F f, T id, int _n) : merge(f), identity(id) { n = 1; while (n < _n) n *= 2; dat.resize(2 * n - 1, identity); } void set_val(int i, T x) { i += n - 1; dat[i] = x; while (i > 0) { i = (i - 1) / 2; dat[i] = merge(dat[i * 2 + 1], dat[i * 2 + 2]); } } T query(int l, int r) { T left = identity, right = identity; l += n - 1; r += n - 1; while (l < r) { if ((l & 1) == 0) left = merge(left, dat[l]); if ((r & 1) == 0) right = merge(dat[r - 1], right); l = l / 2; r = (r - 1) / 2; } return merge(left, right); } }; template <typename T> class FenwickTree { vector<T> data; int n; int p; public: FenwickTree(int n) : n(n) { data.resize(n + 1LL, 0); p = 1; while (p < data.size()) p *= 2; } T sum(int k) { T ret = 0; for (; k > 0; k -= k & -k) ret += data[k]; return (ret); } T sum(int a, int b) { return sum(b) - sum(a); } void add(int k, T x) { for (++k; k <= n; k += k & -k) data[k] += x; } int lower_bound(long long w) { if (w <= 0) return -1; int x = 0; for (int k = p / 2; k > 0; k /= 2) { if (x + k <= n && data[x + k] < w) w -= data[x + k], x += k; } return x; } }; void divisor(long long n, vector<long long>& ret) { for (long long i = 1; i * i <= n; i++) { if (n % i == 0) { ret.push_back(i); if (i * i != n) ret.push_back(n / i); } } sort(ret.begin(), ret.end()); } void prime_factorization(long long n, vector<pair<long long, long long>>& ret) { for (long long i = 2; i * i <= n; i++) { if (n % i == 0) { ret.push_back({i, 0}); while (n % i == 0) { n /= i; ret[ret.size() - 1].second++; } } } if (n != 1) ret.push_back({n, 1}); } long long mod_pow(long long x, long long n, long long mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } long long mod_inv(long long x, long long mod) { return mod_pow(x, mod - 2, mod); } class Combination { public: vector<long long> fact; vector<long long> inv; long long mod; long long mod_inv(long long x) { long long n = mod - 2LL; long long res = 1LL; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } long long nCr(long long n, long long r) { if (n < r) return 0; if (n < mod) return ((fact[n] * inv[r] % mod) * inv[n - r]) % mod; long long ret = 1; while (n || r) { long long _n = n % mod, _r = r % mod; n /= mod; r /= mod; (ret *= nCr(_n, _r)) %= mod; } return ret; } long long nPr(long long n, long long r) { return (fact[n] * inv[n - r]) % mod; } long long nHr(long long n, long long r) { return nCr(r + n - 1, r); } Combination(long long _n, long long _mod) { mod = _mod; long long n = min(_n + 1, mod); fact.resize(n); fact[0] = 1; for (long long(i) = (long long)(0); i < (long long)(n - 1); i++) { fact[i + 1] = (fact[i] * (i + 1LL)) % mod; } inv.resize(n); inv[n - 1] = mod_inv(fact[n - 1]); for (int i = n - 1; i > 0; i--) { inv[i - 1] = inv[i] * i % mod; } } }; long long popcount(long long x) { x = (x & 0x5555555555555555) + (x >> 1 & 0x5555555555555555); x = (x & 0x3333333333333333) + (x >> 2 & 0x3333333333333333); x = (x & 0x0F0F0F0F0F0F0F0F) + (x >> 4 & 0x0F0F0F0F0F0F0F0F); x = (x & 0x00FF00FF00FF00FF) + (x >> 8 & 0x00FF00FF00FF00FF); x = (x & 0x0000FFFF0000FFFF) + (x >> 16 & 0x0000FFFF0000FFFF); x = (x & 0x00000000FFFFFFFF) + (x >> 32 & 0x00000000FFFFFFFF); return x; } void solve() { long long n, k; cin >> n >> k; using T = tuple<long long, long long, long long>; vector<T> m(n); for (long long(i) = (long long)(0); i < (long long)(n); i++) { long long a, b; cin >> a >> b; m[i] = {b, a, i + 1}; } sort((m).begin(), (m).end()); vector3<long long> dp( n + 1, vector2<long long>(k + 1, vector<long long>(n - k + 1, -INF))); dp[0][0][0] = 0; for (long long(i) = (long long)(0); i < (long long)(n); i++) { auto [b, a, num] = m[i]; for (long long(j) = (long long)(0); j < (long long)(k + 1); j++) { for (long long(l) = (long long)(0); l < (long long)(n - k + 1); l++) { if (dp[i][j][l] == -INF) continue; if (l != n - k) chmax(dp[i + 1][j][l + 1], dp[i][j][l] + (k - 1) * b); if (j != k) chmax(dp[i + 1][j + 1][l], dp[i][j][l] + j * b + a); } } } cout << 2 * n - k << "\n"; pair<long long, long long> now = {k, n - k}; vector<long long> s, t; for (int i = n - 1; i >= 0; i--) { auto [b, a, num] = m[i]; auto [j, l] = now; if (l && dp[i + 1][j][l] == dp[i][j][l - 1] + (k - 1) * b) { t.push_back(num); now.second--; } else { s.push_back(num); now.first--; } } reverse((s).begin(), (s).end()); for (long long(i) = (long long)(0); i < (long long)(k - 1); i++) cout << s[i] << " "; for (long long(i) = (long long)(0); i < (long long)(n - k); i++) cout << t[i] << " " << -1 * t[i] << " "; cout << s.back() << "\n"; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n; cin >> n; while (n--) 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" ] }
CORRECT
java
import java.util.Arrays; import java.util.Scanner; /* 1 5 2 5 3 7 0 5 0 4 0 10 0 */ public class SummoningMinions2 { public static void main(String[] args) { Scanner fs=new Scanner(System.in); int T=fs.nextInt(); for (int tt=0; tt<T; tt++) { int n=fs.nextInt(), k=fs.nextInt(); long ans=0; int[][] cost=new int[k][n]; for (int i=0; i<n; i++) { int a=fs.nextInt(), b=fs.nextInt(); ans+=(k-1)*(long)b; for (int spot=0; spot<k; spot++) cost[spot][i]=-(-(k-1)*b+spot*b+a); } // for (int spot=0; spot<k; spot++) { // for (int i=0; i<n; i++) { // System.out.print(cost[spot][i]+" "); // } // System.out.println(); // } hungarian(cost); // for (int spot=0; spot<k; spot++) { // System.out.println("Matched spot "+spot+" with "+rowMatchesWith[spot]); // } boolean[] chosen=new boolean[n]; for (int i=0; i<k; i++) chosen[rowMatchesWith[i]]=true; System.out.println(k-1 + 2*(n-k) + 1); for (int i=0; i+1<k; i++) { System.out.print(1+rowMatchesWith[i]+" "); } for (int i=0; i<n; i++) if (!chosen[i]) System.out.print((1+i)+" -"+(1+i)+" "); System.out.println(rowMatchesWith[k-1]+1); } } // finds MINIMUM total matching cost of // each row to some column, using each col at most once // **nRows must be <= nCols** //rowMatchWith[i] = the column that row i matched with static int[] rowMatchesWith; static int hungarian(int[][] a) { int n=a.length, m=a[0].length; if (n>m) throw null; int[] u=new int[n], v=new int[m+1]; //edge (i->j) >= u[i]+v[j] int[] p=new int[m+1]; //p[j] = left match for right node j Arrays.fill(p, -1); for (int i=0; i<n; i++) {//find alternating path for node i int j0=m; p[j0]=i; int[] dist=new int[m+1], from=new int[m+1]; boolean[] seen=new boolean[m+1]; Arrays.fill(dist, Integer.MAX_VALUE); Arrays.fill(from, -1); while (p[j0]!=-1) { seen[j0]=true; int i0=p[j0], delta=Integer.MAX_VALUE, j1=-1; for (int j=0; j<m; j++) {//consider edges i0 -> everything if (seen[j]) continue; int candDist=a[i0][j]-u[i0]-v[j]; if (candDist<dist[j]) {dist[j]=candDist; from[j]=j0;} if (dist[j]<delta) {delta=dist[j]; j1=j;} } //it costs at least delta to get somewhere else, //subtract that from all distances and add cost to u, v, arrays //from all done -> not done edges for (int j=0; j<=m; j++) { if (seen[j]) {u[p[j]]+=delta; v[j]-=delta;} else dist[j]-=delta; } j0=j1; } //flip alternating path while (j0!=m) { int j1=from[j0]; p[j0]=p[j1]; j0=j1; } } //sum of deltas is stored at v[m] coincidentally int ans=-v[m]; rowMatchesWith=new int[n]; for (int j=0; j<m; j++) if (p[j]!=-1) rowMatchesWith[p[j]]=j; return ans; } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
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) flag[i][j] = 0; 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); 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 = 0; i <= n; ++i) for (int j = 0; j <= m; ++j) f[i][j] = -inf; 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]]); 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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 100; inline int Read(int &r) { int b = 1, c = getchar(); r = 0; while (c < '0' || '9' < c) b = c == '-' ? -1 : b, c = getchar(); while ('0' <= c && c <= '9') r = (r << 1) + (r << 3) + (c ^ '0'), c = getchar(); return r *= b; } int n, m, ndel, cas, tra[N][N], rem[N], del[N]; long long f[N][N]; struct DATA { int key, add, id; DATA() {} DATA(const int &_key, const int &_add, const int &_id) : key(_key), add(_add), id(_id) {} inline friend bool operator<(const DATA &A, const DATA &B) { return A.add < B.add; } } da[N]; int main() { Read(cas); while (cas--) { Read(n), Read(m); for (int i = 1, key, add; i <= n; i++) { Read(key), Read(add); da[i] = DATA(key, add, i); } sort(da + 1, da + 1 + n); memset(f, -0x3f, sizeof f); f[0][0] = 0; for (int i = 1; i <= n; i++) for (int j = 0; j <= i && j <= m; j++) { f[i][j] = f[i - 1][j] + 1ll * (m - 1) * da[i].add; tra[i][j] = 0; long long tmp = f[i - 1][j - 1] + (j - 1ll) * da[i].add + da[i].key; if (j && f[i][j] <= tmp) f[i][j] = tmp, tra[i][j] = 1; } ndel = 0; for (int i = n, j = m; i; j -= tra[i][j], i--) if (tra[i][j]) rem[j] = da[i].id; else del[++ndel] = da[i].id; printf("%d\n", m + 2 * ndel); bool first = true; for (int i = 1; i < m; i++) { if (first) first = false; else putchar(' '); printf("%d", rem[i]); } for (int i = 1; i <= ndel; i++) { if (first) first = false; else putchar(' '); printf("%d -%d", del[i], del[i]); } if (first) first = false; else putchar(' '); printf("%d\n", rem[m]); } 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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; int n, k, dp[76][76]; array<int, 3> a[75]; bool last[76][76]; void solve() { 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); for (int i = 1; i <= n; ++i) { for (int j = 0; j <= i; ++j) { dp[i][j] = (j < i ? dp[i - 1][j] : -INF) + (k - 1) * a[i - 1][0]; last[i][j] = 0; if (j) { int c = dp[i - 1][j - 1] + (j - 1) * a[i - 1][0] + a[i - 1][1]; if (c > dp[i][j]) { dp[i][j] = c; last[i][j] = 1; } } } } vector<int> t, nt; for (int i = n, j = k; i; --i) { if (last[i][j]) { t.push_back(a[i - 1][2]); --j; } else nt.push_back(a[i - 1][2]); } assert(t.size() == k); reverse(t.begin(), t.end()); cout << 2 * n - k << "\n"; for (int i = 0; i < k - 1; ++i) cout << t[i] + 1 << " "; for (int i : nt) cout << i + 1 << " " << -(i + 1) << " "; cout << t[k - 1] + 1 << "\n"; } int main() { ios::sync_with_stdio(0); cin.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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 77; int t, n, k, inv[N]; long long a[N], b[N], a2[N], b2[N], dp[N][N]; bool summon[N][N], s[N], ans[N][N]; long long solve() { dp[0][0] = 0; for (int i = (1); i < (n + 1); i++) { for (int j = (0); j < (min(k, i) + 1); j++) { dp[i][j] = -1; summon[i][j] = true; if (j > 0) { dp[i][j] = dp[i - 1][j - 1] + a[i] + (j - 1) * b[i]; } if (j < i && dp[i - 1][j] + b[i] * (k - 1) > dp[i][j]) { summon[i][j] = false; dp[i][j] = dp[i - 1][j] + b[i] * (k - 1); } } } return dp[n][k]; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> t; while (t--) { cin >> n >> k; vector<tuple<int, int, int>> ve; for (int i = (1); i < (n + 1); i++) { cin >> a[i] >> b[i]; ve.emplace_back(b[i], a[i], i); } sort((ve).begin(), (ve).end()); for (int i = (1); i < (n + 1); i++) { tie(b[i], a[i], inv[i]) = ve[i - 1]; } solve(); int i = n, j = k; while (i > 0) { if (summon[i][j]) { j--; s[i] = true; } else { s[i] = false; } i--; } int cnt = 0; vector<int> v; int last = 0; for (int i = (1); i < (n + 1); i++) { if (s[i]) { if (cnt < k - 1) { v.push_back(inv[i]); cnt++; } else { last = inv[i]; } } } for (int i = (1); i < (n + 1); i++) { if (!s[i]) { v.push_back(inv[i]); v.push_back(-inv[i]); } } v.push_back(last); cout << ((int)(v).size()) << '\n'; for (int x : v) { cout << x << ' '; } 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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int T, N, K, a, b; int main(int argc, const char *argv[]) { cin >> T; for (int t = 0; t < T; t++) { cin >> N >> K; vector<pair<int, pair<int, int>>> minions; for (int i = 1; i <= N; i++) { cin >> a >> b; minions.push_back(make_pair(b, make_pair(a, i))); } sort(minions.begin(), minions.end()); int memo[80][80] = {0}; int choose[80][80] = {0}; for (int i = 0; i < 80; i++) { for (int j = 0; j < 80; j++) { memo[i][j] = INT_MIN; } } for (int i = N; i >= 0; i--) for (int j = K; j >= 0; j--) { if (i == N) { memo[i][j] = j == 0 ? 0 : INT_MIN; continue; } if (j > 0) { int keep = minions[i].second.first + minions[i].first * (K - j) + memo[i + 1][j - 1]; if (keep > memo[i][j]) { memo[i][j] = keep; choose[i][j] = 1; } } int notKeep = minions[i].first * (K - 1) + memo[i + 1][j]; if (notKeep > memo[i][j]) { memo[i][j] = notKeep; choose[i][j] = 0; } } vector<int> keep, notKeep; int num = K; for (int i = 0; i < N; i++) { if (choose[i][num] == 1) { keep.push_back(minions[i].second.second); num--; } else { notKeep.push_back(minions[i].second.second); } } vector<int> moves; for (int i = 0; i < (int)keep.size() - 1; i++) moves.push_back(keep[i]); for (int i = 0; i < (int)notKeep.size(); i++) { moves.push_back(notKeep[i]); moves.push_back(-notKeep[i]); } if ((int)keep.size() > 0) { moves.push_back(keep[(int)keep.size() - 1]); } cout << moves.size() << endl; for (int i = 0; i < (int)moves.size(); i++) cout << moves[i] << (i == (int)moves.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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; namespace IO { const int buffer_size = 1e5 + 5; char buf[buffer_size], *S, *T; bool flag_EOF; inline char read_char() { if (S == T) T = (S = buf) + fread(buf, 1, buffer_size, stdin); return S != T ? *(S++) : EOF; } inline int read_int() { int flag = 1; char c = read_char(); while (c < '0' || c > '9') { if (c == EOF) { flag_EOF = true; return 0; } flag = (c == '-' ? -1 : 1); c = read_char(); } int x = 0; while (c >= '0' && c <= '9') { x = x * 10 + (c ^ 48); c = read_char(); } return x * flag; } char st[13]; int _top; inline void Write(int x) { if (!x) { putchar('0'); return; } if (x < 0) { putchar('-'); x = -x; } while (x) { st[++_top] = x % 10 + '0'; x /= 10; } while (_top > 0) putchar(st[_top--]); } } // namespace IO const int max_n = 75 + 5; int a[max_n], b[max_n]; struct node { int a, b, id; } p[max_n]; inline bool cmp(const node &x, const node &y) { return x.b < y.b; } int f[max_n][max_n]; bool mark[max_n][max_n]; bool use[max_n]; vector<int> ans; int st[max_n], _top; int main() { int T; scanf("%d", &T); while (T--) { int n, k; scanf("%d%d", &n, &k); for (int i = 1; i <= n; ++i) scanf("%d%d", a + i, b + i); int Ans = 0; ans.clear(); for (int i = 1; i <= n; ++i) { int now = a[i] + b[i] * (k - 1); for (int j = 1; j <= n - 1; ++j) p[j].a = a[j], p[j].b = b[j], p[j].id = j; p[i].a = a[n], p[i].b = b[n], p[i].id = n; sort(p + 1, p + n, cmp); memset(f, 128, sizeof(f)); f[0][0] = 0; for (int x = 1; x <= n - 1; ++x) for (int y = 0; y <= k - 1 && y <= x; ++y) { f[x][y] = f[x - 1][y] + p[x].b * (k - 1); mark[x][y] = false; if (y > 0) { int v = f[x - 1][y - 1] + p[x].a + p[x].b * (y - 1); if (v > f[x][y]) { f[x][y] = v; mark[x][y] = true; } } } now += f[n - 1][k - 1]; if (now > Ans) { Ans = now; ans.clear(); for (int j = 1; j <= n; ++j) use[j] = false; int x_now = n - 1, y_now = k - 1; while (x_now || y_now) { if (mark[x_now][y_now]) { use[p[x_now].id] = true; st[++_top] = p[x_now].id; --x_now, --y_now; } else --x_now; } while (_top > 0) ans.push_back(st[_top--]); use[i] = true; for (int j = 1; j <= n; ++j) { if (!use[j]) { ans.push_back(j); ans.push_back(-j); } } ans.push_back(i); } } printf("%d\n", int(ans.size())); for (int i = 0; i < int(ans.size()); ++i) printf("%d%c", ans[i], i + 1 < int(ans.size()) ? ' ' : '\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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long inf; const double eps = 1e-8; const double pi = acos(-1.0); template <class T, class T2> long long chkmin(T &a, T2 b) { return a > b ? a = b, 1 : 0; } template <class T, class T2> long long chkmax(T &a, T2 b) { return a < b ? a = b, 1 : 0; } template <class T> T sqr(T a) { return a * a; } template <class T, class T2> T mmin(T a, T2 b) { return a < b ? a : b; } template <class T, class T2> T mmax(T a, T2 b) { return a > b ? a : b; } template <class T> T aabs(T a) { return a < 0 ? -a : a; } template <class T> long long dcmp(T a, T b) { return a > b; } template <long long *a> long long cmp_a(long long first, long long second) { return a[first] < a[second]; } struct __INIT__ { __INIT__() { memset(&inf, 0x3f, sizeof(inf)); } } __INIT___; namespace io { const long long SIZE = (1 << 21) + 1; char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; long long f, qr; inline void flush() { fwrite(obuf, 1, oS - obuf, stdout); oS = obuf; } inline void putc(char first) { *oS++ = first; if (oS == oT) flush(); } template <typename A> inline bool read(A &first) { for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++); c < '0' || c > '9'; c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) if (c == '-') f = -1; else if (c == EOF) return 0; for (first = 0; c <= '9' && c >= '0'; c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) first = first * 10 + (c & 15); first *= f; return 1; } inline bool read(char &first) { while ((first = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) == ' ' || first == '\n' || first == '\r') ; return first != EOF; } inline bool read(char *first) { while ((*first = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) == '\n' || *first == ' ' || *first == '\r') ; if (*first == EOF) return 0; while (!(*first == '\n' || *first == ' ' || *first == '\r' || *first == EOF)) *(++first) = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++); *first = 0; return 1; } template <typename A, typename... B> inline bool read(A &first, B &...second) { return read(first) && read(second...); } template <typename A> inline bool write(A first) { if (!first) putc('0'); if (first < 0) putc('-'), first = -first; while (first) qu[++qr] = first % 10 + '0', first /= 10; while (qr) putc(qu[qr--]); return 0; } inline bool write(char first) { putc(first); return 0; } inline bool write(const char *first) { while (*first) { putc(*first); ++first; } return 0; } inline bool write(char *first) { while (*first) { putc(*first); ++first; } return 0; } template <typename A, typename... B> inline bool write(A first, B... second) { return write(first) || write(second...); } struct Flusher_ { ~Flusher_() { flush(); } } io_flusher_; } // namespace io using io ::putc; using io ::read; using io ::write; pair<pair<long long, long long>, long long> a[85]; long long sel[85]; long long f[85][85], las[85][85]; signed main() { long long t; read(t); long long n, k; for (; t; --t) { read(n, k); for (long long i = 1; i <= n; ++i) { read(a[i].first.second, a[i].first.first); a[i].second = i; } sort(a + 1, a + n + 1); memset(f, -0x3f, sizeof(f)); long long w = 0; for (long long i = 1; i <= n; ++i) w += a[i].first.first * (k - 1); f[0][0] = w; for (long long i = 0; i < n; ++i) { for (long long j = 0; j <= i && j <= k; ++j) { if (chkmax(f[i + 1][j + 1], f[i][j] + a[i + 1].first.first * (j - k + 1) + a[i + 1].first.second)) las[i + 1][j + 1] = 1; if (chkmax(f[i + 1][j], f[i][j])) las[i + 1][j] = 0; } } for (long long i = n, j = k; i; --i) { if (las[i][j]) { sel[i] = j; --j; } else sel[i] = 0; } write(n + n - k, '\n'); for (long long i = 1; i <= n; ++i) { if (sel[i] && sel[i] < k) write(a[i].second, ' '); } for (long long i = 1; i <= n; ++i) if (!sel[i]) write(a[i].second, ' ', -a[i].second, ' '); for (long long i = 1; i <= n; ++i) if (sel[i] == k) write(a[i].second, '\n'); } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long n, k, dp[100][100], tak[100][100]; pair<long long, pair<long long, long long> > a[100]; void solve() { cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> a[i].second.first >> a[i].first; a[i].second.second = i; } for (int i = 1; i <= 75; i++) for (int j = 0; j <= 75; j++) tak[i][j] = 0; sort(a + 1, a + n + 1); for (int i = 1; i <= n; i++) dp[0][i] = -9999999999999999; for (int i = 1; i <= n; i++) { for (int j = 0; j <= min(k, (long long)i); j++) { long long ra1 = dp[i - 1][j] + a[i].first * (k - 1); long long ra2 = -9999999999999999; if (j) ra2 = dp[i - 1][j - 1] + a[i].second.first + a[i].first * (j - 1); if (ra1 <= ra2) { tak[i][j] = 1; } dp[i][j] = max(ra1, ra2); } for (int j = min(k, (long long)i) + 1; j <= n; j++) dp[i][j] = -9999999999999999; } vector<long long> ans, las; long long p = k; for (int i = n; i >= 1; i--) { if (tak[i][p]) { p--; ans.push_back(a[i].second.second); } else { las.push_back(a[i].second.second); } } reverse(ans.begin(), ans.end()); cout << n + (int)las.size() << '\n'; for (int i = 0; i < (int)ans.size() - 1; i++) cout << ans[i] << " "; for (int i = 0; i < las.size(); i++) { cout << las[i] << " -" << las[i] << " "; } cout << ans[(int)ans.size() - 1] << '\n'; } int main() { ios::sync_with_stdio(false); long long q; cin >> q; while (q--) 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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; bool cmp(pair<pair<long long, long long>, long long> a, pair<pair<long long, long long>, long long> b) { return a.first.second < b.first.second; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t; cin >> t; while (t--) { long long n, k; cin >> n >> k; vector<pair<pair<long long, long long>, long long> > a(n); for (long long i = 0; i < n; i++) { cin >> a[i].first.first >> a[i].first.second; a[i].second = i + 1; } sort(a.begin(), a.end(), cmp); vector<vector<long long> > dp(n + 1, vector<long long>(k + 1, -1e18)); vector<vector<long long> > pred(n + 1, vector<long long>(k + 1, -1)); dp[0][0] = 0; long long ans = 0; for (long long i = 1; i <= n; i++) { for (long long j = 0; j <= k; j++) { if (j + 1 <= k && dp[i - 1][j] + j * a[i - 1].first.second + a[i - 1].first.first >= dp[i][j + 1]) { pred[i][j + 1] = 1; dp[i][j + 1] = max(dp[i][j + 1], dp[i - 1][j] + j * a[i - 1].first.second + a[i - 1].first.first); } if (dp[i][j] < dp[i - 1][j] + (k - 1) * a[i - 1].first.second) { dp[i][j] = max(dp[i][j], dp[i - 1][j] + (k - 1) * a[i - 1].first.second); pred[i][j] = 0; } } } long long st = k; long long i = n; if (dp[n][k - 1] > dp[n][k]) { st = k - 1; } vector<long long> ans1, ans2; while (i > 0) { if (pred[i][st] == 1) { ans1.push_back(i); st--; } else { ans2.push_back(i); } i--; } reverse(ans1.begin(), ans1.end()); cout << ans1.size() + 2 * ans2.size() << "\n"; if (ans1.size() == k) { for (long long i = 0; i < ans1.size() - 1; i++) { cout << a[ans1[i] - 1].second << " "; } } else { for (long long i = 0; i < ans1.size(); i++) { cout << a[ans1[i] - 1].second << " "; } } for (long long i = 0; i < ans2.size(); i++) { cout << a[ans2[i] - 1].second << " "; cout << -a[ans2[i] - 1].second << " "; } if (ans1.size() == k) { cout << a[ans1.back() - 1].second << " "; } 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" ] }
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 (y && 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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = int(1e9) + 7; const int maxd = int(3e5) + 11; const int maxe = int(3e5) + 11; int tot = 0, topt = 0; int head[maxd]; int S, T, e[210][210]; struct Edge { int from, to, cap, cost, next; Edge() {} Edge(int x, int y, int a, int b, int c) : from(x), to(y), cap(a), cost(b), next(c) {} } eage[maxe * 2]; void add(int x, int y, int a, int b) { e[x][y] = tot; eage[tot] = Edge(x, y, a, b, head[x]), head[x] = tot++; eage[tot] = Edge(y, x, 0, -b, head[y]), head[y] = tot++; return; } bool used[maxd], vis[maxd]; int dis[maxd]; queue<int> que; int ans = 0; bool spfa() { for (int i = 0; i < maxd; i++) used[i] = false, dis[i] = INF, vis[i] = false; while (que.size()) que.pop(); que.push(S); used[S] = true; dis[S] = 0; while (que.size()) { int u = que.front(); que.pop(); used[u] = false; for (int i = head[u]; ~i; i = eage[i].next) if (eage[i].cap && dis[eage[i].to] > dis[u] + eage[i].cost) { int v = eage[i].to; dis[v] = dis[u] + eage[i].cost; if (!used[v]) { used[v] = true; que.push(v); } } } return (dis[T] <= 0); } int dfs(int u, int flow) { if (u == T) { ans += dis[u] * flow; return flow; } vis[u] = true; int ret = 0; for (int i = head[u]; ~i; i = eage[i].next) if (!vis[eage[i].to] && eage[i].cap && dis[eage[i].to] == dis[u] + eage[i].cost) { int v = eage[i].to; int newf = dfs(v, min(flow, eage[i].cap)); eage[i].cap -= newf; eage[i ^ 1].cap += newf; ret += newf; flow -= newf; if (!flow) break; } if (!ret) dis[u] = -1; return ret; } int MCMF() { ans = 0; while (spfa()) dfs(S, INF); return ans; } int _, n, k, id[80], a[maxe], b[maxe], kid[80], aa[80]; void init() { memset(head, -1, sizeof head); memset(eage, 0, sizeof eage); memset(id, 0, sizeof id); memset(e, -1, sizeof e); tot = 0; topt = 0; } void solve() { scanf("%d%d", &n, &k); S = ++topt; for (int i = 1; i <= n; i++) id[i] = ++topt; for (int i = 1; i <= k + 1; i++) kid[i] = ++topt; T = ++topt; for (int i = 1; i <= n; i++) add(S, id[i], 1, 0); for (int i = 1; i <= k; i++) add(kid[i], T, 1, 0); add(kid[k + 1], T, n - k, 0); for (int i = 1; i <= n; i++) scanf("%d%d", &a[i], &b[i]); for (int i = 1; i <= n; i++) { for (int j = 1; j <= k; j++) add(id[i], kid[j], 1, -(a[i] + b[i] * (j - 1))); add(id[i], kid[k + 1], 1, -(b[i] * (k - 1))); } MCMF(); for (int i = 1; i <= n; i++) for (int j = 1; j <= k; j++) if (e[id[i]][kid[j]] != -1) { if (eage[e[id[i]][kid[j]]].cap == 0) aa[j] = i; } printf("%d\n", n + (n - k)); for (int i = 1; i < k; i++) printf("%d ", aa[i]); for (int i = 1; i <= n; i++) if (eage[e[id[i]][kid[k + 1]]].cap == 0) printf("%d %d ", i, -i); printf("%d\n", aa[k]); return; } int main() { scanf("%d", &_); while (_--) init(), 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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pil = pair<int, ll>; using pli = pair<ll, int>; using pll = pair<ll, ll>; const int MOD = 1000000007; const int inf = (1 << 30) - 1; const ll INF = (1LL << 60) - 1; template <typename T> bool chmax(T &x, const T &y) { return (x < y) ? (x = y, true) : false; }; template <typename T> bool chmin(T &x, const T &y) { return (x > y) ? (x = y, true) : false; }; struct io_setup { io_setup() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout << fixed << setprecision(15); } } io_setup; int main() { int T; cin >> T; while (T--) { int N, K; cin >> N >> K; vector<pair<pll, int>> v; for (int i = 0; i < N; i++) { ll a, b; cin >> a >> b; v.emplace_back(pll(b, a), i); } sort(v.begin(), v.end()); vector<vector<pli>> dp(N + 1, vector<pli>(K + 1, pli(-INF, -1))); dp[0][0] = pli(0, -1); for (int i = 0; i < N; i++) { for (int j = 0; j < K + 1; j++) { auto [p, k] = dp[i][j]; if (p == -INF) continue; ll np = p + v[i].first.first * (K - 1); chmax(dp[i + 1][j], pli(np, k)); np = p + v[i].first.first * j + v[i].first.second; if (j + 1 <= K) chmax(dp[i + 1][j + 1], pli(np, i)); } } vector<bool> used(N, false); int ni = N, nj = K; while (true) { ni = dp[ni][nj].second; if (ni == -1) break; used[ni] = true; nj--; } vector<int> rem; for (int i = 0; i < N; i++) if (used[i]) rem.emplace_back(i); cout << 2 * N - K << '\n'; for (int i = 0; i < K - 1; i++) cout << v[rem[i]].second + 1 << ' '; for (int i = 0; i < N; i++) { if (!used[i]) cout << v[i].second + 1 << ' ' << -v[i].second - 1 << ' '; } cout << v[rem.back()].second + 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" ] }
CORRECT
java
import java.util.Arrays; import java.util.Scanner; public class SummoningMinions { public static void main(String[] args) { Scanner fs=new Scanner(System.in); int T=fs.nextInt(); for (int tt=0; tt<T; tt++) { int n=fs.nextInt(), k=fs.nextInt(); long ans=0; int[][] cost=new int[k][n]; for (int i=0; i<n; i++) { int a=fs.nextInt(), b=fs.nextInt(); ans+=(k-1)*(long)b; for (int spot=0; spot<k; spot++) cost[spot][i]=-(k-1)*b+spot*b+a; } Hungarian h=new Hungarian(cost); h.run(); boolean[] chosen=new boolean[n]; for (int i=0; i<k; i++) chosen[h.xy[i]]=true; System.out.println(k-1 + 2*(n-k) + 1); for (int i=0; i+1<k; i++) { System.out.print(1+h.xy[i]+" "); } for (int i=0; i<n; i++) if (!chosen[i]) System.out.print((1+i)+" -"+(1+i)+" "); System.out.println(h.xy[k-1]+1); } } // #rows of cost matrix MUST be <= #columns // xy[i] tells you what item i from the left matched with // Given a cost matrix, matches each row with a column that MAXIMIZES // total of selected edges. static class Hungarian { static int inf=100000000; int[][] c; int[] lx, ly, xy, yx, s, sx, p, q; int n, m; boolean[] S, T; public Hungarian(int[][] cost) { c=cost; n=c.length; m=c[0].length; // INIT: lx[n],xy[n],prev[n],q[n],ly[m],yx[m],s[m],sx[m] S=new boolean[n]; T=new boolean[m]; // # // Array initialization lx=new int[n]; ly=new int[m]; xy=new int[n]; yx=new int[m]; s=new int[m]; sx=new int[m]; p=new int[n]; q=new int[n]; // $ } void augment() { int x, y, root=-1, wr=0, rd=0; Arrays.fill(p, -1); Arrays.fill(S, false); Arrays.fill(T, false); for (x=0; x<n; x++) { if (xy[x]==-1) { q[wr++]=root=x; p[x]=-2; S[x]=true; break; } } for (y=0; y<m; y++) { s[y]=lx[root]+ly[y]-c[root][y]; sx[y]=root; } while (y>=m) { while (rd<wr&&y>=m) { x=q[rd++]; for (y=0; y<m; y++) { if (c[x][y]==lx[x]+ly[y]&&!T[y]) { if (yx[y]==-1) break; T[y]=true; q[wr++]=yx[y]; addToTree(yx[y], x); } } } if (y<m) break; updateLabels(); wr=rd=0; for (y=0; y<m; y++) { if (!T[y]&&s[y]==0) { if (yx[y]==-1) { x=sx[y]; break; } else { T[y]=true; if (!S[yx[y]]) { q[wr++]=yx[y]; addToTree(yx[y], sx[y]); } } } } } for (int cx=x, cy=y, ty; cx!=-2; cx=p[cx], cy=ty) { ty=xy[cx]; yx[cy]=cx; xy[cx]=cy; } } void updateLabels() { int x, y, delta=inf; for (y=0; y<m; y++) delta=Math.min(delta, !T[y]?s[y]:inf); for (x=0; x<n; x++) lx[x]-=S[x]?delta:0; for (y=0; y<m; y++) { ly[y]+=T[y]?delta:0; s[y]-=!T[y]?delta:0; } } void addToTree(int x, int prevX) { S[x]=true; p[x]=prevX; for (int y=0; y<m; y++) { if (lx[x]+ly[y]-c[x][y]<s[y]) { s[y]=lx[x]+ly[y]-c[x][y]; sx[y]=x; } } } int run() { Arrays.fill(xy, -1); Arrays.fill(yx, -1); for (int x=0; x<n; x++) for (int y=0; y<m; y++) lx[x]=Math.max(lx[x], c[x][y]); for (int i=0; i<n; i++) augment(); int ret=0; for (int x=0; x<n; x++) ret+=c[x][xy[x]]; return ret; } } }
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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e3 + 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 = 0; if (i - 1 >= j) 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" ] }
CORRECT
python3
def read_int(): return int(input()) def read_ints(): return map(int, input().split(' ')) t = read_int() for case_num in range(t): n, k = read_ints() p = [] for i in range(n): ai, bi = read_ints() p.append((bi, ai, i + 1)) p.sort() dp = [[0 for j in range(k + 1)] for i in range(n + 1)] use = [[False for j in range(k + 1)] for i in range(n + 1)] for i in range(1, n + 1): for j in range(min(i, k) + 1): if i - 1 >= j: dp[i][j] = dp[i - 1][j] + (k - 1) * p[i - 1][0] if j > 0: x = dp[i - 1][j - 1] + (j - 1) * p[i - 1][0] + p[i - 1][1] if x > dp[i][j]: dp[i][j] = x use[i][j] = True used = [] curr = k for i in range(n, 0, -1): if use[i][curr]: used.append(p[i - 1][2]) curr -= 1 used.reverse() seq = used[:-1] st = set(used) for i in range(1, n + 1): if not i in st: seq.append(i) seq.append(-i) seq.append(used[-1]) print(len(seq)) print(' '.join(map(str, seq)))
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
java
import java.util.Arrays; import java.util.Scanner; public class SummoningMinions2 { public static void main(String[] args) { Scanner fs=new Scanner(System.in); int T=fs.nextInt(); for (int tt=0; tt<T; tt++) { int n=fs.nextInt(), k=fs.nextInt(); int[][] cost=new int[k][n]; for (int i=0; i<n; i++) { int a=fs.nextInt(), b=fs.nextInt(); for (int spot=0; spot<k; spot++) cost[spot][i]=-(-(k-1)*b+spot*b+a); } hungarian(cost); boolean[] chosen=new boolean[n]; for (int i=0; i<k; i++) chosen[rowMatchesWith[i]]=true; System.out.println(k-1 + 2*(n-k) + 1); for (int i=0; i+1<k; i++) { System.out.print(1+rowMatchesWith[i]+" "); } for (int i=0; i<n; i++) if (!chosen[i]) System.out.print((1+i)+" -"+(1+i)+" "); System.out.println(rowMatchesWith[k-1]+1); } } // finds MINIMUM total matching cost of // each row to some column, using each col at most once // **nRows must be <= nCols** //rowMatchWith[i] = the column that row i matched with static int[] rowMatchesWith; static int hungarian(int[][] a) { int n=a.length, m=a[0].length; if (n>m) throw null; int[] u=new int[n], v=new int[m+1]; //edge (i->j) >= u[i]+v[j] int[] p=new int[m+1]; //p[j] = left match for right node j Arrays.fill(p, -1); for (int i=0; i<n; i++) {//find alternating path for node i int j0=m; p[j0]=i; int[] dist=new int[m+1], from=new int[m+1]; boolean[] seen=new boolean[m+1]; Arrays.fill(dist, Integer.MAX_VALUE); Arrays.fill(from, -1); while (p[j0]!=-1) { seen[j0]=true; int i0=p[j0], delta=Integer.MAX_VALUE, j1=-1; for (int j=0; j<m; j++) {//consider edges i0 -> everything if (seen[j]) continue; int candDist=a[i0][j]-u[i0]-v[j]; if (candDist<dist[j]) {dist[j]=candDist; from[j]=j0;} if (dist[j]<delta) {delta=dist[j]; j1=j;} } //it costs at least delta to get somewhere else, //subtract that from all distances and add cost to u, v, arrays //from all done -> not done edges for (int j=0; j<=m; j++) { if (seen[j]) {u[p[j]]+=delta; v[j]-=delta;} else dist[j]-=delta; } j0=j1; } //flip alternating path while (j0!=m) { int j1=from[j0]; p[j0]=p[j1]; j0=j1; } } //sum of deltas is stored at v[m] coincidentally int ans=-v[m]; rowMatchesWith=new int[n]; for (int j=0; j<m; j++) if (p[j]!=-1) rowMatchesWith[p[j]]=j; return ans; } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int f[80][80], g[80][80], n, m, is[80]; struct T { int a, b, id; } a[80]; void Solve() { cin >> n >> m, memset(f, 0xc0, sizeof(f)), f[0][0] = 0; for (int i = 1; i <= n; i++) cin >> a[i].a >> a[i].b, a[i].id = i, is[i] = 0; sort(a + 1, a + n + 1, [](T i, T j) { return i.b < j.b; }); for (int i = 1; i <= n; i++) { for (int j = 0; j <= m; j++) { int t1 = (j ? f[i - 1][j - 1] + a[i].a + a[i].b * (j - 1) : -1e9), t2 = f[i - 1][j] + a[i].b * (m - 1); f[i][j] = max(t1, t2), g[i][j] = (t1 > t2); } } for (int i = n, t = m; i && t; i--) if (g[i][t]) is[i] = 1, t--; cout << n * 2 - m << endl; int pos = 0; for (int i = 1, t = 0; i <= n; i++) { if (!is[i]) continue; if ((++t) == m) { pos = a[i].id; break; } cout << a[i].id << ' '; } for (int i = 1; i <= n; i++) if (!is[i]) cout << a[i].id << ' ' << -a[i].id << ' '; cout << pos << endl; } int main() { int t; cin >> t; while (t--) Solve(); }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
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) { if (done == k) return 0; return -1e18; } 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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2 * 1e4 + 100, M = 81; int T, n, k, a[M], b[M], ans, p[M], in[M]; int s, t, d[N], pe[N], pn[N], flow[N], vi[N]; int tot, first[N], nxt[N * 2], point[N * 2], data[N * 2], cost[N * 2]; inline int read() { int f = 1, x = 0; char s = getchar(); while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); } while (s >= '0' && s <= '9') { x = x * 10 + s - '0'; s = getchar(); } return x * f; } inline void add_edge(int x, int y, int z, int h) { nxt[++tot] = first[x], first[x] = tot; point[tot] = y, data[tot] = z, cost[tot] = h; nxt[++tot] = first[y], first[y] = tot; point[tot] = x, data[tot] = 0, cost[tot] = -h; } bool spfa(int t) { queue<int> q; for (int i = 0; i <= t; i++) d[i] = 0x3f3f3f3f, flow[i] = 0; d[s] = 0; vi[s] = 1; q.push(s); flow[s] = 0x3f3f3f3f; while (!q.empty()) { int x = q.front(); q.pop(); vi[x] = 0; for (int i = first[x]; i != -1; i = nxt[i]) { int u = point[i]; if (data[i] && d[u] > d[x] + cost[i]) { d[u] = d[x] + cost[i]; pn[u] = x, pe[u] = i, flow[u] = min(flow[x], data[i]); if (!vi[u]) vi[u] = 1, q.push(u); } } } if (!flow[t]) return 0; int x = t; while (x != s) { data[pe[x]] -= flow[t]; data[pe[x] ^ 1] += flow[t]; x = pn[x]; } return 1; } signed main() { T = read(); while (T--) { tot = -1; ans = 0; memset(first, -1, sizeof(first)); n = read(); k = read(); for (int i = 1; i <= n; i++) a[i] = read(), b[i] = read(); s = 0; t = n + k + 1; for (int i = 1; i <= n; i++) { for (int j = 1; j <= k; j++) add_edge(i, j + n, 1, (k - j) * b[i] - a[i]); } for (int i = 1; i <= n; i++) add_edge(s, i, 1, 0); for (int i = 1; i <= k; i++) add_edge(i + n, t, 1, 0); while (spfa(t)) ; memset(p, 0, sizeof(p)); memset(in, 0, sizeof(in)); for (int i = 0; i <= tot; i += 2) { if (point[i] == s || point[i] == t || point[i ^ 1] == s || point[i ^ 1] == t || data[i]) continue; int u = point[i], v = point[i ^ 1]; if (u > v) swap(u, v); v -= n; p[v] = u; in[u] = 1; } printf("%d\n", 2 * n - k); for (int i = 1; i < k; i++) printf("%d ", p[i]); for (int i = 1; i <= n; i++) if (!in[i]) printf("%d %d ", i, -i); printf("%d\n", p[k]); } }
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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using lld = long long; const int INF = 1e9; const int MAXN = 75; struct Minion { int a, b; int nr; }; bool operator<(const Minion& a, const Minion& b) { if (a.b != b.b) return a.b < b.b; return a.nr < b.nr; } int dp[MAXN + 5][MAXN + 5]; void query(void) { int n, k; cin >> n >> k; vector<Minion> t(n); for (int i = 0; i < n; i++) { cin >> t[i].a >> t[i].b; t[i].nr = i + 1; } sort(t.begin(), t.end()); for (int i = 0; i <= n; i++) for (int j = 0; j <= k; j++) dp[i][j] = -INF; dp[0][0] = 0; for (int i = 0; i < n; i++) { for (int j = 0; j <= k; j++) dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + t[i].b * (k - 1)); for (int j = 1; j <= k; j++) dp[i + 1][j] = max(dp[i + 1][j], dp[i][j - 1] + t[i].a + t[i].b * (j - 1)); } vector<int> res, rem; int x = n, y = k; while (x > 0) { if (y > 0 && dp[x][y] == dp[x - 1][y - 1] + t[x - 1].a + t[x - 1].b * (y - 1)) { res.push_back(t[x - 1].nr); x--, y--; } else { rem.push_back(t[x - 1].nr); x--; } } reverse(res.begin(), res.end()); cout << 2 * n - k << "\n"; for (int i = 0; i < k - 1; i++) cout << res[i] << " "; for (int i : rem) cout << i << " " << -i << " "; cout << res.back() << "\n"; } int main(void) { ios_base::sync_with_stdio(false); cin.tie(nullptr); int q; cin >> q; while (q--) query(); 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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 0, M = 0; int read() { char c; int num, f = 1; while (c = getchar(), !isdigit(c)) if (c == '-') f = -1; num = c - '0'; while (c = getchar(), isdigit(c)) num = num * 10 + c - '0'; return num * f; } struct node { int a, b, id; } p[109]; int n, k, f[109][109], ans[109][109], vis[109]; bool cmp(node x, node y) { return x.b < y.b; } void work() { n = read(); k = read(); for (int i = 1; i <= n; i++) { p[i].a = read(); p[i].b = read(); p[i].id = i; } sort(p + 1, p + 1 + n, cmp); memset(f, 0x3f3f, sizeof(f)); f[0][0] = 0; for (int i = 1; i <= n; i++) { for (int j = 0; j <= i && j <= k; j++) { if (f[i - 1][j] < f[i][j]) { ans[i][j] = 0; f[i][j] = f[i - 1][j]; } if (j && f[i - 1][j - 1] + (k - j) * p[i].b - p[i].a < f[i][j]) { ans[i][j] = 1; f[i][j] = f[i - 1][j - 1] + (k - j) * p[i].b - p[i].a; } } } printf("%d\n", k + 2 * (n - k)); int sum = k; for (int i = n; i; i--) { vis[i] = ans[i][sum]; sum -= ans[i][sum]; } for (int i = 1, now = 1; i < k; i++) { while (!vis[now]) now++; printf("%d ", p[now++].id); } for (int i = 1; i <= n; i++) if (!vis[i]) printf("%d %d ", p[i].id, -p[i].id); for (int i = n; i; i--) if (vis[i]) { printf("%d\n", p[i].id); break; } } int main() { int Case = read(); while (Case--) work(); 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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const double pi = 2 * acos(0.0); const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, -1, 0, 1}; const int dx8[] = {-1, 0, 1, 0, 1, 1, -1, -1}; const int dy8[] = {0, -1, 0, 1, 1, -1, 1, -1}; long long min(long long a, long long b) { if (a < b) return a; return b; } long long max(long long a, long long b) { if (a > b) return a; return b; } long long ceil1(long long a, long long b) { return (a + b - 1) / b; } template <class Cap, class Cost> struct mcf_graph { private: int _n; struct _edge { int to, rev; Cap cap; Cost cost; }; vector<pair<int, int>> pos; vector<vector<_edge>> g; vector<Cost> h; vector<Cost> dist; vector<int> pv, pe; vector<bool> vis; void norm(int s, int t) { h.resize(_n, numeric_limits<Cost>::max()); h[s] = 0; int ch = 1; int it = _n; while (ch-- && it--) { for (int i = 0; i < _n; i++) { if (h[i] == numeric_limits<Cost>::max()) continue; for (auto e : g[i]) { if (e.cap == 0) continue; if (h[i] + e.cost < h[e.to]) { h[e.to] = h[i] + e.cost; ch = 1; } } } assert(it >= 0); } for (int i = 0; i < _n; i++) h[i] -= h[t]; } bool dijkstra(int s, int t) { pv.clear(); pe.clear(); vis.clear(); dist.clear(); dist.resize(_n, numeric_limits<Cost>::max()); pv.resize(_n, -1); pe.resize(_n, -1); vis.resize(_n, false); priority_queue<pair<Cost, int>, vector<pair<Cost, int>>, greater<pair<Cost, int>>> pq; pq.push({0, s}); dist[s] = 0; while (!pq.empty()) { int x = pq.top().second; pq.pop(); if (vis[x]) continue; vis[x] = true; if (x == t) break; for (int i = 0; i < int(g[x].size()); ++i) { auto e = g[x][i]; if (vis[e.to] || e.cap == 0) continue; Cost cost = e.cost + h[x] - h[e.to]; if (dist[e.to] <= dist[x] + cost) continue; dist[e.to] = dist[x] + cost; pv[e.to] = x; pe[e.to] = i; pq.push({dist[e.to], e.to}); } } if (vis[t] == false) return false; for (int i = 0; i < _n; ++i) { if (!vis[i]) continue; h[i] -= dist[t] - dist[i]; } return true; } public: mcf_graph() {} mcf_graph(int n) : _n(n), g(n) {} int add_edge(int from, int to, Cap cap, Cost cost) { assert(from >= 0 && from < _n); assert(to >= 0 && to < _n); int m = int(pos.size()); pos.push_back({from, int(g[from].size())}); int from_id = int(g[from].size()); int to_id = int(g[to].size()); if (from == to) to_id++; g[from].push_back(_edge{to, to_id, cap, cost}); g[to].push_back(_edge{from, from_id, 0, -cost}); return m; } struct edge { int from, to; Cap cap, flow; Cost cost; }; edge get_edge(int i) { int m = int(pos.size()); assert(0 <= i && i < m); auto _e = g[pos[i].first][pos[i].second]; auto _re = g[_e.to][_e.rev]; return edge{ pos[i].first, _e.to, _e.cap + _re.cap, _re.cap, _e.cost, }; } vector<edge> edges() { int m = int(pos.size()); vector<edge> result(m); for (int i = 0; i < m; ++i) result[i] = get_edge(i); return result; } pair<Cap, Cost> flow(int s, int t, bool neg = false) { return flow(s, t, numeric_limits<Cap>::max(), neg); } pair<Cap, Cost> flow(int s, int t, Cap flow_limit, bool neg = false) { return slope(s, t, flow_limit, neg).back(); } vector<pair<Cap, Cost>> slope(int s, int t, bool neg = false) { return slope(s, t, numeric_limits<Cap>::max(), neg); } vector<pair<Cap, Cost>> slope(int s, int t, Cap flow_limit, bool neg = false) { assert(s >= 0 && s < _n); assert(t >= 0 && t < _n); assert(s != t); h.clear(); if (!neg) h.resize(_n, 0); else norm(s, t); Cap flow = 0; Cost cost = 0, prev = -1; vector<pair<Cap, Cost>> result; result.push_back({flow, cost}); while (flow < flow_limit) { if (dijkstra(s, t) == false) break; Cap c = flow_limit - flow; for (int i = t; i != s; i = pv[i]) { c = min(c, g[pv[i]][pe[i]].cap); } for (int i = t; i != s; i = pv[i]) { auto& e = g[pv[i]][pe[i]]; e.cap -= c; g[e.to][e.rev].cap += c; } Cost d = -h[s]; flow += c; cost += c * d; if (prev == d) result.pop_back(); result.push_back({flow, cost}); prev = d; } return result; } }; void solve() { int n, k; cin >> n >> k; vector<pair<long long, long long>> info; for (long long i = 0; i < n; i++) { long long x, y; cin >> x >> y; info.push_back({x, y}); } vector<vector<long long>> arr(n, vector<long long>(n)); for (long long i = 0; i < n; i++) { for (long long j = 0; j < n; j++) { if (j < k) arr[i][j] = info[i].first + j * info[i].second; else arr[i][j] = (k - 1) * info[i].second; } } long long inf = INT_MAX / 10; mcf_graph<long long, long long> g(2 * n + 2); long long s = 2 * n, t = 2 * n + 1; for (long long i = 0; i < n; i++) { g.add_edge(s, i, 1, 0); g.add_edge(i + n, t, 1, 0); } long long cur = 2 * n; for (long long i = 0; i < n; i++) for (long long j = 0; j < n; j++) g.add_edge(i, j + n, 1, inf - arr[i][j]); auto x = g.flow(s, t); auto edge = g.edges(); vector<long long> assignment(n); for (long long i = 0; i < n; i++) for (long long j = 0; j < n; j++) { if (edge[cur].flow == 1) assignment[j] = i + 1; cur++; } cout << 2 * n - k << '\n'; for (long long i = 0; i < k - 1; i++) cout << assignment[i] << " "; for (long long i = k; i < n; i++) cout << assignment[i] << " " << -1 * assignment[i] << " "; cout << assignment[k - 1] << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) solve(); }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
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; memset(f, 0xcf, sizeof(f)); 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", 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" ] }
CORRECT
java
import java.io.*; import java.util.*; public class gym{ static class Edge { int to, f, cap, cost, rev; Edge(int to, int cap, int cost, int rev) { this.to = to; this.cap = cap; this.cost = cost; this.rev = rev; } } public static void addEdge(List<Edge>[] graph, int s, int t, int cap, int cost) { graph[s].add(new Edge(t, cap, cost, graph[t].size())); graph[t].add(new Edge(s, 0, -cost, graph[s].size() - 1)); } static void bellmanFord(List<Edge>[] graph, int s, int[] dist) { int n = graph.length; Arrays.fill(dist, Integer.MAX_VALUE); dist[s] = 0; boolean[] inqueue = new boolean[n]; int[] q = new int[n]; int qt = 0; q[qt++] = s; for (int qh = 0; (qh - qt) % n != 0; qh++) { int u = q[qh % n]; inqueue[u] = false; for (int i = 0; i < graph[u].size(); i++) { Edge e = graph[u].get(i); if (e.cap <= e.f) continue; int v = e.to; int ndist = dist[u] + e.cost; if (dist[v] > ndist) { dist[v] = ndist; if (!inqueue[v]) { inqueue[v] = true; q[qt++ % n] = v; } } } } } public static long[] minCostFlow(List<Edge>[] graph, int s, int t, int maxf) { int n = graph.length; int[] prio = new int[n]; int[] curflow = new int[n]; int[] prevedge = new int[n]; int[] prevnode = new int[n]; int[] pot = new int[n]; bellmanFord(graph, s, pot); // bellmanFord invocation can be skipped if edges costs are non-negative long flow = 0; long flowCost = 0; while (flow < maxf) { PriorityQueue<Long> q = new PriorityQueue<>(); q.add((long) s); Arrays.fill(prio, Integer.MAX_VALUE); prio[s] = 0; boolean[] finished = new boolean[n]; curflow[s] = Integer.MAX_VALUE; while (!finished[t] && !q.isEmpty()) { long cur = q.remove(); int u = (int) (cur & 0xFFFF_FFFFL); int priou = (int) (cur >>> 32); if (priou != prio[u]) continue; finished[u] = true; for (int i = 0; i < graph[u].size(); i++) { Edge e = graph[u].get(i); if (e.f >= e.cap) continue; int v = e.to; int nprio = prio[u] + e.cost + pot[u] - pot[v]; if (prio[v] > nprio) { prio[v] = nprio; q.add(((long) nprio << 32) + v); prevnode[v] = u; prevedge[v] = i; curflow[v] = Math.min(curflow[u], e.cap - e.f); } } } if (prio[t] == Integer.MAX_VALUE) break; for (int i = 0; i < n; i++) if (finished[i]) pot[i] += prio[i] - prio[t]; int df = Math.min(curflow[t], maxf - (int)flow); flow += df; for (int v = t; v != s; v = prevnode[v]) { Edge e = graph[prevnode[v]].get(prevedge[v]); e.f += df; graph[v].get(e.rev).f -= df; flowCost += (long)df * e.cost; } } return new long[] {flow, flowCost}; } static void main() throws Exception{ int n=sc.nextInt(),k=sc.nextInt(); int[][]in=new int[n][]; for(int i=0;i<n;i++){ in[i]=new int[] {sc.nextInt(),sc.nextInt(),i+1}; } int V=2*n+2; int s=V-2,t=V-1; List<Edge>[]adj=new LinkedList[V]; for(int i=0;i<V;i++)adj[i]=new LinkedList<Edge>(); for(int pos=0;pos<n;pos++) { for(int i=0;i<n;i++) { if(pos<k) {//will not be deleted addEdge(adj, pos, i+n, 1, -(in[i][0]+(in[i][1]*pos))); } else { addEdge(adj, pos, i+n, 1, -(in[i][1]*(k-1))); } } } for(int i=0;i<n;i++) { addEdge(adj, i+n, t, 1, 0); } for(int pos=0;pos<n;pos++) { addEdge(adj, s, pos, 1, 0); } minCostFlow(adj, s, t, n); pw.println(k+(n-k)*2); for(int pos=0;pos<k-1;pos++) { int choosen=-1; for(Edge e:adj[pos]) { if(e.f==1) { choosen=e.to-n+1; } } pw.print(choosen+" "); } for(int pos=k;pos<n;pos++) { int choosen=-1; for(Edge e:adj[pos]) { if(e.f==1) { choosen=e.to-n+1; } } pw.print(choosen+" "+(-choosen)+" "); } int choosen=-1; for(Edge e:adj[k-1]) { if(e.f==1) { choosen=e.to-n+1; } } pw.println(choosen); } public static void main(String[] args) throws Exception{ pw=new PrintWriter(System.out); sc = new MScanner(System.in); int tc=sc.nextInt(); while(tc-->0)main(); pw.flush(); } static PrintWriter pw; static MScanner sc; static class MScanner { StringTokenizer st; BufferedReader br; public MScanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public MScanner(String file) throws Exception { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int[] intArr(int n) throws IOException { int[]in=new int[n];for(int i=0;i<n;i++)in[i]=nextInt(); return in; } public long[] longArr(int n) throws IOException { long[]in=new long[n];for(int i=0;i<n;i++)in[i]=nextLong(); return in; } public int[] intSortedArr(int n) throws IOException { int[]in=new int[n];for(int i=0;i<n;i++)in[i]=nextInt(); shuffle(in); Arrays.sort(in); return in; } public long[] longSortedArr(int n) throws IOException { long[]in=new long[n];for(int i=0;i<n;i++)in[i]=nextLong(); shuffle(in); Arrays.sort(in); return in; } public Integer[] IntegerArr(int n) throws IOException { Integer[]in=new Integer[n];for(int i=0;i<n;i++)in[i]=nextInt(); return in; } public Long[] LongArr(int n) throws IOException { Long[]in=new Long[n];for(int i=0;i<n;i++)in[i]=nextLong(); return in; } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public long nextLong() throws IOException { return Long.parseLong(next()); } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } } static void shuffle(int[]in) { for(int i=0;i<in.length;i++) { int idx=(int)(Math.random()*in.length); int tmp=in[i]; in[i]=in[idx]; in[idx]=tmp; } } static void shuffle(long[]in) { for(int i=0;i<in.length;i++) { int idx=(int)(Math.random()*in.length); long tmp=in[i]; in[i]=in[idx]; in[idx]=tmp; } } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using vi = vector<long long>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vvvvi = vector<vvvi>; using vp = vector<pair<long long, long long> >; using vvp = vector<vp>; using vb = vector<bool>; using vvb = vector<vb>; const long long inf = 1001001001001001001; const long long INF = 1001001001; const long long mod = 1000000007; const double eps = 1e-10; template <class T> bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> void out(T a) { cout << a << '\n'; } template <class T> void outp(T a) { cout << '(' << a.first << ',' << a.second << ')' << '\n'; } template <class T> void outvp(T v) { for (long long i = 0; i < (long long)(v.size()); i++) cout << '(' << v[i].first << ',' << v[i].second << ')'; cout << '\n'; } template <class T> void outvvp(T v) { for (long long i = 0; i < (long long)(v.size()); i++) outvp(v[i]); } template <class T> void outv(T v) { for (long long i = 0; i < (long long)(v.size()); i++) { if (i) cout << ' '; cout << v[i]; } cout << '\n'; } template <class T> void outvv(T v) { for (long long i = 0; i < (long long)(v.size()); i++) outv(v[i]); } template <class T> bool isin(T x, T l, T r) { return (l) <= (x) && (x) <= (r); } template <class T> void yesno(T b) { if (b) out("yes"); else out("no"); } template <class T> void YesNo(T b) { if (b) out("Yes"); else out("No"); } template <class T> void YESNO(T b) { if (b) out("YES"); else out("NO"); } template <class T> void noyes(T b) { if (b) out("no"); else out("yes"); } template <class T> void NoYes(T b) { if (b) out("No"); else out("Yes"); } template <class T> void NOYES(T b) { if (b) out("NO"); else out("YES"); } void outs(long long a, long long b) { if (a >= inf - 100) out(b); else out(a); } long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } long long modpow(long long a, long long b) { long long res = 1; a %= mod; while (b) { if (b & 1) res = res * a % mod; a = a * a % mod; b >>= 1; } return res; } int main() { long long t; cin >> t; for (long long tt = 0; tt < (long long)(t); tt++) { long long n, k; cin >> n >> k; vp v(n); for (long long i = 0; i < (long long)(n); i++) cin >> v[i].first >> v[i].second; vp s(n); for (long long i = 0; i < (long long)(n); i++) s[i] = pair<long long, long long>(v[i].second, i); sort(s.begin(), s.end()); reverse(s.begin(), s.end()); if (k == 1) { out(1); long long res = 0; for (long long i = 0; i < (long long)(n); i++) chmax(res, v[i].first); for (long long i = 0; i < (long long)(n); i++) if (v[i].first == res) { out(i + 1); break; } continue; } vvi dp(n + 1, vi(k + 1, -inf)); dp[0][0] = 0; for (long long i = 0; i < (long long)(n); i++) { long long id = s[i].second; for (long long j = 0; j < (long long)(k + 1); j++) { if (dp[i][j] == -inf) continue; chmax(dp[i + 1][j], dp[i][j] + v[id].second * (k - 1)); if (j != k) chmax(dp[i + 1][j + 1], dp[i][j] + v[id].first + v[id].second * (k - 1 - j)); } } vi ans, b; long long c; long long t = k; for (int i = n; i; i--) { long long id = s[i - 1].second; if (dp[i - 1][t] + v[id].second * (k - 1) == dp[i][t]) b.emplace_back(id + 1); else { if (t == 1) c = id + 1; else ans.emplace_back(id + 1); t--; } } for (long long x : b) { ans.emplace_back(x); ans.emplace_back(-x); } ans.emplace_back(c); out(ans.size()); outv(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" ] }
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; } cout << n * 2 - k << '\n'; 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 << '\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" ] }
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, -1e9)); dp[0][0] = 0; for (int ii = 1; ii <= n; ++ii) { int i = order[ii - 1]; for (int j = max(0, k - (n - ii)); j <= min(ii, k); ++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) || cur + (n - i) < k)) { 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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; void __print(int x) { cout << x; } void __print(long x) { cout << x; } void __print(long long x) { cout << x; } void __print(unsigned x) { cout << x; } void __print(unsigned long x) { cout << x; } void __print(unsigned long long x) { cout << x; } void __print(float x) { cout << x; } void __print(double x) { cout << x; } void __print(long double x) { cout << x; } void __print(char x) { cout << '\'' << x << '\''; } void __print(const char *x) { cout << '\"' << x << '\"'; } void __print(const string &x) { cout << '\"' << x << '\"'; } void __print(bool x) { cout << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cout << '{'; __print(x.first); cout << ','; __print(x.second); cout << '}'; } template <typename T> void __print(const T &x) { int f = 0; cout << '{'; for (auto &i : x) cout << (f++ ? "," : ""), __print(i); cout << "}"; } void _print() { cout << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cout << ", "; _print(v...); } long long mod = 1e9 + 7; int main() { cin.sync_with_stdio(0); cin.tie(0); int T = 1; cin >> T; while (T--) { long long n, k; cin >> n >> k; vector<long long> a(n); vector<pair<long long, long long> > b(n); for (int i = 0; i < (n); i++) { b[i].second = i; cin >> a[i] >> b[i].first; } sort(b.begin(), b.end()); long long dp[n + 1][k + 1]; memset(dp, -1, sizeof(dp)); dp[0][0] = 0; for (int i = (1); i <= (n); i++) { dp[i][0] = dp[i - 1][0] + (k - 1) * b[i - 1].first; for (int j = (1); j <= (k); j++) { if (dp[i - 1][j] != -1) dp[i][j] = dp[i - 1][j] + (k - 1) * b[i - 1].first; if (dp[i - 1][j - 1] != -1) dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + (j - 1) * b[i - 1].first + a[b[i - 1].second]); } } long long i = n, j = k; vector<int> final, vis(n + 1, 0); while (j > 0) { if (dp[i][j] == dp[i - 1][j - 1] + (j - 1) * b[i - 1].first + a[b[i - 1].second]) { final.push_back(b[i - 1].second + 1); vis[b[i - 1].second + 1] = 1; j--; } i--; } cout << k + (n - k) * 2 << '\n'; for (int i = 0; i < (k - 1); i++) cout << final[k - 1 - i] << " "; for (int i = (1); i <= (n); i++) if (!vis[i]) cout << i << " " << -i << " "; cout << final[0] << '\n'; } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 80; int n, m; int a[maxn], b[maxn], id[maxn]; int f[maxn][maxn], g[maxn][maxn]; bool used[maxn]; inline int read() { char c = getchar(); int res = 0, f = 1; while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') res = res * 10 + c - '0', c = getchar(); return res * f; } inline bool cmp(int x, int y) { return b[x] < b[y]; } void dfs(int n, int m) { if (!n) return; if (g[n][m]) used[n] = 1, m--; dfs(n - 1, m); } inline void solve() { n = read(), m = read(); for (int i = 1; i <= n; i++) a[i] = read(), b[i] = read(), id[i] = i; sort(id + 1, id + n + 1, cmp); memset(f, 0xcf, sizeof(f)); memset(g, 0, sizeof(g)); 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[id[i]] * (m - 1), g[i][j] = 0; if (j && f[i - 1][j - 1] + a[id[i]] + b[id[i]] * (j - 1) > f[i][j]) f[i][j] = f[i - 1][j - 1] + a[id[i]] + b[id[i]] * (j - 1), g[i][j] = 1; } memset(used, 0, sizeof(used)); dfs(n, m); printf("%d\n", m - 1 + (n - m) * 2 + 1); int last = 0; for (int i = 1; i <= n; i++) { if (!used[i]) continue; last++; if (last == m) { last = id[i]; break; } printf("%d ", id[i]); } for (int i = 1; i <= n; i++) { if (used[i]) continue; printf("%d %d ", id[i], -id[i]); } printf("%d\n", last); } int main() { int T = read(); 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" ] }
CORRECT
cpp
#include <bits/stdc++.h> const int MAXN = 105; const long long INF = (long long)1e18 + 5; using namespace std; struct Mon { long long a, b, id; bool operator<(const Mon &m) const { return b < m.b; } }; long long n; Mon mon[MAXN]; long long dp[MAXN][MAXN], from[MAXN][MAXN]; vector<int> ans; long long solve(int take) { for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) { dp[i][j] = -INF; } } dp[0][0] = 0; for (long long i = 0; i < n; i++) { for (long long j = 0; j <= n; j++) { if (dp[i][j] == -INF) continue; if (j < n) { long long prof = dp[i][j] + mon[i + 1].a + j * mon[i + 1].b; if (dp[i + 1][j + 1] < prof) { dp[i + 1][j + 1] = prof; from[i + 1][j + 1] = 1; } } long long prof = dp[i][j] + (take - 1) * mon[i + 1].b; if (dp[i + 1][j] < prof) { dp[i + 1][j] = prof; from[i + 1][j] = 0; } } } int cur = take; vector<int> yes, no; for (int i = n; i >= 1; i--) { if (from[i][cur]) { yes.push_back(mon[i].id); cur--; } else { no.push_back(mon[i].id); } } reverse(yes.begin(), yes.end()); ans.clear(); for (int i = 0; i < take - 1; i++) { ans.push_back(yes[i]); } for (int x : no) { ans.push_back(x); ans.push_back(-x); } ans.push_back(yes.back()); return dp[n][take]; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int tc; cin >> tc; while (tc--) { int k; cin >> n >> k; for (int i = 1; i <= n; i++) { int a, b; cin >> a >> b; mon[i] = {a, b, i}; } long long best = 0, who = 0; sort(mon + 1, mon + 1 + n); for (int i = 1; i <= k; i++) { long long val = solve(i); if (val > best) { best = val; who = i; } } solve(who); cout << ans.size() << '\n'; for (int i = 0; i < ans.size(); i++) { cout << ans[i] << " \n"[i + 1 == ans.size()]; } } }
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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename T> inline bool cmax(T &x, T y) { return x < y ? x = y, 1 : 0; } template <typename T> inline bool cmin(T &x, T y) { return y < x ? x = y, 1 : 0; } const int _ = 105; struct node { int a, b, i; bool operator<(const node &t) const { return b < t.b; } } w[_]; int T, N, K; int f[_][_], s1[_], s2[_], n, m; void work(int i, int j) { if (i == 0) return; if (f[i][j] == f[i - 1][j] + (K - 1) * w[i].b) s2[++m] = w[i].i, work(i - 1, j); else s1[++n] = w[i].i, work(i - 1, j - 1); } int main() { ios::sync_with_stdio(0), cin.tie(0); cin >> T; while (T--) { cin >> N >> K; for (int i(1), I(N); i <= I; ++i) cin >> w[i].a >> w[i].b, w[i].i = i; sort(w + 1, w + N + 1); memset(f, 0xf7, sizeof f), f[0][0] = 0; for (int i(1), I(N); i <= I; ++i) for (int j(0), I(min(i, K)); j <= I; ++j) f[i][j] = max(j ? f[i - 1][j - 1] + w[i].a + (j - 1) * w[i].b : -1, f[i - 1][j] + (K - 1) * w[i].b); n = m = 0, work(N, K), reverse(s1 + 1, s1 + n + 1); cout << (n + (m << 1)) << endl; for (int i(1), I(n - 1); i <= I; ++i) cout << s1[i] << " "; for (int i(1), I(m); i <= I; ++i) cout << s2[i] << " " << -s2[i] << " "; cout << s1[n] << 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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct pii { int first, second, id; }; const int N = 76, INF = (int)1e9; int n, k; pii p[N]; int dp[N][N], tk[N][N]; int main() { ios::sync_with_stdio(false); cin.tie(NULL); int T; cin >> T; while (T--) { cin >> n >> k; for (int i = 1; i < (n + 1); ++i) cin >> p[i].first >> p[i].second, p[i].id = i; sort(p + 1, p + n + 1, [](const pii &p1, const pii &p2) { return p1.second < p2.second; }); for (int i = 0; i < (n + 1); ++i) fill(dp[i], dp[i] + n + 1, -INF); dp[0][0] = 0; for (int i = 1; i <= n; i++) { for (int j = 0; j <= k; j++) { if (j >= 1) { int take_dp = dp[i - 1][j - 1] + (j - 1) * p[i].second + p[i].first; if (take_dp > dp[i][j]) { dp[i][j] = take_dp; tk[i][j] = 1; } } int not_dp = dp[i - 1][j] + (k - 1) * p[i].second; if (not_dp > dp[i][j]) { dp[i][j] = not_dp; tk[i][j] = 0; } } } vector<int> take, not_take; int cur = k; for (int i = n; i >= 1; i--) { (tk[i][cur] ? take : not_take).push_back(p[i].id); cur -= tk[i][cur]; } reverse(begin(take), end(take)); cout << take.size() + not_take.size() * 2 << '\n'; for (int i = 0; i < k - 1; i++) cout << take[i] << " "; for (auto x : not_take) cout << x << " " << -x << " "; cout << take[k - 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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct min_cost_flow { static const int64_t INF = numeric_limits<int64_t>::max() >> 1; int64_t n, s, t; struct edge { int64_t u, v, c, f, cost; }; vector<edge> E; vector<vector<int64_t>> graph; vector<int64_t> dist, head; vector<bool> inq; min_cost_flow(int64_t n = 0, int64_t s = 0, int64_t t = 0) : n(n), s(s), t(t) { E.clear(); graph.assign(n + 1, vector<int64_t>()); } int64_t add_edge(int64_t u, int64_t v, int64_t c, int64_t cost) { int64_t res = E.size(); graph[u].push_back(E.size()); E.push_back({u, v, c, 0, cost}); graph[v].push_back(E.size()); E.push_back({v, u, 0, 0, -cost}); return res; } bool SPFA() { head.assign(n + 1, -1); dist.assign(n + 1, 1e9); dist[s] = 0; queue<pair<int64_t, int64_t>> wl; wl.push({0, s}); while (not wl.empty()) { int64_t d = wl.front().first, u = wl.front().second; wl.pop(); if (d > dist[u]) continue; for (int64_t i : graph[u]) { if (dist[E[i].v] > d + E[i].cost && E[i].f < E[i].c) { dist[E[i].v] = d + E[i].cost, head[E[i].v] = i; wl.push({dist[E[i].v], E[i].v}); } } } return (head[t] != -1); } int64_t dfs() { int64_t delta = INF; for (int64_t u = t; u != s; u = E[head[u]].u) { int64_t i = head[u]; delta = min(delta, E[i].c - E[i].f); } for (int64_t u = t; u != s; u = E[head[u]].u) { int64_t i = head[u]; E[i].f += delta, E[i ^ 1].f -= delta; } return delta; } int64_t max_flow() { int64_t ans = 0, flow = 0; while (SPFA()) { int64_t det = dfs(); ans += dist[t] * det; flow += det; } return ans; } }; void solve() { int64_t n, k; cin >> n >> k; vector<int64_t> a(n), b(n); for (int64_t i = 0; i < n; i++) cin >> a[i] >> b[i]; int64_t s = n * 2, t = n * 2 + 1; min_cost_flow mcf(n * 2 + 2, s, t); for (int64_t i = 0; i < n; i++) mcf.add_edge(s, i, 1, 0); for (int64_t i = 0; i < n; i++) mcf.add_edge(i + n, t, 1, 0); vector<vector<int64_t>> slot_to_minion(n, vector<int64_t>(n)); for (int64_t i = 0; i < n; i++) for (int64_t j = 0; j < n; j++) { if (j < k) slot_to_minion[i][j] = mcf.add_edge(i, n + j, 1, -(a[i] + j * b[i])); else slot_to_minion[i][j] = mcf.add_edge(i, n + j, 1, -(k - 1) * b[i]); } mcf.max_flow(); cout << n + n - k << endl; for (int64_t i = 0; i < k - 1; i++) { for (int64_t j = 0; j < n; j++) if (mcf.E[slot_to_minion[j][i]].f > 0) { cout << (j + 1) << " "; break; } } for (int64_t i = k; i < n; i++) { for (int64_t j = 0; j < n; j++) if (mcf.E[slot_to_minion[j][i]].f > 0) { cout << (j + 1) << " " << -(j + 1) << " "; break; } } for (int64_t j = 0; j < n; j++) if (mcf.E[slot_to_minion[j][k - 1]].f > 0) { cout << (j + 1) << "\n"; break; } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); int64_t tc; cin >> tc; while (tc--) solve(); return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long maxn = 2e2 + 100; const long long mod = 1e9 + 7; const long long base = 1e18; pair<pair<long long, long long>, long long> a[maxn]; long long dp[maxn][maxn]; bool par[maxn][maxn]; bool lf(pair<pair<long long, long long>, long long> a, pair<pair<long long, long long>, long long> b) { return a.first.second < b.first.second; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); if (fopen("TRIPLES.inp", "r")) { freopen("TRIPLES.inp", "r", stdin); freopen("TRIPLES.out", "w", stdout); } long long t; cin >> t; while (t--) { long long n, k; cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> a[i].first.first >> a[i].first.second; a[i].second = i; } sort(a + 1, a + n + 1, lf); dp[0][0] = 0; for (int j = 1; j <= k; j++) dp[0][j] = -base; for (int i = 1; i <= n; i++) { for (int j = 0; j <= k; j++) { dp[i][j] = -base; if (j) { dp[i][j] = max( dp[i - 1][j - 1] + a[i].first.first + a[i].first.second * (j - 1), dp[i][j]); if (dp[i][j] == dp[i - 1][j - 1] + a[i].first.first + a[i].first.second * (j - 1)) { par[i][j] = 1; } } dp[i][j] = max(dp[i][j], dp[i - 1][j] + (k - 1) * a[i].first.second); if (dp[i][j] == dp[i - 1][j] + (k - 1) * a[i].first.second) { par[i][j] = 0; } } } pair<long long, long long> nw = make_pair(n, k); vector<long long> vt; vector<long long> vt1; while (nw != make_pair(0ll, 0ll)) { auto p = nw; if (par[p.first][p.second]) vt.push_back(a[p.first].second); else vt1.push_back(a[p.first].second); nw.second -= par[p.first][p.second]; nw.first--; } cout << k + (n - k) * 2 << "\n"; reverse(vt.begin(), vt.end()); for (int i = 0; i < vt.size() - 1; i++) cout << vt[i] << " "; for (auto to : vt1) cout << to << " " << -to << " "; cout << vt.back() << " "; 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" ] }
CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize(3, "Ofast", "inline") #pragma GCC target("avx") using namespace std; inline char gc() { static char buf[1 << 16], *p1 = buf, *p2 = buf; if (p1 == p2) { p2 = (p1 = buf) + fread(buf, 1, 1 << 16, stdin); if (p2 == p1) return EOF; } return *p1++; } template <class t> inline t read(t &x) { char c = gc(); bool f = 0; x = 0; while (!isdigit(c)) f |= c == '-', c = gc(); while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48), c = gc(); if (f) x = -x; return x; } template <class t> inline void write(t x) { if (x < 0) putchar('-'), write(-x); else { if (x > 9) write(x / 10); putchar('0' + x % 10); } } const int N = 205, M = 1e4 + 5, P = 80; int en = 1, mc, mf, h[N], dis[N], n, k, ans[P]; bool v[N]; struct edge { int n, v, f, w; } e[M << 1]; struct fafafa { int fa, id; } pre[N]; void add(int x, int y, int f, int w) { e[++en] = (edge){h[x], y, f, w}; h[x] = en; } bool spfa(int s, int t) { memset(v, 0, sizeof v); memset(pre, 0, sizeof pre); memset(dis, 0x3f, sizeof dis); queue<int> q; q.push(s); v[s] = 1; dis[s] = 0; while (!q.empty()) { int x = q.front(); q.pop(); for (int i = h[x]; i; i = e[i].n) { int y = e[i].v; if (e[i].f && dis[x] + e[i].w < dis[y]) { dis[y] = dis[x] + e[i].w; pre[y] = (fafafa){x, i}; if (!v[y]) { v[y] = 1; q.push(y); } } } v[x] = 0; } return dis[t] ^ 0x3f3f3f3f; } void mcmf(int s, int t) { while (spfa(s, t)) { int flow = INT_MAX; for (int i = t; i ^ s; i = pre[i].fa) flow = min(flow, e[pre[i].id].f); for (int i = t; i ^ s; i = pre[i].fa) { e[pre[i].id].f -= flow; e[pre[i].id ^ 1].f += flow; } mf += flow; mc += flow * dis[t]; } } void exadd(int x, int y, int f, int w) { add(x, y, f, w); add(y, x, 0, -w); } void doit() { read(n); read(k); for (int i = 1; i <= n; i++) exadd(0, i, 1, 0), exadd(i + n, n * 2 + 1, 1, 0); for (int i = 1, a, b; i <= n; i++) { read(a); read(b); for (int j = 1, val; j <= n; j++) { if (j < k) val = a + (j - 1) * b; else if (j < n) val = (k - 1) * b; else val = a + (k - 1) * b; exadd(i, j + n, 1, -val); } } mcmf(0, n * 2 + 1); write(k + (n - k) * 2); puts(""); for (int x = 1; x <= n; x++) for (int i = h[x]; i; i = e[i].n) { int y = e[i].v; if (y <= n) continue; if (!e[i].f) ans[y - n] = x; } for (int i = 1; i <= n; i++) { write(ans[i]); putchar(' '); if (i >= k && i < n) write(-ans[i]), putchar(' '); } puts(""); en = 1; mc = mf = 0; for (int i = 0; i <= n * 2 + 1; i++) h[i] = 0; } signed main() { int t; read(t); while (t--) doit(); }
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" ] }
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], dp[i-1][j-1]+mnn[i].a-mnn[i].b*(k-j)); poss[i][j]=(dp[i][j]^dp[i-1][j])==0?false:true; } } for(int i=n,j=k;i>0;i--) { if(poss[i][j]) { inc[i]=true; j--; } } out.println(k+2*(n-k)); for(int i=1,j=0;i<=n;i++) { if(j==k-1) { for(int z=1;z<=n;z++) { if(!inc[z]) { out.print(mnn[z].c+" "+(-mnn[z].c)+" "); } } j++; } if(inc[i]) { out.print(mnn[i].c+" "); j++; } } out.println(); } out.close(); } static class pair implements Comparable<pair>{ int a,b,c; public pair(int x,int y,int z) { a=x;b=y;c=z; } public int compareTo(pair p) { return b-p.b; //if(a>p.a) return 1; } } }