• #388 C Voting(思维题)


    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:

    1. 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).
    2. 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.
    3. 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.
    4. 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.

    Input

    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.

    Output

    Print 'D' if the outcome of the vote will be suitable for depublicans and 'R' if remocrats will win.

    Examples
    input
    5
    DDRRR
    output
    D
    input
    6
    DDRRRR
    output
    R
    Note

    Consider one of the voting scenarios for the first sample:

    1. Employee 1 denies employee 5 to vote.
    2. Employee 2 denies employee 3 to vote.
    3. Employee 3 has no right to vote and skips his turn (he was denied by employee 2).
    4. Employee 4 denies employee 2 to vote.
    5. Employee 5 has no right to vote and skips his turn (he was denied by employee 1).
    6. Employee 1 denies employee 4.
    7. Only employee 1 now has the right to vote so the voting ends with the victory of depublicans.

     题意:给一个字符串其中D代表一方,R代表另一方,一方能把另一方踢出局。从第一个开始求最后留下的是哪一方?

    思路:逐个进行模拟,如果是R则ans++,否则ans--,如果ans>0&&s[i]==R则ans1++,如果ans<0&&s[i]==D则ans2++,直到ans1||ans2为零结束

    AC代码:

     1 #include <iostream>
     2 //#include <bits/stdc++.h>
     3 #include <cstdio>
     4 #include <cstring>
     5 #include <cmath>
     6 #include <algorithm>
     7 #include <queue>
     8 using namespace std;
     9 char s[200005];
    10 char ch[200005];
    11 int main()
    12 {
    13     queue<int>q;
    14     int n;
    15     while(~scanf("%d",&n))
    16     {
    17         scanf("%s",s);
    18         int len=strlen(s);
    19         int ans;
    20         ans=0;
    21         int ans1,ans2;
    22         ans1=ans2=1;
    23         while(ans1&&ans2)
    24         {
    25             int k=0;
    26             ans1=ans2=0;
    27             for(int i=0; i<len; i++)
    28             {
    29                 if(s[i]=='D')
    30                     ans++;
    31                 else
    32                     ans--;
    33                 if(ans>0&&s[i]=='D')
    34                 {
    35                     s[k++]='D';
    36                     ans1++;
    37                 }
    38                 else if(ans<0&&s[i]=='R')
    39                 {
    40                     s[k++]='R';
    41                     ans2++;
    42                 }
    43             }
    44             len=k;
    45         }
    46         if(ans1==0)
    47             printf("R
    ");
    48         else
    49             printf("D
    ");
    50     }
    51     return 0;
    52 }
    View Code
  • 相关阅读:
    杭电1058
    动态规划之背包模版
    按字典序依次打印只由1~n组成的n位数
    杭电1029
    杭电1257
    杭电2191
    杭电1114
    杭电2602
    南阳975
    杭电2138
  • 原文地址:https://www.cnblogs.com/wang-ya-wei/p/6212746.html
Copyright © 2020-2023  润新知