博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
巧用队列之”Voting“
阅读量:4310 次
发布时间:2019-06-06

本文共 3918 字,大约阅读时间需要 13 分钟。

题目:

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 with n 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.

Example  Input

5

DDRRR

6

DDRRRR

Example  Output

D

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 employee2).
  4. Employee 4 denies employee 2 to vote.
  5. Employee 5 has no right to vote and skips his turn (he was denied by employee1).
  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党,进行选举,每个人都有权利踢出一个人,但是如果他被踢出去了,就丧失了这个权利,并且无法投票。问你哪个党派会在此次选举胜出。

解题思路:使用队列模拟这次选举,重点是当投完票之后,要回到队尾(除非被踢掉)。而且使用数字队列更为方便,从队首至队尾只需要自加一个N,也就是参与选举的人数。

AC代码:

1 import java.util.*; 2 import java.util.Queue; 3 import java.util.LinkedList;  4  5 public class Main{ 6     public static void main(String[] args){ 7         Scanner sc = new Scanner(System.in); 8         while(sc.hasNext()){ 9             int s = sc.nextInt();10             Queue
r = new LinkedList
();11 Queue
d = new LinkedList
();12 String enter = sc.nextLine();13 String in = sc.nextLine();14 char o[] = in.toCharArray();15 for(int i = 0;i < s;i ++){16 if(o[i] == 'D'){17 d.offer(i);18 }19 else if(o[i] == 'R'){20 r.offer(i);21 }22 }23 while(d.size() != 0 && r.size() != 0){24 int dd = d.peek();25 int rr = r.peek();26 d.poll();r.poll();27 if(dd < rr){d.offer(dd + s);}28 else{r.offer(rr + s);}29 }30 if(d.size() == 0){System.out.println("R");}31 else{System.out.println("D");}32 }33 }34 }

 

转载于:https://www.cnblogs.com/love-fromAtoZ/p/7245060.html

你可能感兴趣的文章
openstack报错解决三
查看>>
乙未年年终总结
查看>>
子网掩码
查看>>
第一天上班没精神
查看>>
启动eclipse报错:Failed to load the JNI shared library
查看>>
eclipse安装插件的两种方式在线和离线
查看>>
linux下源的相关笔记(suse)
查看>>
linux系统分区文件系统划分札记
查看>>
Linux(SUSE 12)安装Tomcat
查看>>
Linux(SUSE 12)安装jboss4并实现远程访问
查看>>
Neutron在给虚拟机分配网络时,底层是如何实现的?
查看>>
netfilter/iptables全攻略
查看>>
Overlay之VXLAN架构
查看>>
Eclipse : An error occurred while filtering resources(Maven错误提示)
查看>>
在eclipse上用tomcat部署项目404解决方案
查看>>
web.xml 配置中classpath: 与classpath*:的区别
查看>>
suse如何修改ssh端口为2222?
查看>>
详细理解“>/dev/null 2>&1”
查看>>
suse如何创建定时任务?
查看>>
suse搭建ftp服务器方法
查看>>