#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int gcd(int a, int b){
return b == 0 ? a : gcd(b, a%b);
}
int main(void){
int n,k,i,j,tmp,petla=0;
//string cykl;
vector<int> kasa, kasa_temp;
int blocked=0;
scanf("%d",&n);
bool is_blocked[n];
kasa.resize(n);
kasa_temp.resize(n);
for(i=0;i<n;i++) { scanf("%d",&kasa[i]); kasa_temp[i]=kasa[i]; is_blocked[i]=false;}
scanf("%d",&k);
char cykl[k+2];
for(i=0;i<=k;i++) scanf("%c",&cykl[i]);
for(i=1;i<=k;i++) cykl[i-1]=cykl[i]; //printf("%c",cykl[i]);
tmp=((n*k)/gcd(n,k));
while(blocked<n){
petla++;
for(i=0;(i<tmp+n)/*&&(!is_blocked[i%n])*/;i++){
if(!is_blocked[i%n]){
if(i<tmp){
if(cykl[i%k]=='P') {
kasa_temp[i%n]--;
//printf("|%d|",i%n);
if(kasa_temp[i%n]<=0) goto stop;
}
else kasa_temp[i%n]++;
//printf("%d. [%d losowal %c] %d %d = %d\n", i, i%n, cykl[i%k], kasa[i%n]+1,-1,kasa[i%n]);
}
else{
if(kasa_temp[i%n]>=kasa[i%n]){
is_blocked[i%n]=true;
blocked++;
}
kasa=kasa_temp;
}
}
}
}
printf("%d",-1);
return 0;
stop:
printf("%d",((petla-1)*tmp)+i+1);
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 | #include <stdio.h> #include <iostream> #include <vector> #include <string> using namespace std; int gcd(int a, int b){ return b == 0 ? a : gcd(b, a%b); } int main(void){ int n,k,i,j,tmp,petla=0; //string cykl; vector<int> kasa, kasa_temp; int blocked=0; scanf("%d",&n); bool is_blocked[n]; kasa.resize(n); kasa_temp.resize(n); for(i=0;i<n;i++) { scanf("%d",&kasa[i]); kasa_temp[i]=kasa[i]; is_blocked[i]=false;} scanf("%d",&k); char cykl[k+2]; for(i=0;i<=k;i++) scanf("%c",&cykl[i]); for(i=1;i<=k;i++) cykl[i-1]=cykl[i]; //printf("%c",cykl[i]); tmp=((n*k)/gcd(n,k)); while(blocked<n){ petla++; for(i=0;(i<tmp+n)/*&&(!is_blocked[i%n])*/;i++){ if(!is_blocked[i%n]){ if(i<tmp){ if(cykl[i%k]=='P') { kasa_temp[i%n]--; //printf("|%d|",i%n); if(kasa_temp[i%n]<=0) goto stop; } else kasa_temp[i%n]++; //printf("%d. [%d losowal %c] %d %d = %d\n", i, i%n, cykl[i%k], kasa[i%n]+1,-1,kasa[i%n]); } else{ if(kasa_temp[i%n]>=kasa[i%n]){ is_blocked[i%n]=true; blocked++; } kasa=kasa_temp; } } } } printf("%d",-1); return 0; stop: printf("%d",((petla-1)*tmp)+i+1); return 0; } |
English