Codeforces Round 981 (Div. 3)(A~E)

长皆發表於2024-10-25

比賽連結:https://codeforces.com/contest/2033

A. Sakurako and Kosuke


直接模擬好吧

#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 lowbit(x) (x & -x)
using namespace std;
mt19937 rnd(time(0));
typedef long long ll;
const ll mod=1e9+7;
const ll N=2e5+5;
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);
}
int main()
{
	fio();
	ll t;
	cin>>t;
	while(t--)
	{
		ll n;
		cin>>n;
		ll cnt=-1;
		ll ans=1;
		ll i=1;
		ll gs=1;
		while(cnt<=n&&cnt>=-n)
		{
			gs++;
			if(i%2==1)
			{
				cnt+=(2*gs-1);
			}
			else 
			{
				cnt-=(2*gs-1);
			}
			i++;
			ans++;
		}
		if(ans%2==0)
		{
			cout<<"Kosuke"<<endl;
		}
		else 
		{
			cout<<"Sakurako"<<endl;
		}
	}

}

B. Sakurako and Water


直接統計主對角線最大負數然後取正加起來就好

#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 lowbit(x) (x & -x)
using namespace std;
mt19937 rnd(time(0));
typedef long long ll;
const ll mod=1e9+7;
const ll N=2e5+5;
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[600][600];
int main()
{
	fio();
	ll t;
	cin>>t;
	while(t--)
	{
		ll n;
		cin>>n;
		map<ll,ll>q;
		for(ll i=1;i<=n;i++)
		{
			for(ll j=1;j<=n;j++)
			{
				cin>>a[i][j];
				if(a[i][j]<0)
				{
					q[i-j]=min(q[i-j],a[i][j]);
				}
			}
		}
		ll ans=0;
		for(ll i=1;i<=n;i++)
		{
			for(ll j=1;j<=n;j++)
			{
				if(q[i-j]<0)
				{
					ans+=abs(q[i-j]);
					q[i-j]=0;
				}
			}
		}
		cout<<ans<<endl;
	}

}

C. Sakurako's Field Trip


從中心進行擴散,然後考慮影響即可

#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 lowbit(x) (x & -x)
using namespace std;
mt19937 rnd(time(0));
typedef long long ll;
const ll mod=1e9+7;
const ll N=2e5+5;
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;
		for(ll i=1;i<=n;i++)cin>>a[i];
		ll l=(n+1)/2;
		ll r=(n+2)/2;
			while(l>=1)
			{
				ll k=l;
				ll u=r;
				l--;
				r++;
				if(l==0)
				break;
				if(a[l]==a[k]&&a[r]==a[u]&&a[l]!=a[r])
				{
					swap(a[l],a[r]);
				}
				else if(a[l]==a[k]&&a[r]!=a[u])
				{
					swap(a[l],a[r]);
				}
				else if(a[r]==a[u]&&a[l]!=a[k])
				{
					swap(a[l],a[r]);
				}
			}
			ll ans=0;
			for(ll i=1;i<=n-1;i++)
			{
				if(a[i]==a[i+1])ans++;
			}
			cout<<ans<<endl;
	}

}

D. Kousuke's Assignment


從左邊起,如果每次能湊一對就直接拿,然後清空map

#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 lowbit(x) (x & -x)
using namespace std;
mt19937 rnd(time(0));
typedef long long ll;
const ll mod=1e9+7;
const ll N=2e5+5;
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;
	map<ll,ll>q;
	while(t--)
	{
		q.clear();
		ll n;
		cin>>n;
		ll ans=0;
		q[0]++;
		ll l=0;
		for(ll i=1;i<=n;i++)
		{
			cin>>a[i];
			a[i]+=a[i-1];
				if(q[a[i]-a[l]]>0)
				{
					q.clear();
					q[0]++;
					ans++;
					l=i;
					continue;
				}
				q[a[i]-a[l]]++;
		}
		cout<<ans<<endl;
	}

}

E. Sakurako, Kosuke, and the 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 lowbit(x) (x & -x)
using namespace std;
mt19937 rnd(time(0));
typedef long long ll;
const ll mod=1e9+7;
const ll N=2e5+5;
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[2500000];
bool vis[2500000];
ll b[2500000];
int main()
{
	fio();
	ll t;
	cin>>t;
	while(t--)
	{
		ll n;
		cin>>n;
		for(ll i=1;i<=n;i++)
		{
			cin>>a[i],vis[i]=0;
			b[a[i]]=i;
		}
		ll ans=0;
		for(ll i=1;i<=n;i++)
		{
			if(vis[i])continue;
			if(a[a[i]]==i)
			{
				vis[i]=1;
				vis[a[i]]=1;
			}
			else if(a[i]==i)
			{
				vis[i]=1;
			}
			else 
			{
				// 3   1
				ll u=a[a[i]],j=a[b[i]];
				swap(a[a[i]],a[b[i]]);//
				//cout<<a[a[i]]<<" "<<a[b[i]]<<endl;
				swap(b[u],b[j]);
				vis[i]=1;
				vis[a[i]]=1;
				ans++;
			}
		}
		cout<<ans<<endl;
	}
}

相關文章