AtCoder Beginner Contest 329 题解A~F

A - Spread

输入字符串,字符之间加上空格输出

B - Next

输出数组当中第二大的数

C - Count xxx

AtCoder Beginner Contest 329 题解A~F_第1张图片

统计每个字符出现过的最长长度,再累加即可

#include
#pragma GCC optimize("Ofast")
#define INF 0x3f3f3f3f
#define IOS ios::sync_with_stdio(false);cin.tie(0);
#define int long long
#define pb push_back
#define vct vector
#define checkbit __builtin_popcount
#define gcd __gcd
#define use int T;cin>>T;while(T--)
#define LEN length()
#define all(a) a.begin(),a.end()
template bool mmax(T &u, T v) { return u < v ? (u = v, 1) : 0; }
template bool mmin(T &u, T v) { return u > v ? (u = v, 1) : 0; }
#define lowbit(x) (x&(-x))
#define yes cout<<"YES"<pii;
signed main()
{IOS
int n;cin>>n;
string a;
cin>>a;
vctcnt(35,0);
vctmas(35,0);
if(cnt[a[0]-'a']==0)
	{
		cnt[a[0]-'a']++;
		mmax(mas[a[0]-'a'],cnt[a[0]-'a']);
	}
for(int i=1;i

D - Election Quick Report

AtCoder Beginner Contest 329 题解A~F_第2张图片

给定数组,每次第ia_i的票加一,每次都输出最多票的人,

我们用mas记录当前最大票数的人,在第i次投票时,答案只有masa_i两个人,每次视情况输出,并且更新mas的值即可.

#include
#pragma GCC optimize("Ofast")
#define INF 0x3f3f3f3f
#define IOS ios::sync_with_stdio(false);cin.tie(0);
#define int long long
#define pb push_back
#define vct vector
#define checkbit __builtin_popcount
#define gcd __gcd
#define use int T;cin>>T;while(T--)
#define LEN length()
#define all(a) a.begin(),a.end()
template bool mmax(T &u, T v) { return u < v ? (u = v, 1) : 0; }
template bool mmin(T &u, T v) { return u > v ? (u = v, 1) : 0; }
#define lowbit(x) (x&(-x))
#define yes cout<<"YES"<pii;
bool cmp(int a,int b){
	return a>b;
}
signed main()
{IOS
int n,m;cin>>n>>m;
vcta(m+1);
vctcnt(n+1);cin>>a[1];
int mas=a[1];cout<>a[i];
	cnt[a[i]]++;
	if(a[i]!=mas){
	   if(cnt[a[i]]>cnt[mas]){
	   	mas=a[i];
	   	printf("%lld\n",mas);
	   }
	   else if(cnt[a[i]]==cnt[mas]&&a[i]

 E - Stamp

AtCoder Beginner Contest 329 题解A~F_第3张图片

给一张空白的纸,一个印章T,问是否可以印成S的样子(印章每次会覆盖重复的部分) 

利用BFS搜索

#include
#pragma GCC optimize("Ofast")
#define INF 0x3f3f3f3f
#define IOS ios::sync_with_stdio(false);cin.tie(0);
#define int long long
#define pb push_back
#define vct vector
#define checkbit __builtin_popcount
#define gcd __gcd
#define use int T;cin>>T;while(T--)
#define LEN length()
#define all(a) a.begin(),a.end()
template bool mmax(T &u, T v) { return u < v ? (u = v, 1) : 0; }
template bool mmin(T &u, T v) { return u > v ? (u = v, 1) : 0; }
#define lowbit(x) (x&(-x))
#define yes cout<<"YES"<pii;
const int N= 2e5+7;
signed main()
{IOS
    int n,m;cin>>n>>m;
    string s,t;cin>>s>>t;
    vctst(N);
    queue q;
    for(int i=0;i+m-1

F - Colored Ball

AtCoder Beginner Contest 329 题解A~F_第4张图片

每次操作将a位置当中的元素,放到b位置,因为是颜色,故用平衡树

如果按照题目的要求直接写不加优化的话亲测TLE

故当a当中的元素多于b的时候,交换a,b当中的元素,再插入a,效果相同,这样可以达到最优效果

#include
#pragma GCC optimize("Ofast")
#define INF 0x3f3f3f3f
#define IOS ios::sync_with_stdio(false);cin.tie(0);
#define int long long
#define pb push_back
#define vct vector
#define checkbit __builtin_popcount
#define gcd __gcd
#define use int T;cin>>T;while(T--)
#define LEN length()
#define all(a) a.begin(),a.end()
template bool mmax(T &u, T v) { return u < v ? (u = v, 1) : 0; }
template bool mmin(T &u, T v) { return u > v ? (u = v, 1) : 0; }
#define lowbit(x) (x&(-x))
#define yes cout<<"YES"<pii;
const int N= 2e5+7;
signed main()
{IOS
   int n,q;cin>>n>>q;
   int x;
   settot[n+1];
   for(int i=1;i<=n;i++)
   {
   	cin>>x;
   	tot[i].insert(x);
   }
   while(q--){
   	int a,b;cin>>a>>b;
   	if(tot[a].size()>tot[b].size())
   	swap(tot[a],tot[b]);
   	for(auto z:tot[a]){
   		tot[b].insert(z);
   	}
   	tot[a].clear();
  cout<

你可能感兴趣的:(题解,c++,Codeforces,Atcoder)