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

长皆發表於2024-10-18

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

A. United We Stand


選最大的數即可注意題目輸出格式

	#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[2005];
	int main()
	{
		ll t;
		cin>>t;
		while(t--)
		{
			ll n;
			cin>>n;
			ll cnt=0;
			for(ll i=1;i<=n;i++)
			{
				cin>>a[i];
				if(a[i]==a[1])cnt++;
			}
			if(cnt==n)cout<<-1<<endl;
			else 
			{
				sort(a+1,a+1+n);
				cnt=0;
				for(ll i=1;i<=n;i++)if(a[i]==a[n])cnt++;
				cout<<n-cnt<<" "<<cnt<<endl;
				for(ll i=1;i<=n;i++)
				{
					if(a[i]!=a[n])
					cout<<a[i]<<" ";
				}
				cout<<endl;
				for(ll i=1;i<=n;i++){
					if(a[i]==a[n])
					cout<<a[n]<<" ";
				}
				cout<<endl;
			}
		}
	}

B. Olya and Game with Arrays


翻譯有點難看,意思對於每個陣列最多可以轉移一個數到其他的陣列
首先所有陣列中的最小元素一定會拿,所以就找第二小的最小值,然後都丟到那個陣列去即可

		#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[250005];
		set<pair<ll,ll>>q[250000];
		int main()
		{
			ll t;
			cin>>t;
			while(t--)
			{
				ll n;
				cin>>n;
				ll ans=0;
				for(ll j=1;j<=n;j++)//模擬折返點
				{
					ll cnt=0;
					ll u=0;
					for(ll k=1;k<j;k++)
					{
						cnt+=k*k;
						u=max(u,k*k);
					}
					for(ll k=j;k<=n;k++)
					{
						cnt+=k*(n-k+j);
						u=max(u,k*(n-k+j));
					}
					ans=max(ans,cnt-u);
				}
				cout<<ans<<endl;
			}
		}

C. Another Permutation Problem

題目資料範圍小,模擬所有折返點即可

		#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[250005];
		set<pair<ll,ll>>q[250000];
		int main()
		{
			ll t;
			cin>>t;
			while(t--)
			{
				ll n;
				cin>>n;
				ll ans=0;
				for(ll j=1;j<=n;j++)//模擬折返點
				{
					ll cnt=0;
					ll u=0;
					for(ll k=1;k<j;k++)
					{
						cnt+=k*k;
						u=max(u,k*k);
					}
					for(ll k=j;k<=n;k++)
					{
						cnt+=k*(n-k+j);
						u=max(u,k*(n-k+j));
					}
					ans=max(ans,cnt-u);
				}
				cout<<ans<<endl;
			}
		}

D. Andrey and Escape from Capygrad


維護最大位置,用棧帶著排好順序的數跑,如果維護的最大位置<=此時位置那就丟出stack在這個最大位置,剩下的小於\(a[i]\)且沒加入\(stack\)
就不用變位置

		#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;
		}
		map<ll,ll>g1,g2;
		map<ll,ll>k;
		set<ll>f;
		struct s
		{
			ll x,y;
		}p[850000];
		ll a[850000];
		ll ans[850000];
		bool cmp(s x,s y)
		{
			return x.x<y.x;
		}
		int main()
		{
			fio();
			ll t;
			cin>>t;
			while(t--)
			{
				ll n;
				cin>>n;
				g1.clear();
				f.clear();
				k.clear();
				for(ll i=1;i<=n;i++)
				{
					ll l1,r1,a1,b1;
					cin>>l1>>r1>>a1>>b1;
					g1[l1]=max(g1[l1],b1);
					k[l1]++;//大左邊界
					f.insert(r1);
					f.insert(l1);
					f.insert(b1);
				}
				ll oi=0;
				for(auto j:f)
				{
					oi++;
					a[oi]=j;
				}
				ll uo;
				cin>>uo;
				for(ll i=1;i<=uo;i++)
				{
					ll ko;
					cin>>ko;
					p[i].x=ko,p[i].y=i;
				}
				sort(p+1,p+1+uo,cmp);
				ll po=1;
				ll cnt=0;
				stack<pair<ll,ll>>cf;
				for(ll i=1;i<=oi;i++)
				{
					if(i==1)
					{
						while(p[po].x<a[i]&&po<uo+1)
						{
							ans[p[po].y]=p[po].x;
							po++;
						}
						if(k[a[i]]>0)
						{
							cnt=max(cnt,g1[a[i]]);
						}
						while(p[po].x<=cnt&&po<uo+1)
						{
							cf.push({p[po].x,p[po].y});
							po++;
						}
						if(cnt==a[i])
						{
							while(!cf.empty())
							{
								ans[cf.top().second]=cnt;
								cf.pop();
							}
						}
					}
					else if(i==oi)//大右邊界
					{
						//cout<<cf.size()<<endl;
						if(cnt<a[i])
						{
						while(p[po].x<a[i]&&po<uo+1)
						{
							ans[p[po].y]=p[po].x;
							po++;
						}
						while(!cf.empty())
							{
								ans[cf.top().second]=cnt;
								cf.pop();
							}
						}
						//cout<<cf.size()<<endl;
						if(k[a[i]]>0)
						{
							cnt=max(cnt,g1[a[i]]);
						}
						while(p[po].x<=cnt&&po<uo+1)
						{
							cf.push({p[po].x,p[po].y});
							po++;
						}
						if(cnt==a[i])
						{
							while(!cf.empty())
							{
								ans[cf.top().second]=cnt;
								cf.pop();
							}
						}
						while(po<uo+1)
						{
							ans[p[po].y]=p[po].x;
							po++;
						}
					}
					else if(i!=oi)
					{
						//cout<<po<<endl;
						if(cnt<a[i])
						{
						while(p[po].x<a[i]&&po<uo+1)
						{
							ans[p[po].y]=p[po].x;
							po++;
						}
						while(!cf.empty())
							{
								ans[cf.top().second]=cnt;
								cf.pop();
							}
						}
						//cout<<po<<endl;
						//cout<<p[po].x<<endl;
						/*if(a[i]==16)
						{
							cout<<cf.size()<<endl;
							cout<<po<<endl;
						}*/
						if(k[a[i]]>0)
						{
							cnt=max(cnt,g1[a[i]]);
						}
						//cout<<a[i]<<endl;
						while(p[po].x<=cnt&&po<uo+1)
						{
							cf.push({p[po].x,p[po].y});
							po++;
						}
						//cout<<po<<endl;
						if(cnt==a[i])
						{
							while(!cf.empty())
							{
								ans[cf.top().second]=cnt;
								cf.pop();
							}	
						}
					}
				}
				for(ll i=1;i<=uo;i++)cout<<ans[i]<<" ";
				cout<<endl;
			}
		}

相關文章