Educational Codeforces Round 170 (Rated for Div. 2)

比赛链接:https://codeforces.com/contest/2025

A. Two Screens


看前面最长相同前缀,如果有就前缀长+1+剩余长度,否则直接两个字符串长度相加

#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--)
	{
		string a,b;
		cin>>a>>b;
		ll ans=0;
		ll wz=-1;
		for(ll i=0;i<=min(a.size()-1,b.size()-1);i++)
		{
			if(a[i]==b[i])
			{
				ans++;
				wz=i;
			}
			else
			{
				break;
			}
		}
		if(wz!=-1)
		{
			ans++;
		}
		//cout<<ans<<endl;
		ans+=(ll)a.size()-1-wz;
		ans+=(ll)b.size()-1-wz;
		cout<<ans<<endl;
	}
}

B. Binomial Coefficients, Kind Of


手推算得到,k=1或者k=0答案为1,其余则是\(2^k\)

#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 n;
		cin>>n;
		for(ll i=1;i<=n;i++)cin>>a[i];
		for(ll i=1;i<=n;i++)cin>>b[i];
		for(ll i=1;i<=n;i++)
		{
			if(a[i]==b[i]||b[i]==0)
			{
				cout<<1<<endl;
			}
			else
			{
				cout<<ksm(2,b[i])%mod<<endl;
			}
		}
}

C. New Game

本质就是维护队列,先离散化每个数,然后维护两个条件下的队列即可

#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];
set<ll>q;
map<ll,ll>f;
int main()
{
	ll t;
	cin>>t;
	while(t--)
	{
		q.clear();
		f.clear();
		ll n,k;
		cin>>n>>k;
		for(ll i=1;i<=n;i++)
		{
			cin>>a[i];
			q.insert(a[i]);
			f[a[i]]++;
		}
		deque<ll>op;
		ll ans=0;
		ll cnt=0;
		for(auto j:q)
		{
			if(op.empty())
			{
				op.push_back(j);
				cnt+=f[j];
				ans=max(ans,cnt);
			}
			else
			{
				if(j==op.back()+1)
				{
					cnt+=f[j];
					op.push_back(j);
				while((ll)op.size()>k)
				{
					cnt-=f[op.front()];
					op.pop_front();
				}
				ans=max(ans,cnt);
				}
				else
				{
					op.clear();
					op.push_back(j);
					cnt=f[j];
					ans=max(ans,cnt);
				}
			}
		}
		cout<<ans<<endl;
	}
}

D. Attribute Checks


用一维数组差分每次选择后的可能变化,然后每次到0就结算之前的差分数组,以智力为什么时进行dp,最大值可以选择前面的或者现在的.
参考jly代码

	#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 dp[6005];
	ll a[3500000];
	ll f[6005];
	int main()
	{
		fio();
		ll n,m;
		cin>>n>>m;
		ll cnt=0;
		for(ll i=1;i<=n;i++)
		{
			cin>>a[i];
			if(a[i]==0)
			{
				for(ll j=0;j<=m;j++)
				{
					if(j>0)
					{
					dp[j]+=dp[j-1];
					dp[j-1]=0;
					}
					f[j]+=dp[j];
				}
				for(ll j=m;j>=1;j--)
				{
					f[j]=max(f[j],f[j-1]);
				}
				dp[m]=0;
				cnt++;
				continue;
			}
			if(a[i]>0)
			{
				if(cnt>=a[i])
				{
				dp[a[i]]++;
				dp[m+1]--;
				}
			}
			else 
			{
				ll u=abs(a[i]);
				if(cnt>=u)
				{
				dp[0]++;
				dp[cnt-u+1]--;
				}
			}
		}
		for(ll j=0;j<=m;j++)
				{
					if(j>0)
					{
					dp[j]+=dp[j-1];
					dp[j-1]=0;
					}
					f[j]+=dp[j];
				}
					for(ll j=m;j>=1;j--)
				{
					f[j]=max(f[j],f[j-1]);
				}
		ll ans=0;
		for(ll j=0;j<=m;j++)
		{
			ans=max(ans,f[j]);
		}
		cout<<ans<<endl;
	}
posted @ 2024-10-15 15:50  长皆  阅读(84)  评论(0编辑  收藏  举报