//Krzysztof Pieprzak #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef unsigned long long ull; typedef unsigned int uint; typedef vector<int> vi; typedef vector<ll> vll; typedef pair<int,int> pii; typedef pair<long long, long long> pll; typedef vector<pii> vpii; typedef vector<pll> vpll; #define Size(x) (int)x.size() #define VAR(v,n) auto v = (n) #define FOR(i,a,b) for(VAR(i,a); i < (b); ++i) #define FORE(i,a,b) for(VAR(i,a); i <= (b); ++i) #define FORREV(i,a,b) for(VAR(i,b); i >= (a); --i) #define FORSTEP(i,a,b,step) for(VAR(i,a); i < (b); i += (step)) #define FOREACH(i,c) for(VAR(i,(c).begin()); i != (c).end(); ++i) #define FOREACHS(i,c,n) for(VAR(i,&(c)[0]); i-(c)<(n); ++i) #define ALL(x) x.begin(),x.end() #define CLEAR(t) memset(t, 0, sizeof(t)) #define F first #define S second #define MP make_pair #define PUB push_back #define POB pop_back #define pieprzu ios_base::sync_with_stdio(0); const int INF = 1000000001; const double EPS = 10e-9; const int MAXN = 1000000; const int MAXM = 1000000; const int SHIFT = MAXN+MAXM+5; int t[MAXN+5]; char s[MAXM+5]; int n; int mapBoyToLetter[MAXN+5]; vi mapLetterToBoys[MAXM+5]; int visBoy[MAXN+5]; int visLetter[MAXM+5]; vi lettersCycle; int lettersCycleLen; int prefSums[MAXN+MAXM+5]; int mxMinusRound[MAXN+5]; list<int> l; int tmpMinus[MAXN+MAXM+5]; vector<int> q[2*(MAXN+MAXM+5)]; int begs[2*(MAXN+MAXM+5)]; vi qToClean; void pre(int boyId, int n, int m) { lettersCycle.resize(0); visBoy[boyId] = 1; for (int letterId = boyId % m; !visLetter[letterId]; letterId = (letterId + n) % m) { visLetter[letterId] = 1; lettersCycle.PUB(letterId); } for (auto letter : lettersCycle) { for (auto boyId : mapLetterToBoys[letter]) { visBoy[boyId] = 1; } } lettersCycleLen = Size(lettersCycle); FOR (i, 0, lettersCycleLen) lettersCycle.PUB(lettersCycle[i]); } inline int getVal(int letterId) { return s[letterId] == 'W' ? 1 : -1; } inline int getSum(int bg, int ed) { return prefSums[ed] - (bg < 1 ? 0 : prefSums[bg-1]); } void updateList(int id) { if (getVal(lettersCycle[id]) == 1) l.PUB(id); else { l.POB(); if (!l.empty()) l.POB(); l.PUB(id); } } void updateQueue(int id) { if (getVal(lettersCycle[id]) == -1) { int qId = prefSums[id] + SHIFT; q[qId].PUB(id); tmpMinus[id] = qId; qToClean.PUB(qId); } else tmpMinus[id] = -1; } ll countRes() { int lettersCycleVal = 0; FOR (i, 0, lettersCycleLen) lettersCycleVal += getVal(lettersCycle[i]); //mxMinus prefSums[0] = getVal(lettersCycle[0]); FOR (i, 1, 2*lettersCycleLen) prefSums[i] = prefSums[i-1] + getVal(lettersCycle[i]); l.clear(); l.PUB(-1); FOR (i, 0, lettersCycleLen) { updateList(i); } FOR (i, 0, lettersCycleLen) { if (i - l.front() > 0) l.pop_front(); mxMinusRound[i] = getSum(i, l.front()); updateList(i + lettersCycleLen); } //number of full rounds int numberOfRounds = INF; FOR (i, 0, lettersCycleLen) { if (mapLetterToBoys[lettersCycle[i]].empty()) continue; int mnCoins = t[mapLetterToBoys[lettersCycle[i]][0]]; if (mnCoins <= -mxMinusRound[i]) { numberOfRounds = 0; break; } if (lettersCycleVal < 0) { numberOfRounds = min(numberOfRounds, (mnCoins+mxMinusRound[i])/(-lettersCycleVal) + ((mnCoins+mxMinusRound[i])%(-lettersCycleVal) ? 1 : 0)); } } if (numberOfRounds == INF) return -1; //rewind full rounds int boys = 0; FOR (i, 0, lettersCycleLen) { for (auto boyId : mapLetterToBoys[lettersCycle[i]]) { t[boyId] += numberOfRounds*lettersCycleVal; ++boys; } } //number of steps in last round for (auto qId : qToClean) { q[qId].resize(0); begs[qId] = 0; } qToClean.resize(0); FOR (i, 0, lettersCycleLen) { updateQueue(i); } int base = 0; ll res = -1; FOR (i, 0, lettersCycleLen) { if (!mapLetterToBoys[lettersCycle[i]].empty()) { int boyId = mapLetterToBoys[lettersCycle[i]][0]; int qId = base - t[boyId] + SHIFT; if (Size(q[qId]) > begs[qId]) { int step = q[qId][begs[qId]] - i; ll tmpRes = 1LL * step*n + boyId+1; if (res == -1) res = tmpRes; res = min(res, tmpRes); } } updateQueue(i + lettersCycleLen); if (tmpMinus[i] > -1) ++begs[tmpMinus[i]]; base += getVal(lettersCycle[i]); } return 1LL*numberOfRounds*lettersCycleLen*n + res; } void rob(int test) { scanf("%d", &n); FOR (i, 0, n) { scanf("%d", &t[i]); } int m; scanf("%d", &m); scanf("%s", s); FOR (i, 0, n) { mapBoyToLetter[i] = i % m; mapLetterToBoys[i % m].PUB(i); } FOR (i, 0, m) { if (Size(mapLetterToBoys[i]) > 1) { int idMn = 0; FOR (j, 0, Size(mapLetterToBoys[i])) { if (t[mapLetterToBoys[i][j]] < t[mapLetterToBoys[i][idMn]]) { idMn = j; } } swap(mapLetterToBoys[i][0], mapLetterToBoys[i][idMn]); } } ll res = -1; FOR (i, 0, n) { if (!visBoy[i]) { pre(i, n, m); ll tmpRes = countRes(); if (tmpRes != -1) res = (res == -1 ? tmpRes : min(res, tmpRes)); } } printf("%lld\n", res); } int main() { int test = 1; //scanf("%d", &test); FORE (i, 1, test) rob(i); 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 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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | //Krzysztof Pieprzak #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef unsigned long long ull; typedef unsigned int uint; typedef vector<int> vi; typedef vector<ll> vll; typedef pair<int,int> pii; typedef pair<long long, long long> pll; typedef vector<pii> vpii; typedef vector<pll> vpll; #define Size(x) (int)x.size() #define VAR(v,n) auto v = (n) #define FOR(i,a,b) for(VAR(i,a); i < (b); ++i) #define FORE(i,a,b) for(VAR(i,a); i <= (b); ++i) #define FORREV(i,a,b) for(VAR(i,b); i >= (a); --i) #define FORSTEP(i,a,b,step) for(VAR(i,a); i < (b); i += (step)) #define FOREACH(i,c) for(VAR(i,(c).begin()); i != (c).end(); ++i) #define FOREACHS(i,c,n) for(VAR(i,&(c)[0]); i-(c)<(n); ++i) #define ALL(x) x.begin(),x.end() #define CLEAR(t) memset(t, 0, sizeof(t)) #define F first #define S second #define MP make_pair #define PUB push_back #define POB pop_back #define pieprzu ios_base::sync_with_stdio(0); const int INF = 1000000001; const double EPS = 10e-9; const int MAXN = 1000000; const int MAXM = 1000000; const int SHIFT = MAXN+MAXM+5; int t[MAXN+5]; char s[MAXM+5]; int n; int mapBoyToLetter[MAXN+5]; vi mapLetterToBoys[MAXM+5]; int visBoy[MAXN+5]; int visLetter[MAXM+5]; vi lettersCycle; int lettersCycleLen; int prefSums[MAXN+MAXM+5]; int mxMinusRound[MAXN+5]; list<int> l; int tmpMinus[MAXN+MAXM+5]; vector<int> q[2*(MAXN+MAXM+5)]; int begs[2*(MAXN+MAXM+5)]; vi qToClean; void pre(int boyId, int n, int m) { lettersCycle.resize(0); visBoy[boyId] = 1; for (int letterId = boyId % m; !visLetter[letterId]; letterId = (letterId + n) % m) { visLetter[letterId] = 1; lettersCycle.PUB(letterId); } for (auto letter : lettersCycle) { for (auto boyId : mapLetterToBoys[letter]) { visBoy[boyId] = 1; } } lettersCycleLen = Size(lettersCycle); FOR (i, 0, lettersCycleLen) lettersCycle.PUB(lettersCycle[i]); } inline int getVal(int letterId) { return s[letterId] == 'W' ? 1 : -1; } inline int getSum(int bg, int ed) { return prefSums[ed] - (bg < 1 ? 0 : prefSums[bg-1]); } void updateList(int id) { if (getVal(lettersCycle[id]) == 1) l.PUB(id); else { l.POB(); if (!l.empty()) l.POB(); l.PUB(id); } } void updateQueue(int id) { if (getVal(lettersCycle[id]) == -1) { int qId = prefSums[id] + SHIFT; q[qId].PUB(id); tmpMinus[id] = qId; qToClean.PUB(qId); } else tmpMinus[id] = -1; } ll countRes() { int lettersCycleVal = 0; FOR (i, 0, lettersCycleLen) lettersCycleVal += getVal(lettersCycle[i]); //mxMinus prefSums[0] = getVal(lettersCycle[0]); FOR (i, 1, 2*lettersCycleLen) prefSums[i] = prefSums[i-1] + getVal(lettersCycle[i]); l.clear(); l.PUB(-1); FOR (i, 0, lettersCycleLen) { updateList(i); } FOR (i, 0, lettersCycleLen) { if (i - l.front() > 0) l.pop_front(); mxMinusRound[i] = getSum(i, l.front()); updateList(i + lettersCycleLen); } //number of full rounds int numberOfRounds = INF; FOR (i, 0, lettersCycleLen) { if (mapLetterToBoys[lettersCycle[i]].empty()) continue; int mnCoins = t[mapLetterToBoys[lettersCycle[i]][0]]; if (mnCoins <= -mxMinusRound[i]) { numberOfRounds = 0; break; } if (lettersCycleVal < 0) { numberOfRounds = min(numberOfRounds, (mnCoins+mxMinusRound[i])/(-lettersCycleVal) + ((mnCoins+mxMinusRound[i])%(-lettersCycleVal) ? 1 : 0)); } } if (numberOfRounds == INF) return -1; //rewind full rounds int boys = 0; FOR (i, 0, lettersCycleLen) { for (auto boyId : mapLetterToBoys[lettersCycle[i]]) { t[boyId] += numberOfRounds*lettersCycleVal; ++boys; } } //number of steps in last round for (auto qId : qToClean) { q[qId].resize(0); begs[qId] = 0; } qToClean.resize(0); FOR (i, 0, lettersCycleLen) { updateQueue(i); } int base = 0; ll res = -1; FOR (i, 0, lettersCycleLen) { if (!mapLetterToBoys[lettersCycle[i]].empty()) { int boyId = mapLetterToBoys[lettersCycle[i]][0]; int qId = base - t[boyId] + SHIFT; if (Size(q[qId]) > begs[qId]) { int step = q[qId][begs[qId]] - i; ll tmpRes = 1LL * step*n + boyId+1; if (res == -1) res = tmpRes; res = min(res, tmpRes); } } updateQueue(i + lettersCycleLen); if (tmpMinus[i] > -1) ++begs[tmpMinus[i]]; base += getVal(lettersCycle[i]); } return 1LL*numberOfRounds*lettersCycleLen*n + res; } void rob(int test) { scanf("%d", &n); FOR (i, 0, n) { scanf("%d", &t[i]); } int m; scanf("%d", &m); scanf("%s", s); FOR (i, 0, n) { mapBoyToLetter[i] = i % m; mapLetterToBoys[i % m].PUB(i); } FOR (i, 0, m) { if (Size(mapLetterToBoys[i]) > 1) { int idMn = 0; FOR (j, 0, Size(mapLetterToBoys[i])) { if (t[mapLetterToBoys[i][j]] < t[mapLetterToBoys[i][idMn]]) { idMn = j; } } swap(mapLetterToBoys[i][0], mapLetterToBoys[i][idMn]); } } ll res = -1; FOR (i, 0, n) { if (!visBoy[i]) { pre(i, n, m); ll tmpRes = countRes(); if (tmpRes != -1) res = (res == -1 ? tmpRes : min(res, tmpRes)); } } printf("%lld\n", res); } int main() { int test = 1; //scanf("%d", &test); FORE (i, 1, test) rob(i); return 0; } |