Codeforces Round 1004 (Div. 2)BC题解

Codeforces Round 1004 (Div. 2)比赛链接

B. Two Large Bags

题意简述:给出n个数,初始放到第一个数组,你可以进行任何次操作,输出是否可以操作让两个数组变成一样的

  • 操作1:选择第一个数组的数放入第二个数组
  • 操作2:从第一个数组选择一个在第二个数组存在的数,将第一个数组的数加1

根据第二个操作可以知道小的数可以变大,而大的数无法变小,所以要求两个数组的数一样,必须将最小值放到第二个数组中,因为第二个操作不对第二个数组起作用
所以每次都对相邻的两个数进行判断,如果这两个数一样,则可以放一个数,留一个数,并且留的数不能进行操作二
如果接下来存在一样的数,可以进行操作二,将其+1,方便与下一个数进行判断,如果存在一个位置不相等表示一个数出现奇数次,无法平分到两个数组并且这个数加一也不存在相等的数,无法操作,所以不存在
思路出来后代码自然也出来了

#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int n,a[N];
void solve(){
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	sort(a+1,a+n+1);
	int mx=0;
	for(int i=1;i<=n;i+=2){
		if(max(mx,a[i]) != max(mx,a[i+1])){
			cout<<"No\n";
			return;
		}
		mx=max(mx,a[i])+1;
	}
	cout<<"Yes\n";
	return;
}
int main(){
	
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int t;cin>>t;
	while(t--){
		solve();
	}
	
	return 0;
}

C. Devyatkino

题意简述:给定整数n,输出最少需要通过加多少次只含9的整数才会出现数码7

对于任何数,最多只需要增加9次就可以得到7(8增加到7),可以枚举次数,每次加无数个9,这么看没什么性质,但是加1就会变成 \(10^x\) 所以可以相当于加 \(10^x - 1\) 所以如果增加k次,相当于\(n - k + 10^{x_1} + 10^{x_2} + ... + 10^{x_k}\) 每一位数码d想要变成7需要至少增加 \((7-d) mod 7\) 次,找到需要增加的最少次数,如果这个次数枚举到的说明存在一种方案让n出现数码7

#include <bits/stdc++.h>
using namespace std;
int n;
void solve(){
	cin>>n;
	for(int l=0;l<=9;l++){
		string s=to_string(n-l);
		int md=0;
		for(char c:s){
			if(c <= '7'){
				md=max(md,c-'0');
			}
		}
		if(l >= 7-md){
			cout<<l<<"\n";
			return;
		}
	}
}
int main(){
	
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int t;cin>>t;
	while(t--){
		solve();
	}
	
	return 0;
}
posted @ 2026-08-31 11:54  rdrd  阅读(7)  评论(0)    收藏  举报