#include <bits/stdc++.h>
using namespace std;
#define JOIN_(X, Y) X##Y
#define JOIN(X, Y) JOIN_(X, Y)
#define TMP JOIN(tmp, __LINE__)
#define PB push_back
#define SZ(x) int((x).size())
#define REP(i, n) for (int i = 0, TMP = (n); i < TMP; ++i)
#define FOR(i, a, b) for (int i = (a), TMP = (b); i <= TMP; ++i)
#define FORD(i, a, b) for (int i = (a), TMP = (b); i >= TMP; --i)
#ifdef DEBUG
#define DEB(x) (cerr << x)
#else
#define DEB(x)
#endif
typedef long long ll;
typedef pair<int, int> pii;
const ll INF = 1e17 + 9;
struct State {
ll added; // dodane do wszystkich values
vector<ll> values; // malejaco
int get_last_non_negative_position() const {
auto it = upper_bound(values.begin(), values.end(), -added, greater<ll>());
return it - values.begin();
}
ll get(int position) { return values[position] + added; }
void compute(int position) {
if (position == 0) {
// powinno byc values[0] = 0, ale zawsze tak jest dla pozycji 0
} else if (position == SZ(values)) {
values.PB(-added);
} else {
values[position] = max(values[position], -added);
}
}
};
void inline one() {
int n;
cin >> n;
vector<ll> in(n);
REP(i, n) { cin >> in[i]; }
State state;
REP(i, n) {
if (i == 0) {
state.values.PB(0);
state.added = in[i];
} else {
int position = state.get_last_non_negative_position();
DEB("position=" << position << "\n");
state.compute(position);
state.added += in[i];
}
DEB(i + 1 << " (" << in[i] << ") : ");
REP(j, SZ(state.values)) { DEB(state.get(j) << " "); }
DEB("\n");
}
int save = n;
REP(j, SZ(state.values)) {
DEB("g=" << state.get(j) << ", ");
if (state.get(j) >= 0) {
save = j;
}
}
cout << n - 1 - save << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
// int z; cin >> z; while(z--)
one();
}
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 | #include <bits/stdc++.h> using namespace std; #define JOIN_(X, Y) X##Y #define JOIN(X, Y) JOIN_(X, Y) #define TMP JOIN(tmp, __LINE__) #define PB push_back #define SZ(x) int((x).size()) #define REP(i, n) for (int i = 0, TMP = (n); i < TMP; ++i) #define FOR(i, a, b) for (int i = (a), TMP = (b); i <= TMP; ++i) #define FORD(i, a, b) for (int i = (a), TMP = (b); i >= TMP; --i) #ifdef DEBUG #define DEB(x) (cerr << x) #else #define DEB(x) #endif typedef long long ll; typedef pair<int, int> pii; const ll INF = 1e17 + 9; struct State { ll added; // dodane do wszystkich values vector<ll> values; // malejaco int get_last_non_negative_position() const { auto it = upper_bound(values.begin(), values.end(), -added, greater<ll>()); return it - values.begin(); } ll get(int position) { return values[position] + added; } void compute(int position) { if (position == 0) { // powinno byc values[0] = 0, ale zawsze tak jest dla pozycji 0 } else if (position == SZ(values)) { values.PB(-added); } else { values[position] = max(values[position], -added); } } }; void inline one() { int n; cin >> n; vector<ll> in(n); REP(i, n) { cin >> in[i]; } State state; REP(i, n) { if (i == 0) { state.values.PB(0); state.added = in[i]; } else { int position = state.get_last_non_negative_position(); DEB("position=" << position << "\n"); state.compute(position); state.added += in[i]; } DEB(i + 1 << " (" << in[i] << ") : "); REP(j, SZ(state.values)) { DEB(state.get(j) << " "); } DEB("\n"); } int save = n; REP(j, SZ(state.values)) { DEB("g=" << state.get(j) << ", "); if (state.get(j) >= 0) { save = j; } } cout << n - 1 - save << "\n"; } int main() { ios::sync_with_stdio(false); cin.tie(0); // int z; cin >> z; while(z--) one(); } |
English