CF - 749C. Voting - 贪心+队列模拟

50 篇文章 0 订阅
3 篇文章 0 订阅

1.题目描述:

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

2.题意概述:

类似于UNO的游戏,当某人在回合当中可以禁言某人,使得某人这个回合不能发动技能,然后问你最后赢得是谁

3.解题思路:

把每个队伍的人丢到队列里面,每次取顶部相当于发言嘛,然后贪心地“影响”敌人——轮到第i人(D)投票时,应选择把下一个能做决策的对手X(R)淘汰(既淘汰掉一个人,又防止下一个D被淘汰,即可能又多了一次淘汰(R)的机会) 

4.AC代码:

#include <string.h>
#include <stdio.h>
#include <queue>
#define maxn 200200
using namespace std;
char a[maxn];
int vis[maxn];

int main()
{
	int n;
	while (scanf("%d", &n) != EOF)
	{
		memset(vis, 0, sizeof(vis));
		scanf("%s", a);
		queue<int>d;
		queue<int>r;
		for (int i = 0; i < n; i++)
			if (a[i] == 'D')
				d.push(i);
			else
				r.push(i);
		while (!d.empty() && !r.empty())
		{
			for (int i = 0; i < n; i++)
			{
				if (vis[i])
					continue;
				if (a[i] == 'D')
				{
					int tmpd = d.front();
					d.pop();
					int tmpr = r.front();
					r.pop();
					vis[tmpr] = 1;
					d.push(tmpd);
				}
				else
				{
					int tmpr = r.front();
					r.pop();
					int tmpd = d.front();
					d.pop();
					vis[tmpd] = 1;
					r.push(tmpr);
				}
				if (d.empty() || r.empty())
					break;
			}
		}
		if (!d.empty())
			puts("D");
		else
			puts("R");
	}
    return 0;
}

CodeForces - 749C Voting 是一道有趣的计数题,它的题意是给定了两个候选人 A 和 B 的得票情况,票数相等的情况下,我们需要按照一定的规则进行投票,直到最后一个候选人胜出。具体来说,如果当前的票数相等,我们需要按照轮流投票的规则,每次投给 A 或者 B,直到最后一个候选人胜出。如果某一时刻 A 的票数比 B 多 2 票以上,那么 A 就直接胜出,同样的,如果 B 的票数比 A 多 2 票以上,那么 B 就直接胜出。 我们可以用两个变量 sa 和 sb 来表示 A 和 B 的票数,然后依次遍历每一个投票者的选择。如果当前 A 和 B 的票数相等,我们就按照轮流投票的规则,每次投给 A 或者 B,直到最后一个候选人胜出。如果 A 和 B 的票数相差 2 票以上,那么直接输出胜出者的名字。最后一定会有一个候选人胜出,因为在每一轮投票之后,A 和 B 的票数都会有所增加,最终一定会有一个候选人胜出。 下面是该问题的 Java 代码实现: ``` import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String s = sc.next(); int sa = 0, sb = 0, pa = 0, pb = 0; boolean[] sign = new boolean[n]; for (int i = 0; i < n; i++) { if (s.charAt(i) == 'D') sa++; else sb++; } while (sa > 0 && sb > 0) { for (int i = 0; i < n; i++) { if (sign[i]) continue; if (s.charAt(i) == 'D') { if (pb > 0) { pb--; sa--; sign[i] = true; } else pa++; } else { if (pa > 0) { pa--; sb--; sign[i] = true; } else pb++; } if (sa == 0 || sb == 0) break; } } if (sa == 0) System.out.println("R"); else System.out.println("D"); } } ``` 该代码的时间复杂度为 $O(n)$,空间复杂度为 $O(n)$,其中 n 表示投票者的数量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值