CodeForces - 864

博客围绕CodeForces上的三道算法题展开。一是判断牌面能否构成公平比赛,通过记录牌数判断;二是求连续小写字母组成字串的最大元素数,用集合收集连续字串判断;三是计算车k次移动的加油次数,以加油站为节点判断油量能否到达。

Fair Game CodeForces - 864A

A、B两人各抽取n张牌中不同的两张AA和BB,然后两人拿取剩余的和其先前抽取的一样的牌,每位牌手获得的牌数是相同的,当他们可以拿下所有n张牌,那么这场比赛被认为是公平的。问牌面是否可构成公平比赛。直接记录几种牌数判断即可。

#include<cstdio>
#include<map>
#include<algorithm>
#include<cstring>
using namespace std;
int p[105],v[105],ans[105];
bool vis[105];

int main(){
	int n,x;
	while(~scanf("%d",&n)){
		memset(v,0,sizeof(v));
		memset(vis,false,sizeof(vis));
		int len=0;
		for(int i=0;i<n;i++){
			scanf("%d",&p[i]);
			if(!vis[p[i]]){
				ans[len++]=p[i];
				vis[p[i]]=true;
			}
			v[p[i]]++;
		}
		
		if(len==1||len>2){
			printf("NO\n");
			continue;
		}
		if(v[ans[0]]!=v[ans[1]]){
			printf("NO\n");
			continue;
		}
		printf("YES\n");
		printf("%d %d\n",ans[0],ans[1]);
	}
	return 0;
} 

Polycarp and Letters CodeForces - 864B

一组连续小写字母组成的字串的最大元素数。用集合收集连续字串判断。

#include<cstdio>
#include<set>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int N=205;
char s[N];
set<int>v;

int main(){
	int n;
	while(~scanf("%d",&n)){
		scanf("%s",s);
		int ans=0;
		for(int i=0;i<n;i++){
			if(s[i]>='A'&&s[i]<='Z'){
				ans=max(ans,(int)v.size());
				v.clear();
			}
			else
				v.insert(s[i]);
		}
		ans=max(ans,(int)v.size());
		printf("%d\n",ans);
	}
	return 0;
}

Bus CodeForces - 864C

车从x=0位置到x=a位置间来回移动,x=f处可加油,车邮箱容量为b,求k次移动(单方向)需要加几次油。以加油站x=f处为节点,判断路程每次油量是否可到达加油站。

#include<cstdio>
const int N=1e4+5;
int dis[N];

int main(){
	int a,b,f,k;
	while(~scanf("%d%d%d%d",&a,&b,&f,&k)){
		int p1=f*2,p2=(a-f)*2;
		dis[1]=f;
		
		if(k&1){  //no back
			dis[k+1]=a-f;
			int tmp=1;
			for(int i=2;i<=k;i++){
				if(tmp==1) dis[i]=p2;
				else dis[i]=p1;
				
				tmp=-tmp;
			}		
		}
		else{
			dis[k+1]=f;
			int tmp=1;
			for(int i=2;i<=k;i++){
				if(tmp==1) dis[i]=p2;
				else dis[i]=p1;
				
				tmp=-tmp;
			}
		}
		
		int cnt=0;
		int f=0,tmp=0;
		for(int i=1;i<=k+1;i++){
			if(dis[i]>b){
				f=1;break;
			}
			if(tmp+dis[i]<=b)
				tmp+=dis[i];
			else{
				tmp=dis[i];
				cnt++;
			}			
		}
		
		if(f)
			printf("-1\n");
		else
			printf("%d\n",cnt);
	}
	return 0;
}

 

### Codeforces Problem 976C Solution in Python For solving problem 976C on Codeforces using Python, efficiency becomes a critical factor due to strict time limits aimed at distinguishing between efficient and less efficient solutions[^1]. Given these constraints, it is advisable to focus on optimizing algorithms and choosing appropriate data structures. The provided code snippet offers insight into handling string manipulation problems efficiently by customizing comparison logic for sorting elements based on specific criteria[^2]. However, for addressing problem 976C specifically, which involves determining the winner ('A' or 'B') based on frequency counts within given inputs, one can adapt similar principles of optimization but tailored towards counting occurrences directly as shown below: ```python from collections import Counter def determine_winner(): for _ in range(int(input())): count_map = Counter(input().strip()) result = "A" if count_map['A'] > count_map['B'] else "B" print(result) determine_winner() ``` This approach leverages `Counter` from Python’s built-in `collections` module to quickly tally up instances of 'A' versus 'B'. By iterating over multiple test cases through a loop defined by user input, this method ensures that comparisons are made accurately while maintaining performance standards required under tight computational resources[^3]. To further enhance execution speed when working with Python, consider submitting codes via platforms like PyPy instead of traditional interpreters whenever possible since they offer better runtime efficiencies especially important during competitive programming contests where milliseconds matter significantly.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值