• Codeforces 749C. Voting 模拟题


    C. Voting
    time limit per test:
    1 second
    memory limit per test:
    256 megabytes
    input:
    standard input
    output:
    standard output

    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.

    题目链接: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 }
    View Code
    I am a slow walker,but I never walk backwards.
  • 相关阅读:
    拦截器getmodel方法什么时候被调用(没搞懂有什么鸟用,自己搭的项目中用到了这个)
    Convention插件的使用(会涉及content目录,jsp必须放入这个下面才能映射成功基于注解的配置)
    Spring组件扫描<context:component-scan/>使用详解
    Eclipse从数据库逆向生成Hibernate带注解的实体类
    HibernateTool的安装和使用(Eclipse中)
    将一字符串首字母转大写
    二分(折半)查找算法
    按位与运算符
    注解及自定义注解
    根据前序遍历和中序遍历得出后序遍历
  • 原文地址:https://www.cnblogs.com/GeekZRF/p/6283738.html
Copyright © 2020-2023  润新知