Codeforces Round 870 (Div. 2) A~D

A. Trust Nobody

题意:n个人,其中一些人是骗子,每个人会说至少有几个骗子,问到底有多少个骗子(任意成立情况,矛盾输出-1)

思路:枚举骗子人数,枚举到这个骗子人数时,比这个数小的人说的都是真话,其他人都是骗子,因此有 n − 前缀 = = i n-前缀==i n前缀==i

#include
using namespace std;
#define lson(x) (1 << x)
#define rson(x) (1 << x | 1)
#define ll long long
#define lb(x) (x&-x)
#define YSE cout << "yse" << endl;
#define NO cout << "no" << endl;
#define fo(i,n) for(int i = 1;i <= n;i++)
int num[101];
int main()
{
	int t;cin >> t;
	while(t--){
		int n;cin >> n;
		memset(num,0,sizeof(num));
		fo(i,n){
			int x;cin >> x;
			num[x]++;
		}
		bool f = 0;
		int t = 0;
		for(int i = 0;i <= n;i++){//枚举说假话的人数
			t += num[i];
			if(i + t == n){
				f = 1;
				cout << i << endl;
				break;
			}
		}
		if(!f)
			cout << -1 << endl;
	}
}

B. Lunatic Never Content

题意:余数序列为回文数,问最大的用来模的数

思路: a % x = b % x a\%x=b\%x a%x=b%x ( a − b ) % x = 0 (a-b)\%x=0 (ab)%x=0,那么 x x x就为 ( a − b ) (a-b) (ab)的因子,也就是所有 ∣ a [ n − i + 1 ] − a [ i ] ∣ |a[n-i+1]-a[i]| a[ni+1]a[i]的最大因子,也就是最大公约数

#include
using namespace std;
#define lson(x) (1 << x)
#define rson(x) (1 << x | 1)
#define ll long long
#define lb(x) (x&-x)
#define YSE cout << "yse" << endl;
#define NO cout << "no" << endl;
#define fo(i,n) for(int i = 1;i <= n;i++)
const int MAXN = 1e5 + 10;
int a[MAXN];
int gcd(int a,int b){
	if(b == 0) return a;
	else return gcd(b,a%b);
}
int main()
{
	int t;cin >> t;
	while(t--){
		int n;cin >> n;
		fo(i,n) cin >> a[i];
		int gc = max(a[1],a[n]) - min(a[1],a[n]);
		for(int i = 2;i <= n / 2;i++)
			gc = gcd(gc,max(a[i],a[n - i + 1]) - min(a[i],a[n - i + 1]));
		cout << gc << endl;
	}
}

C. Dreaming of Freedom

题意:n个人,m个机器,每次投票只留得票最多的人,问是否一定能只剩下一人,或者有可能无限投票下去

思路:如果m内的数量能正好将n完整分开,即n如果有除1以外的因子,那么它就一定可以无限投票下去

那就变成分解最小的因子,那么开n的方枚举即可

#include
using namespace std;
#define lson(x) (1 << x)
#define rson(x) (1 << x | 1)
#define ll long long
#define lb(x) (x&-x)
#define YES cout << "yes" << endl;
#define NO cout << "no" << endl;
#define fo(i,n) for(int i = 1;i <= n;i++)
const int MAXN = 1e6 + 10;
int main()
{
	int t;cin >> t;
	while(t--){
		int n,m;cin >> n >> m;
		if(n == 1)
			YES
		else if(m >= n)
			NO
		else{
			int sq = sqrt(n);
			bool f = 1;
			for(int i = 2;i <= sq && i <= m;i++){
				if(n % i == 0)
					f = 0;
			}
			if(f)
				YES
			else
				NO
		}
	}
}

D. Running Miles

题意:找最大的 b i 1 + b i 2 + b i 3 − ( r − l ) b_{i_1}+b_{i_2}+b_{i_3}−(r−l) bi1+bi2+bi3(rl)

思路:也就是找最大的 b l + b m i d + b r − ( r − l ) b_l+b_{mid}+b_r-(r-l) bl+bmid+br(rl),转换,让相同的变量放在一起 ( b l + l ) + b m i d + ( b r − r ) (b_l+l)+b_{mid}+(b_r-r) (bl+l)+bmid+(brr),也就转换为对于每个 m i d mid mid,找最大的 ( b l + l ) 和 ( b r − r ) (b_l+l)和(b_r-r) (bl+l)(brr),从前往后递推最大的 b i + i b_i+i bi+i,从后往前递推最大的 b i − i b_i-i bii即可

#include
using namespace std;
#define lson(x) (1 << x)
#define rson(x) (1 << x | 1)
#define ll long long
#define lb(x) (x&-x)
#define YES cout << "yes" << endl;
#define NO cout << "no" << endl;
#define fo(i,n) for(int i = 1;i <= n;i++)
const int MAXN = 1e5 + 10;
ll b[MAXN];
ll l[MAXN],r[MAXN];
int main()
{
	int t;cin >> t;
	while(t--){
		int n;cin >> n;
		fo(i,n) b[i] = l[i] = r[i] = 0;
		fo(i,n) cin >> b[i];
		b[0] = b[n + 1] = l[0] = r[n + 1] = -1e9;
		ll ma = 0;
		fo(i,n) l[i] = max(l[i-1],b[i-1]+i-1);
		for(int i = n;i >= 1;i--) r[i] = max(r[i+1],b[i+1]-i-1);
		for(int i = 2;i <= n - 1;i++){
			ma = max(ma,l[i]+r[i]+b[i]);
		}
		cout << ma << endl;
	}
}

你可能感兴趣的:(练习,c++,算法)