CF#715 (Div. 2)、Educational Codeforces Round 107、108 (Rated for Div. 2)

本文探讨了几个算法问题,包括如何将数字排序以使相邻两数的平均值为整数,如何计算特定数值出现的最大次数,如何在限制条件下分配不同颜色的豆子,如何判断字符串是否能形成'TMT'子序列,以及如何找到两个数的最大公约数的长度。这些题目涉及排序、计数、条件判断和数学逻辑,是算法学习的良好实践。
摘要由CSDN通过智能技术生成

目录

Average Height

Review Site

Red and Blue Beans

TMT Document 

GCD Length

The Cake Is a Lie


Average Height

题意: n个数排序,尽可能多的使得 相邻两个数的平均数是整数;

题解:即只需奇数排在一起,偶数排在一起~ 

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=100010;

int a[2020];
int b[2020];
int main() {
	ios::sync_with_stdio(false);
	int t;
	cin>>t;
	while(t--) {
		int n;
		cin>>n;
		int l=0,r=n;
		for(int i=1; i<=n; i++) {
			cin>>a[i];
			if(a[i]%2)
				b[++l]=a[i];
			else b[r--]=a[i];
		}
		for(int i=1; i<=n; i++)
			cout<<b[i]<<" ";
		cout<<endl;
	}
	return 0;
}

Review Site

turn to topic link

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=100010;

int a[2020];
int main() {
	ios::sync_with_stdio(false);
	int t;
	cin>>t;
	while(t--) {
		int n;
		cin>>n;
		int maxx=0;
		for(int i=1; i<=n; i++) {
			cin>>a[i];
			if(a[i]==1||a[i]==3)
				maxx++;
		}
		cout<<maxx<<endl;
	}
	return 0;
}

Red and Blue Beans

topic link

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=100010;

int main() {
	int t;
	cin>>t;
	while(t--) {
		int r,b,d;
		cin>>r>>b>>d;
		int minn=0,maxx=0;
		if(r<b)	minn=r,	maxx=b;
		else minn=b,maxx=r;
		if(d==0) {
			if(r==b)
				cout<<"YES"<<endl;
			else cout<<"NO"<<endl;
		} else {
///分成minn堆,设颜色为 r,则每堆中有r、b颜色各一个
///(maxx-minn)/minn向上取整就是(maxx-1)/minn
///若满足|number(r)-number(b)|<=d  --YES
			if((maxx-1)/minn<=d)
				cout<<"YES"<<endl;
			else cout<<"NO"<<endl;
		}
	}
	return 0;
}

TMT Document 

topic link

题意:一个长度为n的字符串,如果可以构成n/3个“TMT”子序列~  输出YES,     

分析:首先M个数的二倍要等于T的个数;1.从前往后扫一遍, M左边 T的数量要>M的数量 2.从后往前扫  M右边  T的数量要>M的数量     都满足输出YES

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

bool check(int n,string s) {
	int m=0,t=0;/// the number of M 、 T
	int flag=0;
	for(int i=0; i<n; i++) {
		if(s[i]=='M') {
			m++;
		} else t++;
		if(t<m)return false;
	}
	if(2*m!=t)return false;
	m=0,t=0;
	for(int i=n-1; i>=0; i--) {
		if(s[i]=='M')
			m++;
		else t++;
		if(t<m)
			return false;
	}
	return true;
}
int main() {
	int t;
	cin>>t;
	while(t--) {
		int n;
		cin>>n;
		string s;
		cin>>s;
		if(!check(n,s))cout<<"NO"<<endl;
		else cout<<"YES"<<endl;
	}
	return 0;
}

GCD Length

topic link

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=100010;
///input  a、b、c 分别表示无前导零的a位、b位、c位数
///output gcd(a,b)=c
//输出a和b...仔细看输出,不要在这上面交罚时,血亏..
int main() {
	int t;
	cin>>t;
	while(t--) {
		ll a,b,c;
		cin>>a>>b>>c;
		ll k=b-c,k2=a-c;
		ll s3=0,s2=0,s1=0;
		while(c--)s3=s3*10+1;///
		s2=s3,s1=s3;
		while(k--)s2*=13;///保证位数符合题意
		while(k2--)	s1*=11;///且保证gcd(s1,s2)=s3
		//因为11 13互质 ..
		cout<<s1<<" "<<s2<<endl;
//		cout<<"++"<<__gcd(s1,s2)<<endl;
	}
	return 0;
}

The Cake Is a Lie

topic link

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=100010;
///从(1,1)~(n,m) 总消耗==k,则输出yes
///(x,y)-->(x,y+1)  cost x
///(x,y)-->(x+1,y)  cost y
///两种路线  先(1,1)~(n,1) cost (n-1)*y=n-1;
//       后~(n,m) cost (m-1)*n
///          先(1,1)~(1,m) cost (m-1)*x=m-1;
//       后~(n,m) cost (n-1)*m
int main() {
	int t;
	cin>>t;
	while(t--) {
		int n,m,k;
		cin>>n>>m>>k;
		if(n*m-1==k)
			cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嵩韵儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值