Codeforces Round #829 (Div. 2) 题解

A. Technical Support

思路:显然如果每个问题都要被回答那么A的个数要大于等于Q的个数,但是每个问题在被提出之前的回答显然不是这个问题的回答,所以A的个数要和当前Q的个数取min保证A记录的都是没有重复回答的。

#include <bits/stdc++.h>//#include<iostream>//#include<string.h>//#include<math.h//#include<algorithm>
#define ll long long
#define db double
#define pii pair<int,int>
#define cf int _;cin>>_;while(_--)
#define de cout<<"---"<<endl;
#define mem(x,v) memset(x,v,sizeof(x))
#define L(x) x&(-x)
#define pb push_back//emplace_back//priority_queue <int,vector<int>,greater<int> > q;
#define INF 0x3f3f3f3f
#define endl '\n'
//function<void(int)> dfs = [&](int u);
//#define x first
//#define y second
using namespace std;
const int mod=998244353;
int n;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
int main(){
	ios::sync_with_stdio(0);cin.tie(0),cout.tie(0);
	cf{
		cin>>n;
		string s;
		cin>>s;
		int a,b;
		a=b=0;
		for(auto i:s){
			if(i=='Q')a++;
			else b++;
			b=min(b,a);
		}
		if(b!=a)cout<<"NO";
		else cout<<"YES";
		cout<<endl;
	}
}

B. Kevin and Permutation

思路:通过手写几个样例,会发现价值最大是n/2,假设x=n/2,那么按照x+1,1,x+2,2,x+3,3,...排列即可。

#include <bits/stdc++.h>//#include<iostream>//#include<string.h>//#include<math.h//#include<algorithm>
#define ll long long
#define db double
#define pii pair<int,int>
#define cf int _;cin>>_;while(_--)
#define de cout<<"---"<<endl;
#define mem(x,v) memset(x,v,sizeof(x))
#define L(x) x&(-x)
#define pb push_back//emplace_back//priority_queue <int,vector<int>,greater<int> > q;
#define INF 0x3f3f3f3f
#define endl '\n'
//function<void(int)> dfs = [&](int u);
//#define x first
//#define y second
using namespace std;
const int mod=998244353;
int n;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
int main(){
	ios::sync_with_stdio(0);cin.tie(0),cout.tie(0);
	cf{
		cin>>n;
		int t=n/2;
		for(int i=t;i>=1;i--){
			for(int j=i;j<=n;j+=t){
				cout<<j<<" ";
			}
		}
		cout<<endl;
	}
}

C1 and C2. Make Nonzero Sum

思路:首先我们发现交替和的不会改变-1,0,1这一段数和的奇偶性,那么我们就可以判断所给数组的和是不是偶数去判断是否有解。然后以和s是正数为例,显然这数组中的1多了,那么我们需要将s/2个1变成-1即可,即把他们放在一段的第二位,其他数自己为一段即可,我们可以证明因为一定有多于s个的1,那么这样的操作是一定有解的,s是负数同理。

#include <bits/stdc++.h>//#include<iostream>//#include<string.h>//#include<math.h//#include<algorithm>
#define ll long long
#define db double
#define pii pair<int,int>
#define cf int _;cin>>_;while(_--)
#define de cout<<"---"<<endl;
#define mem(x,v) memset(x,v,sizeof(x))
#define L(x) x&(-x)
#define pb push_back//emplace_back//priority_queue <int,vector<int>,greater<int> > q;
#define INF 0x3f3f3f3f
#define endl '\n'
//function<void(int)> dfs = [&](int u);
//#define x first
//#define y second
using namespace std;
const int mod=998244353;
int n;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
int main(){
	ios::sync_with_stdio(0);cin.tie(0),cout.tie(0);
	cf{
		cin>>n;
		vector<int>a(n+1);
		int t=0;
		for(int i=1;i<=n;i++){
			cin>>a[i];
			t+=a[i];
		}
		if(t%2!=0)cout<<"-1"<<endl;
		else {
			bool ok=1;
			if(t<0){
				ok=0;
				t=-t;
			}
			t/=2;
			cout<<n-t<<endl;
			for(int i=1;i<=n;i++){
				if(t){
					if(ok&&a[i+1]==1){
						cout<<i<<" "<<i+1<<endl;
						t--;
						i++;
					}
					else if(!ok&&a[i+1]==-1){
						cout<<i<<" "<<i+1<<endl;
						t--;
						i++;
					}
					else cout<<i<<" "<<i<<endl;
				}
				else cout<<i<<" "<<i<<endl;
			}
		}
	}
}

D. Factorial Divisibility

思路:首先我们知道(i+1)!=i!*(i+1),那么这一题我们只需要将小的阶乘合成大的阶乘,判断最后是不是只剩x的阶乘即可。

#include <bits/stdc++.h>//#include<iostream>//#include<string.h>//#include<math.h//#include<algorithm>
#define ll long long
#define db double
#define pii pair<int,int>
#define cf int _;cin>>_;while(_--)
#define de cout<<"---"<<endl;
#define mem(x,v) memset(x,v,sizeof(x))
#define L(x) x&(-x)
#define pb push_back//emplace_back//priority_queue <int,vector<int>,greater<int> > q;
#define INF 0x3f3f3f3f
#define endl '\n'
//function<void(int)> dfs = [&](int u);
//#define x first
//#define y second
using namespace std;
const int mod=998244353;
int n;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
int main(){
	ios::sync_with_stdio(0);cin.tie(0),cout.tie(0);
//	cf{
		int x;
		cin>>n>>x;
		vector<int>a(x+1,0);
		for(int i=0;i<n;i++){
			int t;
			cin>>t;
			a[t]++;
		}
		bool ok=1;
		for(int i=1;i<x;i++){
			if(a[i]%(i+1)){
				ok=0;
			}
			else {
				a[i+1]+=a[i]/(i+1);
			}
		}
		if(ok)cout<<"YES";
		else cout<<"NO";
		cout<<endl;
//	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值