#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <set> #include <map> #include <utility> #include <queue> #include <vector> #include <string> #include <cstring> #define REP(a,n) for (int a = 0; a<(n); ++a) #define FOR(a,b,c) for (int a = (b); a<=(c); ++a) #define FORD(a,b,c) for (int a = (b); a>=(c); --a) #define FOREACH(a,v) for (auto a : v) #define MP make_pair #define PB push_back template<class T> inline int size(const T&t) { return t.size(); } using namespace std; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<string> vs; typedef long long LL; /////////////////////////////// int N; map<LL,LL> wyn; int main() { cin >> N; wyn.emplace(0,0); LL cur_en = 0; REP(a, N) { LL wej; cin >> wej; cur_en += wej; auto it = wyn.lower_bound(-cur_en); if (a == N - 1) cout << (it == wyn.end() ? -1 : N - 1 + it->second) << endl; else if (it != wyn.end()) { LL ile = it->second - 1; if (it != wyn.begin()) { --it; if (it->second == ile) wyn.erase(it); } wyn[-cur_en] = ile; } } }
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 <cstdio> #include <cstdlib> #include <algorithm> #include <set> #include <map> #include <utility> #include <queue> #include <vector> #include <string> #include <cstring> #define REP(a,n) for (int a = 0; a<(n); ++a) #define FOR(a,b,c) for (int a = (b); a<=(c); ++a) #define FORD(a,b,c) for (int a = (b); a>=(c); --a) #define FOREACH(a,v) for (auto a : v) #define MP make_pair #define PB push_back template<class T> inline int size(const T&t) { return t.size(); } using namespace std; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<string> vs; typedef long long LL; /////////////////////////////// int N; map<LL,LL> wyn; int main() { cin >> N; wyn.emplace(0,0); LL cur_en = 0; REP(a, N) { LL wej; cin >> wej; cur_en += wej; auto it = wyn.lower_bound(-cur_en); if (a == N - 1) cout << (it == wyn.end() ? -1 : N - 1 + it->second) << endl; else if (it != wyn.end()) { LL ile = it->second - 1; if (it != wyn.begin()) { --it; if (it->second == ile) wyn.erase(it); } wyn[-cur_en] = ile; } } } |