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
java
/****************************************************************************** Online Java Compiler. Code, Compile, Run and Debug java program online. Write your code in this editor and press "Run" button to execute it. *******************************************************************************/ import java.util.*; import java.io.*; public class Main{ public static void main(String []args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); PrintWriter out = new PrintWriter(System.out); int trials = Integer.parseInt(st.nextToken()); for(int trial = 0; trial < trials; trial++){ st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int k = Integer.parseInt(st.nextToken()); int[][] minion = new int[n][3]; for(int i = 0; i < n; i++){ st = new StringTokenizer(br.readLine()); int a = Integer.parseInt(st.nextToken()); int b = Integer.parseInt(st.nextToken()); minion[i][0] = a; minion[i][1] = b; minion[i][2] = i+1; } sort(minion, 0, n-1); int[][] dp = new int[n+1][k+1]; for(int i = 1; i <= n; i++){ dp[i][0] = dp[i-1][0] + (k-1)*minion[i-1][1]; for(int j = 1; j <= k; j++){ if(j < i) dp[i][j] = Math.max(dp[i-1][j] + (k-1)*minion[i-1][1], dp[i-1][j-1] + (j-1)*minion[i-1][1] + minion[i-1][0]); else if(j == i) dp[i][j] = dp[i-1][j-1] + (j-1)*minion[i-1][1] + minion[i-1][0]; } } int i = n; int j = k; Stack<Integer> stay = new Stack<Integer>(); HashSet<Integer> remove = new HashSet<Integer>(); while(j > 0){ if(dp[i][j] == dp[i-1][j-1] + (j-1)*minion[i-1][1] + minion[i-1][0]){ stay.push(minion[i-1][2]); j--; } else remove.add(minion[i-1][2]); i--; } while(i > 0){ remove.add(minion[i-1][2]); i--; } StringJoiner sj = new StringJoiner(" "); for(i = 0; i < k-1; i++) sj.add(Integer.toString(stay.pop())); for(int l:remove){ sj.add(Integer.toString(l)); sj.add(Integer.toString(-l)); } sj.add(Integer.toString(stay.pop())); out.println(2*n-k); out.println(sj); } out.close(); } static void sort(int[][] arr, int l, int r){ if (l < r) { // Find the middle point int m = (l+r)/2; // Sort first and second halves sort(arr, l, m); sort(arr , m+1, r); // Merge the sorted halves merge(arr, l, m, r); } } static void merge(int[][] arr, int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ int L[][] = new int [n1][3]; int R[][] = new int [n2][3]; /*Copy data to temp arrays*/ for (int i=0; i<n1; ++i){ L[i][0] = arr[l + i][0]; L[i][1] = arr[l+i][1]; L[i][2] = arr[l+i][2]; } for (int j=0; j<n2; ++j){ R[j][0] = arr[m + 1+ j][0]; R[j][1] = arr[m+1+j][1]; R[j][2] = arr[m+1+j][2]; } /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i][1] < R[j][1] || (L[i][1] == R[j][1] && L[i][0] < R[j][0])) { arr[k][0] = L[i][0]; arr[k][1] = L[i][1]; arr[k][2] = L[i][2]; i++; } else { arr[k][0] = R[j][0]; arr[k][1] = R[j][1]; arr[k][2] = R[j][2]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k][0] = L[i][0]; arr[k][1] = L[i][1]; arr[k][2] = L[i][2]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k][0] = R[j][0]; arr[k][1] = R[j][1]; arr[k][2] = R[j][2]; j++; 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; const int N = 75 + 10; struct re { int a, b, id; } a[N]; long long f[N][N]; bool plan[N][N]; bool choose[N]; vector<int> actions; void get_plan(int x, int y) { if (x == 0) return; if (plan[x][y]) choose[x] = true; get_plan(x - 1, y - plan[x][y]); } 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].a, &a[i].b); a[i].id = i; } sort(a + 1, a + n + 1, [&](const re &a, const re &b) { return a.b < b.b; }); memset(f, -1, sizeof(f[0]) * (n + 3)); f[0][0] = 0; for (int i = 1; i <= n; ++i) { int upb = min(i - 1, k); for (int j = 0; j <= upb; ++j) { if (f[i - 1][j] == -1) continue; long long tmp = f[i - 1][j] + (long long)(k - 1) * a[i].b; if (tmp > f[i][j]) { f[i][j] = tmp; plan[i][j] = false; } tmp = f[i - 1][j] + a[i].a + (long long)j * a[i].b; if (tmp > f[i][j + 1]) { f[i][j + 1] = tmp; plan[i][j + 1] = true; } } } memset(choose, 0, sizeof(choose[0]) * (n + 3)); get_plan(n, k); actions.clear(); int cnt = 0; for (int i = 1; i <= n && cnt < k - 1; ++i) { if (!choose[i]) continue; cnt++; actions.push_back(a[i].id); } for (int i = 1; i <= n; ++i) { if (choose[i]) continue; actions.push_back(a[i].id); actions.push_back(-a[i].id); } for (int i = n; i; --i) { if (!choose[i]) continue; actions.push_back(a[i].id); break; } int actions_tot = actions.size(); printf("%d\n", actions_tot); for (int i = 0; i < actions_tot; ++i) printf("%d%c", actions[i], " \n"[i == actions_tot - 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; vector<pair<pair<int, int>, int>> v; long long dp[80][80]; pair<int, int> from[80][80]; set<int> s; int main() { int t; cin >> t; while (t) { t--; int i, j, n, k; cin >> n >> k; v.clear(); s.clear(); for (i = 1; i <= n; i++) { int a, b; cin >> a >> b; v.push_back({{b, a}, i}); s.insert(i); } sort(v.begin(), v.end()); for (i = 0; i <= n; i++) { for (j = 0; j <= k; j++) { dp[i][j] = -100000000000; from[i][j] = {-1, -1}; } } dp[0][0] = 0; for (i = 0; i < n; i++) { int ind = v[i].second; long long a = v[i].first.second, b = v[i].first.first; for (j = 0; j <= k; j++) { if (dp[i + 1][j] < dp[i][j] + (k - 1) * b) { from[i + 1][j] = {ind, j}; dp[i + 1][j] = dp[i][j] + (k - 1) * b; } if (j > 0 && dp[i + 1][j] < dp[i][j - 1] + (j - 1) * b + a) { from[i + 1][j] = {ind, j - 1}; dp[i + 1][j] = dp[i][j - 1] + (j - 1) * b + a; } } } vector<int> ans; int nx = n, ny = k; while (ny != 0) { pair<int, int> p = from[nx][ny]; if (p.second != ny) { ans.push_back(p.first); s.erase(p.first); } nx--; ny = p.second; } cout << 2 * n - k << endl; for (i = k - 1; i > 0; i--) { cout << ans[i] << " "; } for (int x : s) { cout << x << " " << -x << " "; } cout << ans[0] << endl; } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T> struct rge { T b, e; }; template <class T> rge<T> range(T i, T j) { return rge<T>{i, j}; } struct debug { template <class T> debug& operator<<(const T&) { return *this; } }; using ll = long long; template <typename T> void min_self(T& a, T b) { a = min(a, b); } template <typename T> void max_self(T& a, T b) { a = max(a, b); } int k; template <typename flow_t, typename cost_t> class MinCostFlow { public: MinCostFlow(int _n, int _source, int _sink) : n(_n), source(_source), sink(_sink) { g.resize(n); cost.resize(n, vector<cost_t>(n)); cap.resize(n, vector<flow_t>(n)); } void add_edge(int from, int to, cost_t weight, flow_t c) { g[from].push_back(to); g[to].push_back(from); cost[to][from] = -weight; cost[from][to] = weight; cap[from][to] = c; } struct State { cost_t dist; int node; bool operator<(const State& rhs) const { return dist > rhs.dist; } }; void shortest_paths(vector<cost_t>& dist, vector<int>& par) { dist.assign(n, COST_INF); par.assign(n, -1); dist[source] = 0; priority_queue<State> pq; pq.push({0LL, source}); while (!pq.empty()) { State u = pq.top(); pq.pop(); if (u.dist > dist[u.node]) continue; for (int v : g[u.node]) { if (dist[v] > u.dist + cost[u.node][v] && cap[u.node][v] > 0) { dist[v] = u.dist + cost[u.node][v]; par[v] = u.node; pq.push({dist[v], v}); } } } } cost_t min_cost_flow() { flow_t mf = 0; cost_t total_cost = 0; vector<cost_t> dist(n); vector<int> par(n); while (true) { shortest_paths(dist, par); if (dist[sink] == COST_INF) { break; } flow_t f = FLOW_INF; int cur = sink; while (cur != source) { f = min(f, cap[par[cur]][cur]); cur = par[cur]; } mf += f; total_cost += dist[sink]; cur = sink; while (cur != source) { cap[par[cur]][cur] -= f; cap[cur][par[cur]] += f; cur = par[cur]; } } int m = (sink - 1) / 2; cout << k + 2 * (m - k) << endl; for (int i = 0; i < int(m); i++) { int pos = i + m; for (int j = 0; j < int(m); j++) { if (cap[j][pos] == 0) { if (i < k - 1 || i == m - 1) { cout << j + 1 << ' '; } else { cout << (j + 1) << ' ' << -(j + 1) << ' '; } } } } cout << endl; return total_cost; } private: const flow_t FLOW_INF = numeric_limits<flow_t>::max() / 2; const cost_t COST_INF = numeric_limits<cost_t>::max() / 2; vector<vector<int>> g; vector<vector<flow_t>> cap; vector<vector<cost_t>> cost; int n, source, sink; }; void test_case() { int n; cin >> n >> k; vector<int> a(n), b(n); for (int i = 0; i < int(n); i++) { cin >> a[i] >> b[i]; } int source = 2 * n, sink = 2 * n + 1; MinCostFlow<int, int> graph(2 * n + 2, source, sink); for (int i = 0; i < n; i++) { graph.add_edge(source, i, 0, 1); graph.add_edge(i + n, sink, 0, 1); } for (int i = 0; i < n; i++) { for (int pos = 0; pos < n; pos++) { int w = 0; if (pos < k - 1) { w = -(a[i] + pos * b[i]); } else if (pos < n - 1) { w = -(k - 1) * b[i]; } else { w = -(a[i] + (k - 1) * b[i]); } graph.add_edge(i, pos + n, w, 1); } } graph.min_cost_flow(); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int tc; cin >> tc; while (tc--) { test_case(); } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\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 double PI = acos(-1.0); array<long long, 3> a[80]; long long dp[80][80]; long long par[80][80]; void solve() { 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; } memset(dp, 0, sizeof dp); sort(a, a + n); for (long long i = 0; i < n; ++i) { for (long long j = 0; j < k + 1; ++j) { if (j > i) continue; if (dp[i + 1][j] < dp[i][j] + (k - 1) * a[i][0]) { dp[i + 1][j] = dp[i][j] + (k - 1) * a[i][0]; par[i + 1][j] = j; } if (j != k and dp[i + 1][j + 1] < dp[i][j] + a[i][1] + j * a[i][0]) { dp[i + 1][j + 1] = dp[i][j] + a[i][1] + j * a[i][0]; par[i + 1][j + 1] = j; } } } vector<long long> taken; vector<long long> not_taken; long long cur = k; for (long long i = n; i >= 1; --i) { if (cur - par[i][cur] == 1) { taken.push_back(a[i - 1][2]); } else { not_taken.push_back(a[i - 1][2]); } cur = par[i][cur]; } reverse(taken.begin(), taken.end()); for (long long i : not_taken) { long long idx = taken.size() - 1; taken.insert(taken.begin() + idx, i); ++idx; taken.insert(taken.begin() + idx, -i); } cout << (long long)taken.size() << '\n'; for (long long i : taken) { cout << i << ' '; } cout << '\n'; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t = 1; 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
python3
from typing import List import sys input = sys.stdin.readline import math ############ ---- Input Functions ---- ############ def inp(): return(int(input())) def inlt(): return(list(map(int,input().split()))) def insr(): s = input().strip() return(list(s[:len(s)])) def invr(): return(map(int,input().strip().split())) def solve_hungarian(a: List[List[int]], n: int, m: int): """ Implementation of Hungarian algorithm in n^2 m """ # potentials u = [0] * (n+1) v = [0] * (m+1) # pair row of each col p = [0] * (m+1) # for each col the number of prev col along the augmenting path way = [0] * (m+1) for i in range(1, n+1): p[0] = i j0 = 0 minv = [float('inf')] * (m+1) used = [False] * (m+1) # iterative Kun starts here condition = True while condition: # mark the current col as reachable used[j0] = True i0 = p[j0] delta = float('inf') # determine which col will become reachable after next potential update for j in range(1, m+1): if not used[j]: 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 # j1 will hold the col with min # way[j1] - the prev col in dfs # update the potential for j in range(0, m+1): if used[j]: # if col j was discovered: u[p[j]] += delta v[j] -= delta else: # not discovered - update min? minv[j] -= delta # j0 becomes the col on which the delta is achieved j0 = j1 # p[j0] == 0 => j0 - a col not in matching condition = p[j0] != 0 # the augmenting path was found - update the mapping condition = True while condition: # j1 is the prev column of j0 in augmenting path j1 = way[j0] p[j0] = p[j1] j0 = j1 condition = j0 != 0 ans = [0] * (n+1) for j in range(1, m+1): ans[p[j]] = j return -v[0], ans def solve(n, k, a, b): A = [[0] * (n+1) for _ in range(n+1) ] for i in range(1, n+1): for j in range(1, k+1): A[i][j] = a[i] + (j-1) * b[i] for j in range(k+1, n+1): A[i][j] = (k-1) * b[i] # turn into a max problem for i, row in enumerate(A): M = max(row) for j in range(n+1): A[i][j] = M - A[i][j] cost, match = solve_hungarian(A, n, n) print(n + (n-k)) role_to_creature = list(zip(match, range(len(match)))) role_to_creature.sort() res = [] for index in range(1, k): res.append(role_to_creature[index][1]) for index in range(k+1, n+1): res.append(role_to_creature[index][1]) res.append(-role_to_creature[index][1]) res.append(role_to_creature[k][1]) print(" ".join(map(str, res))) def from_file(f): return f.readline # with open('test.txt') as f: # input = from_file(f) t = inp() for _ in range(t): n, k = invr() a = [0] b = [0] for _ in range(n): ai, bi = invr() a.append(ai) b.append(bi) solve(n, k, a, b)
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\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 t, n, k, dp[80][80]; pair<pair<long long, long long>, long long> v[100]; long long f(int pos, long long num) { if (dp[pos][num] != -1) return dp[pos][num]; if (pos == n) { if (num != k) return dp[pos][num] = -1000000000000000; else return dp[pos][num] = 0; } long long res = -1000000000000000; res = max(res, f(pos + 1, num) + v[pos].first.first * (k - 1ll)); if (num < k) { res = max(res, f(pos + 1, num + 1) + v[pos].first.first * num + v[pos].first.second); } return dp[pos][num] = res; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> t; while (t--) { memset(dp, -1, sizeof(dp)); cin >> n >> k; for (int i = 0; i < n; i++) { cin >> v[i].first.second >> v[i].first.first; v[i].second = i + 1; } sort(v, v + n); f(0, 0); int pos = 0, num = 0; vector<int> us, notus; while (pos < n) { if (dp[pos + 1][num] != -1 && dp[pos + 1][num] + v[pos].first.first * (k - 1ll) == dp[pos][num]) { notus.push_back(v[pos].second); } else { us.push_back(v[pos].second); num++; } pos++; } cout << us.size() + notus.size() * 2 << "\n"; for (int i = 0; (i + 1) < us.size(); i++) { cout << us[i] << " "; } for (auto i : notus) cout << i << " " << -i << " "; cout << us[us.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; const long long inff = 0x3f3f3f3f3f3f3f3f; const int N = 1e2 + 8; int w[N][N], x[N], y[N], pop, n, k, lx, ly, a[N]; int px[N], py[N], sy[N], slack[N], par[N]; void adjust(int v) { sy[v] = py[v]; if (px[sy[v]] != -2) adjust(px[sy[v]]); } bool fid(int v) { for (int i(0); i <= (pop - 1); ++i) if (py[i] == -1) { if (slack[i] > x[v] + y[i] - w[v][i]) slack[i] = x[v] + y[i] - w[v][i], par[i] = v; if (x[v] + y[i] == w[v][i]) { py[i] = v; if (sy[i] == -1) { adjust(i); return 1; } if (px[sy[i]] != -1) continue; px[sy[i]] = i; if (fid(sy[i])) return 1; } } return 0; } void km() { for (int i(0); i <= (pop - 1); ++i) sy[i] = -1, y[i] = 0; for (int i(0); i <= (pop - 1); ++i) { x[i] = 0; for (int j(0); j <= (pop - 1); ++j) x[i] = max(x[i], w[i][j]); } int flag, m; for (int i(0); i <= (pop - 1); ++i) { for (int j(0); j <= (pop - 1); ++j) px[j] = py[j] = -1, slack[j] = int(0x3f3f3f3f); px[i] = -2; if (fid(i)) continue; flag = 0; while (!flag) { m = int(0x3f3f3f3f); for (int j(0); j <= (pop - 1); ++j) if (py[j] == -1) m = min(m, slack[j]); for (int j(0); j <= (pop - 1); ++j) { if (px[j] != -1) x[j] -= m; if (py[j] != -1) y[j] += m; else slack[j] -= m; } for (int j(0); j <= (pop - 1); ++j) if (py[j] == -1 && !slack[j]) { py[j] = par[j]; if (sy[j] == -1) { adjust(j), flag = 1; break; } px[sy[j]] = j; if (fid(sy[j])) { flag = 1; break; } } } } } int main() { cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { cin >> n >> k; pop = max(n, k); for (int i(1); i <= (n); ++i) { scanf("%d", &lx), scanf("%d", &ly); for (int j(1); j <= (min(k, n)); ++j) w[i - 1][j - 1] = lx + ly * (j - 1); for (int j(k + 1); j <= (n); ++j) w[i - 1][j - 1] = ly * (k - 1); } km(); if (k <= n) cout << 2 * n - k << endl; else cout << n << endl; for (int i(0); i <= (n - 1); ++i) { sy[i]++; if (i == min(n, k) - 1) continue; if (i >= k) cout << sy[i] << " " << -sy[i] << " "; else cout << sy[i] << " "; } cout << sy[min(n, k) - 1] << endl; } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> struct E { int a, b, rt; } s[76]; int dp[76][76], ch[76][76], v[76]; int st[76]; bool mk[76]; void work() { memset(dp, 0xAC, sizeof(dp)); memset(v, 0, sizeof(v)); memset(mk, 0, sizeof(mk)); int n, k; scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) { s[i].rt = i; scanf("%d%d", &s[i].a, &s[i].b); s[i].a -= (k - 1) * s[i].b; } std::sort(s + 1, s + n + 1, [](E x, E y) { return x.b < y.b; }); dp[0][0] = 0; for (int i = 1; i <= n; i++) { for (int j = std::min(i, k); j >= 1; j--) { ch[i][j] = v[j - 1]; dp[i][j] = dp[v[j - 1]][j - 1] + (j - 1) * s[i].b + s[i].a; if (dp[i][j] > dp[v[j]][j]) v[j] = i; } } int x = v[k], y = k; while (y) { st[y] = x; mk[x] = true; x = ch[x][y]; y--; } printf("%d\n", 2 * n - k); for (int i = 1; i < k; i++) printf("%d ", s[st[i]].rt); for (int i = 1; i <= n; i++) if (!mk[i]) printf("%d %d ", s[i].rt, -s[i].rt); printf("%d ", s[st[k]].rt); puts(""); } int main() { int T; scanf("%d", &T); while (T--) work(); }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\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
import sys readline = sys.stdin.readline read = sys.stdin.read ns = lambda: readline().rstrip() ni = lambda: int(readline().rstrip()) nm = lambda: map(int, readline().split()) nl = lambda: list(map(int, readline().split())) prn = lambda x: print(*x, sep='\n') def solve(): n, k = nm() mini = [tuple(nl() + [i+1]) for i in range(n)] mini.sort(key = lambda x: x[1]) # print(mini) dp = [-1]*(k+1) dp[0] = 0 f = [[0]*(k+1) for _ in range(n)] for i in range(n): if dp[k] > 0: dp[k] += (k - 1) * mini[i][1] for j in range(k-1, -1, -1): if dp[j] >= 0: if dp[j+1] < dp[j] + mini[i][0] + j * mini[i][1]: dp[j+1] = dp[j] + mini[i][0] + j * mini[i][1] f[i][j+1] = 1 dp[j] += (k - 1) * mini[i][1] cx = k a = list() b = list() for i in range(n-1, -1, -1): if f[i][cx]: a.append(mini[i][2]) cx -= 1 else: b.append(mini[i][2]) com = list() for x in a[:0:-1]: com.append(x) for x in b: com.append(x) com.append(-x) com.append(a[0]) print(len(com)) print(*com) return T = ni() for _ in range(T): solve()
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; vector<pair<pair<int, int>, int>> v; long long dp[80][80]; pair<int, int> from[80][80]; set<int> s; int main() { int t; cin >> t; while (t) { t--; int i, j, n, k; cin >> n >> k; v.clear(); s.clear(); for (i = 1; i <= n; i++) { int a, b; cin >> a >> b; v.push_back({{b, a - (k - 1) * b}, i}); s.insert(i); } sort(v.begin(), v.end()); for (i = 0; i <= n; i++) { for (j = 0; j <= k; j++) { dp[i][j] = -100000000000; from[i][j] = {-1, -1}; } } dp[0][0] = 0; for (i = 0; i < n; i++) { int ind = v[i].second; long long a = v[i].first.second, b = v[i].first.first; for (j = 0; j <= k; j++) { if (dp[i + 1][j] < dp[i][j]) { from[i + 1][j] = {ind, j}; dp[i + 1][j] = dp[i][j]; } if (j > 0 && dp[i + 1][j] < dp[i][j - 1] + (j - 1) * b + a) { from[i + 1][j] = {ind, j - 1}; dp[i + 1][j] = dp[i][j - 1] + (j - 1) * b + a; } } } vector<int> ans; int nx = n, ny = k; while (ny != 0) { pair<int, int> p = from[nx][ny]; if (p.second != ny) { ans.push_back(p.first); s.erase(p.first); } nx--; ny = p.second; } cout << 2 * n - k << endl; for (i = k - 1; i > 0; i--) { cout << ans[i] << " "; } for (int x : s) { cout << x << " " << -x << " "; } cout << ans[0] << endl; } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int f[505][505], g[505][505], a[505], b[505], id[505], xinyue, n, m, vis[505], ed; int cmp(int x, int y) { return b[x] < b[y]; } void dfs(int n, int m) { if (!n) return; if (g[n][m] == 1) vis[n] = 1, m--; dfs(n - 1, m); } int main() { scanf("%d", &xinyue); while (xinyue--) { scanf("%d%d", &n, &m); for (int i = 0; i <= n; i++) { for (int t = 0; t <= m; t++) { f[i][t] = -1; } } f[0][0] = 0; memset(g, 0, sizeof(g)); for (int i = 1; i <= n; i++) scanf("%d%d", &a[i], &b[i]), id[i] = i; sort(id + 1, id + n + 1, cmp); for (int i = 1; i <= n; i++) { for (int t = 0; t <= min(i, m); t++) { if (f[i - 1][t] != -1) f[i][t] = f[i - 1][t] + b[id[i]] * (m - 1); g[i][t] = 0; if (t && f[i - 1][t - 1] + a[id[i]] + b[id[i]] * (t - 1) > f[i][t] && f[i - 1][t - 1] != -1) { f[i][t] = f[i - 1][t - 1] + a[id[i]] + b[id[i]] * (t - 1); g[i][t] = 1; } } } memset(vis, 0, sizeof(vis)); dfs(n, m); printf("%d\n", m + (n - m) * 2); ed = 0; for (int i = 1; i <= n; i++) { if (vis[i]) { ed++; if (ed == m) { ed = id[i]; break; } printf("%d ", id[i]); } } for (int i = 1; i <= n; i++) { if (!vis[i]) { printf("%d %d ", id[i], -id[i]); } } printf("%d\n", ed); } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; const int inf = 0x3f3f3f3f; const long long int infl = 1e18; long long int din[109][109]; int prv[109][109], t; struct st { int x, y, id; }; st p[100]; int main() { cin >> t; while (t--) { int n, k; cin >> n >> k; memset(din, -1, sizeof(din)); for (int i = 0; i < n; ++i) { cin >> p[i].x >> p[i].y; p[i].id = i; } sort(p, p + n, [](st a, st b) { return a.y < b.y; }); din[0][0] = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < k + 1; ++j) { if (din[i][j] == -1) { continue; } if (j + 1 <= k) { long long int c = din[i][j] + p[i].x + p[i].y * j; if (din[i + 1][j + 1] < c) { prv[i + 1][j + 1] = j; din[i + 1][j + 1] = c; } } long long int c = din[i][j] + p[i].y * (k - 1); if (din[i + 1][j] < c) { prv[i + 1][j] = j; din[i + 1][j] = c; } } } int nx = n, ny = k; vector<int> A, B; while (nx) { if (prv[nx][ny] != ny) { A.push_back(p[nx - 1].id); } else { B.push_back(p[nx - 1].id); } ny = prv[nx][ny]; nx--; } reverse(A.begin(), A.end()); reverse(B.begin(), B.end()); cout << A.size() + B.size() * 2 << "\n"; for (int i = 0; i < A.size() - 1; ++i) { cout << A[i] + 1 << " "; } for (int i : B) { cout << i + 1 << " " << -i - 1 << " "; } cout << A.back() + 1 << "\n"; } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 75; struct node { int a, b, num; } p[maxn + 11]; int f[maxn + 11][maxn + 11][2]; vector<int> v1, v2; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t; cin >> t; for (int kase = 1; kase <= t; kase++) { int n, k; cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> p[i].a >> p[i].b; p[i].num = i; } sort(p + 1, p + 1 + n, [&](node a, node b) { return a.b < b.b; }); memset(f, 0, sizeof(f)); for (int i = 1; i <= n; i++) { f[i][0][0] = f[i - 1][0][0] + p[i].b * (k - 1); for (int j = 1; j <= min(k, i); j++) { if (i > j) f[i][j][0] = max(f[i - 1][j][0], f[i - 1][j][1]) + p[i].b * (k - 1); f[i][j][1] = max(f[i - 1][j - 1][0], f[i - 1][j - 1][1]) + p[i].b * (j - 1) + p[i].a; } } v1.clear(); v2.clear(); int x = n; int y = k; while (x) { if (f[x][y][0] >= f[x][y][1]) { v2.emplace_back(x); x--; } else { v1.emplace_back(x); x--; y--; } } reverse(v1.begin(), v1.end()); int m = (int)v1.size() + 2 * (int)v2.size(); cout << m << endl; for (int i = 0; i < v1.size() - 1; i++) cout << p[v1[i]].num << " "; for (auto u : v2) cout << p[u].num << " " << -p[u].num << " "; cout << p[v1.back()].num << endl; } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; template <typename T1, typename T2> inline void chkmin(T1 &x, const T2 &y) { if (x > y) x = y; } template <typename T1, typename T2> inline void chkmax(T1 &x, const T2 &y) { if (x < y) x = y; } struct fa { int a, b, ind; fa() {} }; int n, k; const int MAXN = 76; fa a[MAXN]; const int INF = 1e9; int dp[MAXN][MAXN][2]; int par[MAXN][MAXN][2]; void relax(int pref, int cnt, int ind, int val, int fpar) { if (dp[pref][cnt][ind] >= val) return; dp[pref][cnt][ind] = val; par[pref][cnt][ind] = fpar; } void solve() { cin >> n >> k; for (int i = 0; i < n; i++) { cin >> a[i].a >> a[i].b; a[i].ind = i + 1; } sort(a, a + n, [&](fa i, fa j) { return i.b < j.b; }); if (k == 1) { int pos = max_element(a, a + n, [&](fa i, fa j) { return i.a < j.a; }) - a; cout << 1 << "\n" << a[pos].ind << "\n"; return; } if (k == n) { cout << n << "\n"; for (int i = 0; i < n; i++) { cout << a[i].ind << " "; } cout << "\n"; return; } for (int i = 0; i < MAXN; i++) { for (int j = 0; j < MAXN; j++) { for (int k = 0; k < 2; k++) { dp[i][j][k] = -INF; } } } cerr << "a = " << endl; for (int i = 0; i < n; i++) { cerr << a[i].a << " " << a[i].b << endl; } dp[0][0][0] = 0; for (int i = 0; i < n; i++) { for (int cnt = 0; cnt <= k - 1; cnt++) { for (int flag = 0; flag < 2; flag++) { if (dp[i][cnt][flag] == -INF) continue; if (cnt < k - 1) relax(i + 1, cnt + 1, flag, dp[i][cnt][flag] + a[i].a + a[i].b * cnt, 1); relax(i + 1, cnt, flag, dp[i][cnt][flag] + a[i].b * (k - 1), 2); if (!flag) { relax(i + 1, cnt, 1, dp[i][cnt][flag] + a[i].a + a[i].b * (k - 1), 3); } } } } int pos = 0, cnt = 0, flag = 0; for (int i = 0; i < MAXN; i++) { if (dp[i][k - 1][1] > dp[pos][cnt][flag]) { pos = i, cnt = k - 1, flag = 1; } } cerr << "my_ans = " << dp[pos][cnt][flag] << endl; vector<pair<int, int>> ans; for (; pos >= 1; pos--) { ans.push_back({par[pos][cnt][flag], pos}); int ncnt = cnt; if (par[pos][cnt][flag] == 1) ncnt--; int nflag = flag; if (par[pos][cnt][flag] == 3) nflag--; cnt = ncnt; flag = nflag; } sort(ans.begin(), ans.end()); vector<int> fout; for (auto i : ans) { fout.push_back(i.second); if (i.first == 2) { fout.push_back(-i.second); } } cout << fout.size() << "\n"; for (auto i : fout) { cout << a[abs(i) - 1].ind * i / abs(i) << " "; } cout << "\n"; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { solve(); } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline long long mod(long long n, long long m) { long long ret = n % m; if (ret < 0) ret += m; return ret; } long long gcd(long long a, long long b) { return (b == 0LL ? a : gcd(b, a % b)); } long long exp(long long a, long long b, long long m) { if (b == 0LL) return 1LL; if (b == 1LL) return mod(a, m); long long k = mod(exp(a, b / 2, m), m); if (b & 1LL) { return mod(a * mod(k * k, m), m); } else return mod(k * k, m); } const long long N = 80; struct coisa { long long a, b, id; coisa() {} coisa(long long A, long long B, long long C) { a = A, b = B, id = C; } } v[N]; long long dp[N][N]; long long n, k; long long solve(long long id, long long j) { if (id == n + 1) { if (j != k) return -1e9; return 0; } long long& x = dp[id][j]; if (x != -1) return x; long long nao = solve(id + 1, j) + (k - 1) * v[id].b; if (j == k) return x = nao; long long pega = solve(id + 1, j + 1) + v[id].b * (j) + v[id].a; return x = max(pega, nao); } vector<long long> tira, col; void rool(long long id, long long j) { if (id == n + 1) return; if (j == k) { tira.push_back(id); rool(id + 1, j); return; } if (solve(id, j) == solve(id + 1, j) + (k - 1) * v[id].b) { tira.push_back(id); rool(id + 1, j); return; } col.push_back(id); rool(id + 1, j + 1); return; } void solve() { cin >> n >> k; for (long long i = 1; i <= n; i++) { long long a, b; cin >> a >> b; v[i] = coisa(a, b, i); } sort(v + 1, v + 1 + n, [&](coisa a, coisa b) { return a.b < b.b; }); if (k == 1) { sort(v + 1, v + 1 + n, [&](coisa a, coisa b) { return a.a > b.a; }); cout << 1 << "\n"; cout << v[1].id << "\n"; return; } if (k == n) { cout << n << "\n"; for (long long i = 1; i <= n; i++) { cout << v[i].id << " "; } cout << "\n"; return; } memset(dp, -1, sizeof(dp)); tira.clear(); col.clear(); rool(1, 0); vector<long long> ans; for (long long i = 0; i < (long long)col.size() - 1; i++) ans.push_back(v[col[i]].id); for (long long x : tira) ans.push_back(v[x].id), ans.push_back(-v[x].id); if ((long long)col.size() - 1 >= 0) ans.push_back(v[col[(long long)col.size() - 1]].id); cout << ans.size() << "\n"; for (long long x : ans) cout << x << " "; cout << "\n"; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long t; cin >> t; while (t--) { solve(); } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; const int N = 100; array<long long, 3> a[N]; long long dp[N][N]; int anc[N][N]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { int n, k; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> a[i][1] >> a[i][0]; a[i][2] = i; } sort(a, a + n); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { dp[i][j] = -INF; } } dp[0][0] = 0; for (int i = 0; i < n; i++) { for (int j = 0; j <= k; j++) { if (dp[i][j] != -INF) { if (dp[i + 1][j] < dp[i][j] + (k - 1) * a[i][0]) { dp[i + 1][j] = dp[i][j] + (k - 1) * a[i][0]; anc[i + 1][j] = j; } if (dp[i + 1][j + 1] < dp[i][j] + a[i][1] + a[i][0] * j) { dp[i + 1][j + 1] = dp[i][j] + a[i][1] + a[i][0] * j; anc[i + 1][j + 1] = j; } } } } vector<int> taken, killed; int x = n, y = k; while (x > 0) { if (anc[x][y] == y) killed.push_back(x - 1); else taken.push_back(x - 1); y = anc[x][y]; x--; } reverse(taken.begin(), taken.end()); cout << taken.size() + killed.size() * 2 << "\n"; for (int i = 0; i + 1 < taken.size(); i++) { cout << a[taken[i]][2] + 1 << " "; } for (int i = 0; i < killed.size(); i++) { cout << (a[killed[i]][2] + 1) << " "; cout << -(a[killed[i]][2] + 1) << " "; } cout << a[taken.back()][2] + 1 << "\n"; } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using cd = complex<double>; int main() { ll t; cin >> t; while (t--) { ll n, k; cin >> n >> k; pair<pair<ll, ll>, ll> ar[n]; for (ll i = 0; i < n; i++) { cin >> ar[i].first.second >> ar[i].first.first; ar[i].second = i + 1; } sort(ar, ar + n); ll dp[n + 1][k + 1], ch[n + 1][k + 1]; for (ll i = 0; i <= n; i++) { for (ll j = 0; j <= k; j++) { dp[i][j] = -(ll)(1e18); } } dp[0][0] = 0; for (ll i = 0; i < n; i++) { for (ll j = 0; j <= k; j++) { ll& tar = dp[i + 1][j + 1]; ll v1 = dp[i][j] + j * ar[i].first.first + ar[i].first.second, v2 = dp[i][j] + (k - 1) * ar[i].first.first; if (j < k && v1 > tar) { tar = v1; ch[i + 1][j + 1] = j; } if (v2 > dp[i + 1][j]) { dp[i + 1][j] = v2; ch[i + 1][j] = j; } } } vector<ll> v1, v2; ll x = n, y = k; for (ll i = 0; i < n; i++) { if (ch[x][y] == y) { v2.push_back(ar[n - 1 - i].second); } else { v1.push_back(ar[n - 1 - i].second); } y = ch[x][y]; x--; } reverse(v1.begin(), v1.end()); cout << v1.size() + 2 * v2.size() << "\n"; ll last = v1.back(); v1.back(); v1.pop_back(); for (ll x : v1) { cout << x << " "; } for (ll x : v2) { cout << x << " " << -x << " "; } cout << last << "\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; 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 used[maxn][maxn], visin[maxn]; int Out[maxn], In[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) { used[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], used[i][j] = 0; else f[i][j] = f[i - 1][j - 1] + tmp + a[i].a, used[i][j] = 1; } } i = n, j = k - 1; while (i && j) { if (!used[i][j]) --i; else In[j] = i, visin[i] = 1, --i, --j; } int cnt(0); for (i = 1; i <= n; ++i) if (!visin[i] && i != st) Out[++cnt] = i; int ct1(0); for (i = 1; i <= cnt; ++i) tmp[++ct1] = a[Out[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[In[i]].a + (i - 1) * a[In[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] = In[i]; for (i = 1; i <= cnt; ++i) ans2[i] = Out[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; long long mod = 1000000007; long long inf = 1LL << 50; const int N = 75; int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin.exceptions(cin.failbit); int T; cin >> T; for (int z = 0; z < T; z++) { int n, k; cin >> n >> k; vector<vector<pair<long long, vector<int>>>> dp( n, vector<pair<long long, vector<int>>>(k + 1, {-inf, vector<int>()})); vector<pair<int, pair<int, int>>> srted(n); for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; srted[i].first = b; srted[i].second.first = a; srted[i].second.second = i; } sort(srted.begin(), srted.end()); dp[0][1].first = srted[0].second.first; dp[0][1].second.push_back(srted[0].second.second); dp[0][0].first = (k - 1) * srted[0].first; for (int i = 1; i < n; i++) { dp[i][0].first = dp[i - 1][0].first + srted[i].first * (k - 1); for (int a = 1; a <= k; a++) { if (dp[i - 1][a].first + srted[i].first * (k - 1) > dp[i - 1][a - 1].first + srted[i].first * (a - 1) + srted[i].second.first) { dp[i][a].first = dp[i - 1][a].first + srted[i].first * (k - 1); dp[i][a].second = dp[i - 1][a].second; } else { dp[i][a].first = dp[i - 1][a - 1].first + srted[i].first * (a - 1) + srted[i].second.first; dp[i][a].second = dp[i - 1][a - 1].second; dp[i][a].second.push_back(srted[i].second.second); } } } cout << n + (n - k) << "\n"; vector<bool> visited(n, false); for (int i = 0; i < dp[n - 1][k].second.size() - 1; i++) { cout << dp[n - 1][k].second[i] + 1 << " "; visited[dp[n - 1][k].second[i]] = true; } for (int i = 0; i < n; i++) { if (!visited[i] && i != dp[n - 1][k].second.back()) { cout << i + 1 << " " << -(i + 1) << " "; } } cout << dp[n - 1][k].second.back() + 1 << '\n'; } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; template <typename T1, typename T2> inline void chkmin(T1 &x, const T2 &y) { if (x > y) x = y; } template <typename T1, typename T2> inline void chkmax(T1 &x, const T2 &y) { if (x < y) x = y; } struct fa { int a, b, ind; fa() {} }; int n, k; const int MAXN = 76; fa a[MAXN]; const int INF = 1e9; int dp[MAXN][MAXN][2]; int par[MAXN][MAXN][2]; void relax(int pref, int cnt, int ind, int val, int fpar) { if (dp[pref][cnt][ind] >= val) return; dp[pref][cnt][ind] = val; par[pref][cnt][ind] = fpar; } void solve() { cin >> n >> k; for (int i = 0; i < n; i++) { cin >> a[i].a >> a[i].b; a[i].ind = i + 1; } sort(a, a + n, [&](fa i, fa j) { return i.b < j.b; }); if (k == 1) { int pos = max_element(a, a + n, [&](fa i, fa j) { return i.a < j.a; }) - a; cout << 1 << "\n" << a[pos].ind << "\n"; return; } if (k == n) { cout << n << "\n"; for (int i = 0; i < n; i++) { cout << a[i].ind << " "; } cout << "\n"; return; } for (int i = 0; i < MAXN; i++) { for (int j = 0; j < MAXN; j++) { for (int k = 0; k < 2; k++) { dp[i][j][k] = -INF; } } } dp[0][0][0] = 0; for (int i = 0; i < n; i++) { for (int cnt = 0; cnt <= k - 1; cnt++) { for (int flag = 0; flag < 2; flag++) { if (dp[i][cnt][flag] == -INF) continue; if (cnt < k - 1) relax(i + 1, cnt + 1, flag, dp[i][cnt][flag] + a[i].a + a[i].b * cnt, 1); relax(i + 1, cnt, flag, dp[i][cnt][flag] + a[i].b * (k - 1), 2); if (!flag) { relax(i + 1, cnt, 1, dp[i][cnt][flag] + a[i].a + a[i].b * (k - 1), 3); } } } } int pos = 0, cnt = 0, flag = 0; for (int i = 0; i < MAXN; i++) { if (dp[i][k - 1][1] > dp[pos][cnt][flag]) { pos = i, cnt = k - 1, flag = 1; } } vector<pair<int, int>> ans; for (; pos >= 1; pos--) { ans.push_back({par[pos][cnt][flag], pos}); int ncnt = cnt; if (par[pos][cnt][flag] == 1) ncnt--; int nflag = flag; if (par[pos][cnt][flag] == 3) nflag--; cnt = ncnt; flag = nflag; } sort(ans.begin(), ans.end()); vector<int> fout; for (auto i : ans) { fout.push_back(i.second); if (i.first == 2) { fout.push_back(-i.second); } } cout << fout.size() << "\n"; for (auto i : fout) { cout << a[abs(i) - 1].ind * i / abs(i) << " "; } cout << "\n"; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { solve(); } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll MOD = 1000000007; const int NINF = -1 * 1e9; void print2d(vector<vector<int>> &v) { for (int i = 0; i < (int)v.size(); i++) { for (int j = 0; j < (int)v[i].size(); j++) { cout << v[i][j] << " "; } cout << endl; } } int watson(int n, int k, vector<tuple<int, int, int>> &v, vector<int> &inc, vector<int> &exl) { vector<vector<int>> dp(n + 1, vector<int>(k + 1, 0)); vector<vector<int>> par(n + 1, vector<int>(k + 1, 0)); for (int j = 1; j <= k; j++) { dp[0][j] = NINF; } for (int i = 1; i <= n; i++) { for (int j = 0; j <= k; j++) { int a = get<1>(v[i - 1]); int b = get<0>(v[i - 1]); int x = NINF, y = NINF; y = dp[i - 1][j] + b * 1LL * (k - 1); if (j > 0) { x = dp[i - 1][j - 1] + a + b * 1LL * (j - 1); } if (x > y) { dp[i][j] = x; par[i][j] = 1; } else { dp[i][j] = y; par[i][j] = 0; } } } int j = k; for (int i = n; i > 0; i--) { int x = get<2>(v[i - 1]); if (par[i][j] == 1) { inc.push_back(x); j -= 1; } else if (par[i][j] == 0) { exl.push_back(x); } } reverse((inc).begin(), (inc).end()); int p = dp[n][k]; par.clear(); dp.clear(); return p; } void sherlock(int t) { int n, k; cin >> n >> k; vector<tuple<int, int, int>> v(n), v1; int a, b; for (int i = 0; i < n; i++) { cin >> a >> b; v[i] = {b, a, i + 1}; } sort((v).begin(), (v).end()); vector<int> inc, exl; watson(n, k, v, inc, exl); cout << inc.size() + 2 * exl.size() << endl; for (int i = 0; i < k - 1; i++) { cout << inc[i] << " "; } for (auto x : exl) { cout << x << " " << -x << " "; } cout << inc.back() << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int tt; cin >> tt; for (int t = 1; t <= tt; t++) sherlock(t); return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; clock_t start; mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count()); template <typename T> void _F(const char *name, T arg1) { cerr << name << " = " << arg1 << '\n'; } template <typename T, typename... Args> void _F(const char *names, T arg1, Args... args) { const char *name = strchr(names, ','); cerr.write(names, name - names) << " = " << arg1 << '\n'; _F(name + 2, args...); } template <typename T1, typename T2> istream &operator>>(istream &in, pair<T1, T2> &q) { in >> q.first >> q.second; return in; } template <typename T1, typename T2> ostream &operator<<(ostream &out, pair<T1, T2> &q) { out << q.first << " " << q.second; return out; } template <typename T1, typename T2> pair<T1, T2> operator+(pair<T1, T2> p1, pair<T1, T2> p2) { return {p1.first + p2.first, p1.second + p2.second}; } template <typename T1, typename T2> pair<T1, T2> operator-(pair<T1, T2> p1, pair<T1, T2> p2) { return {p1.first - p2.first, p1.second - p2.second}; } const int N = 75 * 2 + 10; struct MinCostFlow { struct Edge { int u, c, f; Edge *nex; Edge *rev; Edge(int _u, int _c, int _f, Edge *_nex) : u(_u), c(_c), f(_f), nex(_nex) {} }; std::vector<Edge *> g; MinCostFlow() { g.resize(N); } void AddEdge(int v, int u, int f, int c) { g[v] = new Edge(u, c, f, g[v]); g[u] = new Edge(v, -c, 0, g[u]); g[v]->rev = g[u]; g[u]->rev = g[v]; } pair<int, int> Flow(int src, int sink) { int flow = 0, cost = 0; while (true) { std::vector<int> dis(N, (int)(2e9)); std::vector<bool> vis(N, 0); std::vector<Edge *> par(N); std::vector<int> mn(N, 0); queue<int> q; q.push(src); vis[src] = 1; mn[src] = (int)(2e9); dis[src] = 0; while (!q.empty()) { int v = q.front(); q.pop(); vis[v] = 0; for (auto e = g[v]; e; e = e->nex) { if (e->f <= 0) continue; if (dis[e->u] > dis[v] + e->c) { dis[e->u] = dis[v] + e->c; mn[e->u] = min(mn[v], e->f); par[e->u] = e; if (!vis[e->u]) { vis[e->u] = 1; q.push(e->u); } } } } if (dis[sink] == (int)(2e9)) break; cost += dis[sink]; int cur = sink; flow += mn[sink]; while (cur != src) { Edge *e = par[cur]; e->f -= mn[sink]; e->rev->f += mn[sink]; cur = e->rev->u; } } return make_pair(flow, cost); } int Find(int x) { for (auto e = g[x]; e; e = e->nex) { if (e->f == 1) { return e->u; } } return -1; } void Erase() { while ((int)((g).size())) g.pop_back(); g.resize(N); } } Mcf; void solve() { int n, k; cin >> n >> k; int a[n], b[n]; for (int i = 0; i < n; i++) { cin >> a[i] >> b[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (j < k - 1) Mcf.AddEdge(n + i, j, 1, -a[i] - j * b[i]); else if (j < n - 1) Mcf.AddEdge(n + i, j, 1, -(k - 1) * b[i]); else Mcf.AddEdge(n + i, j, 1, -a[i] - (k - 1) * b[i]); } Mcf.AddEdge(i, N - 2, 1, 0); Mcf.AddEdge(N - 1, i + n, 1, 0); } Mcf.Flow(N - 1, N - 2); cout << k + (n - k) * 2 << '\n'; for (int i = 0; i < k - 1; i++) { cout << Mcf.Find(i) - n + 1 << " "; } for (int i = k - 1; i < n - 1; i++) { cout << Mcf.Find(i) - n + 1 << " "; cout << -(Mcf.Find(i) - n + 1) << " "; } cout << (Mcf.Find(n - 1) - n + 1) << '\n'; Mcf.Erase(); } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; start = clock(); cout << fixed << setprecision(20); cin >> t; for (int i = 1; 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; struct Minion { int ind; long long a, b; Minion(int ii = 0, long long ia = 0, long long ib = 0) { ind = ii; a = ia; b = ib; } }; bool cmp(Minion a, Minion b) { return a.b < b.b; } Minion a[100]; long long dp[100][100]; void solve() { int n, k; cin >> n >> k; long long sum = 0; for (int i = 1; i <= n; i++) { int a, b; cin >> a >> b; ::a[i] = Minion(i, a - k * b, b); } sort(a + 1, a + n + 1, cmp); memset(dp, 0, sizeof(dp)); for (int i = 1; i <= n; i++) { dp[0][i] = -1e14; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1] + a[i].a + j * a[i].b); } for (int j = i + 1; j <= n; j++) { dp[i][j] = -1e14; } } vector<int> ord; set<int> st; int ind = n, cnt = k; while (ind > 0 && cnt > 0) { if (dp[ind][cnt] == dp[ind - 1][cnt - 1] + a[ind].a + cnt * a[ind].b) { ord.push_back(ind); cnt--; st.insert(ind); } ind--; } reverse(ord.begin(), ord.end()); vector<int> op; for (int i = 0; i + 1 < ord.size(); i++) { op.push_back(a[ord[i]].ind); } for (int i = 1; i <= n; i++) { if (st.find(i) == st.end()) { op.push_back(a[i].ind); op.push_back(-a[i].ind); } } op.push_back(a[ord.back()].ind); cout << op.size() << '\n'; for (auto x : op) { cout << x << " "; } cout << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout << setprecision(32); 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 maxn = 1e2 + 10; vector<int> del, add; int n, k, dp[maxn][maxn], pre[maxn][maxn]; struct minion { int a, b, id; bool operator<(const minion &A) const { return b == A.b ? a < A.a : b < A.b; } void read(int i) { id = i; scanf("%d%d", &a, &b); } } da[maxn]; int main() { int t; scanf("%d", &t); while (t--) { scanf("%d%d", &n, &k); for (int i = 1; i <= n; ++i) da[i].read(i); sort(da + 1, da + n + 1); for (int i = 0; i <= n; ++i) for (int j = 0; j <= n; ++j) dp[i][j] = -1; dp[0][0] = 0; add.clear(); del.clear(); for (int i = 0; i < n; ++i) for (int j = 0; j <= k; ++j) if (dp[i][j] >= 0) { if (dp[i + 1][j] < dp[i][j]) { dp[i + 1][j] = dp[i][j]; pre[i + 1][j] = 0; } if (j < k && dp[i + 1][j + 1] < dp[i][j] + da[i + 1].a + da[i + 1].b * j) { dp[i + 1][j + 1] = dp[i][j] + da[i + 1].a + da[i + 1].b * j; pre[i + 1][j + 1] = 1; } if (dp[i + 1][j] < dp[i][j] + da[i + 1].b * (k - 1)) { dp[i + 1][j] = dp[i][j] + da[i + 1].b * (k - 1); pre[i + 1][j] = 2; } } for (int i = n, j = k; i; --i) { if (!pre[i][j]) continue; if (pre[i][j] == 1) { --j; add.push_back(da[i].id); } else { del.push_back(da[i].id); } } printf("%d\n", add.size() + del.size() * 2); for (int i = add.size() - 1; i; --i) printf("%d ", add[i]); for (auto x : del) printf("%d %d ", x, -x); printf("%d\n", add[0]); } 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 me[76][76]; int de[76][76]; int n, k; int res[200]; const int INF = 1000000000; vector<pair<int, pair<int, int> > > mv; int dp(int ac, int ll) { if (me[ac][ll] >= 0) return me[ac][ll]; if (ac == n) return me[ac][ll] = 0; if (ll < k) { me[ac][ll] = mv[ac].first * ll + dp(ac + 1, ll + 1) + mv[ac].second.first; de[ac][ll] = 0; } if (n - ac - 1 + ll >= k) { int can = mv[ac].first * (k - 1) + dp(ac + 1, ll); if (can > me[ac][ll]) { me[ac][ll] = can; de[ac][ll] = 1; } } return me[ac][ll]; } int it; void rec(int ac, int ll) { if (ac == n) return; if (de[ac][ll]) { res[it++] = mv[ac].second.second + 1; res[it++] = -mv[ac].second.second - 1; rec(ac + 1, ll); } else { res[ll] = mv[ac].second.second + 1; rec(ac + 1, ll + 1); } } int main() { cin.tie(0); ios_base::sync_with_stdio(0); int t; cin >> t; while (t--) { cin >> n >> k; mv.clear(); mv.resize(n); for (int i = 0; i < n; i++) { mv[i].second.second = i; cin >> mv[i].second.first >> mv[i].first; } sort(mv.begin(), mv.end()); it = k; memset(me, -1, sizeof me); dp(0, 0); rec(0, 0); cout << it << endl; for (int i = 0; i < k - 1; i++) cout << res[i] << ' '; for (int i = k; i < it; i++) cout << res[i] << ' '; cout << res[k - 1] << endl; } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; void solve() { int n, k; cin >> n >> k; long long a1[n], b1[n]; for (int i = 0; i < n; i++) cin >> a1[i] >> b1[i]; long long dp[n][k]; vector<long long> a, b; vector<pair<pair<long long, long long>, long long> > e; vector<long long> ans; long long mx = 0; for (int p = 0; p < n; p++) { a.clear(); b.clear(); e.clear(); for (int i = 0; i < n; i++) if (i == p) continue; else e.push_back(make_pair(make_pair(b1[i], a1[i]), i)); sort(e.begin(), e.end()); a.push_back(0); b.push_back(0); for (int i = 0; i < e.size(); i++) { a.push_back(e[i].first.second); b.push_back(e[i].first.first); } for (int i = 0; i < n; i++) for (int j = 0; j < k; j++) dp[i][j] = -1e9; dp[0][0] = 0; for (int i = 1; i < n; i++) for (int j = 0; j < k; j++) { dp[i][j] = max(dp[i][j], dp[i - 1][j] + b[i] * (k - 1)); if (j > 0) dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + b[i] * (j - 1) + a[i]); } if (mx < a1[p] + b1[p] * (k - 1) + dp[n - 1][k - 1]) { mx = a1[p] + b1[p] * (k - 1) + dp[n - 1][k - 1]; vector<long long> x, bad; int i = n - 1, j = k - 1; while (dp[i][j] > 0) { if (j > 0) { if (dp[i - 1][j - 1] + b[i] * (j - 1) + a[i] == dp[i][j]) { x.push_back(e[i - 1].second + 1); i--; j--; } else { bad.push_back(-e[i - 1].second - 1); bad.push_back(e[i - 1].second + 1); i--; } } else { bad.push_back(-e[i - 1].second - 1); bad.push_back(e[i - 1].second + 1); i--; } } reverse(x.begin(), x.end()); reverse(bad.begin(), bad.end()); for (int i = 0; i < bad.size(); i++) x.push_back(bad[i]); x.push_back(p + 1); ans = x; } } cout << ans.size() << "\n"; for (int i = 0; i < ans.size(); i++) cout << ans[i] << ' '; cout << "\n"; } int main() { ios::sync_with_stdio(0); int q; cin >> q; while (q--) solve(); }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; void *wmem; char memarr[96000000]; template <class T> inline void walloc1d(T **arr, int x, void **mem = &wmem) { static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; (*mem) = (void *)(((char *)(*mem)) + skip[((unsigned long long)(*mem)) & 15]); (*arr) = (T *)(*mem); (*mem) = ((*arr) + x); } template <class T1, class T2> void sortA_L(int N, T1 a[], T2 b[], void *mem = wmem) { int i; pair<T1, T2> *arr; walloc1d(&arr, N, &mem); for (i = (0); i < (N); i++) { arr[i].first = a[i]; arr[i].second = b[i]; } sort(arr, arr + N); for (i = (0); i < (N); i++) { a[i] = arr[i].first; b[i] = arr[i].second; } } template <class T1, class T2, class T3> void sortA_L(int N, T1 a[], T2 b[], T3 c[], void *mem = wmem) { int i; pair<T1, pair<T2, T3> > *arr; walloc1d(&arr, N, &mem); for (i = (0); i < (N); i++) { arr[i].first = a[i]; arr[i].second.first = b[i]; arr[i].second.second = c[i]; } sort(arr, arr + N); for (i = (0); i < (N); i++) { a[i] = arr[i].first; b[i] = arr[i].second.first; c[i] = arr[i].second.second; } } inline int my_getchar() { static char buf[1048576]; static int s = 1048576; static int e = 1048576; if (s == e && e == 1048576) { e = fread(buf, 1, 1048576, stdin); s = 0; } if (s == e) { return EOF; } return buf[s++]; } inline void rd(int &x) { int k; int m = 0; x = 0; for (;;) { k = my_getchar(); if (k == '-') { m = 1; break; } if ('0' <= k && k <= '9') { x = k - '0'; break; } } for (;;) { k = my_getchar(); if (k < '0' || k > '9') { break; } x = x * 10 + k - '0'; } if (m) { x = -x; } } inline void rd(long long &x) { int k; int m = 0; x = 0; for (;;) { k = my_getchar(); if (k == '-') { m = 1; break; } if ('0' <= k && k <= '9') { x = k - '0'; break; } } for (;;) { k = my_getchar(); if (k < '0' || k > '9') { break; } x = x * 10 + k - '0'; } if (m) { x = -x; } } inline int rd_int(void) { int x; rd(x); return x; } struct MY_WRITER { char buf[1048576]; int s; int e; MY_WRITER() { s = 0; e = 1048576; } ~MY_WRITER() { if (s) { fwrite(buf, 1, s, stdout); } } }; MY_WRITER MY_WRITER_VAR; void my_putchar(int a) { if (MY_WRITER_VAR.s == MY_WRITER_VAR.e) { fwrite(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout); MY_WRITER_VAR.s = 0; } MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a; } inline void wt_L(char a) { my_putchar(a); } inline void wt_L(int x) { int s = 0; int m = 0; char f[10]; if (x < 0) { m = 1; x = -x; } while (x) { f[s++] = x % 10; x /= 10; } if (!s) { f[s++] = 0; } if (m) { my_putchar('-'); } while (s--) { my_putchar(f[s] + '0'); } } int N; int K; int ind[75]; long long A[75]; long long B[75]; long long dp[76][76]; int bk[76][76]; int use[75]; int fg[75]; int ress; int res[150]; int main() { int Lj4PdHRW; wmem = memarr; int KL2GvlyY = rd_int(); for (Lj4PdHRW = (0); Lj4PdHRW < (KL2GvlyY); Lj4PdHRW++) { int i; int j; rd(N); rd(K); { int cTE1_r3A; for (cTE1_r3A = (0); cTE1_r3A < (N); cTE1_r3A++) { rd(A[cTE1_r3A]); rd(B[cTE1_r3A]); } } for (i = (0); i < (N); i++) { ind[i] = i + 1; } sortA_L(N, B, A, ind); for (i = (0); i < (N); i++) { fg[i] = 0; } for (i = (0); i < (N + 1); i++) { for (j = (0); j < (N + 1); j++) { dp[i][j] = -4611686016279904256LL; } } dp[0][0] = 0; for (i = (0); i < (N); i++) { for (j = (0); j < (i + 1); j++) { if (dp[i][j] > -4611686016279904256LL) { if (dp[i + 1][j] < dp[i][j] + B[i] * (K - 1)) { dp[i + 1][j] = dp[i][j] + B[i] * (K - 1); bk[i + 1][j] = j; } if (dp[i + 1][j + 1] < dp[i][j] + A[i] + B[i] * j) { dp[i + 1][j + 1] = dp[i][j] + A[i] + B[i] * j; bk[i + 1][j + 1] = j; } } } } j = K; for (i = (N)-1; i >= (0); i--) { if (bk[i + 1][j] == j) { continue; } j--; use[j] = i; fg[i] = 1; } ress = 0; for (i = (0); i < (K - 1); i++) { res[ress++] = ind[use[i]]; } for (i = (0); i < (N); i++) { if (!fg[i]) { res[ress++] = ind[i]; res[ress++] = -ind[i]; } } res[ress++] = ind[use[K - 1]]; wt_L(ress); wt_L('\n'); { int BUotOFBp; if (ress == 0) { wt_L('\n'); } else { for (BUotOFBp = (0); BUotOFBp < (ress - 1); BUotOFBp++) { wt_L(res[BUotOFBp]); wt_L(' '); } wt_L(res[BUotOFBp]); wt_L('\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 = 200; const int M = 20000; const int oo = 0X10101010; int mym; int S, T; struct adj { int ano, cap, wei, nex; } adj[M]; int nod[N]; void insert(int x, int y, int c, int w) { adj[mym].ano = y; adj[mym].cap = c; adj[mym].wei = w; adj[mym].nex = nod[x]; nod[x] = mym++; adj[mym].ano = x; adj[mym].cap = 0; adj[mym].wei = -w; adj[mym].nex = nod[y]; nod[y] = mym++; } bool use[N]; int dis[N], pre[N], lx[N]; int bfs() { int p = 0, q = 0, x, e; memset(use, 0, sizeof(use)); memset(dis, 0X10, sizeof(dis)); lx[q++] = S; use[S] = true; dis[S] = 0; pre[S] = -1; while (p != q) { x = lx[p++]; p %= N; for (e = nod[x]; e != -1; e = adj[e].nex) { if (adj[e].cap > 0) if (adj[e].wei + dis[x] < dis[adj[e].ano]) { dis[adj[e].ano] = adj[e].wei + dis[x]; pre[adj[e].ano] = e; if (!use[adj[e].ano]) { use[adj[e].ano] = true; lx[q++] = adj[e].ano; q %= N; } } } use[x] = false; } return dis[T]; } int mincostmaxflow() { int ans = 0, hav, x; while (bfs() != oo) { x = T; hav = oo; while (x != S) { if (adj[pre[x]].cap < hav) hav = adj[pre[x]].cap; x = adj[pre[x] ^ 1].ano; } x = T; while (x != S) { ans += hav * adj[pre[x]].wei; adj[pre[x]].cap -= hav; adj[pre[x] ^ 1].cap += hav; x = adj[pre[x] ^ 1].ano; } } return ans; } vector<int> ans[80]; int tt() { int i = 0, j = 0, n = 0, k = 0, e = 0; int a[80] = {0}, b[80] = {0}; memset(nod, 0XFF, sizeof(nod)); mym = 0; scanf("%d%d", &n, &k); for (i = 1; i <= n; i++) { scanf("%d%d", a + i, b + i); } S = 0; T = n + k + 1 + 1; for (i = 0; i <= k + 1; i++) ans[i].clear(); for (i = 1; i <= n; i++) { insert(S, i, 1, 0); for (j = 1; j <= k; j++) { insert(i, n + j, 1, -(a[i] + b[i] * (j - 1))); } insert(i, n + k + 1, 1, -(b[i] * (k - 1))); } for (i = 1; i <= k; i++) { insert(n + i, T, 1, 0); } insert(n + k + 1, T, n - k, 0); mincostmaxflow(); for (i = 1; i <= n; i++) { for (e = nod[i]; e != -1; e = adj[e].nex) { if (adj[e].cap == 0) { ans[adj[e].ano - n].push_back(i); } } } printf("%d\n", k + 2 * (n - k)); for (i = 1; i <= k - 1; i++) { printf("%d ", ans[i][0]); } for (i = 0; i < ans[k + 1].size(); i++) { printf("%d ", ans[k + 1][i]); printf("%d ", -ans[k + 1][i]); } printf("%d\n", ans[k][0]); return 0; } int main() { int o = 0; scanf("%d", &o); while (o--) { tt(); } 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 MX = 80; int n, k; array<int, 3> m[MX]; long long dp[MX][MX]; int dpb[MX][MX]; void solve() { cin >> n >> k; for (int i = (0); i < (n); ++i) { cin >> m[i][1] >> m[i][0]; m[i][2] = i + 1; } sort(m, m + n); for (int i = (0); i < (n + 1); ++i) { memset((dp[i]), -1, (k + 1) * sizeof(dp[i][0])); memset((dpb[i]), -1, (k + 1) * sizeof(dpb[i][0])); } dp[0][0] = 0; for (int i = (0); i < (n); ++i) for (int j = (0); j < (k + 1); ++j) { if (dp[i][j] == -1) continue; auto [b, a, _id] = m[i]; if (dp[i + 1][j] < dp[i][j] + (k - 1) * b) { dp[i + 1][j] = dp[i][j] + (k - 1) * b; dpb[i + 1][j] = 0; } if (j < k && dp[i + 1][j + 1] < dp[i][j] + a + j * b) { dp[i + 1][j + 1] = dp[i][j] + a + j * b; dpb[i + 1][j + 1] = 1; } } vector<int> mf = vector<int>(k); vector<int> rit = vector<int>(); rit.reserve(n - k); int cp = k; for (int i = (n)-1; i >= (0); --i) { auto [b, a, id] = m[i]; if (dpb[i + 1][cp]) mf[--cp] = id; else rit.push_back(id); } cout << 2 * n - k << '\n'; for (int i = (0); i < (k - 1); ++i) cout << mf[i] << ' '; for (int i = (0); i < (n - k); ++i) cout << rit[i] << ' ' << -rit[i] << ' '; cout << mf[k - 1] << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { solve(); } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MX = 80; const long long inf = 1e15; struct info { long long a, b, indx; bool operator<(const info& s) const { return b < s.b; } }; long long n, c; info ara[MX]; vector<long long> add, rem; long long dp[MX][MX], choose[MX][MX]; long long func(long long pos, long long lim) { if (pos >= n) return lim ? -inf : 0; long long& r = dp[pos][lim]; if (r != -1) return r; long long r1 = 0, r2 = 0; r1 = ara[pos].b * (c - 1) + func(pos + 1, lim); if (lim) r2 = ara[pos].a + ara[pos].b * (c - lim) + func(pos + 1, lim - 1); if (r1 < r2) choose[pos][lim] = 1; return r = max(r1, r2); } int main() { long long i, j, k; long long t; cin >> t; while (t--) { cin >> n >> c; for (i = 0; i < n; ++i) { info s; cin >> s.a >> s.b; s.indx = i + 1; ara[i] = s; } sort(ara, ara + n); memset(dp, -1, sizeof dp); memset(choose, 0, sizeof choose); func(0, c); long long pos = 0, lim = c; while (pos < n) { if (choose[pos][lim]) { add.push_back(ara[pos].indx); lim--; } else { rem.push_back(ara[pos].indx); } pos++; } cout << add.size() + 2 * rem.size() << endl; for (i = 0; i + 1 < add.size(); ++i) cout << add[i] << " "; for (i = 0; i < rem.size(); ++i) cout << rem[i] << " " << -rem[i] << " "; cout << add.back() << endl; add.clear(), rem.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
java
import java.util.*; import java.io.*; public class SummoningMinions { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); int t = Integer.parseInt(br.readLine()); Comparator<int[]> comp = (a, b) -> a[2] - b[2]; while (--t >= 0) { String[] line = br.readLine().split(" "); int n = Integer.parseInt(line[0]), k = Integer.parseInt(line[1]); int[][] ab = new int[n][3]; for (int i = 0; i < n; ++i) { line = br.readLine().split(" "); ab[i][0] = i + 1; ab[i][1] = Integer.parseInt(line[0]); ab[i][2] = Integer.parseInt(line[1]); } Arrays.sort(ab, comp); int[][] dp = new int[n+1][n+1]; for (int i = 1; i <= n; ++i) { dp[i][0] = dp[i-1][0] + ab[i-1][2] * (k - 1); for (int j = 1; j < i; ++j) { dp[i][j] = Math.max(dp[i-1][j-1] + ab[i-1][2] * (j - 1) + ab[i-1][1], dp[i-1][j] + ab[i-1][2] * (k - 1)); } dp[i][i] = dp[i-1][i-1] + ab[i-1][2] * (i - 1) + ab[i-1][1]; } int[] res = new int[n*2-k]; int j1 = k, j2 = k - 1; for (int i = n; i > 0; --i) { if (i > j1 && dp[i][j1] == dp[i-1][j1] + ab[i-1][2] * (k - 1)) { res[++j2] = ab[i-1][0]; } else { res[--j1] = ab[i-1][0]; } } bw.write(Integer.toString(res.length)); bw.newLine(); for (int i = 0; i < k - 1; ++i) { bw.write(Integer.toString(res[i])); bw.write(' '); } for (int i = k; i < n; ++i) { bw.write(Integer.toString(res[i])); bw.write(' '); bw.write('-'); bw.write(Integer.toString(res[i])); bw.write(' '); } bw.write(Integer.toString(res[k-1])); bw.newLine(); } br.close(); bw.close(); } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; constexpr static int MAXN = 80; int n, k; int a[MAXN], b[MAXN]; int dp[MAXN][MAXN]; bool added[MAXN][MAXN]; bool used[MAXN]; void solve() { cin >> n >> k; for (int i = 0; i < n; i++) cin >> a[i] >> b[i]; vector<pair<int, int>> bval(n); for (int i = 0; i < n; i++) bval[i] = {b[i], i}; sort(bval.begin(), bval.end()); for (int i = 0; i <= n; i++) for (int j = 0; j <= k; j++) dp[i][j] = -1e9, added[i][j] = false; dp[0][0] = 0; for (int i = 0; i < n; i++) { int l = bval[i].second; for (int j = 0; j <= k; j++) { dp[i + 1][j] = dp[i][j] + (k - 1) * b[l]; if (j == 0) continue; int cost = a[l] + (j - 1) * b[l]; if (dp[i + 1][j] < dp[i][j - 1] + cost) { dp[i + 1][j] = dp[i][j - 1] + cost; added[i + 1][j] = true; } } } for (int i = 0; i < n; i++) used[i] = false; vector<int> c; int j = k; for (int i = n; i > 0; i--) { if (added[i][j]) { c.push_back(bval[i - 1].second); used[c.back()] = true; j--; } } reverse(c.begin(), c.end()); vector<int> moves; for (int i = 0; i + 1 < c.size(); i++) moves.push_back(c[i] + 1); for (int i = 0; i < n; i++) if (!used[i]) moves.push_back(i + 1), moves.push_back(-(i + 1)); moves.push_back(c.back() + 1); cout << moves.size() << endl; for (int i = 0; i < moves.size(); i++) cout << moves[i] << (i + 1 == moves.size() ? '\n' : ' '); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int T; cin >> T; while (T--) { solve(); } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAX = 80; int f[MAX][MAX]; bool take[MAX][MAX]; void run_case() { for (int i = 0; i < MAX; i++) { for (int j = 0; j < MAX; j++) { f[i][j] = -1000000000; take[i][j] = false; } } int n, k; cin >> n >> k; struct item { int first, second, ind; }; vector<item> a(n); for (int i = 0; i < n; i++) { cin >> a[i].first >> a[i].second; a[i].ind = i; } sort(a.begin(), a.end(), [&](const item& w, const item& v) { return w.second < v.second; }); f[n][k] = 0; for (int i = n - 1; i >= 0; i--) { for (int j = 0; j <= k; j++) { f[i][j] = f[i + 1][j] + a[i].second * (k - 1); int cost = f[i + 1][j + 1] + a[i].first + a[i].second * j; if (j < k && cost > f[i][j]) { f[i][j] = cost; take[i][j] = true; } } } cout << k + 2 * (n - k) << '\n'; int j = 0; vector<int> taken, shrek; for (int i = 0; i < n; i++) { if (take[i][j]) { taken.push_back(i); j++; } else { shrek.push_back(i); } } assert(taken.size() == k); for (int i = 0; i + 1 < k; i++) cout << a[taken[i]].ind + 1 << ' '; for (int i = 0; i < n - k; i++) cout << a[shrek[i]].ind + 1 << ' ' << -a[shrek[i]].ind - 1 << ' '; cout << a[taken[k - 1]].ind + 1 << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) run_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 int inf = 0x3f3f3f3f; struct Edge { int to, val, next, cost; } edge[25086]; int s, t; int cnt = -1; int head[25086]; queue<int> q; int d[25086]; int cur[25086]; int inq[25086]; int vis[25086]; int costsum; void addEdge(int u, int v, int w, int f) { cnt++; edge[cnt].to = v; edge[cnt].val = w; edge[cnt].next = head[u]; edge[cnt].cost = f; head[u] = cnt; cnt++; edge[cnt].to = u; edge[cnt].val = 0; edge[cnt].next = head[v]; edge[cnt].cost = -f; head[v] = cnt; } bool bfs() { memset(d, 0x3f, sizeof(d)); memset(vis, 0, sizeof(vis)); d[s] = 0; q.push(s); inq[s] = 1; while (!q.empty()) { int u = q.front(); q.pop(); inq[u] = 0; for (int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to; cur[v] = head[v]; int f = edge[i].cost; if (d[v] > d[u] + f && edge[i].val > 0) { d[v] = d[u] + f; if (!inq[v]) { q.push(v); inq[v] = 1; } } } } return d[t] != inf; } int dfs(int u, int flow) { vis[u] = 1; int nowflow = 0; if (u == t) return flow; for (int i = cur[u]; i != -1; i = edge[i].next) { cur[u] = i; int v = edge[i].to; int w = edge[i].val; int f = edge[i].cost; if (d[v] == d[u] + f && w > 0 && !vis[v]) { int k = dfs(v, min(flow - nowflow, w)); if (k > 0) { edge[i].val -= k; edge[i ^ 1].val += k; nowflow += k; costsum += f * k; if (nowflow == flow) break; } } } vis[u] = 0; if (!nowflow) d[u] = inf; return nowflow; } int dinic() { int ans = 0; while (bfs()) { ans += dfs(s, inf); } return ans; } int T; int n, k, a, b; int opt[25086]; int main() { scanf("%d", &T); while (T--) { memset(head, -1, sizeof(head)), cnt = -1; scanf("%d%d", &n, &k); s = 0, t = 2 * n + 1, costsum = 0; for (int i = 1; i <= n; i++) { scanf("%d%d", &a, &b); addEdge(s, i, 1, 0), addEdge(i + n, t, 1, 0); for (int j = 1; j <= n; j++) { if (j < k) addEdge(i, j + n, 1, -(a + (j - 1) * b)); else if (j < n) addEdge(i, j + n, 1, -(k - 1) * b); else addEdge(i, j + n, 1, -(a + (k - 1) * b)); } } dinic(); for (int i = 1; i <= n; i++) { for (int j = head[i]; j != -1; j = edge[j].next) { if (!edge[j].val) { opt[edge[j].to - n] = i; break; } } } printf("%d\n", k - 1 + (n - k) * 2 + 1); for (int i = 1; i <= n; i++) { if (i < k || i == n) printf("%d ", opt[i]); else printf("%d %d ", opt[i], -opt[i]); } puts(""); } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC target("avx2") #pragma GCC optimization("O3") #pragma GCC optimization("unroll-loops") using namespace std; template <typename T> void DBG(const char* name, T&& H) { cerr << name << " = " << H << ')' << '\n'; } template <typename T, typename... Args> void DBG(const char* names, T&& H, Args&&... args) { const char* NEXT = strchr(names + 1, ','); cerr.write(names, NEXT - names) << " = " << H << " |"; DBG(NEXT + 1, args...); } using ll = long long; using ld = long double; const ll mod = 1e9 + 7; const ld PI = acos(-1.0); const ll maxN = 1e5 + 1; struct Hungarian { using T = int; int N; vector<vector<T>> cost; vector<int> Lmate, Rmate; T minCost = 0; Hungarian(int n = 1e2) { N = n; cost.resize(n, vector<T>(n)); Lmate.resize(n, -1); Rmate.resize(n, -1); } void compute() { vector<T> a(N), b(N); for (int i = 0; i < N; i++) { a[i] = cost[i][0]; for (int j = 1; j < N; j++) { a[i] = min(a[i], cost[i][j]); } } for (int i = 0; i < N; i++) { b[i] = cost[0][i] - a[0]; for (int j = 1; j < N; j++) { b[i] = min(b[i], cost[j][i] - a[j]); } } int matched = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (Rmate[j] != -1) continue; if (abs(cost[i][j] - a[i] - b[j]) == 0) { Lmate[i] = j; Rmate[j] = i; matched++; break; } } } vector<T> dist(N); vector<int> dad(N), seen(N); while (matched < N) { int s = 0; while (Lmate[s] != -1) s++; fill(dad.begin(), dad.end(), -1); fill(seen.begin(), seen.end(), 0); for (int k = 0; k < N; k++) { dist[k] = cost[s][k] - a[s] - b[k]; } int j = 0; while (true) { j = -1; for (int k = 0; k < N; k++) { if (seen[k]) continue; if ((j == -1) || (dist[k] < dist[j])) j = k; } seen[j] = 1; if (Rmate[j] == -1) break; int i = Rmate[j]; for (int k = 0; k < N; k++) { if (seen[k]) continue; int newDist = dist[j] + cost[i][k] - a[i] - b[k]; if (dist[k] > newDist) { dist[k] = newDist; dad[k] = j; } } } for (int k = 0; k < N; k++) { if ((k == j) || (!seen[k])) continue; int i = Rmate[k]; a[i] -= dist[k] - dist[j]; b[k] += dist[k] - dist[j]; } a[s] += dist[j]; while (dad[j] >= 0) { int d = dad[j]; Rmate[j] = Rmate[d]; Lmate[Rmate[j]] = j; j = d; } Rmate[j] = s; Lmate[s] = j; matched++; } for (int i = 0; i < N; i++) { minCost += cost[i][Lmate[i]]; } } }; void Solve() { int n, k; cin >> n >> k; Hungarian order(n); vector<int> a(n), b(n); for (int i = 0; i < n; i++) { cin >> a[i] >> b[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j < k - 1; j++) { order.cost[i][j] = -(a[i] + j * b[i]); } for (int j = k - 1; j < n - 1; j++) { order.cost[i][j] = -((k - 1) * b[i]); } order.cost[i][n - 1] = -(a[i] + (k - 1) * b[i]); } order.compute(); cout << k + 2 * (n - k) << '\n'; for (int i = 0; i < k - 1; i++) { cout << order.Rmate[i] + 1 << " "; } for (int i = k - 1; i < n - 1; i++) { cout << order.Rmate[i] + 1 << " " << -(order.Rmate[i] + 1) << " "; } cout << order.Rmate[n - 1] + 1 << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int tt = 1; cin >> tt; while (tt--) { Solve(); } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 1e8; int main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); int tcase; cin >> tcase; while (tcase--) { int n, k; cin >> n >> k; vector<int> a(n + 1), b(n + 1), id(n + 1); for (int i = 1; i <= n; i++) cin >> a[i] >> b[i], id[i] = i; sort(id.begin() + 1, id.begin() + n + 1, [&](int u, int v) { return b[u] < b[v]; }); vector<vector<int>> dp(n + 1, vector<int>(k + 1, -INF)), prv(n + 1, vector<int>(k + 1, 0)); dp[0][0] = 0; for (int i = 1; i <= n; i++) { for (int j = 0; j <= k; j++) { dp[i][j] = dp[i - 1][j]; if (j) { if (dp[i][j] < dp[i - 1][j - 1] + a[id[i]] - b[id[i]] * (k - j)) { dp[i][j] = dp[i - 1][j - 1] + a[id[i]] - b[id[i]] * (k - j); prv[i][j] = 1; } } } } vector<int> chk(n + 1, 0); int cur_i = n, cur_j = k; while (cur_i) { if (prv[cur_i][cur_j]) chk[cur_i] = 1, cur_i--, cur_j--; else cur_i--; } vector<int> ans1, ans2; for (int i = 1; i <= n; i++) if (chk[i]) ans1.push_back(id[i]); for (int i = 1; i <= n; i++) if (!chk[i]) ans2.push_back(id[i]); vector<int> rlt; for (int i = 0; i < ans1.size() - 1; i++) rlt.push_back(ans1[i]); for (auto it : ans2) rlt.push_back(it), rlt.push_back(-it); rlt.push_back(ans1[ans1.size() - 1]); cout << rlt.size() << "\n"; for (int i = 0; i < rlt.size(); i++) cout << rlt[i] << " \n"[i == rlt.size() - 1]; } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const long long INF = (long long)1000000007 * 1000000007; const long double eps = 1e-8; const long double pi = acos(-1.0); int dx[4] = {1, -1, 0, 0}; int dy[4] = {0, 0, 1, -1}; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } template <class Cap, class Cost> struct MinCostFlow { public: MinCostFlow() {} MinCostFlow(int n) : _n(n), g(n) {} int add_edge(int from, int to, Cap cap, Cost cost) { assert(0 <= from && from < _n); assert(0 <= to && to < _n); int m = int(pos.size()); pos.push_back({from, int(g[from].size())}); g[from].push_back(_edge{to, int(g[to].size()), cap, cost}); g[to].push_back(_edge{from, int(g[from].size()) - 1, 0, -cost}); return m; } struct edge { int from, to; Cap cap, flow; Cost cost; }; edge get_edge(int i) { int m = int(pos.size()); assert(0 <= i && i < m); auto _e = g[pos[i].first][pos[i].second]; auto _re = g[_e.to][_e.rev]; return edge{ pos[i].first, _e.to, _e.cap + _re.cap, _re.cap, _e.cost, }; } std::vector<edge> edges() { int m = int(pos.size()); std::vector<edge> result(m); for (int i = 0; i < m; i++) { result[i] = get_edge(i); } return result; } std::pair<Cap, Cost> flow(int s, int t) { return flow(s, t, std::numeric_limits<Cap>::max()); } std::pair<Cap, Cost> flow(int s, int t, Cap flow_limit) { return slope(s, t, flow_limit).back(); } std::vector<std::pair<Cap, Cost>> slope(int s, int t) { return slope(s, t, std::numeric_limits<Cap>::max()); } std::vector<std::pair<Cap, Cost>> slope(int s, int t, Cap flow_limit) { assert(0 <= s && s < _n); assert(0 <= t && t < _n); assert(s != t); std::vector<Cost> dual(_n, 0), dist(_n); std::vector<int> pv(_n), pe(_n); std::vector<bool> vis(_n); auto dual_ref = [&]() { std::fill(dist.begin(), dist.end(), std::numeric_limits<Cost>::max()); std::fill(pv.begin(), pv.end(), -1); std::fill(pe.begin(), pe.end(), -1); std::fill(vis.begin(), vis.end(), false); struct Q { Cost key; int to; bool operator<(Q r) const { return key > r.key; } }; std::priority_queue<Q> que; dist[s] = 0; que.push(Q{0, s}); while (!que.empty()) { int v = que.top().to; que.pop(); if (vis[v]) continue; vis[v] = true; if (v == t) break; for (int i = 0; i < int(g[v].size()); i++) { auto e = g[v][i]; if (vis[e.to] || !e.cap) continue; Cost cost = e.cost - dual[e.to] + dual[v]; if (dist[e.to] - dist[v] > cost) { dist[e.to] = dist[v] + cost; pv[e.to] = v; pe[e.to] = i; que.push(Q{dist[e.to], e.to}); } } } if (!vis[t]) { return false; } for (int v = 0; v < _n; v++) { if (!vis[v]) continue; dual[v] -= dist[t] - dist[v]; } return true; }; Cap flow = 0; Cost cost = 0, prev_cost = -1; std::vector<std::pair<Cap, Cost>> result; result.push_back({flow, cost}); while (flow < flow_limit) { if (!dual_ref()) break; Cap c = flow_limit - flow; for (int v = t; v != s; v = pv[v]) { c = std::min(c, g[pv[v]][pe[v]].cap); } for (int v = t; v != s; v = pv[v]) { auto &e = g[pv[v]][pe[v]]; e.cap -= c; g[v][e.rev].cap += c; } Cost d = -dual[s]; flow += c; cost += c * d; if (prev_cost == d) { result.pop_back(); } result.push_back({flow, cost}); prev_cost = cost; } return result; } private: int _n; struct _edge { int to, rev; Cap cap; Cost cost; }; std::vector<std::pair<int, int>> pos; std::vector<std::vector<_edge>> g; }; using edge = MinCostFlow<int, int>::edge; const int inf = 10000000; int n, k; vector<int> a(n), b(n); void solve() { MinCostFlow<int, int> mcf(n + k + 5); int ans = inf * n; int S = n + k + 1; for (int i = 0; i < n; i++) { mcf.add_edge(i, n + k, 1, inf - (k - 1) * b[i]); for (int j = 0; j < k; j++) { mcf.add_edge(i, n + j, 1, inf - a[i] - j * b[i]); } } for (int i = 0; i < n; i++) { mcf.add_edge(S, i, 1, 0); } for (int j = 0; j < k; j++) { mcf.add_edge(n + j, S + 1, 1, 0); } mcf.add_edge(n + k, S + 1, n - k, 0); pair<int, int> p = mcf.flow(S, S + 1); auto es = mcf.edges(); vector<int> v(k); vector<bool> used(n); for (auto e : es) { if (e.to - n < 0 || e.to - n >= k) continue; if (e.flow == 1) { v[e.to - n] = e.from; used[e.from] = true; } } printf("%d\n", 2 * n - k); for (int i = 0; i < k - 1; i++) { printf("%d ", v[i] + 1); } for (int i = 0; i < n; i++) { if (!used[i]) { printf("%d %d ", i + 1, -i - 1); } } printf("%d\n", v[k - 1] + 1); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(50); int t; scanf("%d", &t); for (int i = 0; i < t; i++) { scanf("%d%d", &n, &k); a.resize(n); b.resize(n); for (int i = 0; i < n; i++) scanf("%d%d", &a[i], &b[i]); solve(); } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct ANS { long long sss, i; ANS *to; } dp[80][80]; ANS max_get(ANS a, ANS b, ANS *aa, ANS *bb, long long i) { ANS s; if (a.sss > b.sss) { s.sss = a.sss; s.i = 0; s.to = aa; } else { s.sss = b.sss; s.i = i; s.to = bb; } return s; } struct node { long long a, b, i; bool operator<(const node x) const { return b < x.b; } } mp[80]; long long n, m, T; bool vis[80]; void print(ANS *x) { if (x == NULL) return; print(x->to); if (x->i) { printf("%lld ", mp[x->i].i); vis[x->i] = 1; } } int main() { scanf("%lld", &T); while (T--) { memset(vis, 0, sizeof(vis)); scanf("%lld%lld", &n, &m); for (long long i = 1; i <= n; i++) { scanf("%lld%lld", &mp[i].a, &mp[i].b); mp[i].i = i; } sort(mp + 1, mp + n + 1); if (n <= m) { printf("%lld\n", n); for (long long i = 1; i <= n; i++) printf("%lld ", mp[i].i); printf("\n"); continue; } for (long long i = 0; i <= n; i++) for (long long j = 0; j <= m; j++) { dp[i][j].sss = -1e18; dp[i][j].i = 0; dp[i][j].to = NULL; } dp[0][0].sss = 0; for (long long i = 1; i <= n; i++) { dp[i][0].i = 0; dp[i][0].to = &dp[i - 1][0]; dp[i][0].sss = dp[i - 1][0].sss + mp[i].b * (m - 1); for (long long j = 1; j <= m; j++) { ANS xa = dp[i - 1][j], xb = dp[i - 1][j - 1]; xa.sss += mp[i].b * (m - 1); xb.sss += mp[i].b * (j - 1) + mp[i].a; dp[i][j] = max_get(xa, xb, &dp[i - 1][j], &dp[i - 1][j - 1], i); } } printf("%lld\n", 2 * n - m); ANS re = dp[n][m]; while (!re.i) re = *(re.to); print(re.to); vis[re.i] = 1; for (long long i = 1; i <= n; i++) if (!vis[i]) printf("%lld -%lld ", mp[i].i, mp[i].i); printf("%lld\n", mp[re.i].i); } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const double eps = 1e-9; const int inf = 2000000000; const long long infLL = 9000000000000000000; template <typename first, typename second> ostream& operator<<(ostream& os, const pair<first, second>& p) { return os << "(" << p.first << ", " << p.second << ")"; } template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) { os << "{"; for (auto it = v.begin(); it != v.end(); ++it) { if (it != v.begin()) os << ", "; os << *it; } return os << "}"; } template <typename T> ostream& operator<<(ostream& os, const set<T>& v) { os << "["; for (auto it = v.begin(); it != v.end(); ++it) { if (it != v.begin()) os << ","; os << *it; } return os << "]"; } template <typename T> ostream& operator<<(ostream& os, const multiset<T>& v) { os << "["; for (auto it = v.begin(); it != v.end(); ++it) { if (it != v.begin()) os << ", "; os << *it; } return os << "]"; } template <typename first, typename second> ostream& operator<<(ostream& os, const map<first, second>& v) { os << "["; for (auto it = v.begin(); it != v.end(); ++it) { if (it != v.begin()) os << ", "; os << it->first << " = " << it->second; } return os << "]"; } void faltu() { cerr << '\n'; } template <typename T> void faltu(T a[], int n) { for (int i = 0; i < n; ++i) cerr << a[i] << ' '; cerr << '\n'; } template <typename T, typename... hello> void faltu(T arg, const hello&... rest) { cerr << arg << ' '; faltu(rest...); } int n, k; vector<pair<pair<int, int>, int>> vec; int dp[80][80]; vector<int> store; int recur(int pos, int taken) { if (pos == n) { if (taken == k) return 0; return -inf; } if (dp[pos][taken] != -1) return dp[pos][taken]; int ret = 0; if (taken < k) ret = vec[pos].first.second + vec[pos].first.first * taken + recur(pos + 1, taken + 1); ret = max(ret, vec[pos].first.first * (k - 1) + recur(pos + 1, taken)); dp[pos][taken] = ret; return ret; } void print(int pos, int taken) { if (pos == n) return; if (taken < k && recur(pos, taken) == vec[pos].first.second + vec[pos].first.first * taken + recur(pos + 1, taken + 1)) { store.push_back(vec[pos].second + 1); print(pos + 1, taken + 1); return; } if (recur(pos, taken) == vec[pos].first.first * (k - 1) + recur(pos + 1, taken)) { store.push_back(-vec[pos].second - 1); print(pos + 1, taken); return; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int T; cin >> T; while (T--) { memset(dp, -1, sizeof(dp)); vec.clear(); store.clear(); cin >> n >> k; vec.resize(n); for (int i = 0; i < n; ++i) cin >> vec[i].first.second >> vec[i].first.first, vec[i].second = i; sort(vec.begin(), vec.end()); cout << 2 * n - k << '\n'; print(0, 0); for (int i = 0, j = 0; i < n && j < k - 1; ++i) { if (store[i] > 0) { ++j; cout << store[i] << " "; } } for (int i = 0; i < n; ++i) { if (store[i] < 0) { cout << -store[i] << " " << store[i] << " "; } } for (int i = n - 1; i >= 0; --i) { if (store[i] > 0) { cout << store[i] << '\n'; break; } } } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = (1e2) + 10; const int M = (1e5) + 10; const int INF = (1e9) + 10; const int maxn = 5e6 + 100; int dp[N][N]; int p[N][N]; struct trr { int first; int second; int poz; }; bool comp(trr a, trr b) { return a.first < b.first; } void solve() { int n, k; cin >> n >> k; vector<trr> arr(n); vector<int> ans[2]; for (int i = int(0); i < int(n); i++) { int a, b; cin >> a >> b; arr[i] = {b, a, i + 1}; } sort(arr.begin(), arr.end(), comp); memset(dp, -1, sizeof(dp)); dp[0][0] = 0; for (int i = int(0); i < int(n); i++) { for (int j = int(0); j < int(k + 1); j++) if (dp[i][j] >= 0) { if (dp[i + 1][j + 1] < dp[i][j] + arr[i].first * j + arr[i].second) { dp[i + 1][j + 1] = dp[i][j] + arr[i].first * j + arr[i].second; p[i + 1][j + 1] = 1; } if (dp[i + 1][j] < dp[i][j] + arr[i].first * (k - 1)) { dp[i + 1][j] = dp[i][j] + arr[i].first * (k - 1); p[i + 1][j] = 0; } } } int pos = k; for (int i = n; i >= 1; i--) { ans[p[i][pos]].push_back(arr[i - 1].poz); pos -= p[i][pos]; } reverse(ans[0].begin(), ans[0].end()); reverse(ans[1].begin(), ans[1].end()); cout << ans[0].size() * 2 + ans[1].size() << endl; for (int i = int(0); i < int(ans[1].size() - 1); i++) cout << ans[1][i] << ' '; for (int i = int(0); i < int(ans[0].size()); i++) cout << ans[0][i] << ' ' << -ans[0][i] << ' '; cout << ans[1][ans[1].size() - 1]; cout << endl; } int main() { int t = 1; cin >> t; while (t) { solve(); t--; } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; } template <typename A, typename B> ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; } void dbg_out() { cerr << endl; } template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); } template <typename T> void output_vector(const vector<T> &v, bool add_one = false, int start = -1, int end = -1) { if (start < 0) start = 0; if (end < 0) end = int(v.size()); for (int i = start; i < end; i++) cout << v[i] + (add_one ? 1 : 0) << (i < end - 1 ? ' ' : '\n'); } const int64_t INF64 = int64_t(2e18) + 5; vector<int> assignment; template <typename T> int64_t hungarian(vector<vector<T>> costs) { int n = int(costs.size()); int m = costs.empty() ? 0 : int(costs[0].size()); if (n > m) { vector<vector<T>> new_costs(m, vector<T>(n)); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) new_costs[j][i] = costs[i][j]; swap(costs, new_costs); swap(n, m); } vector<int64_t> u(n + 1), v(m + 1); vector<int> p(m + 1), way(m + 1); for (int i = 1; i <= n; i++) { vector<int64_t> min_v(m + 1, INF64); vector<bool> used(m + 1, false); p[0] = i; int j0 = 0; do { used[j0] = true; int i0 = p[j0], j1 = 0; int64_t delta = INF64; for (int j = 1; j <= m; j++) if (!used[j]) { int64_t cur = costs[i0 - 1][j - 1] - u[i0] - v[j]; if (cur < min_v[j]) { min_v[j] = cur; way[j] = j0; } if (min_v[j] < delta) { delta = min_v[j]; j1 = j; } } for (int j = 0; j <= m; j++) if (used[j]) { u[p[j]] += delta; v[j] -= delta; } else { min_v[j] -= delta; } j0 = j1; } while (p[j0] != 0); do { int j1 = way[j0]; p[j0] = p[j1]; j0 = j1; } while (j0 != 0); } assignment = p; return -v[0]; } void run_case() { 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<int>> costs(N, vector<int>(N, 0)); for (int i = 0; i < N; i++) for (int position = 0; position < N; position++) if (position < K) costs[i][position] = -(A[i] + position * B[i]); else costs[i][position] = -((K - 1) * B[i]); int64_t score = -hungarian(costs); ; vector<int> solution; for (int k = 1; k < K; k++) solution.push_back(assignment[k]); for (int k = K + 1; k <= N; k++) { solution.push_back(assignment[k]); solution.push_back(-assignment[k]); } solution.push_back(assignment[K]); assert(int(solution.size()) == 2 * N - K); cout << solution.size() << '\n'; output_vector(solution); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int tests; cin >> tests; while (tests-- > 0) run_case(); }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\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 NMAX = 200001; const long long INF = (1 << 30); const long long MOD = 1000000007; const long long BLOCK = 101; const long long nr_of_bits = 18; int dp[76][76]; struct ura { ; int first, second, third; } v[76]; bool cmp(ura a, ura b) { return a.second < b.second; } vector<int> sol; vector<int> sel; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) { int n, k, i, j; cin >> n >> k; sol.clear(); sel.clear(); for (i = 0; i <= n; i++) { for (j = 0; j <= k; j++) dp[i][j] = 0; } for (i = 1; i <= n; i++) { cin >> v[i].first >> v[i].second; v[i].third = i; } sort(v + 1, v + n + 1, cmp); for (i = 1; i <= n; i++) { for (j = 0; j <= min(i, k); j++) { if (i - 1 >= j) dp[i][j] = max(dp[i][j], dp[i - 1][j] + (k - 1) * v[i].second); if (j != 0) { dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + (j - 1) * v[i].second + v[i].first); } } } i = n, j = k; while (i != 0) { if (i - 1 >= j && dp[i][j] == dp[i - 1][j] + (k - 1) * v[i].second) { sol.push_back(v[i].third); sol.push_back(-v[i].third); } else if (j != 0) { if (dp[i][j] == dp[i - 1][j - 1] + (j - 1) * v[i].second + v[i].first) { sel.push_back(v[i].third); j--; } } i--; } reverse(sel.begin(), sel.end()); cout << sol.size() + sel.size() << "\n"; for (int i = 0; i < sel.size() - 1; i++) { cout << sel[i] << " "; } for (auto x : sol) { cout << x << " "; } cout << sel.back() << "\n"; } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct Edge { long long to, dis, next, cost; } edge[240050]; long long num = -1; bool vis[100010]; long long mincost; long long pre[100010], head[100010], cost[100010], last[100010], flow[100010], 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]; memset(edge, 0, sizeof edge); memset(pre, 0, sizeof pre); memset(cost, 0, sizeof cost); memset(flow, 0, sizeof flow); memset(last, 0, sizeof last); 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
cpp
#include <bits/stdc++.h> const int N = 77; int e, n, k, a[N], b[N], id[N], s[N], p[N][N], f[N][N]; int cmp(int x, int y) { return b[x] < b[y]; } int main() { for (scanf("%d", &e); e; e--) { memset(f, -1, sizeof f); memset(p, 0, sizeof p); memset(s, 0, sizeof s); scanf("%d%d", &n, &k); f[0][0] = 0; int c = 0; for (int i = 1; i <= n; i++) scanf("%d%d", a + i, b + i), id[i] = i; std::sort(id + 1, id + 1 + n, cmp); for (int i = 1; i <= n; i++) for (int j = 0; j <= k && j <= i; j++) { if (f[i - 1][j] != -1) f[i][j] = f[i - 1][j] + b[id[i]] * (k - 1), p[i][j] = 0; if (j > 0 && f[i - 1][j - 1] != -1) { int t = f[i - 1][j - 1] + a[id[i]] + b[id[i]] * (j - 1); if (t > f[i][j]) f[i][j] = t, p[i][j] = 1; } } for (int i = n, j = k; i > 0; i--) if (p[i][j]) s[id[i]] = 1, j--; printf("%d\n", k + (n - k) * 2); for (int i = 1; i <= n; i++) if (s[id[i]]) { c++; if (c == k) { c = id[i]; break; } printf("%d ", id[i]); } for (int i = 1; i <= n; i++) if (!s[i]) printf("%d %d ", i, -i); printf("%d\n", c); } 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 = 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", (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; struct flow { static const int N = 200005, INF = 1e9; int n, s, t; struct edge { int v, w, f, c, nxt; } e[N]; int head[N], cur[N], cnt; void addedge(int u, int v, int w, int f, int c) { cnt++; e[cnt].v = v, e[cnt].w = w, e[cnt].f = f, e[cnt].c = c; e[cnt].nxt = head[u]; head[u] = cnt; } void add(int u, int v, int w, int c = 0) { addedge(u, v, w, w, c); addedge(v, u, w, 0, -c); } void init() { cnt = 0; for (int i = 1; i <= n; i++) head[i] = 0; } int dep[N], q[N]; int bfs() { for (int i = 1; i <= n; i++) dep[i] = -1, cur[i] = head[i]; dep[s] = 0; int l = 0, r = 1; q[0] = s; while (l < r) { int u = q[l++]; for (int i = head[u]; i; i = e[i].nxt) { if (dep[e[i].v] == -1 && e[i].f > 0) { dep[e[i].v] = dep[u] + 1; q[r++] = e[i].v; } } } return dep[t] != -1; } int dfs(int u, int lim) { if (u == t || lim == 0) return lim; int flow = 0, f; for (int i = cur[u]; i; i = e[i].nxt) { cur[u] = i; if (dep[e[i].v] == dep[u] + 1 && (f = dfs(e[i].v, min(lim, e[i].f)))) { flow += f; lim -= f; e[i].f -= f; e[(i - 1 ^ 1) + 1].f += f; if (!lim) break; } } return flow; } int max_flow() { int mf = 0; while (bfs()) mf += dfs(s, INF); return mf; } int h[N], dis[N], pre[N], pu[N]; priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; int min_cost(int f) { int res = 0; for (int i = 1; i <= cnt; i += 2) e[i].f = e[i].w; for (int i = 2; i <= cnt; i += 2) e[i].f = 0; while (f > 0) { for (int i = 1; i <= n; i++) dis[i] = INF; dis[s] = 0; pq.emplace(0, s); while (!pq.empty()) { auto p = pq.top(); pq.pop(); int u = p.second; if (dis[u] < p.first) continue; for (int i = head[u]; i; i = e[i].nxt) { int v = e[i].v, c = e[i].c, f = e[i].f; if (f > 0 && dis[v] > dis[u] + c + h[u] - h[v]) { dis[v] = dis[u] + c + h[u] - h[v]; pre[v] = i, pu[v] = u; pq.emplace(dis[v], v); } } } if (dis[t] == INF) return -1; for (int i = 1; i <= n; i++) h[i] += dis[i]; int d = f; for (int u = t; u != s; u = pu[u]) d = min(d, e[pre[u]].f); f -= d; res += d * h[t]; for (int u = t; u != s; u = pu[u]) { e[pre[u]].f -= d; e[(pre[u] - 1 ^ 1) + 1].f += d; } } return res; } } g; const int N = 200; int n, k, a[N], b[N], x[N], y[N], t; vector<int> ans; int main() { scanf("%d", &t); while (t--) { scanf("%d%d", &n, &k); g.n = 2 * n + 2, g.s = 2 * n + 1, g.t = 2 * n + 2; g.init(); for (int i = 1; i <= n; i++) { g.add(g.s, i, 1, 0); g.add(i + n, g.t, 1, 0); scanf("%d%d", &a[i], &b[i]); for (int j = 1; j < k; j++) g.add(i, j + n, 1, -(a[i] + (j - 1) * b[i])); for (int j = k; j < n; j++) g.add(i, j + n, 1, -((k - 1) * b[i])); g.add(i, n + n, 1, -(a[i] + (k - 1) * b[i])); } g.min_cost(g.max_flow()); for (int i = 1; i <= n; i++) { for (int j = g.head[i]; j; j = g.e[j].nxt) if (g.e[j].v != g.s && g.e[j].f == 0) x[i] = g.e[j].v - n; y[x[i]] = i; } ans.clear(); for (int i = 1; i < k; i++) if (1 <= y[i] && y[i] <= n) ans.push_back(y[i]); for (int i = k; i < n; i++) if (1 <= y[i] && y[i] <= n) ans.push_back(y[i]), ans.push_back(-y[i]); ans.push_back(y[n]); printf("%d\n", ans.size()); for (auto i : ans) printf("%d ", i); puts(""); } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long t, n, k; long long a[80][80], x[80], y[80]; void hungarian() { vector<long long> u(n + 1), v(n + 1), p(n + 1), way(n + 1); for (long long i = 1; i <= n; i++) { p[0] = i; long long j0 = 0; vector<long long> minv(n + 1, 1000000000000000000); vector<char> used(n + 1, false); do { used[j0] = true; long long i0 = p[j0], delta = 1000000000000000000, j1; for (long long j = 1; j <= n; j++) if (!used[j]) { long long 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 (long long j = 0; j <= n; j++) { if (used[j]) u[p[j]] += delta, v[j] -= delta; else minv[j] -= delta; } j0 = j1; } while (p[j0] != 0); do { long long j1 = way[j0]; p[j0] = p[j1]; j0 = j1; } while (j0); } vector<long long> ans(n + 1), br(n + 1); for (long long j = 1; j <= n; j++) ans[p[j]] = j; for (long long i = 1; i <= n; i++) br[ans[i]] = i; cout << k + (n - k) * 2 << "\n"; for (long long i = 1; i <= n; i++) { if (i < k) cout << br[i] << " "; else if (i > k) cout << br[i] << " " << -br[i] << " "; } cout << br[k] << "\n"; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> t; while (t--) { cin >> n >> k; for (long long i = 1; i <= n; i++) cin >> x[i] >> y[i]; for (long long i = 1; i <= n; i++) { for (long long j = 1; j <= n; j++) { if (j <= k) a[i][j] = -(x[i] + (j - 1) * y[i]); else a[i][j] = -((k - 1) * y[i]); } } hungarian(); } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\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 Sol{ static class specInt { int restore[]; int val; public specInt(int val) { this.val = val; restore = new int[n+1]; } } public static int mxN = 76; public static specInt dp[][] = new specInt[mxN][mxN]; public static int arr[][]; public static int n, k; public static int restore[]; public static void main(String[] args) throws IOException{ FastIO sc = new FastIO(System.in); PrintWriter out = new PrintWriter(System.out); int t = sc.nextInt(); while(t-->0) { n = sc.nextInt(); k = sc.nextInt(); arr = new int[n+1][3]; restore = new int[n+1]; for(int i=0; i<mxN; ++i) { for(int j=0; j<mxN; ++j) { dp[i][j] = new specInt(Integer.MIN_VALUE); } } for(int i=1; i<=n; ++i) { arr[i][0] = sc.nextInt(); arr[i][1] = sc.nextInt(); arr[i][2] = i; } ColumnSort(arr, 1); dp[0][0].val = 0; for(int i=1; i<=n; ++i) { for(int j=0; j<=k; ++j) { //if(dp[i-1][j].val!=Integer.MIN_VALUE) { if(arr[i][1]*(k-1)+dp[i-1][j].val>dp[i][j].val) { dp[i][j].val = arr[i][1]*(k-1)+dp[i-1][j].val; for(int b=1; b<=n; ++b) { dp[i][j].restore[b] = dp[i-1][j].restore[b]; } dp[i][j].restore[i] = 0; } if(j<k&&arr[i][0] + arr[i][1]*j+dp[i-1][j].val>dp[i][j+1].val) { dp[i][j+1].val = arr[i][0] + arr[i][1]*j+dp[i-1][j].val; for(int b=1; b<=n; ++b) { dp[i][j+1].restore[b] = dp[i-1][j].restore[b]; } dp[i][j+1].restore[i] = 1; } //} } } int idx = 0; int num = 0; for(int i=1; i<=n; ++i) { if(dp[n][k].restore[i]==1) num++; else num+=2; } out.println(num); for(int i=1; i<=n; ++i) { if(idx<k-1&&dp[n][k].restore[i]==1) { out.print(arr[i][2] + " "); idx++; } } for(int i=1; i<=n; ++i) { if(dp[n][k].restore[i]==0) { out.print(arr[i][2] + " " + (-arr[i][2]) + " "); } } for(int i=n; i>=0; --i) { if(dp[n][k].restore[i]==1) { out.println(arr[i][2]); break; } } out.println(); } out.close(); } public static void ColumnSort(int arr[][], int col) { Arrays.sort(arr, new Comparator<int[]>() { @Override public int compare(int[] entry1, int[] entry2) { Integer a = entry1[col]; Integer b = entry2[col]; return a.compareTo(b); } }); } static class FastIO { // Is your Fast I/O being bad? InputStream dis; byte[] buffer = new byte[1 << 17]; int pointer = 0; public FastIO(String fileName) throws IOException { dis = new FileInputStream(fileName); } public FastIO(InputStream is) throws IOException { dis = is; } int nextInt() throws IOException { int ret = 0; byte b; do { b = nextByte(); } while (b <= ' '); boolean negative = false; if (b == '-') { negative = true; b = nextByte(); } while (b >= '0' && b <= '9') { ret = 10 * ret + b - '0'; b = nextByte(); } return (negative) ? -ret : ret; } long nextLong() throws IOException { long ret = 0; byte b; do { b = nextByte(); } while (b <= ' '); boolean negative = false; if (b == '-') { negative = true; b = nextByte(); } while (b >= '0' && b <= '9') { ret = 10 * ret + b - '0'; b = nextByte(); } return (negative) ? -ret : ret; } byte nextByte() throws IOException { if (pointer == buffer.length) { dis.read(buffer, 0, buffer.length); pointer = 0; } return buffer[pointer++]; } String next() throws IOException { StringBuffer ret = new StringBuffer(); byte b; do { b = nextByte(); } while (b <= ' '); while (b > ' ') { ret.appendCodePoint(b); b = nextByte(); } return ret.toString(); } } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct minion { int a, b, id; }; bool sortb(minion x, minion y) { return x.b < y.b; } int n, k; minion ar[100]; int dp[100][100]; void calc() { sort(ar, ar + n, sortb); for (int i = 0; i <= n; ++i) for (int j = 0; j <= k; ++j) dp[i][j] = -1e9; dp[0][0] = ar[0].b * (k - 1); dp[0][1] = ar[0].a; for (int i = 1; i < n; ++i) { dp[i][0] = dp[i - 1][0] + ar[i].b * (k - 1); for (int j = 1; j <= k; ++j) dp[i][j] = max(dp[i - 1][j - 1] + ar[i].a + (j - 1) * ar[i].b, dp[i - 1][j] + ar[i].b * (k - 1)); } int place = k; vector<minion> used, notused; for (int i = n - 1; i > 0; --i) { if (place == 0 || dp[i][place] == dp[i - 1][place] + ar[i].b * (k - 1)) notused.push_back(ar[i]); else { --place; used.push_back(ar[i]); } } if (place == 1) used.push_back(ar[0]); else notused.push_back(ar[0]); cout << 2 * n - k << '\n'; for (int i = used.size() - 1; i > 0; --i) cout << used[i].id << " "; for (auto u : notused) cout << u.id << " -" << u.id << ' '; cout << used[0].id << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t--) { cin >> n >> k; for (int i = 0; i < n; ++i) { cin >> ar[i].a >> ar[i].b; ar[i].id = i + 1; } calc(); } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename flow_type, typename cost_type> struct min_cost_max_flow { struct edge { size_t src, dst, rev; flow_type flow, cap; cost_type cost; }; int n; vector<vector<edge>> adj; min_cost_max_flow(int n) : n(n), adj(n), potential(n), dist(n), back(n) {} void add_edge(size_t src, size_t dst, flow_type cap, cost_type cost) { adj[src].push_back({src, dst, adj[dst].size(), 0, cap, cost}); if (src == dst) adj[src].back().rev++; adj[dst].push_back({dst, src, adj[src].size() - 1, 0, 0, -cost}); } vector<cost_type> potential; inline cost_type rcost(const edge &e) { return e.cost + potential[e.src] - potential[e.dst]; } void bellman_ford(int source) { for (int k = 0; k < n; ++k) for (int u = 0; u < n; ++u) for (edge &e : adj[u]) if (e.cap > 0 && rcost(e) < 0) potential[e.dst] += rcost(e); } const cost_type oo = numeric_limits<cost_type>::max(); vector<cost_type> dist; vector<edge *> back; cost_type dijkstra(int source, int sink) { fill(dist.begin(), dist.end(), oo); priority_queue<pair<cost_type, int>, vector<pair<cost_type, int>>, greater<pair<cost_type, int>>> pq; for (pq.push({dist[source] = 0, source}); !pq.empty();) { pair<cost_type, int> p = pq.top(); pq.pop(); if (dist[p.second] < p.first) continue; if (p.second == sink) break; for (edge &e : adj[p.second]) if (e.flow < e.cap && dist[e.dst] > dist[e.src] + rcost(e)) { back[e.dst] = &e; pq.push({dist[e.dst] = dist[e.src] + rcost(e), e.dst}); } } return dist[sink]; } pair<flow_type, cost_type> max_flow(int source, int sink) { flow_type flow = 0; cost_type cost = 0; for (int u = 0; u < n; ++u) for (edge &e : adj[u]) e.flow = 0; potential.assign(n, 0); dist.assign(n, 0); back.assign(n, nullptr); bellman_ford(source); while (dijkstra(source, sink) < oo) { for (int u = 0; u < n; ++u) if (dist[u] < dist[sink]) potential[u] += dist[u] - dist[sink]; flow_type first = numeric_limits<flow_type>::max(); for (edge *e = back[sink]; e; e = back[e->src]) first = min(first, e->cap - e->flow); for (edge *e = back[sink]; e; e = back[e->src]) e->flow += first, adj[e->dst][e->rev].flow -= first; flow += first; cost += first * (potential[sink] - potential[source]); } return {flow, cost}; } }; int ai[105], bi[105]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { long long n, k, a, b; cin >> n >> k; min_cost_max_flow<int, int> g(4 + k + n); g.add_edge(0, n + 1, k, 0); g.add_edge(0, n + 2, n - k, 0); for (int i = 0; i < k; i++) g.add_edge(n + 1, n + 4 + i, 1, 0); for (int i = 0; i < n; i++) { cin >> a >> b; ai[i] = a; bi[i] = b; g.add_edge(n + 2, i + 1, 1, -((k - 1) * b)); for (int j = 0; j < k; j++) g.add_edge(n + 4 + j, i + 1, 1, -(a + (j * b))); g.add_edge(i + 1, n + 3, 1, 0); } long long x = -(g.max_flow(0, n + 3).second); vector<pair<long long, long long>> ks; set<int> af; for (int i = 0; i < n; i++) af.insert(i); for (int i = 0; i < k; i++) for (auto edge : g.adj[n + 4 + i]) if (edge.flow == 1) { ks.push_back( pair<long long, long long>(bi[edge.dst - 1], edge.dst - 1)); af.erase(edge.dst - 1); } sort((ks).begin(), (ks).end()); vector<int> res; for (int i = 0; i < k - 1; i++) res.push_back(ks[i].second + 1); for (auto y : af) { res.push_back(y + 1); res.push_back(-(y + 1)); } res.push_back(ks[k - 1].second + 1); cout << (res.size()) << '\n'; for (auto y : res) cout << y << ' '; 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 MX = 100, inf = 1e9; struct FlowGraph { struct Edge { int to; int flow, cap, cost; Edge *res; Edge() : to(0), flow(0), cap(0), cost(0), res(0) {} Edge(int to, int flow, int cap, int cost) : to(to), flow(flow), cap(cap), cost(cost), res(0) {} void addFlow(int f) { flow += f; res->flow -= f; } }; vector<vector<Edge *>> adj; vector<int> dis, pos; int n; FlowGraph(int n) : n(n), adj(n), dis(n), pos(n) {} void add(int u, int v, int cap, int cost) { Edge *x = new Edge(v, 0, cap, cost); Edge *y = new Edge(u, cap, cap, -cost); x->res = y; y->res = x; adj[u].push_back(x); adj[v].push_back(y); } pair<long long, long long> maxFlowMinCost(int s, int t) { vector<bool> inQueue(n); vector<int> dis(n), cap(n); vector<Edge *> par(n); long long maxFlow = 0, minCost = 0; while (1) { fill(dis.begin(), dis.end(), inf); fill(par.begin(), par.end(), nullptr); fill(cap.begin(), cap.end(), 0); dis[s] = 0; cap[s] = inf; queue<int> q; q.push(s); while (q.size()) { int u = q.front(); q.pop(); inQueue[u] = 0; for (Edge *v : adj[u]) { if (v->cap > v->flow && dis[v->to] > dis[u] + v->cost) { dis[v->to] = dis[u] + v->cost; par[v->to] = v; cap[v->to] = min(cap[u], v->cap - v->flow); if (!inQueue[v->to]) { q.push(v->to); inQueue[v->to] = 1; } } } } if (!par[t]) break; maxFlow += cap[t]; minCost += cap[t] * dis[t]; for (int u = t; u != s; u = par[u]->res->to) par[u]->addFlow(cap[t]); } return {maxFlow, minCost}; } }; int n, k, a[MX], b[MX], res[MX]; void solve() { cin >> n >> k; for (int i = 0; i < n; i++) cin >> a[i] >> b[i]; FlowGraph g(2 * n + 5); int s = g.n - 1, t = s - 2; for (int i = 0; i < n; i++) { g.add(s, i, 1, 0); for (int j = 0; j < n; j++) { if (j + 1 == n) { g.add(i, n + j, 1, -(a[i] + (k - 1) * b[i])); } else if (j < k - 1) { g.add(i, n + j, 1, -(a[i] + j * b[i])); } else { g.add(i, n + j, 1, -((k - 1) * b[i])); } } } for (int j = 0; j < n; j++) g.add(n + j, t, 1, 0); g.maxFlowMinCost(s, t); for (int i = 0; i < n; i++) for (auto *e : g.adj[i]) if (e->to >= n && e->flow == e->cap) res[e->to - n] = i; vector<int> v; for (int i = 0; i < n; i++) { v.push_back(res[i] + 1); if (i < k - 1 || i + 1 == n) continue; v.push_back(-(res[i] + 1)); } cout << v.size() << '\n'; for (int r : v) cout << r << " "; cout << '\n'; } int main() { ios_base::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
java
/* If you want to aim high, aim high Don't let that studying and grades consume you Just live life young ****************************** If I'm the sun, you're the moon Because when I go up, you go down ******************************* I'm working for the day I will surpass you https://www.a2oj.com/Ladder16.html */ import java.util.*; import java.io.*; import java.math.*; public class x1354F2 { public static void main(String omkar[]) throws Exception { BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(infile.readLine()); int T = Integer.parseInt(st.nextToken()); StringBuilder sb = new StringBuilder(); while(T-->0) { st = new StringTokenizer(infile.readLine()); int N = Integer.parseInt(st.nextToken()); int K = Integer.parseInt(st.nextToken()); Minion[] arr = new Minion[N]; for(int i=0; i < N; i++) { st = new StringTokenizer(infile.readLine()); int a = Integer.parseInt(st.nextToken()); int b = Integer.parseInt(st.nextToken()); arr[i] = new Minion(a, b, i); } Arrays.sort(arr); Node[][] par = new Node[N][K+1]; long[][] dp = new long[N][K+1]; for(int i=0; i < N; i++) Arrays.fill(dp[i], -1); dp[0][1] = arr[0].a; par[0][1] = new Node(-1, 420); if(N != K) { dp[0][0] = arr[0].b*(K-1); par[0][0] = new Node(-1, 420); } for(int i=1; i < N; i++) for(int k=0; k <= K; k++) { if(i+1 < k) break; int boof = i-k+1; long max = -1L; if(i >= k && dp[i-1][k] != -1) max = dp[i-1][k]+arr[i].b*(K-1); if(k > 0 && dp[i-1][k-1] != -1) max = Math.max(max, dp[i-1][k-1]+arr[i].a+arr[i].b*(k-1)); dp[i][k] = max; if(max == dp[i-1][k]+arr[i].b*(K-1) && dp[i-1][k] != -1) par[i][k] = new Node(i-1, k); else if(max != -1) par[i][k] = new Node(i-1, k-1); } //restore ArrayList<Integer> ls = new ArrayList<Integer>(); HashSet<Integer> set = new HashSet<Integer>(); for(int i=0; i < N; i++) set.add(i); Node curr = new Node(N-1, K); while(curr.dex > 0) { int i = curr.dex; int k = curr.cnt; Node next = par[i][k]; if(k == next.cnt+1) { ls.add(arr[i].dex); set.remove(arr[i].dex); } curr = next; } if(curr.cnt == 1) { ls.add(arr[0].dex); set.remove(arr[0].dex); } Collections.reverse(ls); ArrayList<Integer> res = new ArrayList<Integer>(); for(int i=0; i < ls.size()-1; i++) res.add(ls.get(i)+1); for(int x: set) { res.add(x+1); res.add(-x-1); } res.add(ls.get(ls.size()-1)+1); sb.append(res.size()+"\n"); for(int x: res) sb.append(x+" "); sb.append("\n"); } System.out.print(sb); } } class Minion implements Comparable<Minion> { public long a; public long b; public int dex; public Minion(long x, long y, int d) { a = x; b = y; dex = d; } public int compareTo(Minion oth) { if(b == oth.b) return (int)(oth.a-a); return (int)(b-oth.b); } } class Node { public int dex; public int cnt; public Node(int a, int c) { dex = a; cnt = c; } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T> void splitstr(const string &s, vector<T> &out) { istringstream in(s); out.clear(); copy(istream_iterator<T>(in), istream_iterator<T>(), back_inserter(out)); } template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } static void redirect(int argc, const char **argv) { ios::sync_with_stdio(false); cin.tie(NULL); if (argc > 1) { static filebuf f; f.open(argv[1], ios::in); cin.rdbuf(&f); if (!cin) { cerr << "Failed to open '" << argv[1] << "'" << endl; exit(1); } } if (argc > 2) { static filebuf f; f.open(argv[2], ios::out | ios::trunc); cout.rdbuf(&f); if (!cout) { cerr << "Failed to open '" << argv[2] << "'" << endl; } } cin.exceptions(ios::failbit); } struct minion { int a, b; int idx; bool operator<(const minion &other) const { return b < other.b; } }; int main(int argc, const char **argv) { redirect(argc, argv); int T; cin >> T; for (int cas = 0; cas < T; cas++) { int N, K; cin >> N >> K; vector<minion> m(N); for (int i = 0; i < N; i++) { cin >> m[i].a >> m[i].b; m[i].idx = i + 1; } sort(begin(m), end(m)); vector<vector<int> > dp(N + 1, vector<int>(K + 1, INT_MIN / 2)); vector<vector<int> > team(N + 1, vector<int>(K + 1, 0)); dp[0][0] = 0; for (int i = 0; i < N; i++) { for (int j = 0; j <= min(i, K); j++) dp[i + 1][j] = dp[i][j] + m[i].b * (K - 1); for (int j = 0; j <= min(i, K - 1); j++) { int score = dp[i][j] + m[i].a + j * m[i].b; if (score > dp[i + 1][j + 1]) { dp[i + 1][j + 1] = score; team[i + 1][j + 1] = 1; } } } int n = N; int k = K; vector<int> in, out; while (n > 0) { int t = team[n][k]; n--; k -= t; if (t) in.push_back(m[n].idx); else out.push_back(m[n].idx); } reverse(begin(in), end(in)); cout << ((long long)(in).size()) + 2 * ((long long)(out).size()) << '\n'; for (int i = 0; i < K - 1; i++) cout << in[i] << ' '; for (int v : out) cout << v << ' ' << -v << ' '; cout << in.back() << '\n'; } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename T1, typename T2> string print_iterable(T1 begin_iter, T2 end_iter, int counter) { bool done_something = false; stringstream res; res << "["; for (; begin_iter != end_iter and counter; ++begin_iter) { done_something = true; counter--; res << *begin_iter << ", "; } string str = res.str(); if (done_something) { str.pop_back(); str.pop_back(); } str += "]"; return str; } vector<int> SortIndex(int size, std::function<bool(int, int)> compare) { vector<int> ord(size); for (int i = 0; i < size; i++) ord[i] = i; sort(ord.begin(), ord.end(), compare); return ord; } template <typename T> bool MinPlace(T& a, const T& b) { if (a > b) { a = b; return true; } return false; } template <typename T> bool MaxPlace(T& a, const T& b) { if (a < b) { a = b; return true; } return false; } template <typename S, typename T> ostream& operator<<(ostream& out, const pair<S, T>& p) { out << "{" << p.first << ", " << p.second << "}"; return out; } template <typename T> ostream& operator<<(ostream& out, const vector<T>& v) { out << "["; for (int i = 0; i < (int)v.size(); i++) { out << v[i]; if (i != (int)v.size() - 1) out << ", "; } out << "]"; return out; } template <class TH> void _dbg(const char* name, TH val) { clog << name << ": " << val << endl; } template <class TH, class... TA> void _dbg(const char* names, TH curr_val, TA... vals) { while (*names != ',') clog << *names++; clog << ": " << curr_val << ", "; _dbg(names + 1, vals...); } void solve() { 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]; vector<int> ord = SortIndex(N, [&](int i, int j) { return b[i] < b[j]; }); vector<vector<int>> din(N + 1, vector<int>(N + 1, -1e9)); din[0][0] = 0; for (int i = 0; i < N; i++) { int it = ord[i]; din[i + 1][0] = din[i][0] + (K - 1) * b[it]; for (int j = 1; j <= N; j++) { din[i + 1][j] = max(din[i][j] + (K - 1) * b[it], din[i][j - 1] + (j - 1) * b[it] + a[it]); } } vector<bool> destroyed(N + 1, false); int cnt = K; for (int i = N - 1; i >= 0; i--) { int it = ord[i]; if (din[i + 1][cnt] == din[i][cnt] + (K - 1) * b[it]) { destroyed[i] = true; } else cnt--; } assert(cnt == 0); vector<int> ans; int last; for (int i = 0; i < N; i++) { if (!destroyed[i]) { if (((int)((ans).size())) < K - 1) ans.push_back(ord[i] + 1); else last = ord[i]; } } for (int i = 0; i < N; i++) { if (destroyed[i]) { ans.push_back(ord[i] + 1); ans.push_back(-ord[i] - 1); } } ans.push_back(last + 1); cout << ans.size() << "\n"; for (int x : ans) cout << x << " "; cout << "\n"; } int main() { ios::sync_with_stdio(false); cin.tie(0); int T; cin >> T; for (int t = 0; t < T; 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 <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; } template <typename A, typename B> ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; } void dbg_out() { cerr << endl; } template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); } template <typename T1, typename T2> bool maximize(T1 &a, const T2 &b) { if (a < b) { a = b; return true; } return false; } template <typename T> void output_vector(const vector<T> &v, bool add_one = false, int start = -1, int end = -1) { if (start < 0) start = 0; if (end < 0) end = int(v.size()); for (int i = start; i < end; i++) cout << v[i] + (add_one ? 1 : 0) << (i < end - 1 ? ' ' : '\n'); } const int INF = 1e9 + 5; struct minion { int A, B, index; bool operator<(const minion &other) const { return B < other.B; } }; void run_case() { int N, K; cin >> N >> K; vector<minion> minions(N); for (minion &m : minions) cin >> m.A >> m.B; for (int i = 0; i < N; i++) minions[i].index = i + 1; sort(minions.begin(), minions.end()); vector<int> dp(K + 1, -INF); dp[0] = 0; vector<vector<bool>> previous(N + 1, vector<bool>(K + 1, false)); for (int i = 0; i < N; i++) { vector<int> next_dp(K + 1, -INF); for (int k = 0; k <= K; k++) { if (maximize(next_dp[k], dp[k] + (K - 1) * minions[i].B)) previous[i + 1][k] = false; if (k < K && maximize(next_dp[k + 1], dp[k] + minions[i].A + k * minions[i].B)) previous[i + 1][k + 1] = true; } dp = next_dp; }; vector<int> assignment(N, -1); for (int n = N, k = K, end = N; n > 0; n--) if (previous[n][k]) assignment[--k] = minions[n - 1].index; else assignment[--end] = minions[n - 1].index; vector<int> solution; for (int k = 0; k < K - 1; k++) solution.push_back(assignment[k]); for (int k = K; k < N; k++) { solution.push_back(assignment[k]); solution.push_back(-assignment[k]); } solution.push_back(assignment[K - 1]); assert(int(solution.size()) == 2 * N - K); cout << solution.size() << '\n'; output_vector(solution); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int tests; cin >> tests; while (tests-- > 0) run_case(); }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\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; struct Edge { int to, val, next, cost; } edge[25086]; int s, t; int cnt = -1; int head[25086]; queue<int> q; int d[25086]; int cur[25086]; int inq[25086]; int vis[25086]; int costsum; void addEdge(int u, int v, int w, int f) { cnt++; edge[cnt].to = v; edge[cnt].val = w; edge[cnt].next = head[u]; edge[cnt].cost = f; head[u] = cnt; cnt++; edge[cnt].to = u; edge[cnt].val = 0; edge[cnt].next = head[v]; edge[cnt].cost = -f; head[v] = cnt; } bool bfs() { memset(d, 0x3f, sizeof(d)); memset(vis, 0, sizeof(vis)); d[s] = 0; q.push(s); inq[s] = 1; while (!q.empty()) { int u = q.front(); q.pop(); inq[u] = 0; for (int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to; cur[v] = head[v]; int f = edge[i].cost; if (d[v] > d[u] + f && edge[i].val > 0) { d[v] = d[u] + f; if (!inq[v]) { q.push(v); inq[v] = 1; } } } } return d[t] != inf; } int dfs(int u, int flow) { vis[u] = 1; int nowflow = 0; if (u == t) return flow; for (int i = cur[u]; i != -1; i = edge[i].next) { cur[u] = i; int v = edge[i].to; int w = edge[i].val; int f = edge[i].cost; if (d[v] == d[u] + f && w > 0 && !vis[v]) { int k = dfs(v, min(flow - nowflow, w)); if (k > 0) { edge[i].val -= k; edge[i ^ 1].val += k; nowflow += k; costsum += f * k; if (nowflow == flow) break; } } } vis[u] = 0; if (!nowflow) d[u] = inf; return nowflow; } int dinic() { int ans = 0; while (bfs()) { ans += dfs(s, inf); } return ans; } int T; int n, k, a, b; int opt[25086]; int main() { scanf("%d", &T); while (T--) { memset(head, -1, sizeof(head)), cnt = -1; scanf("%d%d", &n, &k); s = 0, t = 2 * n + 1, costsum = 0; for (int i = 1; i <= n; i++) { scanf("%d%d", &a, &b); addEdge(s, i, 1, 0), addEdge(i + n, t, 1, 0); for (int j = 1; j <= n; j++) { if (j < k) addEdge(i, j + n, 1, -(a + (j - 1) * b)); else if (j < n) addEdge(i, j + n, 1, -(k - 1) * b); else addEdge(i, j + n, 1, -(a + (k - 1) * b)); } } dinic(); for (int i = 1; i <= n; i++) { for (int j = head[i]; j != -1; j = edge[j].next) { if (!edge[j].val) { opt[edge[j].to - n] = i; break; } } } printf("%d\n", k - 1 + (n - k) * 2 + 1); for (int i = 1; i <= n; i++) { if (i < k || i == n) printf("%d ", opt[i]); else printf("%d %d ", opt[i], -opt[i]); } puts(""); } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct Edge { int from, to, capacity, cost; Edge(int from, int to, int capacity, int cost) : from(from), to(to), capacity(capacity), cost(cost){}; }; vector<vector<int>> adj, cost, capacity; const int INF = 2e9; void shortest_paths(int n, int v0, vector<int>& d, vector<int>& p) { d.assign(n, INF); d[v0] = 0; vector<bool> inq(n, false); queue<int> q; q.push(v0); p.assign(n, -1); while (!q.empty()) { int u = q.front(); q.pop(); inq[u] = false; for (int v : adj[u]) { if (capacity[u][v] > 0 && d[v] > d[u] + cost[u][v]) { d[v] = d[u] + cost[u][v]; p[v] = u; if (!inq[v]) { inq[v] = true; q.push(v); } } } } } int min_cost_flow(int N, vector<Edge>& edges, int s, int t) { adj.assign(N, vector<int>()); cost.assign(N, vector<int>(N, 0)); capacity.assign(N, vector<int>(N, 0)); for (Edge e : edges) { adj[e.from].push_back(e.to); adj[e.to].push_back(e.from); cost[e.from][e.to] = e.cost; cost[e.to][e.from] = -e.cost; capacity[e.from][e.to] = e.capacity; } int flow = 0; int cost = 0; vector<int> d, p; while (1) { shortest_paths(N, s, d, p); if (d[t] == INF) break; int f = 2000000000; int cur = t; while (cur != s) { f = min(f, capacity[p[cur]][cur]); cur = p[cur]; } flow += f; cost += f * d[t]; cur = t; while (cur != s) { capacity[p[cur]][cur] -= f; capacity[cur][p[cur]] += f; cur = p[cur]; } } return cost; } int ab[76][2]; int main() { int T; int i, j; int n, k; int s, t; scanf("%d", &T); while (T--) { vector<Edge> edges; vector<int> res; scanf("%d %d", &n, &k); s = n + n, t = n + n + 1; for (i = 0; i < n; i++) { scanf("%d %d", &ab[i][0], &ab[i][1]); } for (i = 0; i < n; i++) { edges.emplace_back(s, i, 1, 0); edges.emplace_back(i + n, t, 1, 0); } for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (i >= k - 1 && i < n - 1) edges.emplace_back(j, i + n, 1, -(k - 1) * ab[j][1]); else if (i == n - 1) edges.emplace_back(j, i + n, 1, -(ab[j][0] + (k - 1) * ab[j][1])); else edges.emplace_back(j, i + n, 1, -(ab[j][0] + i * ab[j][1])); } } min_cost_flow(n + n + 2, edges, s, t); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (!capacity[j][i + n]) { if (i >= k - 1 && i < n - 1) { res.push_back(j + 1); res.push_back(-j - 1); } else res.push_back(j + 1); } } } printf("%d\n", 2 * n - k); for (auto o : res) printf("%d ", o); printf("\n"); } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ull = unsigned long long; using ll = long long; using ld = long double; const int mod = 1e9 + 7; const double pi = acos(-1.0); const int inf = INT_MAX; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t--) { 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> ind(n); iota(ind.begin(), ind.end(), 0); sort(ind.begin(), ind.end(), [&](int i, int j) { return b[i] < b[j]; }); vector<vector<int>> dp(n, vector<int>(n + 1)); dp[0][0] = (k - 1) * b[ind[0]]; dp[0][1] = a[ind[0]]; for (int i = 1; i < n; i++) { dp[i][0] = (k - 1) * b[ind[i]] + dp[i - 1][0]; for (int j = 1; j <= i + 1; j++) { if (j < i + 1) { dp[i][j] = (k - 1) * b[ind[i]] + dp[i - 1][j]; dp[i][j] = max(dp[i][j], (j - 1) * b[ind[i]] + a[ind[i]] + dp[i - 1][j - 1]); } else { dp[i][j] = (j - 1) * b[ind[i]] + a[ind[i]] + dp[i - 1][j - 1]; } } } vector<int> final; for (int i = n - 1, j = k; j > 0;) { if (j < i + 1 && dp[i][j] == (k - 1) * b[ind[i]] + dp[i - 1][j]) { i--; } else { final.push_back(ind[i]); i--, j--; } } reverse(final.begin(), final.end()); cout << k + 2 * (n - k) << '\n'; for (int i = 0; i < k - 1; i++) { cout << final[i] + 1 << ' '; } for (int i = 0; i < n; i++) { if (count(final.begin(), final.end(), i) == 0) { cout << i + 1 << ' ' << -i - 1 << ' '; } } cout << final[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; const double PI = acos(-1); const double eps = 1e-12; const int inf = 2000000000; const long long int infLL = (long long int)1e18; long long int MOD = 1000000007; int MOD1 = 1000000007; int MOD2 = 1000000009; inline bool checkBit(long long int n, long long int i) { return n & (1LL << i); } inline long long int setBit(long long int n, long long int i) { return n | (1LL << i); ; } inline long long int resetBit(long long int n, long long int i) { return n & (~(1LL << i)); } int dx[] = {0, 0, +1, -1}; int dy[] = {+1, -1, 0, 0}; inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } inline bool isLeapYear(long long int year) { return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); } inline void normal(long long int &a) { a %= MOD; (a < 0) && (a += MOD); } inline long long int modMul(long long int a, long long int b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a * b) % MOD; } inline long long int modAdd(long long int a, long long int b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a + b) % MOD; } inline long long int modSub(long long int a, long long int b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; } inline long long int modPow(long long int b, long long int p) { long long int r = 1LL; while (p) { if (p & 1) r = modMul(r, b); b = modMul(b, b); p >>= 1LL; } return r; } inline long long int modDiv(long long int a, long long int b) { return modMul(a, modPow(b, MOD - 2)); } bool comp(const pair<long long int, pair<long long int, long long int> > &p1, const pair<long long int, pair<long long int, long long int> > &p2) { return p1.first > p2.first; } bool comp1(const pair<long long int, long long int> &p1, const pair<long long int, long long int> &p2) { if (p1.first == p2.first) { return p1.second > p2.second; } return p1.first < p2.first; } long long int converter(string a) { long long int i, mul = 1LL, r, t, ans = 0LL; if (a.length() == 0) return 0; for (i = a.length() - 1; i >= 0; i--) { t = a[i] - '0'; r = t % 10; ans += (mul * r); mul = mul * 10; } return ans; } int msb(unsigned x) { union { double a; int b[2]; }; a = x; return (b[1] >> 20) - 1023; } const int MAX = 78; long long int t, n, k; pair<pair<long long int, long long int>, long long int> p[MAX]; long long int dp[MAX][MAX]; vector<long long int> good, bad; long long int solve(long long int idx, long long int cnt) { if (idx == n + 1) { if (cnt != k) return -infLL; return 0; } if (dp[idx][cnt] != -1) return dp[idx][cnt]; long long int ret = -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(long long int idx, long long 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) { trace(idx + 1, cnt + 1); good.push_back(p[idx].second); } else { trace(idx + 1, cnt); bad.push_back(p[idx].second); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cout.unsetf(ios::floatfield); cout.precision(20); cout.setf(ios::fixed, ios::floatfield); ; cin >> t; while (t--) { good.clear(); bad.clear(); long long int maxi = 0, idx; cin >> n >> k; for (int i = 1; i <= n; ++i) { cin >> p[i].first.second >> p[i].first.first; p[i].second = i; if (p[i].first.second >= maxi) maxi = p[i].first.second, idx = i; } if (k == 1) { cout << 1 << '\n'; cout << idx << '\n'; continue; } sort(p + 1, p + n + 1); for (int i = 1; i <= n; ++i) { ; } memset(dp, -1, sizeof(dp)); long long int now = solve(1, 0); ; trace(1, 0); reverse((good).begin(), (good).end()); reverse((bad).begin(), (bad).end()); if (good.size() == 0) { string ss = ""; ss += to_string(n); ss += '#'; ss += to_string(k); ss += '#'; for (int i = 1; i <= n; ++i) { ss += to_string(p[i].first.first); ss += '#'; ss += to_string(p[i].first.second); ss += '&'; } cout << ss << '\n'; continue; } cout << good.size() + (2 * bad.size()) << '\n'; for (int i = 0; i < (int)good.size() - 1; ++i) { cout << good[i] << " "; } for (int i = 0; i < bad.size(); ++i) { cout << bad[i] << " " << -1 * bad[i] << " "; } cout << good[good.size() - 1] << '\n'; } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 80; struct minion { int a, b, ind; bool operator<(const minion &other) const { return b < other.b; } } minions[N]; int n, k; int memo[N][N], vis[N][N], vid; int solve(int i, int cnt) { if (i == n) return cnt == k ? 0 : -1e9; int &ret = memo[i][cnt]; if (vis[i][cnt] == vid) return ret; vis[i][cnt] = vid; ret = solve(i + 1, cnt) + minions[i].b * (k - 1); if (cnt < k) ret = max(ret, solve(i + 1, cnt + 1) + minions[i].a + minions[i].b * cnt); return ret; } vector<int> keep, dest; void path(int i, int cnt) { if (i == n) return; if (solve(i + 1, cnt) + minions[i].b * (k - 1) == solve(i, cnt)) { dest.push_back(minions[i].ind); return path(i + 1, cnt); } keep.push_back(minions[i].ind); path(i + 1, cnt + 1); } void run() { cin >> n >> k; for (int i = 0; i < n; i++) { cin >> minions[i].a >> minions[i].b; minions[i].ind = i + 1; } sort(minions, minions + n); ++vid; keep.clear(); dest.clear(); path(0, 0); cout << keep.size() + dest.size() * 2 << '\n'; for (int i = 0; i + 1 < keep.size(); i++) cout << keep[i] << ' '; for (int x : dest) cout << x << ' ' << -x << ' '; cout << keep.back() << '\n'; } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t; cin >> t; while (t--) run(); }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\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 mod = 998244353; const int inf = 1e9; const int N = 5005; const int mf[] = {0, 0, 1, -1}, mc[] = {1, -1, 0, 0}; const double eps = 1e-9; const double pi = acos(-1); int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { int n, k; cin >> n >> k; vector<pair<pair<int, int>, int>> v(n); for (int i = 0; i < n; i++) { cin >> v[i].first.first >> v[i].first.second; v[i].second = i + 1; } sort((v).begin(), (v).end(), [&](pair<pair<int, int>, int> i, pair<pair<int, int>, int> j) { return i.first.second < j.first.second; }); vector<vector<int>> dp(n + 1, vector<int>(k + 1, -inf)); dp[0][0] = 0; for (int i = 1; i <= n; i++) { int a = v[i - 1].first.first; int b = v[i - 1].first.second; dp[i][0] = dp[i - 1][0] + (k - 1) * b; for (int j = 1; j <= k; j++) dp[i][j] = max(dp[i - 1][j] + (k - 1) * b, dp[i - 1][j - 1] + a + (j - 1) * b); } vector<bool> mark(n + 1); vector<int> stay; int j = k; for (int i = n; i >= 1; i--) { int a = v[i - 1].first.first; int b = v[i - 1].first.second; int id = v[i - 1].second; if (dp[i][j] == dp[i - 1][j - 1] + a + (j - 1) * b) { mark[id] = true; stay.push_back(id); j--; } } vector<int> ans; for (int i = k - 1; i > 0; i--) ans.push_back(stay[i]); for (int i = 1; i <= n; i++) if (!mark[i]) { ans.push_back(i); ans.push_back(-i); } ans.push_back(stay[0]); cout << k + 2 * (n - k) << '\n'; for (int &i : ans) cout << i << " \n"[&i == &ans.back()]; } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
java
import java.io.*; import java.util.*; import java.math.*; public class F { static final boolean RUN_TIMING = false; static PushbackReader in = new PushbackReader(new BufferedReader(new InputStreamReader(System.in)), 1024); static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); public void go() throws IOException { // in = new PushbackReader(new BufferedReader(new FileReader(new File("test.txt"))), 1024); // out = new PrintWriter(new FileWriter(new File("output.txt"))); int zzz = ipar(); for (int zz = 0; zz < zzz; zz++) { int n = ipar(); int k = ipar(); Minion[] arr = new Minion[n]; for (int i = 0; i < n; i++) { int a = ipar(); int b = ipar(); arr[i] = new Minion(a, b, i); } Arrays.sort(arr, (a, b) -> a.b - b.b); int[][] dp = new int[n+1][k+1]; int[][] index = new int[n+1][k+1]; for (int i = 0; i < k; i++) { dp[n][i] = Integer.MIN_VALUE; } for (int i = n-1; i >= 0; i--) { for (int e = 0; e <= k; e++) { dp[i][e] = dp[i+1][e] + arr[i].getDestroy(k-1); index[i][e] = -arr[i].i-1; } for (int e = 0; e < k; e++) { if (dp[i+1][e+1] + arr[i].getAdd(e) > dp[i][e]) { dp[i][e] = dp[i+1][e+1] + arr[i].getAdd(e); index[i][e] = arr[i].i+1; } } } ArrayList<Integer> adds = new ArrayList<>(); ArrayList<Integer> destroys = new ArrayList<>(); for (int i = 0; i < n; i++) { if (index[i][adds.size()] > 0) { adds.add(index[i][adds.size()]); } else { destroys.add(-index[i][adds.size()]); } } // for (int[] a : dp) { // out.println(Arrays.toString(a)); // } out.println(adds.size() + destroys.size()*2); for (int i = 0; i < k-1; i++) { out.print(adds.get(i)); out.print(" "); } for (int i = 0; i < n-k; i++) { out.print(destroys.get(i)); out.print(" "); out.print(-destroys.get(i)); out.print(" "); } out.println(adds.get(k-1)); } out.flush(); in.close(); } private class Minion { int a, b, i; public Minion(int a, int b, int i) { this.a = a; this.b = b; this.i = i; } public int getAdd(int t) { return a + b*t; } public int getDestroy(int t) { return b*t; } public String toString() { return String.format("%d=(%d,%d)", i, a, b); } } public int ipar() throws IOException { return Integer.parseInt(spar()); } public int[] iapar(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = ipar(); } return arr; } public long lpar() throws IOException { return Long.parseLong(spar()); } public long[] lapar(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = lpar(); } return arr; } public double dpar() throws IOException { return Double.parseDouble(spar()); } public String spar() throws IOException { StringBuilder sb = new StringBuilder(1024); int c; do { c = in.read(); } while (Character.isWhitespace(c) && c != -1); if (c == -1) { throw new NoSuchElementException("Reached EOF"); } do { sb.append((char)c); c = in.read(); } while (!Character.isWhitespace(c) && c != -1); while (c != '\n' && Character.isWhitespace(c) && c != -1) { c = in.read(); } if (c != -1 && c != '\n') { in.unread(c); } return sb.toString(); } public String linepar() throws IOException { StringBuilder sb = new StringBuilder(1024); int c; while ((c = in.read()) != '\n' && c != -1) { if (c == '\r') { continue; } sb.append((char)c); } return sb.toString(); } public boolean haspar() throws IOException { String line = linepar(); if (line.isEmpty()) { return false; } in.unread('\n'); in.unread(line.toCharArray()); return true; } public static void main(String[] args) throws IOException { long time = 0; time -= System.nanoTime(); new F().go(); time += System.nanoTime(); if (RUN_TIMING) { System.out.printf("%.3f ms%n", time/1000000.0); } out.flush(); in.close(); } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const pair<long long, long long> IMPOSSIBLE = make_pair(-999999999999999999, -1); class Minion { public: long long a, b; int minionId; bool operator<(const Minion& that) const { return b < that.b; } Minion(int ma, int mb, int mid) : a(ma), b(mb), minionId(mid) {} Minion() {} }; vector<int> getMaximumPowerSequence(int n, int k, vector<Minion> minions) { sort(minions.begin(), minions.end()); pair<long long, long long> dp[n + 2][k + 1]; for (int i = 0; i <= k; i++) dp[n + 1][i] = IMPOSSIBLE; dp[n + 1][0] = make_pair(0, -1); for (int oi = 0; oi < n; oi++) { pair<long long, long long>* olddp = dp[n + 1]; if (oi > 0) olddp = dp[oi - 1]; pair<long long, long long>* newdp = dp[oi]; for (int i = 0; i <= k; i++) newdp[i] = IMPOSSIBLE; long long NOT_CHOSEN_BONUS = (long long)(k - 1) * minions[oi].b; for (int i = 0; i <= k; i++) { newdp[i] = max(newdp[i], make_pair(olddp[i].first + NOT_CHOSEN_BONUS, olddp[i].second)); if (i + 1 <= k) { long long CHOSEN_BONUS = minions[oi].b * i + minions[oi].a; newdp[i + 1] = max(newdp[i + 1], make_pair(olddp[i].first + CHOSEN_BONUS, (long long)oi)); } } } int currentStanding = n - 1; int toSelect = k; stack<int> selectedMinion; vector<bool> isSelected(n + 1, false); while (toSelect > 0) { int nextOffer = dp[currentStanding][toSelect].second; selectedMinion.push(minions[nextOffer].minionId); isSelected[minions[nextOffer].minionId] = true; currentStanding = nextOffer - 1; toSelect--; } vector<int> ans; while (selectedMinion.size() > 1) { int cm = selectedMinion.top(); selectedMinion.pop(); ans.push_back(cm); } for (int i = 1; i <= n; i++) { if (!isSelected[i]) { ans.push_back(i); ans.push_back(-i); } } while (selectedMinion.size() > 0) { int cm = selectedMinion.top(); selectedMinion.pop(); ans.push_back(cm); } return ans; } int main() { int tc; scanf("%d", &tc); while (tc--) { int n, k; scanf("%d%d", &n, &k); vector<Minion> minions; for (int i = 1; i <= n; i++) { int a, b; scanf("%d%d", &a, &b); minions.push_back(Minion(a, b, i)); } vector<int> answer_sequence = getMaximumPowerSequence(n, k, minions); printf("%d\n", answer_sequence.size()); for (int v : answer_sequence) printf("%d ", v); 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; template <typename T> T &read(T &x) { x = 0; bool f = 0; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') { f = 1; } ch = getchar(); } while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); } if (f) { x = -x; } return x; } const double eps = 1e-8; inline int sgn(double x) { if (x < -eps) { return -1; } return x > eps; } void fp() { freopen(".in", "r", stdin); freopen(".out", "w", stdout); } struct pai { int a, b, num; } c[80]; inline bool cmp(pai a, pai b) { return a.b < b.b; } int f[80][80], g[80][80], ans1[155], ans2[155]; int main() { int t = read(t); while (t--) { int n = read(n), k = read(k), i, j; for (i = 1; i <= n; i++) { read(c[i].a); read(c[i].b); c[i].num = i; } sort(c + 1, c + n + 1, cmp); for (i = 1; i <= n; i++) { for (j = 0; j <= min(k, i); j++) { if (j == i) { f[i][j] = f[i - 1][j - 1] + c[i].a + c[i].b * (j - 1); g[i][j] = 2; continue; } if (!j) { f[i][j] = f[i - 1][j] + c[i].b * (k - 1); g[i][j] = 1; continue; } if (f[i - 1][j - 1] + c[i].a + c[i].b * (j - 1) < f[i - 1][j] + c[i].b * (k - 1)) { f[i][j] = f[i - 1][j] + c[i].b * (k - 1); g[i][j] = 1; } else { f[i][j] = f[i - 1][j - 1] + c[i].a + c[i].b * (j - 1); g[i][j] = 2; } } } printf("%d\n", 2 * n - k); int nowx = n, nowy = k, cnt1 = 0, cnt2 = 0; for (i = 1; i <= n; i++) { if (g[nowx][nowy] == 1) { ans1[++cnt1] = -c[nowx].num; ans1[++cnt1] = c[nowx].num; nowx--; } else { nowx--; nowy--; } } nowx = n; nowy = k; for (i = 1; i <= n; i++) { if (g[nowx][nowy] == 1) { nowx--; } else { ans2[++cnt2] = c[nowx].num; nowx--; nowy--; } } for (i = cnt2; i >= 2; i--) { printf("%d ", ans2[i]); } for (i = cnt1; i; i--) { printf("%d ", ans1[i]); } printf("%d ", ans2[1]); puts(""); } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC target("sse4") using namespace std; const double PI = acos(-1.0); mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); long long MinCostMatching(const vector<vector<long long>> &cost, vector<long long> &Lmate, vector<long long> &Rmate) { long long n = (long long)(cost.size()); vector<long long> u(n); vector<long long> v(n); for (long long i = 0; i < n; i++) { u[i] = cost[i][0]; for (long long j = 1; j < n; j++) { u[i] = min(u[i], cost[i][j]); } } for (long long j = 0; j < n; j++) { v[j] = cost[0][j] - u[0]; for (long long i = 1; i < n; i++) { v[j] = min(v[j], cost[i][j] - u[i]); } } Lmate = vector<long long>(n, -1); Rmate = vector<long long>(n, -1); long long mated = 0; for (long long i = 0; i < n; i++) { for (long long j = 0; j < n; j++) { if (Rmate[j] != -1) { continue; } if (abs(cost[i][j] - u[i] - v[j]) == 0) { Lmate[i] = j; Rmate[j] = i; mated++; break; } } } vector<long long> dist(n); vector<long long> dad(n); vector<long long> seen(n); while (mated < n) { long long s = 0; while (Lmate[s] != -1) { s++; } fill(dad.begin(), dad.end(), -1); fill(seen.begin(), seen.end(), 0); for (long long k = 0; k < n; k++) { dist[k] = cost[s][k] - u[s] - v[k]; } long long j = 0; while (true) { j = -1; for (long long k = 0; k < n; k++) { if (seen[k]) { continue; } if (j == -1 || dist[k] < dist[j]) { j = k; } } seen[j] = 1; if (Rmate[j] == -1) { break; } const long long i = Rmate[j]; for (long long k = 0; k < n; k++) { if (seen[k]) { continue; } const long long new_dist = dist[j] + cost[i][k] - u[i] - v[k]; if (dist[k] > new_dist) { dist[k] = new_dist; dad[k] = j; } } } for (long long k = 0; k < n; k++) { if (k == j || !seen[k]) { continue; } const long long i = Rmate[k]; v[k] += dist[k] - dist[j]; u[i] -= dist[k] - dist[j]; } u[s] += dist[j]; while (dad[j] >= 0) { const long long d = dad[j]; Rmate[j] = Rmate[d]; Lmate[Rmate[j]] = j; j = d; } Rmate[j] = s; Lmate[s] = j; mated++; } long long value = 0; for (long long i = 0; i < n; i++) { value += cost[i][Lmate[i]]; } return value; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); long long t; cin >> t; while (t--) { long long n, k; cin >> n >> k; long long a[n + 5], b[n + 5]; for (long long i = 0; i < n; i++) { cin >> a[i] >> b[i]; } vector<vector<long long>> mat(n, vector<long long>(n, 0)); for (long long i = 0; i < (k - 1); i++) { for (long long j = 0; j < n; j++) { long long pft = a[j] + min(i, k - 1) * b[j]; mat[i][j] = -pft; } } for (long long i = k - 1; i < n - 1; i++) { for (long long j = 0; j < n; j++) { long long pft = min(i, k - 1) * b[j]; mat[i][j] = -pft; } } for (long long j = 0; j < n; j++) { long long i = n - 1; long long pft = a[j] + min(i, k - 1) * b[j]; mat[i][j] = -pft; } vector<long long> l, r; long long pft = -MinCostMatching(mat, l, r); cerr << "pft" << "=" << pft << "\n"; cout << k + 2 * (n - k) << "\n"; for (long long i = 0; i < k - 1; i++) { cout << l[i] + 1 << " "; } for (long long i = k - 1; i < n - 1; i++) { cout << l[i] + 1 << " "; cout << -l[i] - 1 << " "; } cout << l[n - 1] + 1 << " "; 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
cpp
#include <bits/stdc++.h> using namespace std; template <typename T> inline void read(T &x) { static char _c; static bool _f; x = 0; _f = 0; _c = getchar(); while (_c < '0' || '9' < _c) { if (_c == '-') _f = true; _c = getchar(); } while ('0' <= _c && _c <= '9') { x = (x << 1) + (x << 3) + (_c & 15); _c = getchar(); } if (_f) x = -x; } template <typename T, typename... Args> inline void read(T &x, Args &...args) { read(x); read(args...); } template <typename T> inline void Min(T &x, T y) { if (y < x) x = y; } template <typename T> inline void Max(T &x, T y) { if (x < y) x = y; } const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3f; const double pi = (double)acos(-1.0); const double eps = (double)1e-8; const int e5 = (int)1e5 + 5; const int MOD = (int)1000000007; template <typename T0, typename T1> inline void depair(T0 &x, T1 &y, pair<T0, T1> &p) { x = p.first, y = p.second; } inline int sig(double x) { return x < -eps ? -1 : eps < x; } long long fp(long long a, long long n, long long mod = MOD) { if (n < 0) a = fp(a, mod - 2, mod), n = -n; long long res = 1; for (; n; n >>= 1, a = a * a % mod) if (n & 1) res = res * a % mod; return res; } struct Mint { int x; Mint() { x = 0; } Mint(int _x) : x(_x) { if (x < 0 || x >= MOD) x = (x % MOD + MOD) % MOD; } Mint(long long _x) : x(_x) { if (x < 0 || x >= MOD) x = (x % MOD + MOD) % MOD; } Mint operator-() const { return Mint(MOD - x); } Mint operator+(const Mint &rhs) const { return Mint(x + rhs.x >= MOD ? x + rhs.x - MOD : x + rhs.x); } Mint operator-(const Mint &rhs) const { return Mint(x - rhs.x < 0 ? x - rhs.x + MOD : x - rhs.x); } Mint operator*(const Mint &rhs) const { return Mint((long long)x * rhs.x % MOD); } Mint operator/(const Mint &rhs) const { return Mint(x * fp(rhs.x, -1) % MOD); } Mint &operator+=(const Mint &rhs) { x += rhs.x; if (x >= MOD) x -= MOD; return *this; } Mint &operator*=(const Mint &rhs) { x = ((long long)x * rhs.x) % MOD; return *this; } bool operator==(const Mint &rhs) const { return x == rhs.x; } bool operator!=(const Mint &rhs) const { return x != rhs.x; } friend ostream &operator<<(ostream &out, const Mint &rhs) { return out << rhs.x; } friend istream &operator>>(istream &in, Mint &rhs) { return in >> rhs.x; } }; const int maxn = (int)2e5 + 20; const int maxm = (int)1e6 + 20; void work() { int n, k; cin >> n >> k; vector<tuple<int, int, int> > a(n); for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; a[i] = make_tuple(x, y, i); } sort(a.begin(), a.end(), [](tuple<int, int, int> lhs, tuple<int, int, int> rhs) { return get<1>(lhs) < get<1>(rhs); }); vector<vector<int> > dp(n + 1, vector<int>(k + 1, -INF)); dp[0][0] = 0; for (int i = 0; i < n; i++) { int x, y, id; tie(x, y, id) = a[i]; for (int j = 0; j <= k; j++) { Max(dp[i + 1][j], dp[i][j] + (k - 1) * y); if (j != k) Max(dp[i + 1][j + 1], dp[i][j] + x + j * y); } } vector<int> sta(n, -1); int ni = n, nj = k; int cnt = k; while (ni) { int x, y, id; tie(x, y, id) = a[ni - 1]; if (nj && dp[ni - 1][nj - 1] + (nj - 1) * y + x == dp[ni][nj]) { sta[id] = cnt--; nj--; } ni--; } vector<int> opt; for (int i = 1; i <= k; i++) { if (i == k) { for (int j = 0; j < n; j++) if (sta[j] == -1) opt.push_back(j + 1), opt.push_back(-j - 1); } for (int j = 0; j < n; j++) if (sta[j] == i) opt.push_back(j + 1); } cout << opt.size() << endl; for (int i = 0; i < opt.size(); i++) { cout << opt[i]; if (i != opt.size() - 1) cout << " "; } cout << endl; } int main(int argc, char **argv) { int tc = 1; read(tc); for (int ca = 1; ca <= tc; ca++) { 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
java
import java.io.*; import java.text.*; import java.util.*; import java.math.*; public class template { public static void main(String[] args) throws Exception { new template().run(); } public void run() throws Exception { FastScanner f = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int asdf = f.nextInt(); while(asdf-->0) { int n = f.nextInt(), k = f.nextInt(); Pair[] arr = new Pair[n]; for(int i = 0; i < n; i++) arr[i] = new Pair(f.nextInt(), f.nextInt(),i); Arrays.sort(arr); boolean[][] use = new boolean[n+1][k+1]; int[][] dp = new int[n+1][k+1]; for(int[] i : dp) Arrays.fill(i, -2147483648); dp[0][0] = 0; for(int i = 0; i < n; i++) for(int j = 0; j <= k; j++) { if(dp[i+1][j] < dp[i][j]+(k-1)*(arr[i].b)) { dp[i+1][j] = dp[i][j]+(k-1)*(arr[i].b); use[i+1][j] = false; } if(j != k && dp[i+1][j+1] < dp[i][j]+j*arr[i].b+arr[i].a) { dp[i+1][j+1] = dp[i][j]+j*arr[i].b+arr[i].a; use[i+1][j+1] = true; } } int j = k; boolean[] del = new boolean[n]; for(int i = n; i > 0; i--) { if(use[i][j]) j--; else del[i-1] = true; } out.println(2*n-k); int last = -1; for(int i = 0; i < n; i++) if(!del[i]) { if(last != -1) out.print(last + " "); last = arr[i].i+1; } for(int i = 0; i < n; i++) if(del[i]) out.print(arr[i].i+1+" " + -(arr[i].i+1) + " "); out.println(last); } out.flush(); } class Pair implements Comparable<Pair> { int a, b, i; public Pair(int a, int b, int i) { this.a = a; this.b = b; this.i = i; } public int compareTo(Pair p) { return Integer.compare(b, p.b); } public String toString() { return a + "," + b; } } static class FastScanner { public BufferedReader reader; public StringTokenizer tokenizer; public FastScanner() { reader = new BufferedReader(new InputStreamReader(System.in), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { try { return reader.readLine(); } catch(IOException e) { throw new RuntimeException(e); } } } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize("Ofast, unroll-loops", "omit-frame-pointer", "inline") #pragma GCC option("arch=native", "tune=native", "no-zero-upper") #pragma GCC target( \ "sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native,avx2") using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int getrnd(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); } ll getrndll(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rng); } template <typename T1, typename T2> inline bool relax(T1& a, const T2& b) { if (a > b) { a = b; return 1; } return 0; } template <typename T1, typename T2> inline bool strain(T1& a, const T2& b) { if (a < b) { a = b; return 1; } return 0; } void solve() { int n, k; cin >> n >> k; vector<pair<int, pair<int, int>>> a(n); for (int i = 0; i < n; ++i) cin >> a[i].second.first >> a[i].first, a[i].second.second = i; sort(a.begin(), a.end()); vector<int> dp(k + 1, -1e9); vector<vector<int>> from(n, vector<int>(k + 1)); dp[0] = 0; for (int i = 0; i < n; ++i) { vector<int> ndp(k + 1, -1e9); for (int j = 0; j <= min(i, k); ++j) { if (strain(ndp[j], dp[j] + (k - 1) * a[i].first)) from[i][j] = 0; if (j + 1 <= k && strain(ndp[j + 1], dp[j] + a[i].second.first + a[i].first * j)) from[i][j + 1] = 1; } dp.swap(ndp); } int cur = k; vector<int> have; for (int i = n - 1; i >= 0; --i) { if (from[i][cur]) { have.push_back(a[i].second.second); --cur; } } assert(cur == 0); reverse(have.begin(), have.end()); vector<int> ans; for (int i = 0; i < k - 1; ++i) ans.push_back(have[i] + 1); for (int i = 0; i < n; ++i) if (count(have.begin(), have.end(), i) == 0) ans.push_back(i + 1), ans.push_back(-(i + 1)); ans.push_back(have.back() + 1); cout << ans.size() << '\n'; for (int x : ans) cout << x << ' '; cout << '\n'; } int main() { ios::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr); srand(time(0)); int t = 1; 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; template <typename T> void __read(T &a) { cin >> a; } template <typename T, typename... Args> void __read(T &a, Args &...args) { cin >> a; __read(args...); } constexpr long long M7 = 1000000007ll; constexpr long long M9 = 1000000009ll; constexpr long long MFFT = 998244353ll; template <class T> void outv(T &a) { for (auto &x : a) cout << x << ' '; } static mt19937 rnd(static_cast<unsigned>( chrono::steady_clock::now().time_since_epoch().count())); auto __fast_io__ = (ios_base::sync_with_stdio(false), cin.tie(nullptr)); int32_t main() { int t; __read(t); while (t--) { int n, k; __read(n, k); vector<pair<int, int>> a(n); for (auto &[l, r] : a) { cin >> l >> r; } vector<int> ind(n); iota((ind).begin(), (ind).end(), 0); sort((ind).begin(), (ind).end(), [&](int i, int j) { return a[i].second < a[j].second; }); vector<vector<int>> dp(n + 1, vector<int>(k + 1, -1)); vector<vector<int>> pr(n + 1, vector<int>(k + 1, -1)); dp[0][0] = 0; for (int i = 0; i < n; ++i) { auto &[l, r] = a[ind[i]]; for (int j = 0; j <= k; ++j) { if (dp[i][j] == -1) { continue; } if (j + 1 <= k) { if (dp[i + 1][j + 1] < dp[i][j] + l + r * j) { dp[i + 1][j + 1] = dp[i][j] + l + r * j; pr[i + 1][j + 1] = j; } } if (dp[i + 1][j] < dp[i][j] + r * (k - 1)) { dp[i + 1][j] = dp[i][j] + r * (k - 1); pr[i + 1][j] = j; } } } vector<int> g, b; int j = k; for (int i = n - 1; i >= 0; --i) { if (pr[i + 1][j] == j) { b.push_back(ind[i]); } else { g.push_back(ind[i]); } j = pr[i + 1][j]; } reverse((g).begin(), (g).end()); reverse((b).begin(), (b).end()); cout << b.size() * 2 + g.size() << '\n'; for (int i = 0; i + 1 < g.size(); ++i) { cout << g[i] + 1 << ' '; } for (auto &i : b) { cout << i + 1 << ' ' << -(i + 1) << ' '; } cout << g.back() + 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; using uint = unsigned int; using ll = long long; using ld = long double; using ull = unsigned long long; using pii = pair<int, int>; using pll = pair<ll, ll>; void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...); } template <typename T1, typename T2> ostream &operator<<(ostream &out, const pair<T1, T2> &item) { out << '(' << item.first << ", " << item.second << ')'; return out; } template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) { for (const auto &item : v) out << item << ' '; return out; } const int N = 80; struct Minion { int id, a, b; bool operator<(const Minion &o) const { return b < o.b; } }; ll s[N]; bool ok[N]; int prv[N][N]; ll dp[N][N]; Minion v[N]; int main() { ios_base::sync_with_stdio(false); int t, n, k; for (cin >> t; t; --t) { cin >> n >> k; for (int i = 1; i <= n; ++i) cin >> v[i].a >> v[i].b, v[i].id = i; sort(v + 1, v + n + 1); for (int i = 1; i <= n; ++i) s[i] = s[i - 1] + v[i].b; memset(dp, -1, sizeof dp); dp[0][0] = 0; int lst = -1; ll ans = 0; for (int i = 1; i <= n; ++i) for (int nr = 1; nr <= i && nr <= k; ++nr) { for (int j = 0; j < i; ++j) { if (dp[j][nr - 1] < 0) continue; ll val = dp[j][nr - 1] + v[i].a + 1LL * v[i].b * (nr - 1) + 1LL * (s[i - 1] - s[j]) * (k - 1); if (val > dp[i][nr]) dp[i][nr] = val, prv[i][nr] = j; } if (dp[i][k] >= 0) { ll val = dp[i][k] + 1LL * (s[n] - s[i]) * (k - 1); if (val > ans) ans = val, lst = i; } } memset(ok, 0, sizeof ok); for (int i = lst, nr = k; nr; i = prv[i][nr], --nr) ok[i] = true; cout << 2 * n - k << '\n'; for (int i = 1, nr = 0; nr < k - 1; ++i) if (ok[i]) cout << v[i].id << ' ', ++nr; for (int i = 1; i <= n; ++i) if (!ok[i]) cout << v[i].id << ' ' << -v[i].id << ' '; cout << v[lst].id << '\n'; } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 2e9; int g[80][80]; int n, k, a[80], b[80]; int job[80]; int u[80], v[80]; bool used[80]; int dist[80]; int par[80]; void hungarian() { for (int i = 1; i <= n; i++) { job[0] = i; int j0 = 0; for (int l = 0; l < 80; l++) { used[l] = 0; dist[l] = INF; par[l] = 79; } while (job[j0]) { used[j0] = 1; int i0 = job[j0], d = INF, j1 = 79; for (int j = 1; j <= n; j++) { if (used[j]) continue; int w = g[i0][j] - u[i0] - v[j]; if (w < dist[j]) { dist[j] = w; par[j] = j0; } if (dist[j] < d) { d = dist[j]; j1 = j; } } for (int j = 0; j <= n; j++) { if (used[j]) { u[job[j]] += d; v[j] -= d; } else dist[j] -= d; } j0 = j1; } while (j0) { int j1 = par[j0]; job[j0] = job[j1]; j0 = j1; } } } void solve() { memset(g, 0, sizeof(g)); memset(u, 0, sizeof(u)); memset(v, 0, sizeof(v)); memset(job, 0, sizeof(job)); cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> a[i] >> b[i]; } for (int i = 1; i <= n; i++) { g[1][i] = a[i] + b[i] * (k - 1); for (int j = 2; j <= k; j++) { g[j][i] = a[i] + (j - 2) * b[i]; } for (int j = k + 1; j <= n; j++) { g[j][i] = b[i] * (k - 1); } } for (int i1 = 1; i1 <= n; i1++) for (int i2 = 1; i2 <= n; i2++) { g[i1][i2] = -g[i1][i2]; } hungarian(); vector<int> ans; for (int idx = 2; idx <= k; idx++) { for (int i = 1; i <= n; i++) { if (job[i] == idx) ans.push_back(i); } } for (int idx = k + 1; idx <= n; idx++) { for (int i = 1; i <= n; i++) { if (job[i] == idx) { ans.push_back(i); ans.push_back(-i); } } } for (int i = 1; i <= n; i++) { if (job[i] == 1) { ans.push_back(i); } } cout << ans.size() << endl; for (auto xd : ans) cout << xd << " "; cout << endl; } int main() { int t; cin >> t; while (t--) solve(); return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
python3
T=int(input()) for t in range(T): n,k=map(int,input().split()) dict=[] for i in range(n): temp=list(map(int,input().split())) dict.append((i+1,temp[0],temp[1])) lst=sorted(dict,key=lambda var:var[1],reverse=True) dict=sorted(dict,key=lambda var:var[2],reverse=True) if k==1: print(1) print(lst[0][0]) elif k==n: print(n) temp=[str(var[0]) for var in lst] print(" ".join(temp)) else: moves=[] i=1 moves.append(str(lst[0][0])) lst.remove(lst[0]) while i<k: moves.append(str(dict[0][0])) dict.remove(dict[0]) i+=1 moves.append("-{}".format(moves[-1])) moves.append(str(dict[0][0])) print(len(moves)) print(" ".join(moves))
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAX = 80; int f[MAX][MAX]; bool take[MAX][MAX]; void run_case() { for (int i = 0; i < MAX; i++) { for (int j = 0; j < MAX; j++) { f[i][j] = -1000000000; take[i][j] = false; } } int n, k; cin >> n >> k; struct item { int first, second, ind; }; vector<item> a(n); for (int i = 0; i < n; i++) { cin >> a[i].first >> a[i].second; a[i].ind = i; } sort(a.begin(), a.end(), [&](const item& w, const item& v) { return w.second < v.second; }); f[n][k] = 0; for (int i = n - 1; i >= 0; i--) { for (int j = 0; j <= k; j++) { f[i][j] = f[i + 1][j] + a[i].second * (k - 1); int cost = f[i + 1][j + 1] + a[i].first + a[i].second * j; if (j < k && cost > f[i][j]) { f[i][j] = cost; take[i][j] = true; } } } cout << k + 2 * (n - k) << '\n'; int j = 0; vector<int> taken, shrek; for (int i = 0; i < n; i++) { if (take[i][j]) { taken.push_back(i); j++; } else { shrek.push_back(i); } } assert(taken.size() == k); for (int i = 0; i + 1 < k; i++) cout << a[taken[i]].ind + 1 << ' '; for (int i = 0; i < n - k; i++) cout << a[shrek[i]].ind + 1 << ' ' << -a[shrek[i]].ind - 1 << ' '; cout << a[k - 1].ind + 1 << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) run_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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct EdmondsKarp { struct edge { int from, to, f, cap, cost; }; vector<vector<long long> > G; vector<edge> E; int source, sink; const int inf = 1e9; EdmondsKarp(int s, int t) : source(s), sink(t), G(t + 1){}; void add_edge(int from, int to, int cap, int cost) { G[from].push_back(E.size()); E.push_back({from, to, cap, cap, -cost}); G[to].push_back(E.size()); E.push_back({to, from, 0, cap, cost}); } int solve() { auto bfs = [&]() { vector<int> dad(sink + 1, -1), e(sink + 1); vector<int> cost(sink + 1, 1e9); dad[source] = -2; cost[source] = 0; queue<int> q; q.push(source); while (!q.empty()) { int v = q.front(); q.pop(); if (v == sink) continue; for (int i = 0; i < G[v].size(); ++i) { int idx = G[v][i]; int next = E[idx].to; if (E[idx].f) { if (cost[v] + E[idx].cost < cost[next]) { if (dad[next] == -1) q.push(next); dad[next] = v; e[next] = idx; cost[next] = cost[v] + E[idx].cost; } } } } if (dad[sink] == -1) return 0; int cur = sink, flow = inf; while (cur != source) { int idx = e[cur]; flow = min(flow, E[idx].f); cur = dad[cur]; } cur = sink; while (cur != source) { int idx = e[cur]; E[idx].f -= flow; E[idx ^ 1].f += flow; cur = dad[cur]; } return flow; }; int flow = 0, f; while (f = bfs()) flow += f; return flow; } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t, n, m; cin >> t; while (t--) { cin >> n >> m; vector<int> data(n), cost(n); for (int i = 0; i < n; ++i) cin >> data[i] >> cost[i]; int s = 0, t = 2 * n + 1; EdmondsKarp flow(s, t); for (int i = 1; i <= n; ++i) { flow.add_edge(s, i, 1, 0); flow.add_edge(i + n, t, 1, 0); for (int j = 1; j < m; ++j) flow.add_edge(i, j + n, 1, data[i - 1] + cost[i - 1] * (j - 1)); for (int j = m; j < n; ++j) flow.add_edge(i, j + n, 1, cost[i - 1] * (m - 1)); flow.add_edge(i, n + n, 1, data[i - 1] + cost[i - 1] * (m - 1)); } flow.solve(); vector<int> pos(n + 1); for (auto &e : flow.E) { if (e.from <= n && e.to > n && !e.f) pos[e.to - n] = e.from; } cout << m + (n - m) * 2 << "\n"; for (int i = 1; i < m; ++i) cout << pos[i] << " "; for (int i = m; i < n; ++i) cout << pos[i] << " " << -pos[i] << " "; cout << pos[n] << "\n"; } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1); const double eps = 1e-12; const int inf = 2000000000; const long long int infLL = (long long int)1e18; long long int MOD = 1000000007; int MOD1 = 1000000007; int MOD2 = 1000000009; inline bool checkBit(long long int n, long long int i) { return n & (1LL << i); } inline long long int setBit(long long int n, long long int i) { return n | (1LL << i); ; } inline long long int resetBit(long long int n, long long int i) { return n & (~(1LL << i)); } int dx[] = {0, 0, +1, -1}; int dy[] = {+1, -1, 0, 0}; inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } inline bool isLeapYear(long long int year) { return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); } inline void normal(long long int &a) { a %= MOD; (a < 0) && (a += MOD); } inline long long int modMul(long long int a, long long int b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a * b) % MOD; } inline long long int modAdd(long long int a, long long int b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a + b) % MOD; } inline long long int modSub(long long int a, long long int b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; } inline long long int modPow(long long int b, long long int p) { long long int r = 1LL; while (p) { if (p & 1) r = modMul(r, b); b = modMul(b, b); p >>= 1LL; } return r; } inline long long int modDiv(long long int a, long long int b) { return modMul(a, modPow(b, MOD - 2)); } bool comp(const pair<long long int, pair<long long int, long long int> > &p1, const pair<long long int, pair<long long int, long long int> > &p2) { return p1.first > p2.first; } bool comp1(const pair<long long int, long long int> &p1, const pair<long long int, long long int> &p2) { if (p1.first == p2.first) { return p1.second > p2.second; } return p1.first < p2.first; } long long int converter(string a) { long long int i, mul = 1LL, r, t, ans = 0LL; if (a.length() == 0) return 0; for (i = a.length() - 1; i >= 0; i--) { t = a[i] - '0'; r = t % 10; ans += (mul * r); mul = mul * 10; } return ans; } int msb(unsigned x) { union { double a; int b[2]; }; a = x; return (b[1] >> 20) - 1023; } const int MAX = 78; long long int t, n, k; pair<pair<long long int, long long int>, long long int> p[MAX]; long long int dp[MAX][MAX]; vector<long long int> good, bad; long long int solve(long long int idx, long long int cnt) { if (idx == n + 1) { if (cnt != k) return -infLL; return 0; } if (dp[idx][cnt] != -1) return dp[idx][cnt]; long long int ret = 0; if (cnt + 1 <= k) { ret = max(ret, cnt * p[idx].first.first + p[idx].first.second + solve(idx + 1, cnt + 1)); } ret = max(ret, ((k - 1) * p[idx].first.first) + solve(idx + 1, cnt)); return dp[idx][cnt] = ret; } void trace(long long int idx, long long int cnt) { if (idx == n + 1) return; long long int ret1 = 0LL, ret2 = 0LL; if (cnt + 1 <= k) ret1 = cnt * p[idx].first.first + p[idx].first.second + solve(idx + 1, cnt + 1); ret2 = ((k - 1) * p[idx].first.first) + solve(idx + 1, cnt); if (ret1 > ret2) { trace(idx + 1, cnt + 1); good.push_back(p[idx].second); } else { trace(idx + 1, cnt); bad.push_back(p[idx].second); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cout.unsetf(ios::floatfield); cout.precision(20); cout.setf(ios::fixed, ios::floatfield); ; cin >> t; while (t--) { good.clear(); bad.clear(); long long int maxi = 0, idx; cin >> n >> k; for (int i = 1; i <= n; ++i) { cin >> p[i].first.second >> p[i].first.first; p[i].second = i; if (p[i].first.second >= maxi) maxi = p[i].first.second, idx = i; } if (k == 1) { cout << 1 << '\n'; cout << idx << '\n'; continue; } if (k == n) { cout << n << '\n'; for (int i = 1; i <= n; ++i) { cout << p[i].second << " "; } cout << '\n'; continue; } sort(p + 1, p + n + 1); memset(dp, -1, sizeof(dp)); long long int now = solve(1, 0); ; trace(1, 0); reverse((good).begin(), (good).end()); reverse((bad).begin(), (bad).end()); cout << good.size() + (2 * bad.size()) << '\n'; for (int i = 0; i < (int)good.size() - 1; ++i) { cout << good[i] << " "; } for (int i = 0; i < bad.size(); ++i) { cout << bad[i] << " " << -1 * bad[i] << " "; } cout << good[good.size() - 1] << '\n'; } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int inf = 1e09 + 5e3; const long long linf = 2e18 + 5e3; const int mxn = 1e5; void solve() { int n, k; cin >> n >> k; vector<pair<int, int> > vp; int temp, temp2; long long total = 0; for (int i = 0; i < n; i++) { cin >> temp >> temp2; vp.emplace_back(temp, temp2); total += temp2 * (k - 1); } set<int> na; vector<int> out; for (int i = 0; i < k; i++) { long long best = -linf, pos = -1; for (int j = 0; j < n; j++) { if (na.count(j)) continue; if ((vp[j].first - vp[j].second * i) > best) { best = (vp[j].first - vp[j].second * i); pos = j; } } total += best; na.insert(pos); out.push_back(pos); } sort((out).begin(), (out).end(), [vp](const int &lhs, const int &rhs) { return vp[lhs].second < vp[rhs].second; }); cout << 2 * n - k << '\n'; for (int i = 0; i < (int)out.size() - 1; i++) { cout << out[i] + 1 << ' '; } for (int i = 0; i < n; i++) { if (!na.count(i)) cout << i + 1 << ' ' << -i - 1 << ' '; } cout << out.back() + 1; cout << '\n'; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t; cin >> t; while (t--) { solve(); } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int test, n, k, a[88], b[88], path[88][88], x, y, ap[88], ans[888], cnt, sz, nt; vector<int> v; long long dp[88][88], mx, cc; pair<pair<int, int>, int> arr[88]; int main() { scanf("%d", &test); while (test--) { scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) { scanf("%d%d", &a[i], &b[i]); arr[i] = make_pair(make_pair(b[i], a[i]), i); } sort(arr + 1, arr + n + 1); mx = -1e18; for (int t = 1; t <= n; t++) { for (int i = 0; i <= n; i++) { for (int j = 0; j <= k; j++) dp[i][j] = -1e18; } dp[0][0] = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < k; j++) { if (i + 1 == t) { dp[i + 1][j] = dp[i][j]; continue; } if (dp[i + 1][j] < dp[i][j] + 1ll * j * arr[i + 1].first.first) { dp[i + 1][j] = dp[i][j] + 1ll * j * arr[i + 1].first.first; path[i + 1][j] = 0; } if (dp[i + 1][j + 1] < dp[i][j] + 1ll * j * arr[i + 1].first.first + arr[i + 1].first.second) { dp[i + 1][j + 1] = dp[i][j] + 1ll * j * arr[i + 1].first.first + arr[i + 1].first.second; path[i + 1][j + 1] = 1; } } } cc = dp[n][k - 1] + 1ll * (k - 1) * arr[t].first.first + arr[t].first.second; if (cc > mx) { mx = cc; nt = t; } } memset(path, 0, sizeof(path)); memset(ap, 0, sizeof(ap)); cnt = 0; for (int i = 0; i <= n; i++) { for (int j = 0; j <= k; j++) dp[i][j] = -1e18; } dp[0][0] = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < k; j++) { if (i + 1 == nt) { dp[i + 1][j] = dp[i][j]; continue; } if (dp[i + 1][j] < dp[i][j] + 1ll * j * arr[i + 1].first.first) { dp[i + 1][j] = dp[i][j] + 1ll * j * arr[i + 1].first.first; path[i + 1][j] = 0; } if (dp[i + 1][j + 1] < dp[i][j] + 1ll * j * arr[i + 1].first.first + arr[i + 1].first.second) { dp[i + 1][j + 1] = dp[i][j] + 1ll * j * arr[i + 1].first.first + arr[i + 1].first.second; path[i + 1][j + 1] = 1; } } } x = n; y = k - 1; while (x >= 1) { if (x == nt) { x--; continue; } ap[x] = y; y -= path[x][y]; x--; } cnt = 0; y = 0; v.clear(); sz = 0; for (int i = 1; i <= n; i++) { if (i == nt) continue; if (ap[i] == y + 1) { if (sz < k) { ans[++cnt] = arr[i].second; sz++; } else { ans[++cnt] = -v.back(); v.pop_back(); ans[++cnt] = arr[i].second; } } else { if (sz < k) { ans[++cnt] = arr[i].second; sz++; v.push_back(arr[i].second); } else { ans[++cnt] = -v.back(); v.pop_back(); ans[++cnt] = arr[i].second; v.push_back(arr[i].second); } } y = ap[i]; } for (int i = 0; i < v.size(); i++) { ans[++cnt] = -v[i]; } ans[++cnt] = arr[nt].second; printf("%d\n", cnt); for (int i = 1; i <= cnt; i++) printf("%d ", ans[i]); 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 333333, M = 12222222; long long MOD = 1e9 + 7; int intINF = int(1e9); long long llINF = long long(2e18); double eps = 1e-8; int dx[4] = {-1, 1, 0, 0}; int dy[4] = {0, 0, -1, 1}; long double pi = acos(-1.0L); long double s2 = sqrt(2.0); int read() { int v = 0, f = 1; char c = getchar(); while (c < 48 || 57 < c) { if (c == '-') f = -1; c = getchar(); } while (48 <= c && c <= 57) v = (v << 3) + v + v + c - 48, c = getchar(); return v * f; } long long readll() { long long v = 0, f = 1; char c = getchar(); while (c < 48 || 57 < c) { if (c == '-') f = -1; c = getchar(); } while (48 <= c && c <= 57) v = (v << 3) + v + v + c - 48, c = getchar(); return v * f; } inline char nc() { static char buf[100000], *p1, *p2; return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++; } int rd() { int x = 0; char ch = nc(); while (ch < '0' || ch > '9') ch = nc(); while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = nc(); return x; } struct node { long long a, b; int id; } a[N]; struct record { int x, y, z; } pre[111][111]; long long dp[111][111]; int ans[111], n, K; vector<int> c; bool cmp(node a, node b) { return a.b < b.b; } void init() { n = read(), K = read(); for (int i = (int)1; i <= (int)n; i++) a[i].a = readll(), a[i].b = readll(), a[i].id = i; } void solve() { sort(a + 1, a + n + 1, cmp); memset(dp, 0, sizeof(dp)); memset(pre, 0, sizeof(pre)); memset(ans, 0, sizeof(ans)); for (int i = (int)1; i <= (int)n; i++) { for (int j = (int)0; j <= (int)i; j++) { if (j > K) continue; int k = i - j; if (k < 0 || k > n - K || k > i) continue; if (j - 1 >= 0) { long long t = dp[j - 1][k] + a[i].a + (j - 1) * a[i].b; if (t > dp[j][k]) { dp[j][k] = t; pre[j][k] = (record){j - 1, k, 0}; } } if (k - 1 >= 0) { long long t = dp[j][k - 1] + a[i].b * (K - 1); if (t > dp[j][k]) { dp[j][k] = t; pre[j][k] = (record){j, k - 1, 1}; } } } } printf("ans=%I64d\n", dp[K][n - K]); int i = K, j = n - K; while (i + j) { record tmp = pre[i][j]; ans[i + j] = tmp.z; i = tmp.x, j = tmp.y; } c.clear(); int s = 0; for (int i = (int)1; i <= (int)n; i++) if (!ans[i] && s + 1 <= K - 1) { c.push_back(a[i].id), s++; } for (int i = (int)1; i <= (int)n; i++) if (ans[i]) { c.push_back(a[i].id), c.push_back(-a[i].id); } for (int i = (int)n; i >= (int)1; i--) if (!ans[i]) { c.push_back(a[i].id); break; } printf("%d\n", (int)c.size()); for (auto t : c) printf("%d ", t); printf("\n"); } int main() { int cas = read(); while (cas--) { 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update using namespace std; using namespace __gnu_pbds; //typedef tree< // pair<long long, int>, // change type // null_type, // less<pair<long long, int> >, // change type // rb_tree_tag, // tree_order_statistics_node_update> // ordered_set; template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; vector<string> vec_splitter(string s) { s += ','; vector<string> res; while(!s.empty()) { res.push_back(s.substr(0, s.find(','))); s = s.substr(s.find(',') + 1); } return res; } void debug_out( vector<string> __attribute__ ((unused)) args, __attribute__ ((unused)) int idx, __attribute__ ((unused)) int LINE_NUM) { cerr << endl; } template <typename Head, typename... Tail> void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) { if(idx > 0) cerr << ", "; else cerr << "Line(" << LINE_NUM << ") "; stringstream ss; ss << H; cerr << args[idx] << " = " << ss.str(); debug_out(args, idx + 1, LINE_NUM, T...); } #define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__) typedef long long ll; #define rep(i, start, end) for(int i = start; i < end; ++i) #define sz(x) (int)((x).size()) #define pb push_back #define X first #define Y second #define all(x) x.begin(), x.end() #define clr(d, v) memset(d, v, sizeof(d)) #define pii pair<int, int> //#define debug(x) cerr << #x << " : " << (x) << " " const double PI = 3.14159265358979323846; const double eps = (1e-10); int isGreater(long double x, long double y) { if (abs(x - y) < eps) return 0; if (x > y) return 1; return -1; } const int N = 80; struct S{ int a, b, idx; bool operator<(const S& other) const{ return b < other.b; } }; S v[N]; int n, k; vector<int> o, d; int lst; ll mem[N][N][2]; ll solve(int i, int c, int l) { if (i == n){ if (c == k - 1 && l) return 0; return -1e17; } ll &ret = mem[i][c][l]; if (ret != -1) return -1; ret = solve(i +1, c, l) + (k - 1) * v[i].b; if (c < k - 1) ret = max(ret, solve(i + 1, c + 1, l) + v[i].a + c * v[i].b); if (!l) ret = max(ret, solve(i + 1, c, true) + v[i].a + v[i].b * (k - 1)); return ret; } void build(int i, int c, int l) { if (i == n){ return; } ll &ret = mem[i][c][l]; if (ret == solve(i +1, c, l) + (k - 1) * v[i].b) { d.pb(v[i].idx); build(i + 1, c, l); return; } if (c < k - 1) { if (ret == solve(i + 1, c + 1, l) + v[i].a + c * v[i].b){ o.pb(v[i].idx); build(i + 1, c + 1, l); return; } } lst = v[i].idx; build(i + 1, c, true); } void SOLVE(){ cin >> n >> k; rep(i,0,n){ cin >> v[i].a >> v[i].b; v[i].idx = i; } sort(v, v + n); clr(mem, -1); d.clear(); o.clear(); solve(0, 0, 0); build(0 ,0 , 0); cout << sz(o) + sz(d) * 2 + 1 << '\n'; for (int item : o) cout << item << " "; for (int item : d) cout << item << " " << -item << " "; cout << lst << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); // freopen("AC.txt", "w", stdout); #endif int tc; cin >> tc; while(tc--) SOLVE(); return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; void Freopen() { freopen( "title" ".in", "r", stdin); freopen( "title" ".out", "w", stdout); } int read() { int g = 0, f = 1; char ch = getchar(); while (ch < '0' || '9' < ch) { if (ch == '-') f = -1; ch = getchar(); } while ('0' <= ch && ch <= '9') { g = g * 10 + ch - '0'; ch = getchar(); } return g * f; } const int N = 80; const int INF = 1e9; struct node { int a, b, id; } p[N]; int f[N][N], n, m, from[N][N], ans[N], vis[N]; void solve() { n = read(), m = read(); for (int i = (1); i <= (n); 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 = (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 = (1); j <= (m); j++) { if (f[i - 1][j] + (m - 1) * p[i].b > f[i][j]) f[i][j] = f[i - 1][j] + (m - 1) * p[i].b, from[i][j] = j; if (f[i - 1][j - 1] + (j - 1) * p[i].b + p[i].a > f[i][j]) f[i][j] = f[i - 1][j - 1] + (j - 1) * p[i].b + p[i].a, from[i][j] = j - 1; } int now = m, cnt = 0; memset(vis, 0, sizeof(vis)); for (int i = (n); i >= (1); i--) if (from[i][now] == now - 1) ans[++cnt] = p[i].id, now--, vis[p[i].id] = 1; for (int i = (cnt); i >= (2); i--) cout << ans[i] << ' '; for (int i = (1); i <= (n); i++) if (!vis[i]) cout << i << " -" << i << ' '; cout << ans[1] << '\n'; } signed main() { for (int T = read(); T--;) solve(); return signed(); }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; void read(long long &x) { char ch = getchar(); x = 0; while (!isdigit(ch)) ch = getchar(); while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar(); } const long long N = 77; long long n, k, f[N][N], g[N][N]; pair<long long, pair<long long, long long> > a[N]; vector<long long> res, ans; signed main() { long long T; read(T); while (T--) { read(n), read(k); k = min(k, n); for (long long i = 1; i <= n; ++i) read(a[i].second.first), read(a[i].first), a[i].second.second = i; if (k == 1) { puts("1"); long long w = 0; for (long long i = 1; i <= n; ++i) if (a[i].second.first > a[w].second.first) w = i; printf("%lld\n", w); } sort(a + 1, a + n + 1); memset(f, 0xcf, sizeof(f)); f[0][0] = 0; for (long long i = 1; i <= n; ++i) for (long long j = 0; j <= i && j <= k; ++j) { if (j && f[i][j] < f[i - 1][j - 1] + a[i].second.first + a[i].first * (k - 1)) f[i][j] = f[i - 1][j - 1] + a[i].second.first + a[i].first * (j - 1), g[i][j] = 1; if (f[i][j] < f[i - 1][j] + a[i].first * (k - 1)) f[i][j] = f[i - 1][j] + a[i].first * (k - 1), g[i][j] = 0; } res.clear(); ans.clear(); for (long long i = n, j = k; i; --i) { if (g[i][j]) res.push_back(a[i].second.second), --j; else ans.push_back(a[i].second.second); } reverse(res.begin(), res.end()); printf("%lld\n", res.size() + ans.size() * 2); for (long long i = 0; i < k - 1; ++i) printf("%lld ", res[i]); for (long long i : ans) printf("%lld %lld ", i, -i); printf("%lld\n", res[k - 1]); } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; #define int long long #define S second #define F first #define pb push_back #define all(c) (c).begin(),(c).end() #define rall(c) (c).rbegin(),(c).rend() #define lb lower_bound #define si(c) (int)((c).size()) #define lcm(a, b) (a * (b / __gcd(a,b))) #define inf (int)(2e9) #define endl '\n' #define mp make_pair #define time(s) (double(clock()-s)/double(CLOCKS_PER_SEC)) #define debug(args...) _F(#args, args) #define vi std::vector<int> #define pii pair<int, int> #define vpi vector<pii > clock_t start; mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count()); template<typename T> void _F(const char *name, T arg1){ cerr << name << " = " << arg1 << endl; } template<typename T, typename... Args> void _F(const char *names, T arg1, Args... args){ const char *name = strchr(names, ',');cerr.write(names, name-names) << " = " << arg1 << endl;_F(name+2, args...); } template<typename T1, typename T2> istream& operator>>(istream& in, pair<T1, T2> &q){ in >> q.F >> q.S; return in; } template<typename T1, typename T2> ostream& operator<<(ostream& out, pair<T1, T2> &q){ out << q.F << " " << q.S; return out; } template< typename T1, typename T2> pair<T1, T2> operator+(pair<T1, T2> p1, pair<T1, T2> p2){ return {p1.F+p2.F, p1.S+p2.S}; } template< typename T1, typename T2> pair<T1, T2> operator-(pair<T1, T2> p1, pair<T1, T2> p2){ return {p1.F-p2.F, p1.S-p2.S}; } const int N = 75+1; int dp[N][N]; int n, k; vi ans1, ans2; struct Query{ int a, b, id; bool operator <(Query other){ return mp(b, a) < mp(other.b, other.a); } }a[N]; void PrintSolution(int n, int k){ if(n == 0)return; if(dp[n][k] == dp[n-1][k-1]+a[n].a+(k-1)*a[n].b){ PrintSolution(n-1, k-1); ans1.pb(a[n].id); } else { PrintSolution(n-1, k); ans2.pb(a[n].id); } } void solve() { cin >> n >> k; for(int i = 1; i <= n; i++){ cin >> a[i].a >> a[i].b; a[i].id = i; } sort(a+1, a+n+1); memset(dp, 0, sizeof(dp)); for(int j = 1; j <= k; j++) dp[0][j] = -inf; for(int i = 1; i <= n; i++){ for(int j = 0; j <= k; j++){ dp[i][j] = dp[i-1][j]+(k-1)*a[i].b; if(j)dp[i][j] = max(dp[i][j], dp[i-1][j-1]+a[i].a+(j-1)*a[i].b); } } cout << k+(n-k)*2 << endl; // cout << dp[n][k] << endl; PrintSolution(n, k); for(int i = 0; i < si(ans1)-1; i++){ cout << ans1[i] << " "; } for(int i = 0; i < si(ans2); i++){ cout << ans2[i] << " "; cout << -ans2[i] << " "; } cout << ans1.back() << endl; ans1.clear(); ans2.clear(); } signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; start = clock(); cout << fixed << setprecision(20); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); freopen("error.txt", "w", stderr); #endif cin >> t; for(int i = 1; i <= t; ++i){ //cout << "Case #" << i << ": "; solve(); //cout << endl; } //cerr << time(start); return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
python3
def m(e): return e['m'] def h(e): return e['h'] a = int(input()) for i in range(a): num = [int(x) for x in input().split()] mi = [] for j in range(num[0]): a = [int(x) for x in input().split()] k = {} k['m'] = a[0] k['h'] = a[1] k['i'] = j+1 mi.append(k) mi.sort(key=m,reverse = True) cho = mi nch = mi cho = mi[:num[1]] nch = mi[num[1]:] last = 0 for element in cho[:num[1]-1]: print(element['i'],end = " ") #print() #print(nch) for element in nch: print(element['i'],end = " ") print(-1*element['i'],end = " ") print(cho[num[1]-1]['i'],end = " ")
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
python3
def read_int(): return int(input()) def read_ints(): return map(int, input().split(' ')) t = read_int() for case_num in range(t): n, k = read_ints() p = [] for i in range(n): ai, bi = read_ints() p.append((bi, ai, i + 1)) p.sort() dp = [[0 for j in range(k + 1)] for i in range(n + 1)] use = [[False for j in range(k + 1)] for i in range(n + 1)] for i in range(1, n + 1): for j in range(min(i, k) + 1): 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): if not i in st: seq.append(i) seq.append(-i) seq.append(used[-1]) print(len(seq)) print(' '.join(map(str, seq)))
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int a[75]; int b[75]; int order[75]; int main() { cin.tie(0); ios_base::sync_with_stdio(0); int t; cin >> t; while (t--) { int n, k; cin >> n >> k; for (int i = 0; i < n; ++i) cin >> a[i] >> b[i], order[i] = i; sort(order, order + n, [&](int x, int y) { if (b[x] == b[y]) return a[x] > a[y]; else return b[x] < b[y]; }); vector<vector<int>> dp(n + 1, vector<int>(n + 1, 0)); for (int ii = 1; ii <= n; ++ii) { int i = order[ii - 1]; for (int j = 0; j <= ii; ++j) { dp[ii][j] = dp[ii - 1][j] + b[i] * (k - 1); if (j) dp[ii][j] = max(dp[ii][j], dp[ii - 1][j - 1] + a[i] + b[i] * (j - 1)); } } vector<int> ans; int cur = k; int last = -1; for (int i = n; i >= 1; i--) { if (cur && (dp[i - 1][cur - 1] + a[order[i - 1]] + b[order[i - 1]] * (cur - 1) >= dp[i - 1][cur] + b[order[i - 1]] * (k - 1))) { if (last == -1) last = order[i - 1] + 1; else ans.push_back(order[i - 1] + 1); cur--; } else { ans.push_back(-order[i - 1] - 1); } } reverse(ans.begin(), ans.end()); vector<int> ans2; for (auto q : ans) if (q > 0) ans2.push_back(q); for (auto q : ans) if (q < 0) ans2.push_back(q), ans2.push_back(-q); ans2.push_back(last); cout << ans2.size() << endl; for (auto q : ans2) cout << q << " "; cout << endl; } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
python3
T = int(input().strip()) for t in range(T): n, k = list(map(int, input().split())) s = [] for i in range(n): a, b = list(map(int, input().split())) s.append(((a,b, i+1))) s = sorted(s, key=lambda x:x[0]) army = sorted(s[-k:], key=lambda x:x[1]) res = [] if k>1: for i in range(k - 1): res.append(army[i][2]) for i in range(n-k): if s[i][1]>0: res.append(s[i][2]) res.append(-s[i][2]) res.append(army[k-1][2]) print(len(res)) print(*res)
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T> using V = vector<T>; template <class T, size_t SZ> using AR = array<T, SZ>; template <class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } template <class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } constexpr int pct(int x) { return __builtin_popcount(x); } constexpr int bits(int x) { return 31 - __builtin_clz(x); } long long cdiv(long long a, long long b) { return a / b + ((a ^ b) > 0 && a % b); } long long fdiv(long long a, long long b) { return a / b - ((a ^ b) < 0 && a % b); } long long half(long long x) { return fdiv(x, 2); } template <class T, class U> T fstTrue(T lo, T hi, U first) { hi++; assert(lo <= hi); while (lo < hi) { T mid = half(lo + hi); first(mid) ? hi = mid : lo = mid + 1; } return lo; } template <class T, class U> T lstTrue(T lo, T hi, U first) { lo--; assert(lo <= hi); while (lo < hi) { T mid = half(lo + hi + 1); first(mid) ? lo = mid : hi = mid - 1; } return lo; } template <class T> void remDup(vector<T>& v) { sort(begin(v), end(v)); v.erase(unique(begin(v), end(v)), end(v)); } template <class A> void re(complex<A>& c); template <class A, class B> void re(pair<A, B>& p); template <class A> void re(vector<A>& v); template <class A, size_t SZ> void re(array<A, SZ>& a); template <class T> void re(T& x) { cin >> x; } void re(double& d) { string t; re(t); d = stod(t); } void re(long double& d) { string t; re(t); d = stold(t); } template <class H, class... T> void re(H& h, T&... t) { re(h); re(t...); } template <class A> void re(complex<A>& c) { A a, b; re(a, b); c = {a, b}; } template <class A, class B> void re(pair<A, B>& p) { re(p.first, p.second); } template <class A> void re(vector<A>& x) { for (auto& a : x) re(a); } template <class A, size_t SZ> void re(array<A, SZ>& x) { for (auto& a : x) re(a); } string to_string(char c) { return string(1, c); } string to_string(const char* second) { return (string)second; } string to_string(string second) { return second; } string to_string(bool b) { return to_string((int)b); } template <class A> string to_string(complex<A> c) { stringstream ss; ss << c; return ss.string(); } string to_string(vector<bool> v) { string res = "{"; for (int i = (0); i < ((int)(v).size()); ++i) res += char('0' + v[i]); res += "}"; return res; } template <size_t SZ> string to_string(bitset<SZ> b) { string res = ""; for (int i = (0); i < (SZ); ++i) res += char('0' + b[i]); return res; } template <class A, class B> string to_string(pair<A, B> p); template <class T> string to_string(T v) { bool fst = 1; string res = ""; for (const auto& x : v) { if (!fst) res += " "; fst = 0; res += to_string(x); } return res; } template <class A, class B> string to_string(pair<A, B> p) { return to_string(p.first) + " " + to_string(p.second); } template <class A> void pr(A x) { cout << to_string(x); } template <class H, class... T> void pr(const H& h, const T&... t) { pr(h); pr(t...); } void ps() { pr("\n"); } template <class H, class... T> void ps(const H& h, const T&... t) { pr(h); if (sizeof...(t)) pr(" "); ps(t...); } void DBG() { cerr << "]" << endl; } template <class H, class... T> void DBG(H h, T... t) { cerr << to_string(h); if (sizeof...(t)) cerr << ", "; DBG(t...); } int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int max(int a, int b, int c) { return max(a, max(b, c)); } int min(int a, int b, int c) { return min(a, min(b, c)); } long long max(long long a, long long b, long long c) { return max(a, max(b, c)); } long long min(long long a, long long b, long long c) { return min(a, min(b, c)); } void dbga(int arr[], int n) { vector<int> v; for (int i = (0); i < (n); ++i) v.push_back(arr[i]); 0; } void dbga(long long arr[], int n) { vector<int> v; for (int i = (0); i < (n); ++i) v.push_back(arr[i]); 0; } void setIn(string second) { freopen(second.c_str(), "r", stdin); } void setOut(string second) { freopen(second.c_str(), "w", stdout); } void unsyncIO() { cin.tie(0)->sync_with_stdio(0); } void setIO(string second = "") { unsyncIO(); if ((int)(second).size()) { setIn(second + ".in"), setOut(second + ".out"); } } const int MOD = 1e9 + 7; const int MX = 75 + 5; const long long INF = 1e18; const long double PI = acos((long double)-1); const int xd[4] = {1, 0, -1, 0}, yd[4] = {0, 1, 0, -1}; mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); int n, k; int dp[MX][MX]; vector<pair<pair<int, int>, int> > ev; vector<int> del; vector<int> ord; int LST = -1; void CONSTRUCT(int i, int j) { if (i == 0) return; if (i - 1 == LST) { CONSTRUCT(i - 1, j - 1); return; } int a = ev[i - 1].first.second, b = ev[i - 1].first.first; if (j && dp[i][j] == dp[i - 1][j - 1] + (j - 1) * b + a) { ord.push_back(i - 1); CONSTRUCT(i - 1, j - 1); } else { assert(dp[i][j] == dp[i - 1][j] + (k - 1) * b); del.push_back(i - 1); CONSTRUCT(i - 1, j); } } void solve() { ev.clear(); del.clear(); ord.clear(); re(n, k); for (int i = (0); i < (n); ++i) { int a, b; re(a, b); ev.push_back({{b, a}, i + 1}); } sort(begin(ev), end(ev)); 0; for (int i = (0); i < (n + 1); ++i) for (int j = (0); j < (k + 1); ++j) dp[i][j] = -MOD; dp[0][0] = 0; int mx = 0; for (int i = (0); i < (n); ++i) for (int j = (0); j < (k + 1); ++j) { int a = ev[i].first.second, b = ev[i].first.first; ckmax(dp[i + 1][j + 1], dp[i][j] + a + j * b); ckmax(dp[i + 1][j], dp[i][j] + (k - 1) * b); } LST = n - 1; ps(dp[n][k]); CONSTRUCT(n, k); 0; reverse(begin(ord), end(ord)); 0; ps((int)(ord).size() + 1 + 2 * (int)(del).size()); for (auto& x : ord) pr(ev[x].second, " "); for (auto& x : del) pr(ev[x].second, " ", -ev[x].second, " "); ps(ev[LST].second); } int main() { setIO(); int t = 1; re(t); while (t--) solve(); }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
python3
def m(e): return e['m'] def h(e): return e['h'] a = int(input()) for i in range(a): num = [int(x) for x in input().split()] mi = [] for j in range(num[0]): a = [int(x) for x in input().split()] k = {} k['m'] = a[0] k['h'] = a[1] k['i'] = j+1 mi.append(k) mi.sort(key=m,reverse = True) cho = mi nch = mi cho = mi[:num[1]] nch = mi[num[1]:] ksum = 0 klen = 0 k = [] cho.sort(key=h) k.append(cho[0]['i']) klen +=1 ksum+=cho[0]['m'] if(num[1]>1): for element in cho[1:num[1]-1]: #print(element['i'],end = " ") k.append(element['i']) klen +=1 ksum+=element['m'] ksum+=element['h'] fix = klen #print() #print(nch) if(num[1]!=1): nch.sort(key = h) for element in nch: if(element['h']!=0): #print(element['i'],end = " ") #print(-1*element['i'],end = " ") k.append(element['i']) klen +=1 k.append(-1*element['i']) klen +=1 ksum+=(element['h']) k.append(cho[num[1]-1]['i']) klen+=1 ksum+=cho[num[1]-1]['m'] ksum+=cho[num[1]-1]['h'] #print(cho[num[1]-1]['i'],end = " ") mi.sort(key = h) csum = 0 clen = 0 c = [] if(num[1]>1): c.append(mi[0]['i']) clen +=1 csum+=mi[0]['m'] mi = mi[1:] mi.sort(key=m,reverse = True) cho = mi nch = mi cho = mi[:num[1]-1] nch = mi[num[1]-1:] cho.sort(key=h) for element in cho[:num[1]-2]: #print(element['i'],end = " ") c.append(element['i']) clen +=1 csum+=element['m'] csum+=element['h'] fix = clen #print() #print(nch) if(num[1]!=1): nch.sort(key = h) for element in nch: if(element['h']!=0): #print(element['i'],end = " ") #print(-1*element['i'],end = " ") c.append(element['i']) clen +=1 c.append(-1*element['i']) clen +=1 csum+=(element['h']) c.append(cho[num[1]-2]['i']) clen+=1 csum+=cho[num[1]-2]['m'] csum+=cho[num[1]-2]['h'] #print(cho[num[1]-1]['i'],end = " ") if(csum>ksum): print(clen) for j in c: print(j,end = " ") print() else: print(klen) for j in k: print(j,end = " ") print() """ print(c) print(csum) print(k) print(ksum)""" """"5 2 15816 66663 61957 87105 32741 59526 73544 75763 44144 1104 8 4 5 -5 3 -3 1 -1 2 8 5 2 -2 1 -1 3 -3 4 5 2 3538 43176 24258 77210 92123 70606 44495 37855 65913 67119 """
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 75; struct node { int a, b, num; } p[maxn + 11]; int f[maxn + 11][maxn + 11][2]; vector<int> v1, v2; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t; cin >> t; for (int kase = 1; kase <= t; kase++) { int n, k; cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> p[i].a >> p[i].b; p[i].num = i; } if (kase == 10) { cout << n << " " << k << endl; for (int i = 1; i <= n; i++) cout << p[i].a << " " << p[i].b << endl; continue; } sort(p + 1, p + 1 + n, [&](node a, node b) { return a.b < b.b; }); memset(f, 0, sizeof(f)); for (int i = 1; i <= n; i++) { f[i][0][0] = f[i - 1][0][0] + p[i].b * (k - 1); for (int j = 1; j <= min(k, i); j++) { if (i > j) f[i][j][0] = max(f[i - 1][j][0], f[i - 1][j][1]) + p[i].b * (k - 1); f[i][j][1] = max(f[i - 1][j - 1][0], f[i - 1][j - 1][1]) + p[i].b * (j - 1) + p[i].a; } } v1.clear(); v2.clear(); int x = n; int y = k; while (x) { if (f[x][y][0] > f[x][y][1]) { v2.emplace_back(x); x--; } else { v1.emplace_back(x); x--; y--; } } reverse(v1.begin(), v1.end()); int m = (int)v1.size() + 2 * (int)v2.size(); cout << m << endl; for (int i = 0; i < v1.size() - 1; i++) cout << p[v1[i]].num << " "; for (auto u : v2) cout << p[u].num << " " << -p[u].num << " "; cout << p[v1.back()].num << endl; } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int Maxn = 88; struct node { int a, b, id; } g[Maxn]; int f[Maxn][Maxn], s[Maxn]; bool flag[Maxn][Maxn], vis[Maxn]; int a[Maxn], b[Maxn], id[Maxn]; int n, m, cnt; inline bool cmp(node x, node y) { return x.b < y.b; } void init() { for (int i = 1; i <= n; ++i) for (int j = 0; j <= m; ++j) f[i][j] = flag[i][j] = 0; for (int i = 1; i <= n; ++i) s[i] = vis[i] = a[i] = b[i] = id[i] = 0; cnt = n = m = 0; } inline int read() { int s = 0, w = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') w = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar(); return s * w; } int main() { int T = read(); while (T--) { n = read(), m = read(); for (int i = 1; i <= n; ++i) g[i].a = read(), g[i].b = read(), g[i].id = i; sort(g + 1, g + 1 + n, cmp); for (int i = 1; i <= n; ++i) a[i] = g[i].a, b[i] = g[i].b, id[i] = g[i].id; for (int i = 1; i <= n; ++i) for (int j = 0; j <= min(i, m); ++j) { f[i][j] = f[i - 1][j] + b[i] * (m - 1), flag[i][j] = 0; if (j && f[i - 1][j - 1] + a[i] + b[i] * (j - 1) > f[i][j]) f[i][j] = f[i - 1][j - 1] + a[i] + b[i] * (j - 1), flag[i][j] = 1; } int i = n, j = m; while (i) { if (flag[i][j]) s[++cnt] = i, vis[i] = 1, --j; --i; } printf("%d\n", m - 1 + ((n - m) << 1) + 1); for (int i = cnt; i > 1; --i) printf("%d ", id[s[i]]); for (int i = 1; i <= n; ++i) if (!vis[i]) printf("%d %d ", id[i], -id[i]); printf("%d\n", id[s[1]]); init(); } return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
python3
for _ in range(int(input())): n, k = map(int, input().split()) values = [] for i in range(n): a, b = map(int, input().split()) values.append((i + 1, a, b)) if k == 1: best = -1 score = 0 for (i, a, b) in values: if a > score: best = i score = a print(1) print(best) else: values = sorted(values, key=lambda x: x[1], reverse=True) keepers = sorted(values[:k], key=lambda x: x[2]) destroyers = sorted([x for x in values[k:] if x[2] > 0], key=lambda x: x[2]) if len(destroyers): turns = [] for i, a, b in keepers[:-1]: turns.append(i) for i, a, b in destroyers: turns.append(i) turns.append(-i) turns.append(keepers[-1][0]) else: turns = [i for i,a, b in keepers] print(len(turns)) print(*turns)
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; 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; } inline int read() { int s = 0, w = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') w = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar(); return s * w; } int main() { int T = read(); while (T--) { n = read(), m = read(); for (int i = 1; i <= n; ++i) g[i].a = read(), g[i].b = read(), g[i].id = i; sort(g + 1, g + 1 + n, cmp); for (int i = 1; i <= n; ++i) a[i] = g[i].a, b[i] = g[i].b, id[i] = g[i].id; for (int i = 1; i <= n; ++i) for (int j = min(i, m); j >= 0; --j) { f[i][j] = f[i - 1][j] + b[i] * (m - 1); if (j && f[i - 1][j - 1] + a[i] + b[i] * (j - 1) > f[i][j]) f[i][j] = f[i - 1][j - 1] + a[i] + b[i] * (j - 1), flag[i][j] = 1; } int i = n, j = m; while (i) { if (flag[i][j]) s[++cnt] = i, vis[i] = 1, --j; --i; } printf("%d\n", (n << 1) - m); for (int i = cnt - 1; i; --i) printf("%d ", id[s[i]]); for (int i = 1; i <= n; ++i) if (!vis[i]) printf("%d ", -id[i]); printf("%d\n", id[s[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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long dp[76][76]; long long col[76][76]; vector<pair<pair<int, int>, int> > v; bool sortf(const pair<pair<int, int>, int> &a, const pair<pair<int, int>, int> &b) { return a.first.second < b.first.second; } int main() { int t; cin >> t; for (int num_tests = 0; num_tests < t; ++num_tests) { int n, k; cin >> n >> k; v.clear(); for (int i = 0; i < n; ++i) { int a, b; cin >> a >> b; v.push_back(make_pair(make_pair(a, b), i + 1)); } sort(v.begin(), v.end(), sortf); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; j++) { dp[i][j] = 0; col[i][j] = 0; } } dp[0][0] = (k - 1) * v[0].first.second; dp[0][1] = v[0].first.first; for (int i = 1; i < n; ++i) { dp[i][0] = dp[i - 1][0] + v[i].first.second; col[i][0] = 2; for (int j = 1; j < i + 1; j++) { long long tmp1 = dp[i - 1][j] + (k - 1) * v[i].first.second; long long tmp2 = dp[i - 1][j - 1] + v[i].first.first + (j - 1) * v[i].first.second; if (tmp1 > tmp2) { dp[i][j] = tmp1; col[i][j] = 2; } else { dp[i][j] = tmp2; col[i][j] = 1; } } dp[i][i + 1] = dp[i - 1][i] + v[i].first.first + (i)*v[i].first.second; col[i][i + 1] = 1; } vector<int> group1; vector<int> group2; int i2 = k; for (int i = n - 1; i >= 0; i--) { if (col[i][i2] == 2) { group2.push_back(v[i].second); } else { group1.push_back(v[i].second); i2--; } } reverse(group1.begin(), group1.end()); cout << 2 * n - k << endl; if (group1.size() != k) { cout << "why??" << endl; } for (int i = 0; i < group1.size() - 1; i++) { cout << group1[i] << endl; } for (int i = 0; i < group2.size(); ++i) { cout << group2[i] << endl; cout << -group2[i] << endl; } cout << group1[k - 1] << endl; } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
java
import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Scanner; /** * * @author MasterEx */ public class ECR87F { static class Minion { int pos; long power; long benefit; public Minion(int p, long po, long b) { pos = p + 1; power = po; benefit = b; } } public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); for (int i = 0; i < t; i++) { int n = in.nextInt(); int k = in.nextInt(); LinkedList<Minion> minions = new LinkedList<>(); for (int j = 0; j < n; j++) { long a = in.nextInt(); long b = in.nextInt(); minions.add(new Minion(j, a, b)); } minions.sort((Minion m1, Minion m2) -> { if (m1.power == m2.power) { return Long.compare(m1.benefit, m2.benefit); } return Long.compare(m2.power, m1.power); }); String s = ""; int m = 0; for (int j = 0; j < k - 1 && !minions.isEmpty(); j++) { m++; s += minions.removeFirst().pos + " "; } if (!minions.isEmpty()) { minions.sort((m1, m2) -> { if (m1.benefit == m2.benefit) { return Long.compare(m2.power, m1.power); } return Long.compare(m2.benefit, m1.benefit); }); } while (!minions.isEmpty() && m <= 2 * n) { Minion minion = minions.removeFirst(); m++; s += minion.pos; if (minion.benefit == 0 || !minions.isEmpty() && minion.benefit == minions.peek().benefit) { break; } if (!minions.isEmpty()) { m++; s += " -" + minion.pos + " "; } } System.out.println(m); System.out.println(s); } } }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const long long INF = 0x3f3f3f3f3f3f3f3f; const int N = 75; struct ca { int a, b, id; } c[N + 7]; int f[N + 7][N + 7], p[N + 7][N + 7], mk[N + 7]; int cmp(const ca first, const ca second) { return first.b < second.b; } int cmp2(const ca first, const ca second) { if (mk[first.id] != mk[second.id]) return mk[first.id] > mk[second.id]; return first.a < second.a; } void KonnyWen() { int n, k; scanf("%d%d", &n, &k), k--; for (int i = 1; i <= n; i++) scanf("%d%d", &c[i].a, &c[i].b), c[i].id = i; sort(c + 1, c + n + 1, cmp); fill(f[0] + 1, f[0] + k + 1, 0); for (int i = 1; i <= n; i++) { fill(f[i] + 1, f[i] + k + 1, 0); for (int j = 1; j <= min(i, k); j++) { if (f[i - 1][j - 1] + c[i].a + c[i].b * (j - 1) > f[i - 1][j]) f[i][j] = f[i - 1][j - 1] + c[i].a + c[i].b * (j - 1), p[i][j] = i; else f[i][j] = f[i - 1][j], p[i][j] = p[i - 1][j]; } } for (int i = k, j = p[n][k]; i >= 1; i--) mk[c[j].id] = 1, j = p[j - 1][i - 1]; printf("%d\n", 2 * n - k - 1); for (int i = 1; i <= n; i++) if (mk[i]) printf("%d ", i); sort(c + 1, c + n + 1, cmp2); for (int i = k + 1; i <= n - 1; i++) printf("%d %d ", c[i].id, -c[i].id); printf("%d\n", c[n].id); } int main() { int t; scanf("%d", &t); while (t--) KonnyWen(); return 0; }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 80; int f[maxn][maxn], pre[maxn][maxn]; int n, k, id[maxn], a[maxn], b[maxn]; void solve() { scanf("%d%d", &n, &k); for (int(i) = (1); (i) <= (n); (i)++) scanf("%d%d", a + i, b + i); iota(id + 1, id + n + 1, 1); sort(id + 1, id + n + 1, [&](int i, int j) { return b[i] < b[j]; }); memset(f, 0, sizeof(f)); int bi = 0, bj = 0; for (int(i) = (1); (i) <= (n); (i)++) for (int(j) = (0); (j) <= ((min(i + 1, k + 1)) - 1); (j)++) { if (j != min(i, k)) { f[i][j] = f[i - 1][j] + j * b[id[i]]; pre[i][j] = j; } if (j && f[i][j] < f[i - 1][j - 1] + a[id[i]] + (j - 1) * b[id[i]]) { f[i][j] = f[i - 1][j - 1] + a[id[i]] + (j - 1) * b[id[i]]; pre[i][j] = j - 1; if (j == k) { int s = 0; for (int(l) = (i + 1); (l) <= (n); (l)++) s += (k - 1) * b[id[l]]; if (f[i][j] + s > f[bi][bj]) { bi = i; bj = j; f[i][j] += s; } } } } int j = pre[bi][bj]; stack<int> ans; ans.emplace(id[bi]); for (int(l) = (n); (l) >= (bi + 1); (l)--) { ans.emplace(-id[l]); ans.emplace(id[l]); } for (int(i) = (bi - 1); (i) >= (1); (i)--) { if (pre[i][j] == j) ans.emplace(-id[i]); ans.emplace(id[i]); j = pre[i][j]; } printf("%lu\n", ans.size()); while (!ans.empty()) { printf("%d ", ans.top()); ans.pop(); } putchar('\n'); } int main() { int T; scanf("%d", &T); while (T--) solve(); }
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
python3
def m(e): return e['m'] def h(e): return e['h'] a = int(input()) for i in range(a): num = [int(x) for x in input().split()] mi = [] for j in range(num[0]): a = [int(x) for x in input().split()] k = {} k['m'] = a[0] k['h'] = a[1] k['i'] = j+1 mi.append(k) mi.sort(key=m,reverse = True) cho = mi nch = mi cho = mi[:num[1]] nch = mi[num[1]:] ksum = 0 klen = 0 k = [] cho.sort(key=h) for element in cho[:num[1]-1]: #print(element['i'],end = " ") k.append(element['i']) klen +=1 ksum+=element['m'] ksum+=element['h']*(klen-1) fix = klen #print() #print(nch) if(num[1]!=1): for element in nch: if(element['h']!=0): #print(element['i'],end = " ") #print(-1*element['i'],end = " ") k.append(element['i']) klen +=1 k.append(-1*element['i']) klen +=1 ksum+=(element['h']*fix) k.append(cho[num[1]-1]['i']) klen+=1 ksum+=cho[num[1]-1]['m'] ksum+=cho[num[1]-1]['h']*(klen-1) #print(cho[num[1]-1]['i'],end = " ") mi.sort(key = h) csum = 0 clen = 0 c = [] if(num[1]>1): c.append(mi[0]['i']) clen +=1 csum+=mi[0]['m'] mi = mi[1:] mi.sort(key=m,reverse = True) cho = mi nch = mi cho = mi[:num[1]-1] nch = mi[num[1]-1:] cho.sort(key=h) for element in cho[:num[1]-2]: #print(element['i'],end = " ") c.append(element['i']) clen +=1 csum+=element['m'] csum+=element['h']*(clen-1) fix = clen #print() #print(nch) if(num[1]!=1): for element in nch: if(element['h']!=0): #print(element['i'],end = " ") #print(-1*element['i'],end = " ") c.append(element['i']) clen +=1 c.append(-1*element['i']) clen +=1 csum+=(element['h']*fix) c.append(cho[num[1]-2]['i']) clen+=1 csum+=cho[num[1]-2]['m'] csum+=cho[num[1]-2]['h']*(clen-1) #print(cho[num[1]-1]['i'],end = " ") if(csum>ksum): print(clen) for j in c: print(j,end = " ") print() else: print(klen) for j in k: print(j,end = " ") print() ''' print(c) print(csum) print(k) print(ksum)''' """"5 5 60005 41309 13864 47046 63344 20154 42138 43756 16588 37266 5 3 1 4 5 2 5 3 5 1 4 2 """
1354_F. Summoning Minions
Polycarp plays a computer game. In this game, the players summon armies of magical minions, which then fight each other. Polycarp can summon n different minions. The initial power level of the i-th minion is a_i, and when it is summoned, all previously summoned minions' power levels are increased by b_i. The minions can be summoned in any order. Unfortunately, Polycarp cannot have more than k minions under his control. To get rid of unwanted minions after summoning them, he may destroy them. Each minion can be summoned (and destroyed) only once. Polycarp's goal is to summon the strongest possible army. Formally, he wants to maximize the sum of power levels of all minions under his control (those which are summoned and not destroyed). Help Polycarp to make up a plan of actions to summon the strongest possible army! Input The first line contains one integer T (1 ≀ T ≀ 75) β€” the number of test cases. Each test case begins with a line containing two integers n and k (1 ≀ k ≀ n ≀ 75) β€” the number of minions availible for summoning, and the maximum number of minions that can be controlled by Polycarp, respectively. Then n lines follow, the i-th line contains 2 integers a_i and b_i (1 ≀ a_i ≀ 10^5, 0 ≀ b_i ≀ 10^5) β€” the parameters of the i-th minion. Output For each test case print the optimal sequence of actions as follows: Firstly, print m β€” the number of actions which Polycarp has to perform (0 ≀ m ≀ 2n). Then print m integers o_1, o_2, ..., o_m, where o_i denotes the i-th action as follows: if the i-th action is to summon the minion x, then o_i = x, and if the i-th action is to destroy the minion x, then o_i = -x. Each minion can be summoned at most once and cannot be destroyed before being summoned (and, obviously, cannot be destroyed more than once). The number of minions in Polycarp's army should be not greater than k after every action. If there are multiple optimal sequences, print any of them. Example Input 3 5 2 5 3 7 0 5 0 4 0 10 0 2 1 10 100 50 10 5 5 1 5 2 4 3 3 4 2 5 1 Output 4 2 1 -1 5 1 2 5 5 4 3 2 1 Note Consider the example test. In the first test case, Polycarp can summon the minion 2 with power level 7, then summon the minion 1, which will increase the power level of the previous minion by 3, then destroy the minion 1, and finally, summon the minion 5. After this, Polycarp will have two minions with power levels of 10. In the second test case, Polycarp can control only one minion, so he should choose the strongest of them and summon it. In the third test case, Polycarp is able to summon and control all five minions.
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
{ "input": [ "3\n5 2\n5 3\n7 0\n5 0\n4 0\n10 0\n2 1\n10 100\n50 10\n5 5\n1 5\n2 4\n3 3\n4 2\n5 1\n" ], "output": [ "8\n2 3 -3 4 -4 1 -1 5\n3\n1 -1 2\n5\n5 4 3 2 1\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 2e9; int g[80][80]; int n, k, a[80], b[80]; int job[80]; int u[80], v[80]; bool used[80]; int dist[80]; int par[80]; void hungarian() { for (int i = 1; i <= n; i++) { job[0] = i; int j0 = 0; for (int l = 0; l < 80; l++) { used[l] = 0; dist[l] = INF; par[l] = 79; } while (job[j0]) { used[j0] = 1; int i0 = job[j0], d = INF, j1 = 79; for (int j = 1; j <= n; j++) { if (used[j]) continue; int w = g[i0][j] - u[i0] - v[j]; if (w < dist[j]) { dist[j] = w; par[j] = j0; } if (dist[j] < d) { d = dist[j]; j1 = j; } } for (int j = 1; j <= n; j++) { if (used[j]) { u[job[j]] += d; v[j] -= d; } else dist[j] -= d; } j0 = j1; } while (j0) { int j1 = par[j0]; job[j0] = job[j1]; j0 = j1; } } } void solve() { memset(g, 0, sizeof(g)); memset(u, 0, sizeof(u)); memset(v, 0, sizeof(v)); memset(job, 0, sizeof(job)); cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> a[i] >> b[i]; } for (int i = 1; i <= n; i++) { g[i][1] = a[i] + b[i] * (k - 1); for (int j = 2; j <= k; j++) { g[i][j] = a[i] + (j - 1) * b[i]; } for (int j = k + 1; j <= n; j++) { g[i][j] = b[i] * (k - 1); } } for (int i1 = 1; i1 <= n; i1++) for (int i2 = 1; i2 <= n; i2++) { g[i1][i2] = -g[i1][i2]; } hungarian(); vector<int> ans; for (int idx = 2; idx <= k; idx++) { for (int i = 1; i <= n; i++) { if (job[i] == idx) ans.push_back(i); } } for (int idx = k + 1; idx <= n; idx++) { for (int i = 1; i <= n; i++) { if (job[i] == idx) { ans.push_back(i); ans.push_back(-i); } } } for (int i = 1; i <= n; i++) { if (job[i] == 1) { ans.push_back(i); } } cout << ans.size() << endl; for (auto xd : ans) cout << xd << " "; cout << endl; } int main() { int t; cin >> t; while (t--) solve(); return 0; }