「杂题乱刷」CF954C

题目链接

题目链接(CF)

题目链接(luogu)

题意简述

有一个 \(n \times m\) 的矩阵,矩阵上的数字 \(1 \sim n \times m\) 自上到下,自左到右,对于每次操作,你可以向上,下,左或右移动一步,你需要构造出符合操作序列的 \(n\)\(m\) 或报告无解。

解题思路

容易证明,合法的操作序列相邻两项绝对值之差最后最多两种取值,否则一定无解。

然后就可以直接模拟了,这题需要注意一些细节:

  1. 对于相邻的两次操作,一定不能站在同一个格子上。

hackin:

3
1 2 2

hackout:

NO
  1. 对于相邻的两次操作,编号相差 \(1\) 的两个格子不一定能够互相到达。

hackin:

5
1 2 4 3 2

hackout:

NO
  1. 向左走要注意减去列的长度

hackin:

8
1 2 3 2 3 4 3 8

hackout:

YES
1000000000

判掉了这几点,就能轻松 AC 了。

参考代码

// LUOGU_RID: 148123666
/*
Tips:
你数组开小了吗?
你MLE了吗?
你觉得是贪心,是不是该想想dp?
一个小时没调出来,是不是该考虑换题?
*/
#include<bits/stdc++.h>
using namespace std;
#define map unordered_map
#define forl(i,a,b) for(register long long i=a;i<=b;i++)
#define forr(i,a,b) for(register long long i=a;i>=b;i--)
#define lc(x) x<<1
#define rc(x) x<<1|1
#define mid ((l+r)>>1)
#define cin(x) scanf("%lld",&x)
#define cout(x) printf("%lld",x)
#define lowbit(x) x&-x
#define pb push_back
#define pf push_front
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define endl '\n'
#define QwQ return 0;
#define ll long long
#define lcm(x,y) x/__gcd(x,y)*y
ll t;
void solve()
{
	ll n,pd=0,num=0,su=0,sum=0;
	cin>>n;
	ll a[n+10]={0};
	forl(i,1,n)
		cin>>a[i];
	forl(i,2,n)
	{
		if(abs(a[i]-a[i-1])==0)
		{
			su=0;
			cout<<"NO\n";
			return ;
		}
		if(abs(a[i]-a[i-1])>1)
		{
			su=0;
			if(pd && abs(a[i]-a[i-1])!=num)
			{
				cout<<"NO\n";
				return ;
			}
			else
				pd=1,num=abs(a[i]-a[i-1]);
		}
		if(abs(a[i]-a[i-1])==1)
			su+=a[i]-a[i-1];
		sum=max(su,sum);
	}
	if(pd)
	{
		if(num<sum)
			cout<<"NO\n";
		else
		{
			forl(i,2,n)
			{
				if(abs(a[i]-a[i-1])==1)
				{
					if(min(a[i],a[i-1])%num==0 && num!=1)
					{
						cout/*<<i*/<<"NO\n";
						return ;
					}
				}
			}
			cout<<"YES\n"<<1000000000<<' '<<num<<endl;
		}
	}
	else
		cout<<"YES\n"<<1000000000<<' '<<1000000000<<endl;
}
int main()
{
	IOS;
	t=1;
//	cin>>t;
	while(t--)
		solve();
    /******************/
	/*while(L<q[i].l) */
	/*    del(a[L++]);*/
	/*while(L>q[i].l) */
	/*    add(a[--L]);*/
	/*while(R<q[i].r) */
	/*	  add(a[++R]);*/
	/*while(R>q[i].r) */
	/*    del(a[R--]);*/
    /******************/
	QwQ;
}

评价:

本题是一道不可多得的模拟题。

推荐题目

posted @ 2024-02-23 20:55  wangmarui  阅读(12)  评论(0编辑  收藏  举报