Codeforces Round 987 (Div. 2)題解記錄(A~D)

长皆發表於2024-11-16

比賽連結:https://codeforces.com/contest/2031
明天再補題面,A題漏看題意了卡了到19分才過;C題知道9,16,25的情況,但是一直沒去想27,卡了50多分鐘;D題最後30秒交,有個判斷刪了就可以過,差點點。今天運勢不佳,各種錯誤併發,太累了,明天再補D思路。

A. Penchick and Modern Monument

思路:就是看哪個數最多,然後n-這個最大值即可。因為垃圾翻譯,所以認為陣列數無序的,於是寫了個dp

#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 dp[500][55];
ll a[250000];
int main()
{
	fio();
	ll t;
	cin>>t;
	while(t--)
	{
		ll n;
		cin>>n;
		ll ans=99999999999;
		for(ll i=1;i<=n;i++)
		{
			cin>>a[i];
			for(ll j=1;j<=50;j++)
			{
				if(a[i]==j)
				dp[i][j]=0;
				else dp[i][j]=1;
			}
			for(ll j=1;j<=50;j++)
			{
				ll u=99999999;
				for(ll k=1;k<=j;k++)
				{
				 u=min(dp[i][k],u);
				}
				dp[i][j]=dp[i-1][j]+u;
				if(i==n)
				ans=min(ans,dp[i][j]);
			}
		}
		cout<<ans<<endl;
	}
}

B. Penchick and Satay Sticks

思路:透過題目要求可以推得,如果一個數離他原本位置距離大於1,則一定無解

#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 ans=0;
		for(ll i=1;i<=n;i++)
		{
			cin>>a[i];
			if(abs(a[i]-i)<=1)continue;
			else ans=1;
		}
		if(ans==0)cout<<"YES"<<endl;
		else cout<<"NO"<<endl;

	}
}

C. Penchick and BBQ Buns

思路:偶數直接1 ,1 ,2, 2,3,3...即可,奇數,考慮吧出現x1x1+x2x2=(x1+x2)*(x1+x2)情況即可,只能3,4,5存在,所以對於長度等於27的陣列構造方法,可以是先9,16,26相同為1,然後27,23等於2,其餘空格為3,3;4,4慢慢填進去即可,對於大於27的奇數陣列,多的部分直接把多出來的部分按照偶數陣列的形式放置即可

#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;
		ll k=n;
		for(ll i=1;i<=n;i++)a[i]=0;
		if(n&1)
		{
			if(n>=27)
			{
				ll cnt=1;
				while(n>27)
				{
					a[n]=cnt;
					a[n-1]=cnt;
					n-=2;
					cnt++;
				}
				a[27]=a[23]=cnt;
				cnt++;
				a[26]=a[10]=a[1]=cnt;
				cnt++;
				for(ll i=1;i<=27;i++)
				{
					if(a[i]==0)
					{
						a[i]=cnt;
						a[i+1]=cnt;
						cnt++;
					}
				}
				for(ll i=1;i<=k;i++)
				cout<<a[i]<<" ";
				cout<<endl;
			}
			else 
			cout<<-1<<endl;
		}
		else 
		{
			ll cnt=1;
			for(ll i=1;i<=n;i+=2)
			{
				cout<<cnt<<" "<<cnt<<" ";
				cnt++;
			}	
			cout<<endl;
		}

	}
}

D. Penchick and Desert Rabbit

思路:

#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[654000];
ll b[625000];
ll pre[625000];
ll sub[625000];
int main()
{
fio();
    ll t;
	cin>>t;
	while(t--)
	{
		ll n;
		cin>>n;
		//ll ans=0;
		sub[n+1]=999999999999;
		for(ll i=1;i<=n;i++)cin>>a[i],pre[i]=max(a[i],pre[i-1]),b[i]=a[i];
		ll u=99999999999;
		ll ans;
		for(ll i=n;i>=1;i--)
		{
			if(i==n)
			{
				u=min(u,a[i]);
				ans=max(a[i],pre[i-1]);
				b[i]=ans;
			}
			if(pre[i-1]==ans||a[i]==ans)
			{
				b[i]=ans;
				u=min(u,a[i]);
			}
			else if(pre[i-1]>u||a[i]>u)
			{
				b[i]=ans;
				u=min(u,a[i]);
			}			
			else 
			{
				ans=max(pre[i-1],a[i]);
				u=a[i];
				b[i]=ans;
			}
		}
		for(ll i=1;i<=n;i++)cout<<b[i]<<" ";
		cout<<endl;
	}
}

相關文章