Codeforces Round #691 (Div. 2) A、B、C题解

原文链接

A 题目链接

题意

有n张牌,每个牌上面有红色和绿色的两个数字,将这n张牌随机打乱顺序,问大概率是哪种颜色组成的数字大。

解题思路

想想如果这张牌的红色与绿色的数字相等的话,是不是无论放再那个位置都对最后的答案没有影响,所以可以只看红色与绿色都不相同的卡片,对于这种卡片,只要将他放在第一个位置就能对大小起上决定性的作用。所以只用判断红色数字大于绿色数字卡牌的数量与绿色数字大于红色数字卡片的数量即可。

代码

#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<ctime>
#include<cctype>
#include<iostream>
#include<algorithm>
//#include<chrono>
#include<vector>
#include<list>
#include<queue>
#include<string>
#include<set>
#include<map>
//#include<unordered_set>
//#include<unordered_map>
#define LL long long
#define li i<<1
#define ri i<<1|1
using namespace std;
inline LL read()
{
    char c=getchar();LL x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}
const int maxn=1005;
LL t,n,sum_r,sum_b;
char r[maxn],b[maxn]; 
int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	t=read();
	while(t--){
		n=read();
		sum_r=sum_b=0;
		scanf("%s",r+1);
		scanf("%s",b+1);
		for(int i=1;i<=n;++i)
			if(r[i]>b[i])
				++sum_r;
			else if(r[i]<b[i])
				++sum_b;
		if(sum_r==sum_b)
			printf("EQUAL\n");
		else if(sum_r>sum_b)
			printf("RED\n");
		else
			printf("BLUE\n");
	}
	return 0;
}

B 题目链接

题意

再一个无限大的二位平面内,有一个机器人再(0,0)的位置,每次可以向东、西、南、北的方向走一步,现在可以走n步,每步的方向必须与上步走的方向成直角,意思就是上一步是面朝北方的,则这步只能向西或者东走,求最后机器人可能停留的位置有几个。

解题思路

只会BFS跑暴力,得出前几项答案,在某网站找到一个公式。
a[n] = (3*n*(n + 4)-((-1)^n)*(n *(n+4)+2)+10)/8
等我弄懂了之后再来跟新思路,也许不会跟新了

跑暴力代码

#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<ctime>
#include<cctype>
#include<iostream>
#include<algorithm>
//#include<chrono>
#include<vector>
#include<list>
#include<queue>
#include<string>
#include<set>
#include<map>
//#include<unordered_set>
//#include<unordered_map>
#define LL long long
#define li i<<1
#define ri i<<1|1
using namespace std;
inline LL read()
{
    char c=getchar();LL x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}
struct node{
	int x,y,times;
	char s;  // -,|;
};
map<int,map<int,bool> > use;
void bfs(int x,int y,int times){
	char s;
	int cas=0,fx,fy;
	queue<node> q;
	node f;
	f.x=0;
	f.y=0;
	f.s='-';
	f.times=times;
	q.push(f);
	f.s='|';
	q.push(f);
	LL ans=0;
	while(!q.empty()){
		f=q.front();
		q.pop();
		x=f.x;
		y=f.y;
		times=f.times;
		s=f.s;
		if(times>cas){
			printf("cas %d:%d\n",cas,ans);
			ans=0;
			cas=times;
			use.clear();
		}
		if(use[x][y]==false)
			++ans;
		use[x][y]=true;
		if(s=='-'){
			f.x=x+1,f.y=y,f.s='|',f.times=times+1,q.push(f);
			f.x=x-1,q.push(f);
		}else if(s=='|'){
			f.x=x,f.y=y+1,f.s='-',f.times=times+1,q.push(f);
			f.y=y-1,q.push(f);
		}
	}
}
int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	bfs(0,0,0);
	return 0;
}

Python代码

n = int(input())
print((3*n*(n + 4) - ((-1)**n)*(n *(n + 4) + 2) + 10)//8)

C 题目链接

题意

有一个长度为n的a数组,之后给你m个k,对于每一个k来说,让你求出gcd(a[1]+k,a[2]+k,a[3]+k,…,a[n]+k)。

解题思路

首先我们知道gcd(a,b) = gcd(a,b-a) ,推广一下可以得到,gcd(a,b,c,d…) = gcd(a,b-a,c-b,d-c,…),知道这个后答案就能轻易得出来了。
要求gcd(a[1]+k,a[2]+k,a[3]+k,…,a[n]+k),通过上面得公式,可以等价于求 gcd(a[1]+k,a[2]-a[1],a[3]-a[2],…,a[n]-a[n-1]),发现这个gcd除了第一项,其它项的值与k无关,所以先求出g = gcd(a[2]-a[1],a[3]-a[2],…,a[n]-a[n-1]) 之后,在求gcd(a[1]+k,g)即可。注意一点,需要先将a数组排序,使得a[i]-a[i-1]一定是非负数,这样就不用考虑负数对gcd的影响了,也可以选择不排序,最后g=abs(g)也是一样可以的。 (前者为O(n*logn),后者O(n))

代码

#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<ctime>
#include<cctype>
#include<iostream>
#include<algorithm>
//#include<chrono>
#include<vector>
#include<list>
#include<queue>
#include<string>
#include<set>
#include<map>
//#include<unordered_set>
//#include<unordered_map>
#define LL long long
#define li i<<1
#define ri i<<1|1
using namespace std;
inline LL read()
{
    char c=getchar();LL x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}
const int maxn=2e+5;
LL n,m,a[maxn],k,g;
int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	n=read();
	m=read();
	for(int i=0;i<n;++i)
		a[i]=read();
	sort(a,a+n);
	for(int i=1;i<n;++i)
		g=__gcd(g,a[i]-a[i-1]);
	// g = abs(g);  // 若没排序,加上这句话使得g为非负数答案也是正确的.
	while(m--){
		k=read();
		printf("%lld ",__gcd(g,a[0]+k));
	}
	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值