#include <bits/stdc++.h> using namespace std; const int MAX_N = 50000; int N, M; u_short a[MAX_N]; int counter[129]; u_short d[MAX_N]; void process(){ int last_element = M + a[0]; for(int v = 0; v < 129; v++){ counter[v] = 0; } int i = 1; while(i < N){ // we assume that counter contains inside all // the indices in range [0, i - 2]. We further // assume that all a[j], where j in range [0, i - 2] // has already been increased in preparation for // this new plan. (as well as last_element). // sum of all elements. int SUM = 0; for(int v = 128; v >= 0; v--){ SUM += v * counter[v]; } // I "must" zero out (i + 1)/2 values. int real_amount_zeroed = 0; if(last_element > 128){ real_amount_zeroed++; } else { SUM += last_element; counter[last_element]++; } // now we find which elements are being zeroed out. int last_v = 129; int last_zeroed_amt = 0; if(real_amount_zeroed >= (i + 1)/2 && SUM <= M){ } else { for(int v = 128; v >= 0; v--){ // remove the minimal amount needed to satisfy some conditions. last_v = v; last_zeroed_amt = (i + 1)/2 - real_amount_zeroed; if(v > 0){ last_zeroed_amt = max(last_zeroed_amt, (SUM - M + v - 1)/v); } last_zeroed_amt = min(last_zeroed_amt, counter[v]); SUM -= v * last_zeroed_amt; real_amount_zeroed += last_zeroed_amt; // cout << "v = " << v << " counter = " << counter[v] << " last_zeroed_amt = " << last_zeroed_amt << endl; if(real_amount_zeroed >= (i + 1)/2 && SUM <= M){ break; } } } int new_i = i + 1; // cout << "i = " << i << " real_amount_zeroed = " << real_amount_zeroed << endl; // now we check if we have to "wait" before the plan is accepted. if(real_amount_zeroed > (i + 1)/2){ // in this case we have to wait till this new i. while(new_i - real_amount_zeroed < (new_i/2)){ new_i++; } new_i++; // in the case that this new_i is > N, then my // resulting plan will throw Nth guy overboard. if(new_i > N){ // revert current additions. for(int j = 0; j < i - 1; j++){ d[j] -= a[j]; } last_element -= a[i - 1]; // print out the results for(int j = N - 1; j >= i; j--){ cout << -1 << ' '; } cout << last_element << ' '; for(int j = i - 2; j >= 0; j--){ cout << d[j] << ' '; } return; } } for(int v = 0; v < 129; v++){ counter[v] = 0; } if(last_v <= last_element){ // in this case I zeroed last_element. d[i - 1] = a[i - 1]; if(last_element == last_v){ last_zeroed_amt--; } } else { d[i - 1] = last_element + a[i - 1]; } counter[d[i - 1]]++; // this loop is the most intense! for(int j = i - 2; j >= 0; j--){ if(d[j] > last_v){ d[j] = a[j]; } else if(d[j] == last_v && last_zeroed_amt > 0){ last_zeroed_amt--; d[j] = a[j]; } else { d[j] += a[j]; } counter[d[j]]++; } for(int j = i; j < new_i - 1; j++){ d[j] = a[j]; counter[a[j]]++; } last_element = M - SUM + a[new_i - 1]; // for(int j = 0; j < new_i - 1; j++){ // cout << d[j] - a[j] << ' '; // } // cout << last_element - a[new_i - 1] << endl; i = new_i; } // finally, revert all the changes and print out. // revert current additions. for(int j = 0; j < N - 1; j++){ d[j] -= a[j]; } last_element -= a[N - 1]; cout << last_element << ' '; for(int j = N - 2; j >= 0; j--){ cout << d[j] << ' '; } } int main(){ cin.tie(NULL); ios_base::sync_with_stdio(0); cin >> N >> M; for(int i = 0; i < N; i++){ cin >> a[N - 1 - i]; } process(); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | #include <bits/stdc++.h> using namespace std; const int MAX_N = 50000; int N, M; u_short a[MAX_N]; int counter[129]; u_short d[MAX_N]; void process(){ int last_element = M + a[0]; for(int v = 0; v < 129; v++){ counter[v] = 0; } int i = 1; while(i < N){ // we assume that counter contains inside all // the indices in range [0, i - 2]. We further // assume that all a[j], where j in range [0, i - 2] // has already been increased in preparation for // this new plan. (as well as last_element). // sum of all elements. int SUM = 0; for(int v = 128; v >= 0; v--){ SUM += v * counter[v]; } // I "must" zero out (i + 1)/2 values. int real_amount_zeroed = 0; if(last_element > 128){ real_amount_zeroed++; } else { SUM += last_element; counter[last_element]++; } // now we find which elements are being zeroed out. int last_v = 129; int last_zeroed_amt = 0; if(real_amount_zeroed >= (i + 1)/2 && SUM <= M){ } else { for(int v = 128; v >= 0; v--){ // remove the minimal amount needed to satisfy some conditions. last_v = v; last_zeroed_amt = (i + 1)/2 - real_amount_zeroed; if(v > 0){ last_zeroed_amt = max(last_zeroed_amt, (SUM - M + v - 1)/v); } last_zeroed_amt = min(last_zeroed_amt, counter[v]); SUM -= v * last_zeroed_amt; real_amount_zeroed += last_zeroed_amt; // cout << "v = " << v << " counter = " << counter[v] << " last_zeroed_amt = " << last_zeroed_amt << endl; if(real_amount_zeroed >= (i + 1)/2 && SUM <= M){ break; } } } int new_i = i + 1; // cout << "i = " << i << " real_amount_zeroed = " << real_amount_zeroed << endl; // now we check if we have to "wait" before the plan is accepted. if(real_amount_zeroed > (i + 1)/2){ // in this case we have to wait till this new i. while(new_i - real_amount_zeroed < (new_i/2)){ new_i++; } new_i++; // in the case that this new_i is > N, then my // resulting plan will throw Nth guy overboard. if(new_i > N){ // revert current additions. for(int j = 0; j < i - 1; j++){ d[j] -= a[j]; } last_element -= a[i - 1]; // print out the results for(int j = N - 1; j >= i; j--){ cout << -1 << ' '; } cout << last_element << ' '; for(int j = i - 2; j >= 0; j--){ cout << d[j] << ' '; } return; } } for(int v = 0; v < 129; v++){ counter[v] = 0; } if(last_v <= last_element){ // in this case I zeroed last_element. d[i - 1] = a[i - 1]; if(last_element == last_v){ last_zeroed_amt--; } } else { d[i - 1] = last_element + a[i - 1]; } counter[d[i - 1]]++; // this loop is the most intense! for(int j = i - 2; j >= 0; j--){ if(d[j] > last_v){ d[j] = a[j]; } else if(d[j] == last_v && last_zeroed_amt > 0){ last_zeroed_amt--; d[j] = a[j]; } else { d[j] += a[j]; } counter[d[j]]++; } for(int j = i; j < new_i - 1; j++){ d[j] = a[j]; counter[a[j]]++; } last_element = M - SUM + a[new_i - 1]; // for(int j = 0; j < new_i - 1; j++){ // cout << d[j] - a[j] << ' '; // } // cout << last_element - a[new_i - 1] << endl; i = new_i; } // finally, revert all the changes and print out. // revert current additions. for(int j = 0; j < N - 1; j++){ d[j] -= a[j]; } last_element -= a[N - 1]; cout << last_element << ' '; for(int j = N - 2; j >= 0; j--){ cout << d[j] << ' '; } } int main(){ cin.tie(NULL); ios_base::sync_with_stdio(0); cin >> N >> M; for(int i = 0; i < N; i++){ cin >> a[N - 1 - i]; } process(); } |