A - Voting(queue)
Problem description
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 with n 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.
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:
- 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.
解题思路:仔细读一下题目,还是挺简单的。就是有两个门派D和R,给定一个字符串(只由D和R组成),只要位置靠前的人就可以将后面的对手deny掉,当轮到这个对手时,由于已被前面的人deny掉,所以此人再也没有deny他人的权利,直接跳过。不断循环,后面的人也可以deny前面还有选择权利的人,怎么实现呢?我们用队列来维护它们的位置(下标),当前面的人deny后面的对手后,此时就弹出两个队的队首元素(表示位置序号都已失效),但deny别人的人还有选择的权利,也可能会被后面的对手deny掉,于是只需将其位置序号加上n后入自己的队列中(满足位置序号比后面大,即后面的人可以deny掉"前面"还有选择权的对手)。最终,哪个队列不为空,谁就拥有vote的权利。
AC代码:
#include<bits/stdc++.h>
using namespace std;
int n;char s[];
queue<int> d,r;
int main(){
cin>>n;getchar();
cin>>s;
for(int i=;i<n;++i){
if(s[i]=='D')d.push(i);
else r.push(i);
}
while(!d.empty()&&!r.empty()){
int dd=d.front(),rr=r.front();
if(dd<rr){d.pop();r.pop();d.push(n+dd);}
else{d.pop();r.pop();r.push(n+rr);}
}
if(!d.empty())cout<<'D'<<endl;
else cout<<'R'<<endl;
return ;
}
A - Voting(queue)的更多相关文章
- Nodejs事件引擎libuv源码剖析之:高效队列(queue)的实现
声明:本文为原创博文,转载请注明出处. 在libuv中,有一个只使用简单的宏封装成的高效队列(queue),现在我们就来看一下它是怎么实现的. 首先,看一下queue中最基本的几个宏: typede ...
- Berkeley DB的数据存储结构——哈希表(Hash Table)、B树(BTree)、队列(Queue)、记录号(Recno)
Berkeley DB的数据存储结构 BDB支持四种数据存储结构及相应算法,官方称为访问方法(Access Method),分别是哈希表(Hash Table).B树(BTree).队列(Queue) ...
- [置顶] ※数据结构※→☆线性表结构(queue)☆============队列 顺序存储结构(queue sequence)(八)
队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表.进行插入操作的端称为队尾,进行删除操作的 ...
- [置顶] ※数据结构※→☆线性表结构(queue)☆============循环队列 顺序存储结构(queue circular sequence)(十)
循环队列 为充分利用向量空间,克服"假溢出"现象的方法是:将向量空间想象为一个首尾相接的圆环,并称这种向量为循环向量.存储在其中的队列称为循环队列(Circular Queue). ...
- [置顶] ※数据结构※→☆线性表结构(queue)☆============优先队列 链式存储结构(queue priority list)(十二)
优先队列(priority queue) 普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除.在优先队列中,元素被赋予优先级.当访问元素时,具有最高优先级的元素最先删除.优先队列具有 ...
- PHP实现队列(Queue)数据结构
队列(Queue),是一种特殊的先进先出线性表,其只能在前端进行删除操作(一般称为出队),在后端进行插入操作(一般称为入队).进行删除操作的端称为队头,进行插入操作的端称为队尾.队列,是按照先进先出或 ...
- C# 堆栈(Stack)和队列(Queue)
一.什么是堆?(Heap) 堆是无序的,是一片不连续的内存域,由用户自己来控制和释放,如果用户自己不释放的话,当内存达到一定的特定值时,通过垃圾回收器(GC)来回收. 是程序运行期 ...
- python 数据结构 队列(queue)
如需转发,请注明出处:小婷儿的python https://www.cnblogs.com/xxtalhr/p/10293817.html 欢迎关注小婷儿的博客: 有问题请在博客下留言或加作者微信:t ...
- C# 队列(Queue)和 堆栈(Stack)
C# 队列(Queue)和 堆栈(Stack) C# 队列(Queue) 队列(Queue)代表了一个先进先出的对象集合.当您需要对各项进行先进先出的访问时,则使用队列.当您在列表中添加一项,称为入队 ...
随机推荐
- 换个语言学一下 Golang (4)——变量与常量
一.变量定义 所谓的变量就是一个拥有指定名称和类型的数据存储位置. //看一个例子 package main import ( "fmt" ) func main() { var ...
- 新建springcloud 找不到请求路径
新建 启动类 controller 都不可以直接放在 java 目录下 否则启动失败
- LeetCode--寻找数组中心索引
给定一个整数类型的数组 nums,请编写一个能够返回数组“中心索引”的方法. 我们是这样定义数组中心索引的:数组中心索引的左侧所有元素相加的和等于右侧所有元素相加的和. 如果数组不存在中心索引,那么我 ...
- mysql在windows上安装
一.在window上安装mysql MySQL是一个小巧玲珑但功能强大的数据库,目前十分流行.但是官网给出的安装包有两种格式,一个是msi格式,一个是zip格式的.很多人下了zip格式的解压发现没有s ...
- Hibernate连接池断开自动重连
异常: javax.servlet.ServletException: org.springframework.transaction.CannotCreateTransactionException ...
- PAT 1106 Lowest Price in Supply Chain
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...
- python库文件路径
python中import语句导入库文件路径可通过sys.path查看.写一个简单的小程序: import sys print sys.path 运行它,本机上得到的结果如下: ['', '/usr/ ...
- Jmeter 学习imooc
https://www.imooc.com/video/14718 1. BS Vs CS BS架构: browser server CS架构: Client server(安装到本地)
- [poj3321]Apple Tree_dfs序_树状数组
Apple Tree poj-3321 题目大意:给你一个根固定的树,每一个点的点权是0或1,查询子树点权和. 注释:$1\le n \le 10^5$. 想法:刚刚学习dfs序,刷到水题偶哈哈. 什 ...
- DTrace Probes In MySQL 自定义探针
Inserting user-defined DTrace probes into MySQL source code is very useful to help user identify the ...