0129-0203部分校赛题解复盘

swjswjswj / 2024-02-04 / 原文

vj第一场

A题 https://codeforces.com/gym/103480/problem/A
该题让我们可以从回文串的特点入手,即两个相同的字母便可增加长度2,所以并不用思考该回文串要如何排序出来,而是看有多少对相同的字母,使用map<char,int>来记录字母出现的次数,再计算可以除以2的次数即可。

点击查看代码
#include <bits/stdc++.h>
using namespace std;
void solve()
{
	int n;
	char c;
	map<char,int>q;
	cin>>n;
	while(n--)
	{
		cin>>c;
		q[c]++;
	}
	
	int ans=0;
	for(auto i:q) ans+=i.second/2;
	ans*=2;
	if(ans!=n) ans++;
	cout<<ans<<endl;

}

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		solve();
	}
}

B题 https://codeforces.com/gym/103480/problem/B
该题利用前缀和和lower__bound去找到第一个大于等于s[i-1]+7777的s[x],再检测一下该数-7777是否为0,若为0则区间和为7777的cnt++。

点击查看代码
#include <bits/stdc++.h>
using namespace std;
void solve ()
{
	int n,cnt=0;
	cin>>n;
	vector<int >q(n+1);
	for(int i=1;i<=n;i++)
	{
		cin>>q[i];
		q[i]=q[i]+q[i-1];//前缀和数组 
	}
    //从每个Si开始,当一个s找到一个刚好比他大7777的另一个sx时即说明从i-x这个区间的和为7777
	for(int i=1;i<=n;i++)
	{
		int k=*lower_bound(q.begin(),q.end(),q[i-1]+7777); 
		if(k-q[i-1]==7777) cnt++;
	}
	
	cout<<cnt<<endl;
}

int main(){
	int t;
	cin>>t;
	while(t--)
	{
		solve();
	}
}

I题 https://codeforces.com/gym/103480/problem/I
使用vector<pair<int,string>>q并对其进行排序即可
sort(q.begin(),q.end(),greater<pair<int,string>>() );

点击查看代码
#include <bits/stdc++.h>
using namespace std;
void solve()
{
	int n;
	cin>>n;
	vector<pair<int,string> >q;
	for(int i=0;i<n;i++)
	{
		int x;
		string s;
		cin>>x>>s;
		q.push_back({x,s});//注意q.push_back()如果放的pair要用{} 
	}
	int k;
	cin>>k;
	sort(q.begin(),q.end(),greater<pair<int,string>>() );//按降序排序 
	cout<<q[k].second<<endl;
}




int main()
{
	int t=1;
	while(t--)
	{
		solve();
	}
	return 0;
	
 } 

D题https://codeforces.com/gym/103480/problem/M
题意相当于把1 3 5 6 4 2 变为 1 2 3 4 5 6只要双指针输出即可,即输出1 2再输出3 4在输出5 6

点击查看代码
#include <bits/stdc++.h>
using namespace std;
void solve(){
	string s;
	char k;
	vector<string>q;
	while(cin>>s){
		if(s.back()=='.'||s.back()=='!'||s.back()=='?')
		{
			k=s.back();
			s.pop_back();
			q.push_back(s);
			break;//遇到标点即为最后一个就跳出循环 
		}
		q.push_back(s);
	}
	
	for(int l=0,r=q.size()-1;l<=r;l++,r--){
		if(l==r) cout<<q[l];
		else{
			cout<<q[l]<<" "<<q[r];
			if(r-l>1) cout<<" ";//如果是放在最后就不要在标点符号前再加个空格 
		}

	}
	cout<<k;
	cout<<endl;
	
}



int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		solve();
	}
	return 0;
}

VJ第二场

J题 https://codeforces.com/gym/104101/problem/J
奇数+奇数=偶数 偶数+偶数=奇数 偶数+奇数=奇数
所以当总和为偶数时 无论你怎么选 最后S1-S2都为偶数
反之当为奇数 无论怎么选 s1-s2都为奇数

点击查看代码
#include <bits/stdc++.h>
using namespace std;
void solve()
{
	int n,sum=0,a;
	
	cin>>n;
	while(n--){
		cin>>a;
		sum+=a;
	}
	if(sum%2==0) cout<<"Bob";
	else cout<<"Alice"; 
	
}

int main()
{
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	int t=1;
	while(t--)
	{
		solve();
	}
	return 0;
}