#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
const int MAXN=50000;
int n;
int m;
int ai[MAXN];
int wynik[MAXN];
//priority_queue< pair<int, int> > pir, tmp, tmp2; // MINUS ile trzeba zaplacic, nr pirata,
vector<pair<int, int> > pir; // ile trzeba zaplacic, MINUS nr pirata,
int main() {
cin >> n >> m;
for(int i = 0; i < n; i++)
cin >> ai[i];
for(int i = 0; i < n; i++)
wynik[i] = -1;
pir.push_back( pair<int,int>(m+ai[n-1], - (n-1) ) );
for(int planujacy = n-2; planujacy >= 0; planujacy--) {
sort(pir.begin(), pir.end());
int wydatek = 0;
for(int j = 0; j < (pir.size()+0)/2; j++) {
wydatek += pir[j].first;
}
if(m >= wydatek) {
for(int j = 0; j < (pir.size()+2)/2; j++) {
wynik[-pir[j].second] = pir[j].first;
pir[j].first += ai[-pir[j].second];
}
for(int j = (pir.size()+0)/2; j < pir.size(); j++) {
wynik[-pir[j].second] = 0;
pir[j].first = ai[-pir[j].second];
}
pir.push_back( pair<int,int>( m-wydatek + ai[planujacy], -planujacy) );
wynik[planujacy] = m-wydatek;
} else {
pir.push_back( pair<int,int>( 0, -planujacy) );
}
}
for(int i = 0; i < n; i++)
cout << wynik[i] << " ";
cout << endl;
return 0;
}
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 | #include <iostream> #include <queue> #include <vector> #include <algorithm> using namespace std; const int MAXN=50000; int n; int m; int ai[MAXN]; int wynik[MAXN]; //priority_queue< pair<int, int> > pir, tmp, tmp2; // MINUS ile trzeba zaplacic, nr pirata, vector<pair<int, int> > pir; // ile trzeba zaplacic, MINUS nr pirata, int main() { cin >> n >> m; for(int i = 0; i < n; i++) cin >> ai[i]; for(int i = 0; i < n; i++) wynik[i] = -1; pir.push_back( pair<int,int>(m+ai[n-1], - (n-1) ) ); for(int planujacy = n-2; planujacy >= 0; planujacy--) { sort(pir.begin(), pir.end()); int wydatek = 0; for(int j = 0; j < (pir.size()+0)/2; j++) { wydatek += pir[j].first; } if(m >= wydatek) { for(int j = 0; j < (pir.size()+2)/2; j++) { wynik[-pir[j].second] = pir[j].first; pir[j].first += ai[-pir[j].second]; } for(int j = (pir.size()+0)/2; j < pir.size(); j++) { wynik[-pir[j].second] = 0; pir[j].first = ai[-pir[j].second]; } pir.push_back( pair<int,int>( m-wydatek + ai[planujacy], -planujacy) ); wynik[planujacy] = m-wydatek; } else { pir.push_back( pair<int,int>( 0, -planujacy) ); } } for(int i = 0; i < n; i++) cout << wynik[i] << " "; cout << endl; return 0; } |
English