There are n employees in Alternative Cake Manufacturing (ACM). They are now voting on some very important question and the leading world media are trying to predict the outcome of the vote.
Each of the employees belongs to one of two fractions: depublicans or remocrats, and these two fractions have opposite opinions on what should be the outcome of the vote. The voting procedure is rather complicated:
- Each of n employees makes a statement. They make statements one by one starting from employees 1 and finishing with employee n. If at the moment when it's time for the i-th employee to make a statement he no longer has the right to vote, he just skips his turn (and no longer takes part in this voting).
- When employee makes a statement, he can do nothing or declare that one of the other employees no longer has a right to vote. It's allowed to deny from voting people who already made the statement or people who are only waiting to do so. If someone is denied from voting he no longer participates in the voting till the very end.
- When all employees are done with their statements, the procedure repeats: again, each employees starting from 1 and finishing withn who are still eligible to vote make their statements.
- The process repeats until there is only one employee eligible to vote remaining and he determines the outcome of the whole voting. Of course, he votes for the decision suitable for his fraction.
You know the order employees are going to vote and that they behave optimal (and they also know the order and who belongs to which fraction). Predict the outcome of the vote.
The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of employees.
The next line contains n characters. The i-th character is 'D' if the i-th employee is from depublicans fraction or 'R' if he is from remocrats.
Print 'D' if the outcome of the vote will be suitable for depublicans and 'R' if remocrats will win.
5
DDRRR
D
6
DDRRRR
R
Consider one of the voting scenarios for the first sample:
- Employee 1 denies employee 5 to vote.
- Employee 2 denies employee 3 to vote.
- Employee 3 has no right to vote and skips his turn (he was denied by employee 2).
- Employee 4 denies employee 2 to vote.
- Employee 5 has no right to vote and skips his turn (he was denied by employee 1).
- Employee 1 denies employee 4.
- Only employee 1 now has the right to vote so the voting ends with the victory of depublicans.
题目链接:http://codeforces.com/problemset/problem/749/C
题意:有n个属于D或R部分的员工进行投票。从1到n依次投票,他们可以给不与自己相同部分的人进行投票,被投票的人将会被剔除出去,不参与投票。一直重复上述步骤,直至不能再进行投票。求最后胜出的部分。
思路:假设D进行投票,投给R,只有投给D后面的R才可能保证D留下的足够多。因为D后面的R会对D进行投票,把D剔除出去。
代码:
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N = 200000+10; 4 char str[N]; 5 int sign[N]; 6 int main() 7 { 8 int n; 9 memset(str,0,sizeof(str)); 10 memset(sign,0,sizeof(sign)); 11 scanf("%d %s",&n,str+1); 12 int R1=0,R2=0,D1=0,D2=0; 13 ///R1表示剩下的R,R2表示多少位R没有投后面的D;D同理 14 char flag=0; 15 int cou=0; 16 while(!(R1&&D1)) 17 { 18 for(int i=1; i<=n; i++) 19 { 20 if(sign[i]==1) continue; 21 if(cou>=1&&flag!=str[i]) 22 { 23 sign[i]=1; 24 cou--; 25 continue; 26 } 27 if(str[i]=='R') 28 { 29 if(D2>=1) sign[i]=1,D2--; 30 else R1++,R2++; 31 } 32 else 33 { 34 if(R2>=1) sign[i]=1,R2--; 35 else D1++,D2++; 36 } 37 } 38 if(!(R1&&D1)) break; 39 cou=R2+D2; 40 flag=(R2!=0)?'R':'D'; 41 D1=D2=R1=R2=0; 42 } 43 if(R1!=0) cout<<"R"<<endl; 44 else cout<<"D"<<endl; 45 return 0; 46 }