#include <iostream> #include <string> using namespace std; int main() { int portfele[1000000] = {0}; int portfelPierwszyOryg = 0; bool cykl[1000000]; int liczbaKolegow = 0; int dlugoscCyklu = 0; string cyklString; int ileP = 0; int najchudszyPortfelPieniadze = 1e12; int najchudszyPortfelKtoryKolega = 1e12; cin >> liczbaKolegow; for(int i = 0; i < liczbaKolegow; i++) { cin >> portfele[i]; if(najchudszyPortfelPieniadze > portfele[i]) { najchudszyPortfelPieniadze = portfele[i]; najchudszyPortfelKtoryKolega = i + 1; } } portfelPierwszyOryg = portfele[0]; cin >> dlugoscCyklu; cin >> cyklString; for(int i = 0; i < dlugoscCyklu; i++) { if(cyklString[i] == 'P') { cykl[i] = false; ileP++; } else { cykl[i] = true; } } if(ileP == 0) { cout << "-1" << endl; return 0; } if(ileP == dlugoscCyklu) { cout << liczbaKolegow * (najchudszyPortfelPieniadze - 1) + najchudszyPortfelKtoryKolega << endl; return 0; } bool probableInfiniteLoop = ileP <= dlugoscCyklu / 2; int dlugoscCykluKwadrat = dlugoscCyklu * dlugoscCyklu; for(int i = 1; ; i++) { if(probableInfiniteLoop && i == dlugoscCykluKwadrat && portfelPierwszyOryg <= portfele[0]) { cout << "-1" << endl; break; } int ktoryKrokCyklu = (i - 1) % dlugoscCyklu; int ktoryKolega = (i - 1) % liczbaKolegow; if(cykl[ktoryKrokCyklu]) portfele[ktoryKolega]++; else portfele[ktoryKolega]--; if(portfele[ktoryKolega] == 0) { cout << i << endl; return 0; } } 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 | #include <iostream> #include <string> using namespace std; int main() { int portfele[1000000] = {0}; int portfelPierwszyOryg = 0; bool cykl[1000000]; int liczbaKolegow = 0; int dlugoscCyklu = 0; string cyklString; int ileP = 0; int najchudszyPortfelPieniadze = 1e12; int najchudszyPortfelKtoryKolega = 1e12; cin >> liczbaKolegow; for(int i = 0; i < liczbaKolegow; i++) { cin >> portfele[i]; if(najchudszyPortfelPieniadze > portfele[i]) { najchudszyPortfelPieniadze = portfele[i]; najchudszyPortfelKtoryKolega = i + 1; } } portfelPierwszyOryg = portfele[0]; cin >> dlugoscCyklu; cin >> cyklString; for(int i = 0; i < dlugoscCyklu; i++) { if(cyklString[i] == 'P') { cykl[i] = false; ileP++; } else { cykl[i] = true; } } if(ileP == 0) { cout << "-1" << endl; return 0; } if(ileP == dlugoscCyklu) { cout << liczbaKolegow * (najchudszyPortfelPieniadze - 1) + najchudszyPortfelKtoryKolega << endl; return 0; } bool probableInfiniteLoop = ileP <= dlugoscCyklu / 2; int dlugoscCykluKwadrat = dlugoscCyklu * dlugoscCyklu; for(int i = 1; ; i++) { if(probableInfiniteLoop && i == dlugoscCykluKwadrat && portfelPierwszyOryg <= portfele[0]) { cout << "-1" << endl; break; } int ktoryKrokCyklu = (i - 1) % dlugoscCyklu; int ktoryKolega = (i - 1) % liczbaKolegow; if(cykl[ktoryKrokCyklu]) portfele[ktoryKolega]++; else portfele[ktoryKolega]--; if(portfele[ktoryKolega] == 0) { cout << i << endl; return 0; } } return 0; } |