Codeforces 707(Div. 2) B、C 题解

B Restore Modulo
题目链接

题目大意

给出四个数 n,m,c,s,n表示数列的长度,s是非负整数, 0 ≤ c < m 0\leq c< m 0c<m
数列第一个数为: a 1 = s   m o d   m a_1=s\ mod\ m a1=s mod m
随后第2至n个数为: a i = ( a i − 1 + c )   m o d   m a_i=(a_{i-1} + c)\ mod\ m ai=(ai1+c) mod m
现在给出这样一个数列,请判断是否存在这样四个数,可以构造出这个序列。
如果无法构造输出-1,如果这样的m可以是无穷大,那么输出0,否则输出满足题意的最大的m和任意满足的一个c。

样例输入

6
6
1 9 17 6 14 3
3
4 2 2
3
7 3 4
3
2 2 4
5
0 1000000000 0 1000000000 0
2
1 1

样例输出

19 8
-1
-1
-1
2000000000 1000000000
0

思路

对于这四个数,显然确定 c c c是至关重要的。

如果数列中存在两个连续的数 x x x, y y y,且 x ≤ y x\leq y xy,那么一定有 c = y − x c=y-x c=yx成立。因为如果 x + c ≥ m x+c\geq m x+cm,显然此时 m = x + c − y m=x+c-y m=x+cy,就不满足 c < m c<m c<m的条件。

所以通过一次循环找到所有这样连续的两个数,计算出每个 c c c,如果 c c c不一致,那么必然无解。

计算出 c c c后,再将所有连续两个数 x x x, y y y 且满足 x > y x>y x>y的组合全部找出,此时 m = g c d ( m , x + c − y ) 。 m=gcd(m,x+c-y)。 m=gcd(m,x+cy)如果最终的结果 m ≤ m a x ( a i ) m\leq max(a_i) mmax(ai),亦无解。
现在要做的就是判断 m m m是否可以是无穷大。这也很容易判断,当数列是个等差数列时, m m m就能无穷大。

参考代码

#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
int a[100010],n;
int main(){
	IO;
	int T;
	cin>>T;
	while (T--){
		cin>>n;
		int ok=1,c=-1,mx=-1;
		for (int i=1;i<=n;i++){//第一步,求c
			cin>>a[i];
			mx=max(a[i],mx);
			if (i!=1&&a[i]>=a[i-1]){
				if (c==-1) c=a[i]-a[i-1];
				else if (c!=a[i]-a[i-1]) ok=-1;
			}	
		}
		int g=-1;
		for (int i=2;i<=n;i++){//第二步,求m(g)
			if (a[i]<a[i-1]){
				if (g==-1) g=a[i-1]+c-a[i];
				else g=__gcd(g,a[i-1]+c-a[i]);
			}
		}
		if (g<=mx) ok=-1;
		bool tmp=true;
		for (int i=2;i<n;i++){//第三步,判断是否m是无穷大
			if (a[i]-a[i-1]!=a[i+1]-a[i]) {
				tmp=false;
				break;
			}
		}
		if (tmp) ok=0;
		if (ok==1) {
			cout<<g<<" "<<c<<endl;
		}else cout<<ok<<endl;
	}
	return 0;
}
 
 

C Basic Diplomacy
题目链接

题目大意

一个人有n个朋友,有m天假期,他每天都要找一个朋友玩。任何一个朋友被邀请的次数不能超过 c e i l ( m 2 ) ceil(\frac{m}{2}) ceil(2m)(取上整)。现在给出每天能被邀请的朋友的名单,求是否有满足上面条件的玩耍方式?如果有输出YES和任意一种安排,否则输出NO。

样例输入

2
4 6
1 1
2 1 2
3 1 2 3
4 1 2 3 4
2 2 3
1 3
2 2
1 1
1 1

样例输出

YES
1 2 1 1 2 3
NO

思路

贪心。

按照每天空闲的朋友的数量排序。首先将每天只能邀请一个朋友先安排上,假设此时就有一个朋友被邀请的次数超过限制,必然就无解了。

其余情况均有解,此时,只需要统计一下空闲天数最多的朋友,若该朋友空闲的天数小于上限,必然有解;若该朋友空闲的天数大于上限,只需要邀请该朋友 c e i l ( m 2 ) ceil(\frac{m}{2}) ceil(2m)次,其余天数随便邀请其他人即可。

参考代码

#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
struct node{
	int num,id;
	vector <int> v;
}a[100100];
int n,m;
int frees[100100],tot[100100],ans[100100];
bool cmp(node x,node y){
	return x.num<y.num;
}
int main(){
	IO;
	int T;
	cin>>T;
	while (T--){
		cin>>n>>m;
		memset(a,0,sizeof a);
		memset(frees,0,sizeof frees);
		memset(tot,0,sizeof tot);
		bool ok=true;
		for (int i=1,x;i<=m;i++){
			cin>>x;
			a[i].num=x;
			a[i].id=i;
			for (int j=1,tmp;j<=x;j++){
				cin>>tmp;
				frees[tmp]++;
				a[i].v.push_back(tmp);
			}
		}
		int p=ceil(1.0*m/2);
		sort(a+1,a+1+m,cmp);//排序
		for (int i=1;i<=m;i++){//判断所有天中只能邀请1个人的情况
			if (a[i].num==1) {
				tot[a[i].v[0]]++;
				if (tot[a[i].v[0]]>p) ok=false;
			}else break;
		}
		if (!ok) cout<<"NO"<<endl;
		else {
			int pos=0,mx=0;
			for (int i=1;i<=n;i++){//找最闲的人
				if (frees[i]>mx){
					mx=frees[i];
					pos=i;
				}
			}
			if (mx>p) mx=p;
			for (int i=1;i<=m;i++){//安排
				bool gets=false;
				for (int j=0;j<a[i].num;j++){
					if (pos==a[i].v[j]&&mx){
						ans[a[i].id]=pos;
						mx--;
						gets=true;
						break;
					}
				}
				if (!gets) {
					if (a[i].v[0]!=pos)
						ans[a[i].id]=a[i].v[0];
					else 
						ans[a[i].id]=a[i].v[1];
				}
			}
			cout<<"YES"<<endl;
			for (int i=1;i<=m;i++){
				cout<<ans[i]<<" ";
			}
			cout<<endl;
		}
	}
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值