Codeforces Round 988 (Div. 3)題解記錄(A~F)

长皆發表於2024-11-18

比賽連結:https://codeforces.com/contest/2037
本場F題卡了快一小時?G題20分鐘內一直在想如何容斥,有空就補思路

A. Twice

#include<iostream>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<deque>
#include<cctype>
#include<string.h>
#include<math.h>
#include<time.h>
#include<random>
#include<stack>
#include<string>
#define ll                                     long long
#define lowbit(x) (x & -x)
#define endl "\n"//                           互動題記得刪除
using namespace std;
mt19937 rnd(time(0));
const ll mod = 998244353;
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 gcd(ll x, ll y)
{
	if (y == 0)
		return x;
	else
		return gcd(y, x % y);
}
void fio()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}
ll a[250000];
int main()
{
	fio();
	ll t;
	cin>>t;
	while(t--)
	{
		ll n;
		cin>>n;
		map<ll,ll>q;
		ll cnt=0;
		for(ll i=1;i<=n;i++)
		{
			ll x;
			cin>>x;
			if(q[x]==0)q[x]++;
			else cnt++,q[x]=0;
		}
		cout<<cnt<<endl;
	}

}

B. Intercepted Inputs

#include<iostream>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<deque>
#include<cctype>
#include<string.h>
#include<math.h>
#include<time.h>
#include<random>
#include<stack>
#include<string>
#define ll                                     long long
#define lowbit(x) (x & -x)
#define endl "\n"//                           互動題記得刪除
using namespace std;
mt19937 rnd(time(0));
const ll mod = 998244353;
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 gcd(ll x, ll y)
{
	if (y == 0)
		return x;
	else
		return gcd(y, x % y);
}
void fio()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}
ll a[250000];
int main()
{
	fio();
	ll t;
	cin>>t;
	while(t--)
	{
		map<ll,ll>q;
		ll n;
		cin>>n;
		ll u=n-2;
		ll l,r;
		for(ll i=1;i<=n;i++)
		{
			ll x;
			cin>>x;
			if(x*x==u)
			{
				q[x]++;
				if(q[x]>=2)
				{
					l=x,r=x;
				}
			}
			else if(u%x==0)
			{
				q[x]++;
				if(q[x]&&q[u/x])
				{
					l=x,r=u/x;
				}
			}
		}
		cout<<l<<" "<<r<<endl;
	}

}

C. Superultra's Favorite Permutation

#include<iostream>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<deque>
#include<cctype>
#include<string.h>
#include<math.h>
#include<time.h>
#include<random>
#include<stack>
#include<string>
#define ll                                     long long
#define lowbit(x) (x & -x)
#define endl "\n"//                           互動題記得刪除
using namespace std;
mt19937 rnd(time(0));
const ll mod = 998244353;
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 gcd(ll x, ll y)
{
	if (y == 0)
		return x;
	else
		return gcd(y, x % y);
}
void fio()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}
ll a[250000];
ll b[250000];
int main()
{
	fio();
	ll t;
	cin>>t;
	while(t--)
	{
		ll n;
		cin>>n;
		if(n<=4)
		cout<<-1<<endl;
		else 
		{
			ll l,r;
			l=r=0;
			for(ll i=6;i<=n;i++)
			{
				if(i%2==0)
				{
					l++;
					a[l]=i;
				}
				else 
				{
					r++;
					b[r]=i;
				}
			}
			for(ll i=1;i<=l;i++)cout<<a[i]<<" ";
			cout<<2<<" "<<4<<" "<<5<<" "<<1<<" "<<3<<" ";
			for(ll i=1;i<=r;i++)cout<<b[i]<<" ";
			cout<<endl;
		}
	}

}

D. Sharky Surfing

#include<iostream>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<deque>
#include<cctype>
#include<string.h>
#include<math.h>
#include<time.h>
#include<random>
#include<stack>
#include<string>
#define ll                                     long long
#define lowbit(x) (x & -x)
#define endl "\n"//                           互動題記得刪除
using namespace std;
mt19937 rnd(time(0));
const ll mod = 998244353;
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 gcd(ll x, ll y)
{
	if (y == 0)
		return x;
	else
		return gcd(y, x % y);
}
void fio()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}
ll a[250000];
ll b[250000];
struct s
{
	ll x, y;
}p[250000];
s p1[250000];
int main()
{
	fio();
	ll t;
	cin>>t;
	while(t--)
	{
		ll n,m,l;
		cin>>n>>m>>l;
		for(ll i=1;i<=n;i++)
		{
			cin>>p[i].x>>p[i].y;
		}
		for(ll i=1;i<=m;i++)
		{
			cin>>p1[i].x>>p1[i].y;
		}
		ll cnt=1;
		ll o=1;
		ll wz=1;
		ll c=1;
		priority_queue<ll>f;
		ll pd=0;
		ll gs=0;
		while(wz<p[o].x&&o<=n)
		{
			while(c<=m)
			{
				if(p1[c].x<p[o].x)
				{
					f.push(p1[c].y);
					c++;
				}
				else break;
			}
			while(!f.empty()&&cnt+p[o].x-1<=p[o].y)
			{
				gs++;
				cnt+=f.top();
				f.pop();
			}
			if(cnt+p[o].x-1<=p[o].y)
			{
				pd=1;
				break;
			}
			else 
			{
				wz=p[o].y+1;
				o++;
			}
		}
		if(pd||wz!=p[n].y+1)
		cout<<-1<<endl;
		else 
		cout<<gs<<endl;
	}
}

