Codeforces Round 893 (Div. 2)題解記錄

长皆發表於2024-10-17

題目連結:https://codeforces.com/contest/1858

A. Buttons


從自身角度出發,我想贏就得保證我的按鈕儘可能多所以,大家都優先按\(c\),然後分析先後順序即可

	#include<iostream>
	#include<string.h>
	#include<map>
	#include<vector>
	#include<set>
	#include<unordered_set>
	#include<stack>
	#include<queue>
	#include<algorithm>
	#include<time.h>
	#include<random>
	#include<string>
	#include<string.h>
	#include<cctype>
	using namespace std;
	typedef long long ll;
	mt19937 rnd(time(0));
	const ll mod = 1e9 + 7;
	void fio()
	{
		ios::sync_with_stdio(0);
		cin.tie(0);
		cout.tie(0);
	}
	ll gcd(ll x, ll y)
	{
		if (y == 0)
			return x;
		else
			return gcd(y, x % y);
	}
	ll ksm(ll x, ll y)
	{
		ll ans = 1;
		while (y)
		{
			if (y & 1)
				ans = ans%mod*(x%mod)%mod;
			x = x%mod*(x%mod)%mod;
			y >>= 1;
		}
		return ans%mod%mod;
	}
	int main()
	{
		ll t;
		cin>>t;
		while(t--)
		{
			ll a,b,c;
			cin>>a>>b>>c;
			//swap(b,c);
			ll j=c%2;
			if(j==0)
			{
				if(a<=b)
				{
					cout<<"Second"<<endl;
				}
				else
				cout<<"First"<<endl;
			}
			else
			{
				if(b<=a)
				{
					cout<<"First"<<endl;
				}
				else
				cout<<"Second"<<endl;
			}
		}
	}

B. The Walkway

題目乍看比較繁瑣且翻譯有問題?

一個餅乾店的有無只會影響周圍餅乾店到這個餅乾店的途中的吃餅乾次數
分類討論下即可

	#include<iostream>
	#include<string.h>
	#include<map>
	#include<vector>
	#include<set>
	#include<unordered_set>
	#include<stack>
	#include<queue>
	#include<algorithm>
	#include<time.h>
	#include<random>
	#include<string>
	#include<string.h>
	#include<cctype>
	using namespace std;
	typedef long long ll;
	mt19937 rnd(time(0));
	const ll mod = 1e9 + 7;
	void fio()
	{
		ios::sync_with_stdio(0);
		cin.tie(0);
		cout.tie(0);
	}
	ll gcd(ll x, ll y)
	{
		if (y == 0)
			return x;
		else
			return gcd(y, x % y);
	}
	ll ksm(ll x, ll y)
	{
		ll ans = 1;
		while (y)
		{
			if (y & 1)
				ans = ans%mod*(x%mod)%mod;
			x = x%mod*(x%mod)%mod;
			y >>= 1;
		}
		return ans%mod%mod;
	}
	ll a[250000];
	ll b[250000];
	int main()
	{
		ll t;
		cin>>t;
		while(t--)
		{
			ll n,m,d;
			cin>>n>>m>>d;
			for(ll i=0;i<=m;i++)b[i]=0;
			for(ll i=1;i<=m;i++)
			{
				cin>>a[i];
			}
			ll op=0;ll k=0;
			ll ok=0;
			for(ll i=1;i<=m;i++)
			{
				ll ans=0;
				ll cnt=0;
				if(i==1)
				{
					if(a[i]==1)
					{
						op=0;
						k=1;
						ok++;
					}
					else
					{
						ans=(a[i]-1-1)/d+2+((a[i+1]-1)-a[i])/d+1;
						cnt=(a[i+1]-1-1)/d+2;
						//cout<<ans<<" "<<cnt<<endl;
						ok+=(a[i]-1-1)/d+2;
						if(ans-cnt>op)
						{
							op=ans-cnt;
							k=1;
						}
						else if(ans-cnt==op)k++;
					}
				}
				else if(i==m)
				{
					ans=(a[i]-1-a[i-1])/d+1+(n-a[i])/d;
					cnt=(n-a[i-1])/d;
					ok+=ans;
					if(ans-cnt>op)
					{
						op=ans-cnt;
							k=1;
					}
					else if(ans-cnt==op)k++;
				}
				else
				{
					ans=(a[i]-1-a[i-1])/d+1+(a[i+1]-1-a[i])/d+1;
					cnt=(a[i+1]-1-a[i-1])/d+1;
					ok+=(a[i]-1-a[i-1])/d+1;
					if(ans-cnt>op)
					{
						op=ans-cnt;
							k=1;
					}
					else if(ans-cnt==op)k++;
				}
			}
		//	cout<<ok<<endl;
			cout<<ok-op<<" "<<k<<endl;
		}
	}

