Codeforces---Hello 2024---(A~D)

A. Wallet Exchange

问题:

目录

A. Wallet Exchange

 B. Plus-Minus Split

C. Grouping Increases

D. 01 Tree


思路:Alice先手,奇数则胜,否则Bob胜利!

#include
using namespace std;
using ll = long long;
void solve()
{
	ll a,b;cin>>a>>b;
	ll sum=a+b;
	if(sum&1) cout<<"Alice"<<'\n';
	else cout<<"Bob"<<'\n';
}
int main()
{
	ll t;cin>>t;
	while(t--) 
		solve();
}

 B. Plus-Minus Split

题目:

Codeforces---Hello 2024---(A~D)_第1张图片

思路: 计算-与+的个数,计算两者的差然后输出绝对值即可!一开始忘了加绝对值,wa了一发wuwu---

#include
using namespace std;
using ll = long long;
void solve()
{
	ll n;cin>>n;
	string a;cin>>a;
	a=' '+a;
	ll sum=0,x=0,y=0;
	for(ll i=1;i<=n;i++)
	{
		if(a[i]=='-')x++;
		else y++;
	}
	if(x==n) 
	{
		cout<>t;
	while(t--) 
		solve();
}

C. Grouping Increases

题目:Codeforces---Hello 2024---(A~D)_第2张图片

思路: 即计算a11.同时大于两个top, 2.大于一个top,3.小于两个top!  目的是将ai放入top较小的栈中,这样更满足题意!

#include
using namespace std;
using ll = long long;
void solve()
{
	ll n,ans=0;cin>>n;
	vectora(n+1);
	for(ll i=1;i<=n;i++) cin>>a[i];
	stackb,c;
	b.push(1e10);
	c.push(1e10);
	for(ll i=1;i<=n;i++)
	{
		ll x=b.top();
		ll y=c.top();
		if(a[i]<=x&&a[i]<=y)
		{
			if(xy)
		{
			b.push(a[i]);
		}
		else if(a[i]>x&&a[i]<=y)
		{
			c.push(a[i]);
		}
		else{
			if(x>y) c.push(a[i]);
			else b.push(a[i]);
			ans++;
		}
		
	}
	cout<>t;
	while(t--) 
		solve();
}

D. 01 Tree

题目:Codeforces---Hello 2024---(A~D)_第3张图片

思路: 两次栈往返一次即可!

#include 
using namespace std;
using ll= long long;
void solve()
{
	ll n;
	cin>>n;
	vectora(n+1),sta(n+1),b(n+1),c(n+1);
	ll cnt=0;
	for(ll i=1;i<=n;i++){
		cin>>a[i];
		if(!a[i]) cnt++;
	}
	if(cnt!=1){
		cout<<"NO"<<'\n';
		return;
	}
	ll top=0;
	for(ll i=1;i<=n;i++){
		while(top && a[sta[top]]>=a[i]) top--;
		if(top) b[i]=a[sta[top]];
		else b[i]=-1;
		sta[++top]=i;
	}
	top=0;
	for(ll i=n;i>=1;i--){
		while(top&&a[sta[top]]>=a[i]) top--;
		if(top) c[i]=a[sta[top]];
		else c[i]=-1;
		sta[++top]=i;
	}
	ll ans=1;
	for(ll i=1;i<=n;i++){
		if(max(b[i],c[i])!=a[i]-1) ans=0;
	}
	if(ans) cout<<"YES"<<'\n';
	else cout<<"NO"<<'\n';
}
int main(){
	ll t;
	cin>>t;
	while(t--)
		solve();
}

你可能感兴趣的:(CF比赛(练习),算法,c++,数据结构)