#include <iostream> using namespace std; char m[300001]; int wyn[300001], l, p; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin>>n; for(int i = 1; i <= n; i++) { cin>>m[i]; if(m[i] == 'P') p++; } for(int i = n; i > 0; i--) { if(m[i] == 'P') wyn[i] = min(2*l, 2*--p + 1); else wyn[i] = min(2*p, 2*l++ + 1); } for(int i = 1; i <= n; i++) cout<<wyn[i]<<' '; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include <iostream> using namespace std; char m[300001]; int wyn[300001], l, p; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin>>n; for(int i = 1; i <= n; i++) { cin>>m[i]; if(m[i] == 'P') p++; } for(int i = n; i > 0; i--) { if(m[i] == 'P') wyn[i] = min(2*l, 2*--p + 1); else wyn[i] = min(2*p, 2*l++ + 1); } for(int i = 1; i <= n; i++) cout<<wyn[i]<<' '; } |