C. Yet Another Permutation Problem


優先考慮二倍遞增,這樣子一定gcd種類最多

	#include<iostream>
	#include<string.h>
	#include<map>
	#include<vector>
	#include<set>
	#include<unordered_set>
	#include<stack>
	#include<queue>
	#include<algorithm>
	#include<time.h>
	#include<random>
	#include<string>
	#include<string.h>
	#include<cctype>
	using namespace std;
	typedef long long ll;
	mt19937 rnd(time(0));
	const ll mod = 1e9 + 7;
	void fio()
	{
		ios::sync_with_stdio(0);
		cin.tie(0);
		cout.tie(0);
	}
	ll gcd(ll x, ll y)
	{
		if (y == 0)
			return x;
		else
			return gcd(y, x % y);
	}
	ll ksm(ll x, ll y)
	{
		ll ans = 1;
		while (y)
		{
			if (y & 1)
				ans = ans%mod*(x%mod)%mod;
			x = x%mod*(x%mod)%mod;
			y >>= 1;
		}
		return ans%mod%mod;
	}
	ll a[250000];
	int main()
	{
		ll t;
		cin>>t;
		while(t--)
		{
			ll n;
			cin>>n;
			//map<ll,ll>q;
			set<ll>f;
			for(ll i=1;i<=n;i++)
			{
				f.insert(i);
			}
			ll pd=0;
			for(ll i=1;i<=n;i++)
			{
				if(pd==0)
				{
					pd=*f.begin();
					a[i]=pd;	
					f.erase(pd);
				}
				else
				{
					while(pd<=n)
					{
						pd*=2;
						if(f.find(pd)!=f.end())
						{
						a[i]=pd;
						f.erase(pd);
						break;
						}
					}
					if(pd>n)
					{
						pd=*f.begin();
						a[i]=pd;
						f.erase(pd);
					}
				}
			}
			//cout<<f.size()<<endl;
			for(ll i=1;i<=n;i++)cout<<a[i]<<" ";
			cout<<endl;
		}
	}

D. Trees and Segments