E. Kachina's Favorite Binary String

#include<iostream>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<deque>
#include<cctype>
#include<string.h>
#include<math.h>
#include<time.h>
#include<random>
#include<stack>
#include<string>
#define ll                                     long long
#define lowbit(x) (x & -x)
//#define endl "\n"//                           互動題記得刪除
using namespace std;
mt19937 rnd(time(0));
const ll mod = 998244353;
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 gcd(ll x, ll y)
{
	if (y == 0)
		return x;
	else
		return gcd(y, x % y);
}
void fio()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}
ll q(ll l,ll r)
{
	cout<<"? "<<l<<" "<<r<<endl;
	cout.flush();
	ll x;
	cin>>x;
	return x;
}
ll a[250000];
int main()
{
	fio();
	ll t;
	cin>>t;
	while(t--)
	{
		ll n;
		cin>>n;
		ll cnt=0;
		a[1]=1;
		for(ll i=2;i<=n;i++)
		{
			if(cnt==0)
			{
				ll j=q(1,i);
				if(j==0)
				{
					a[i]=1;
				}
				else  
				{
					cnt=j;
					a[i]=1;
					for(ll k=i-1;k>=i-1-cnt+1;k--)
					{
						a[k]=0;
					}
				//	cout<<a[i]<<endl; 
				}
			}
			else 
			{
				ll j=q(1,i);
				if(j==cnt)
				{
					a[i]=0;
				}
				else 
				{
					cnt=j;
					a[i]=1;
				}
			}
		}
		if(cnt==0)
		{
			cout<<"! IMPOSSIBLE"<<endl;
			continue;
	    }  
		cout<<"! ";
		for(ll i=1;i<=n;i++)
		{
			cout<<a[i];
		}
		cout<<endl;
	}
}

F. Ardent Flames

#include<iostream>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<deque>
#include<cctype>
#include<string.h>
#include<math.h>
#include<time.h>
#include<random>
#include<stack>
#include<string>
#define ll                                     long long
#define lowbit(x) (x & -x)
//#define endl "\n"//                           互動題記得刪除
using namespace std;
mt19937 rnd(time(0));
const ll mod = 998244353;
void fio()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}
ll q(ll l,ll r)
{
	cout<<"? "<<l<<" "<<r<<endl;
	cout.flush();
	ll x;
	cin>>x;
	return x;
}
ll a[250000];
ll b[250000];
ll c[250000];
ll n,m,k;
struct s
{
	ll l,r;
}p[4500000];
bool cmp(s x, s y)
{
	if(x.l!=y.l)
	return x.l<y.l;
	else 
	return x.r<y.r;
}
ll ck(ll x)
{
	ll l=0;
	for(ll i=1;i<=n;i++)
	{
		ll u=a[i]/x;
		if(a[i]%x!=0)u++;
		if(u>m)continue;
		else if(u==m) 
		{
			l++;
			p[l].l=p[l].r=b[i];
		}
		else 
		{
			l++;
			p[l].l=b[i]-(m-u);
			p[l].r=b[i]+(m-u);
		}
	}
	sort(p+1,p+1+l,cmp);
	priority_queue<ll,vector<ll>,greater<ll>>q;
	ll ans=0;
	for(ll i=1;i<=l;i++)
	{
		if(q.empty())
		{
			q.push(p[i].r);
		}
		else 
		{
			while(!q.empty()&&q.top()<p[i].l)
			{
				q.pop();
			}
			q.push(p[i].r);
		}
		ans=max(ans,(ll)q.size());
	}
	if(ans>=k)return 1;
	else return 0;
}
int main()
{
	fio();
	ll t;
	cin>>t;
	while(t--)
	{
		cin>>n>>m>>k;
		for(ll i=1;i<=n;i++)cin>>a[i];
		for(ll i=1;i<=n;i++)cin>>b[i];
		ll l=1,r=5e14;
		while(l<r)
		{
			ll mid=(l+r)>>1;
			if(ck(mid))
			r=mid;
			else l=mid+1;
		}
		if(r==5e14)cout<<-1<<endl;
		else 
		cout<<r<<endl;
	}
}

相關文章