Election

本文介绍了一个选举预测的问题,通过数学概率来提前判断候选人的胜算。文章提供了完整的代码实现,使用了高中水平的排列组合知识来解决该问题。

F(1967): Election

            Time Limit: 1 Sec       Memory Limit: 128 Mb       Submitted: 495       Solved: 44    

Description

After all the fundraising, campaigning and debating, the election day has finally arrived. Only two candidates remain on the ballot and you work as an aide to one of them.

Reports from the voting stations are starting to trickle in and you hope that you can soon declare a victory.

There are N voters and everyone votes for one of the two candidates (there are no spoiled ballots). In order to win, a candidate needs more than half of the votes. A certain number M ≤ N of ballots have been counted, and there are Vi votes for candidate i (V1+V2 = M), where V1 is the number of votes your candidate received.

Due to the historical data and results of highly scientific polls, you know that each of the remaining votes has a 50% chance to go to your candidate. That makes you think that you could announce the win before all the votes are counted. So, if the probability of winning strictly exceeds a certain threshold W, the victory is yours! We just hope you are sure of this, we don’t want any scandals...

Input

The first line of input contains a single positive integer T ≤ 100 indicating the number of test cases. Next T lines each contain four integers: N, V1V2 and W as described above.

The input limits are as follows:

1 ≤ N ≤ 50
50 ≤ W < 100
V1V2 ≥ 0
V1 + V2 ≤ N

Output

For each test case print a single line containing the appropriate action:

  • If the probability that your candidate will win is strictly greater than W%, print GET A CRATE OF CHAMPAGNE FROM THE BASEMENT!
  • If your candidate has no chance of winning, print RECOUNT!
  • Otherwise, print PATIENCE, EVERYONE!

Sample Input

4
5 0 3 75
5 0 2 75
6 1 0 50
7 4 0 75

Sample Output

RECOUNT!
PATIENCE, EVERYONE!
PATIENCE, EVERYONE!
GET A CRATE OF CHAMPAGNE FROM THE BASEMENT!
题意:如果您的候选人获胜的概率严格大于W%,输出 GET A CRATE OF CHAMPAGNE FROM THE BASEMENT!
如果您的候选人没有获胜的机会,输出 RECOUNT!
否则,输出  PATIENCE, EVERYONE!
 
直接贴代码:
/*
F题:
高中的排列组合知识就可以解决,从n个物品中选择m个物品,每个物品被选择的概率都是0.5;
C(m,n)*(0.5)^m(选中)*(0.5)^(n-m)(未选中)=C(m,n)*(0.5)^n;
根据这个公式,再判断v1得到几票可以赢。
*/
#include<cstdio>
#include<cmath>
using namespace std;
typedef long long ll;
ll C(int n, int m)
{
	ll ans = 1;
	ll ans2 = 1;
	int i, j;
	for (i = n, j = m; i>n - m; i--, j--)
	{
		ans *= i;
		if (ans%j == 0)
			ans /= j;
		else
			ans2 *= j;
	}
	return ans / ans2;
}
int main()
{
	int n, v1, v2, t;
	double w;
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d%d%d%lf", &n, &v1, &v2, &w);
		double sum = 0;
		w /= 100.0;
		for (int i = n / 2 + 1 - v1; i <= n - v1 - v2; i++)
		{
			sum += 1.0*C(n - v1 - v2, i)*pow(0.5, n - v1 - v2);//重点知识 
		}
		if (v2 == n / 2 && n % 2 == 0)
			printf("RECOUNT!\n");//特判 
		else
		{
			if (v1 >= (n / 2 + 1) || sum>w)
				printf("GET A CRATE OF CHAMPAGNE FROM THE BASEMENT!\n");
			else if (v2 >= (n / 2 + 1))
				printf("RECOUNT!\n");
			else
				printf("PATIENCE, EVERYONE!\n");
		}
	}
	return 0;
}

election存储”这种表述不太明确,推测可能有以下几种情况及对应的存储思路: ### 选举数据存储 若“election”指选举,选举数据包含候选人信息、选民信息、投票结果等。可以使用关系型数据库如 MySQL 进行存储。 - 创建数据库和表: ```sql -- 创建选举数据库 CREATE DATABASE election_db; USE election_db; -- 创建候选人表 CREATE TABLE candidates ( candidate_id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL, party VARCHAR(50) ); -- 创建选民表 CREATE TABLE voters ( voter_id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL, age INT, location VARCHAR(100) ); -- 创建投票结果表 CREATE TABLE voting_results ( result_id INT AUTO_INCREMENT PRIMARY KEY, candidate_id INT, voter_id INT, vote_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (candidate_id) REFERENCES candidates(candidate_id), FOREIGN KEY (voter_id) REFERENCES voters(voter_id) ); ``` - 插入数据: ```sql -- 插入候选人数据 INSERT INTO candidates (name, party) VALUES ('Candidate A', 'Party X'); INSERT INTO candidates (name, party) VALUES ('Candidate B', 'Party Y'); -- 插入选民数据 INSERT INTO voters (name, age, location) VALUES ('Voter 1', 30, 'City A'); INSERT INTO voters (name, age, location) VALUES ('Voter 2', 25, 'City B'); -- 插入投票结果数据 INSERT INTO voting_results (candidate_id, voter_id) VALUES (1, 1); INSERT INTO voting_results (candidate_id, voter_id) VALUES (2, 2); ``` ### 选举算法中间结果存储 若“election”在分布式系统中指选举算法(如 Paxos、Raft),中间结果可以使用内存数据库如 Redis 进行临时存储,确保快速读写。 - 安装并启动 Redis 服务。 - 使用 Python 的 Redis 客户端操作: ```python import redis # 连接到 Redis r = redis.Redis(host='localhost', port=6379, db=0) # 存储选举中间结果 r.set('election_state', 'in_progress') r.set('leader_candidate', 'node_3') # 获取选举中间结果 state = r.get('election_state') leader = r.get('leader_candidate') print(f"Election state: {state.decode('utf-8')}") print(f"Leader candidate: {leader.decode('utf-8')}") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鳄鱼儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值