/* ----------------------- Autor: Tomasz Boguslawski -------------------------- */ #include<cstdio> #include<cstdlib> #include<iostream> #include<fstream> #include<iomanip> #include<string> #include<sstream> #include<cstring> #include<map> #include<vector> #include<set> #include<queue> #include<algorithm> #include <fstream> #include<math.h> #define LL long long #define FOR(x, b, e) for(LL x = b; x <= (e); x++) #define FORS(x, b, e, s) for(LL x = b; x <= (e); x+=s) #define FORD(x, b, e) for(LL x = b; x >= (e); x--) #define VAR(v, n) __typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define DEBUG if (debug) #define MIN(a,b) ((a>b)?b:a) #define MAX(a,b) ((a>b)?a:b) using namespace std; LL n; bool P[310000]; void loadData() { cin >> n; string s; cin >> s; FOR(i,1,n) P[i]=(s[i-1]=='P'); } LL ileP[310000]; LL ileL[310000]; void calcIleLP() { ileP[0]=0; FOR(i,1,n) if (P[i]) ileP[i]=ileP[i-1]+1; else ileP[i]=ileP[i-1]; ileL[n+1]=0; FORD(i,n,1) if (P[i]) ileL[i]=ileL[i+1]; else ileL[i]=ileL[i+1]+1; } LL madr[310000]; void madrak() { calcIleLP(); LL poLewo, poPrawo, mniej, wynik; FOR(x,1,n) { poLewo=ileP[x-1]; poPrawo=ileL[x+1]; mniej=MIN(poLewo, poPrawo); wynik=2*mniej; if (P[x] && (poPrawo>mniej)) wynik++; if (!(P[x]) && (poLewo>mniej)) wynik++; madr[x]=wynik; } } LL glup[310000]; bool P2[310000]; void gluptak() { FOR(i,1,n) { glup[i]=0; P2[i]=P[i];}; bool repeat=true; while(repeat) { repeat=false; LL i=1; while (i<n) { if (P2[i] && !(P2[i+1])) { repeat=true; P2[i]=false; glup[i]++; P2[i+1]=true; glup[i+1]++; i+=2; } else i+=1; } } } void makeRandom() { FOR(i,1,n) P[i]=(rand()%2==0); } void testuj() { n=300000; while(true) { makeRandom(); FOR(i,1,n) if (P[i]) cout << 'P'; else cout << 'L'; cout << "\n"; gluptak(); madrak(); FOR(i,1,n) cout << glup[i] << ' '; cout << "\n"; FOR(i,1,n) cout << madr[i] << ' '; cout << "\n"; FOR(i,1,n) if (glup[i]!=madr[i]) { cout << "BLAD!\n"; exit(1); }; } } void prod() { n=300000; makeRandom(); cout << n << "\n"; FOR(i,1,n) if (P[i]) cout << 'P'; else cout << 'L'; cout << "\n"; } /// MAIN int main(int argc, char* argv[]) { // magic formula, which makes streams work faster: ios_base::sync_with_stdio(0); loadData(); /* gluptak(); FOR(i,1,n) cout << glup[i] << ' '; cout << "\n"; */ madrak(); FOR(i,1,n) cout << madr[i] << ' '; cout << "\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 134 135 136 137 138 139 140 141 142 143 144 145 | /* ----------------------- Autor: Tomasz Boguslawski -------------------------- */ #include<cstdio> #include<cstdlib> #include<iostream> #include<fstream> #include<iomanip> #include<string> #include<sstream> #include<cstring> #include<map> #include<vector> #include<set> #include<queue> #include<algorithm> #include <fstream> #include<math.h> #define LL long long #define FOR(x, b, e) for(LL x = b; x <= (e); x++) #define FORS(x, b, e, s) for(LL x = b; x <= (e); x+=s) #define FORD(x, b, e) for(LL x = b; x >= (e); x--) #define VAR(v, n) __typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define DEBUG if (debug) #define MIN(a,b) ((a>b)?b:a) #define MAX(a,b) ((a>b)?a:b) using namespace std; LL n; bool P[310000]; void loadData() { cin >> n; string s; cin >> s; FOR(i,1,n) P[i]=(s[i-1]=='P'); } LL ileP[310000]; LL ileL[310000]; void calcIleLP() { ileP[0]=0; FOR(i,1,n) if (P[i]) ileP[i]=ileP[i-1]+1; else ileP[i]=ileP[i-1]; ileL[n+1]=0; FORD(i,n,1) if (P[i]) ileL[i]=ileL[i+1]; else ileL[i]=ileL[i+1]+1; } LL madr[310000]; void madrak() { calcIleLP(); LL poLewo, poPrawo, mniej, wynik; FOR(x,1,n) { poLewo=ileP[x-1]; poPrawo=ileL[x+1]; mniej=MIN(poLewo, poPrawo); wynik=2*mniej; if (P[x] && (poPrawo>mniej)) wynik++; if (!(P[x]) && (poLewo>mniej)) wynik++; madr[x]=wynik; } } LL glup[310000]; bool P2[310000]; void gluptak() { FOR(i,1,n) { glup[i]=0; P2[i]=P[i];}; bool repeat=true; while(repeat) { repeat=false; LL i=1; while (i<n) { if (P2[i] && !(P2[i+1])) { repeat=true; P2[i]=false; glup[i]++; P2[i+1]=true; glup[i+1]++; i+=2; } else i+=1; } } } void makeRandom() { FOR(i,1,n) P[i]=(rand()%2==0); } void testuj() { n=300000; while(true) { makeRandom(); FOR(i,1,n) if (P[i]) cout << 'P'; else cout << 'L'; cout << "\n"; gluptak(); madrak(); FOR(i,1,n) cout << glup[i] << ' '; cout << "\n"; FOR(i,1,n) cout << madr[i] << ' '; cout << "\n"; FOR(i,1,n) if (glup[i]!=madr[i]) { cout << "BLAD!\n"; exit(1); }; } } void prod() { n=300000; makeRandom(); cout << n << "\n"; FOR(i,1,n) if (P[i]) cout << 'P'; else cout << 'L'; cout << "\n"; } /// MAIN int main(int argc, char* argv[]) { // magic formula, which makes streams work faster: ios_base::sync_with_stdio(0); loadData(); /* gluptak(); FOR(i,1,n) cout << glup[i] << ' '; cout << "\n"; */ madrak(); FOR(i,1,n) cout << madr[i] << ' '; cout << "\n"; return 0; } |