///MEMENTO MEMO, MEMENTO LONG LONG #include <bits/stdc++.h> #define DEBUG if(0) #define COUT cout << "\e[36m" #define ENDL "\e[39m" << endl #define VAR(v) " [\e[32m" << #v << "\e[36m=\e[91m" << v << "\e[36m] " using namespace std; typedef long long LL; const int MAXINT = 1e9+1; int n, m; vector<int> city, dp; struct Segtree { int l; vector<int> treeVec; void print() { DEBUG { int k = 1, j= 1; for (int i = 1; i < 2*l; ++i) { COUT << treeVec[i] << " "; j++; if(j > k) { COUT << ENDL; k*=2; j = 1; } } } } int queryMin(int b, int e) { b += l; e += l; int out = MAXINT; //DEBUG COUT<< VAR(b) << VAR(e) << VAR(l) << VAR(m) << ENDL; while(b <= e) { if(b%2 == 1) out = min(out, treeVec[b]); if(e%2 == 0) out = min(out, treeVec[e]); b = (b+1)/2; e = (e-1)/2; } return out; } void setMin(int pos, int val) { pos += l; while(pos > 0) { treeVec[pos] = min(treeVec[pos], val); pos/=2; } } Segtree(/**size is m*/) { l = 1; while(l < m) l*= 2; treeVec.resize(2*l, MAXINT); } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; city.resize(n+1); vector<LL> suff(n+2); for (int i = 1; i <= n; ++i) { cin >> city[i]; } map<LL, int> suffToId; suffToId[0] = -1; for (int i = n; i >= 0; --i) { suff[i] = city[i]+((i+1 < n+1)? suff[i+1] : 0); suffToId[suff[i]] = -1; } map<LL,int>::iterator it = suffToId.begin(); for (int i = 0; it != suffToId.end(); it++, i++) { it->second = i; } m = suffToId.size(); DEBUG { DEBUG COUT << "CITY" << ENDL; for(auto el : city) COUT << el << " "; COUT << ENDL; DEBUG COUT << "SUFF" << ENDL; for(auto el : suff) COUT << el << " "; COUT << ENDL; DEBUG COUT << "SUFFtoID" << ENDL; for(auto el : suffToId) COUT << "[" << el.first << ":" << el.second << "] "; COUT << ENDL; } dp.resize(n+1, MAXINT); dp[0] = 0; Segtree tree; for (int i = 1; i <= n; ++i) { ///set dp[i-1]+n-i on tree(sufftoid[suff[i]]) tree.setMin(suffToId[suff[i]], dp[i-1]+(n-i)); ///dp[i] = tree.min(B, sufftoid.size()-1) -(n-i) dp[i] = tree.queryMin(suffToId[suff[i+1]], m-1)-(n-i); DEBUG COUT << VAR(i) << VAR(dp[i]) << ENDL; DEBUG tree.print(); DEBUG COUT << ENDL; } cout << ((dp[n] > n-1)? -1 : dp[n]) << "\n"; 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 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 | ///MEMENTO MEMO, MEMENTO LONG LONG #include <bits/stdc++.h> #define DEBUG if(0) #define COUT cout << "\e[36m" #define ENDL "\e[39m" << endl #define VAR(v) " [\e[32m" << #v << "\e[36m=\e[91m" << v << "\e[36m] " using namespace std; typedef long long LL; const int MAXINT = 1e9+1; int n, m; vector<int> city, dp; struct Segtree { int l; vector<int> treeVec; void print() { DEBUG { int k = 1, j= 1; for (int i = 1; i < 2*l; ++i) { COUT << treeVec[i] << " "; j++; if(j > k) { COUT << ENDL; k*=2; j = 1; } } } } int queryMin(int b, int e) { b += l; e += l; int out = MAXINT; //DEBUG COUT<< VAR(b) << VAR(e) << VAR(l) << VAR(m) << ENDL; while(b <= e) { if(b%2 == 1) out = min(out, treeVec[b]); if(e%2 == 0) out = min(out, treeVec[e]); b = (b+1)/2; e = (e-1)/2; } return out; } void setMin(int pos, int val) { pos += l; while(pos > 0) { treeVec[pos] = min(treeVec[pos], val); pos/=2; } } Segtree(/**size is m*/) { l = 1; while(l < m) l*= 2; treeVec.resize(2*l, MAXINT); } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; city.resize(n+1); vector<LL> suff(n+2); for (int i = 1; i <= n; ++i) { cin >> city[i]; } map<LL, int> suffToId; suffToId[0] = -1; for (int i = n; i >= 0; --i) { suff[i] = city[i]+((i+1 < n+1)? suff[i+1] : 0); suffToId[suff[i]] = -1; } map<LL,int>::iterator it = suffToId.begin(); for (int i = 0; it != suffToId.end(); it++, i++) { it->second = i; } m = suffToId.size(); DEBUG { DEBUG COUT << "CITY" << ENDL; for(auto el : city) COUT << el << " "; COUT << ENDL; DEBUG COUT << "SUFF" << ENDL; for(auto el : suff) COUT << el << " "; COUT << ENDL; DEBUG COUT << "SUFFtoID" << ENDL; for(auto el : suffToId) COUT << "[" << el.first << ":" << el.second << "] "; COUT << ENDL; } dp.resize(n+1, MAXINT); dp[0] = 0; Segtree tree; for (int i = 1; i <= n; ++i) { ///set dp[i-1]+n-i on tree(sufftoid[suff[i]]) tree.setMin(suffToId[suff[i]], dp[i-1]+(n-i)); ///dp[i] = tree.min(B, sufftoid.size()-1) -(n-i) dp[i] = tree.queryMin(suffToId[suff[i+1]], m-1)-(n-i); DEBUG COUT << VAR(i) << VAR(dp[i]) << ENDL; DEBUG tree.print(); DEBUG COUT << ENDL; } cout << ((dp[n] > n-1)? -1 : dp[n]) << "\n"; return 0; } |