SRM 504 DIV1 Easy - MathContest

問題


http://community.topcoder.com/stat?c=problem_statement&pm=11233&rd=14433

解き方


キューが連続的になっているということに気をつければ、
現在左から見るか右から見るかの情報、
ひっくり返っているかそうでないかの情報を持つことで
配列を操作せずに解くことができる。

コード


string str;

class MathContest {

public: int countBlack(string ballSequence, int repetitions) {
int ret=0;

str="";
FORE(i,0,repetitions)str+=ballSequence;
int n=str.size();

int turn=1,dir=1;
int l=0,r=n-1;
while(l<=r){
if(dir){
if(turn==(str[l]=='B')){
turn=1-turn;
ret++;
}
else dir=1-dir;
l++;
}else{
if(turn==(str[r]=='B')){
turn=1-turn;
ret++;
}
else dir=1-dir;
r--;
}
}

return ret;
}

};

Share this

Related Posts