#include <iostream> #include <list> using namespace std; int n,m; int* a; char* c; int getNextL(int l) { int ile = n%m; l += ile; if(l>=m)l = l%m; return l; } int normalized(int l) { return l%m; } int getNumber(int k) { int result = a[k]; if(c[k] == 'P') result -=2; return result; } int main() { std::ios_base::sync_with_stdio(false); cin>>n; a = new int[n]; for(int k = 0; k<n; k++) { cin>>a[k]; } cin>>m; c = new char[m]; for(int k = 0; k<m; k++) { cin>>c[k]; } long long bestResult = -1; for(int k=0;k<n;k++) { int dlugoscCyklu=1; int petla = 0; int lowest = 0; int l = normalized(k); if(c[l] == 'P') petla --; else petla++; while((l = getNextL(l)) != normalized(k)) { //cout<<l<<" "<<c[l]<<endl; if(c[l] == 'P') petla --; else petla++; if(petla < lowest) lowest = petla; dlugoscCyklu++; } //cout<<petla<<" "<<dlugoscCyklu<<" "<<lowest<<endl; if (petla >= 0 && lowest*(-1) < a[k]) continue; long long licznik = a[normalized(k)]; long long ile = (a[k]-lowest*(-1))/(petla*(-1)); licznik = a[k] - ile * petla * (-1); //cout<<a[k]<<" "<<ile<<" "<<licznik<<endl; //cout<<"startowy licznik: "<<licznik<<endl; long long currentResult = k + 1 + ile * dlugoscCyklu * n; if (licznik>0) { l = normalized(k); if(c[l] == 'P') licznik --; else licznik++; while(licznik>0) { l = getNextL(l); if(c[l] == 'P') licznik --; else licznik++; currentResult +=n; } } else currentResult -=n * dlugoscCyklu; //cout<<currentResult<<endl; if(bestResult == -1) bestResult = currentResult; if(currentResult < bestResult) bestResult = currentResult; } cout<<bestResult<<endl; }
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 | #include <iostream> #include <list> using namespace std; int n,m; int* a; char* c; int getNextL(int l) { int ile = n%m; l += ile; if(l>=m)l = l%m; return l; } int normalized(int l) { return l%m; } int getNumber(int k) { int result = a[k]; if(c[k] == 'P') result -=2; return result; } int main() { std::ios_base::sync_with_stdio(false); cin>>n; a = new int[n]; for(int k = 0; k<n; k++) { cin>>a[k]; } cin>>m; c = new char[m]; for(int k = 0; k<m; k++) { cin>>c[k]; } long long bestResult = -1; for(int k=0;k<n;k++) { int dlugoscCyklu=1; int petla = 0; int lowest = 0; int l = normalized(k); if(c[l] == 'P') petla --; else petla++; while((l = getNextL(l)) != normalized(k)) { //cout<<l<<" "<<c[l]<<endl; if(c[l] == 'P') petla --; else petla++; if(petla < lowest) lowest = petla; dlugoscCyklu++; } //cout<<petla<<" "<<dlugoscCyklu<<" "<<lowest<<endl; if (petla >= 0 && lowest*(-1) < a[k]) continue; long long licznik = a[normalized(k)]; long long ile = (a[k]-lowest*(-1))/(petla*(-1)); licznik = a[k] - ile * petla * (-1); //cout<<a[k]<<" "<<ile<<" "<<licznik<<endl; //cout<<"startowy licznik: "<<licznik<<endl; long long currentResult = k + 1 + ile * dlugoscCyklu * n; if (licznik>0) { l = normalized(k); if(c[l] == 'P') licznik --; else licznik++; while(licznik>0) { l = getNextL(l); if(c[l] == 'P') licznik --; else licznik++; currentResult +=n; } } else currentResult -=n * dlugoscCyklu; //cout<<currentResult<<endl; if(bestResult == -1) bestResult = currentResult; if(currentResult < bestResult) bestResult = currentResult; } cout<<bestResult<<endl; } |