首先發現如果要求出\(a⋅l0+l1\)的最大值,並不是\(l0\)越大越好。所以考慮字首求出每個\(l0\)對應的每個\(l1\)最大值
由於不能\((n^3)\),所以直接用字首和字尾陣列去維護從某個點(且有多少改變機會)出發後的最長冷杉長度。
於是最後暴力下\(l0\)對應的\(l1\)最大值,答案得解

	#include<iostream>
	#include<string.h>
	#include<map>
	#include<vector>
	#include<set>
	#include<unordered_set>
	#include<stack>
	#include<queue>
	#include<algorithm>
	#include<time.h>
	#include<random>
	#include<string>
	#include<string.h>
	#include<cctype>
	using namespace std;
	typedef int ll;
	mt19937 rnd(time(0));
	const ll mod = 1e9 + 7;
	void fio()
	{
		ios::sync_with_stdio(0);
		cin.tie(0);
		cout.tie(0);
	}
	ll gcd(ll x, ll y)
	{
		if (y == 0)
			return x;
		else
			return gcd(y, x % y);
	}
	ll ksm(ll x, ll y)
	{
		ll ans = 1;
		while (y)
		{
			if (y & 1)
				ans = ans%mod*(x%mod)%mod;
			x = x%mod*(x%mod)%mod;
			y >>= 1;
		}
		return ans%mod%mod;
	}
	ll a[3500];
	ll b[3005][3005];
	ll c[3005][3005];
	int main()
	{
		ll t;
		cin>>t;
		while(t--)
		{
		ll n,g;
		cin>>n>>g;
		string f;
		cin>>f;
		for(ll i=0;i<=n+1;i++)
		{
			for(ll k=0;k<=g+1;k++)
			{
				b[i][k]=0;
				c[i][k]=0;
			}
		}
		//cout<<99<<endl;
		for(ll i=0;i<=g;i++)
		{
			ll sz=0;
			deque<ll>q;
			for(ll j=0;j<n;j++)
			{
				if(q.empty())
				{
					q.push_back(j);
					if(f[j]=='0')sz++;
					while(sz>i)
					{
						if(f[q.front()]=='0')sz--;
						b[q.front()][i]=max(b[q.front()][i],(ll)q.size()-1);
						q.pop_front();
					}
				}
				else
				{
					q.push_back(j);
					if(f[j]=='0')sz++;
					while(sz>i)
					{
						if(f[q.front()]=='0')sz--;
						b[q.front()][i]=max(b[q.front()][i],(ll)q.size()-1);
						q.pop_front();
					}
				}
				if(!q.empty())
				b[q.front()][i]=max((ll)q.size(),b[q.front()][i]);
			}
			while(!q.empty())
			{
				b[q.front()][i]=max(b[q.front()][i],(ll)q.size());
				q.pop_front();
			}
			for(ll j=n-1;j>=0;j--)b[j][i]=max(b[j+1][i],b[j][i]);
		}
		for(ll i=0;i<=g;i++)
		{
			ll sz=0;
			deque<ll>q;
			for(ll j=n-1;j>=0;j--)
			{
				if(q.empty())
				{
					q.push_back(j);
					if(f[j]=='0')sz++;
					while(sz>i)
					{
						if(f[q.front()]=='0')sz--;
							c[q.front()][i]=max(c[q.front()][i],(ll)q.size()-1);
						q.pop_front();
					}
				}
				else
				{
					q.push_back(j);
					if(f[j]=='0')sz++;
					while(sz>i)
					{
						if(f[q.front()]=='0')sz--;
						c[q.front()][i]=max(c[q.front()][i],(ll)q.size()-1);
						q.pop_front();
					}
				}
				if(!q.empty())
				c[q.front()][i]=max((ll)q.size(),c[q.front()][i]);
			}
			while(!q.empty())
			{
				c[q.front()][i]=max(c[q.front()][i],(ll)q.size());
				q.pop_front();
			}
			for(ll j=1;j<=n-1;j++)c[j][i]=max(c[j-1][i],c[j][i]);
		}
		for(ll i=0;i<=n-1;i++)
		{
		for(ll j=g;j>=1;j--)
		{
			b[i][j]=max(b[i][j-1],b[i][j]);	
			c[i][j]=max(c[i][j],c[i][j-1]);
		}
		}
	//	cout<<b[0][0]<<endl;
	/*for(ll i=0;i<n;i++)
	{
		for(ll j=0;j<=g;j++)
		cout<<b[i][j]<<" "<<c[i][j]<<endl;
	}*/
       pair<ll,ll>ans[5000];
	   ll op=0;
	   op=1;
	   ans[op]={0,b[0][g]};
	   //cout<<99<<endl;
        for(ll i=1;i<=n;i++)
		{
			ll cnt=0;
			ll sz=0;
			ll u=0;
			ll pd=0;
			deque<ll>q;
			for(ll j=0;j<n;j++)
			{
				q.push_back(j);
				if(f[j]=='1')sz++;
				while(sz>g)
				{
					if(f[q.front()]=='1')sz--;
					q.pop_front();
				}
				while((ll)q.size()>i)
				{
					if(f[q.front()]=='1')sz--;
					q.pop_front();
				}
				if(q.size()==i)
				{
					pd=1;
					if(q.front()==0&&q.back()==n-1)
					u=max(u,0);
					else if(q.front()==0)
					u=max(u,b[q.back()+1][g-sz]);
					else if(q.back()==n-1)
					u=max(u,c[q.front()-1][g-sz]);
					else
					u=max({u,b[q.back()+1][g-sz],c[q.front()-1][g-sz]});
				}
			}
			if(pd)
			{
				op++;
				ans[op]={i,u};
			}
		}
		//cout<<55<<endl;
		for(ll i=1;i<=n;i++)
		{
			ll u=0;
			for(ll j=1;j<=op;j++)
			{
				ll x=ans[j].first;
				ll y=ans[j].second;
				u=max(u,i*x+y);
			}
			cout<<u<<" ";
		}
		cout<<endl;
		}
	}

相關文章