跳到正文

题解

题解:CF1658E Gojou and Matrix Game

zyn0309约 8 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 8 分钟
题目
CF1658E

版本

  1. v1当前

CF1658E Gojou and Matrix Game

题目传送门

首先考虑什么情况是无效的。如果从一个点跳到了一个比它的值跟小的点,那么另一个人一定可以再调回来,并获得更大的分数。

因此我们可以钦定一个点只能向比它更大的点跳,此时值最大的点显然是必败的。

然后注意到,一个点如果必胜,一定是能跳到一个必败的点,因为我们已经钦定了只能向更大的点跳,所以可以按照权值从大到小处理,然后在每个点查询是否有距离大于 kk 的必败点。

本题中的距离为曼哈顿距离,带有两项绝对值,不太好维护,所以可以拆成下面这样。

max⁡((x−x0+y−y0),(x0−x+y−y0),(x−x0+y0−y),(x0−x+y0−y))>k\max((x-x_0+y-y_0),(x_0-x+y-y_0),(x-x_0+y_0-y),(x_0-x+y_0-y))>k

其中 xx 和 yy 表示当前点的坐标,x0x_0 和 y0y_0 表示之前处理出的必败态的坐标。

注意到绝对值和上面的式子是等价的,同时由于绝对值的方向取反了只会让上面的式子变小,所以可以对四种情况分别维护取最大值,这样原来能取到的一定还能取到,原来取不到的也肯定取不到,所以答案正确。

Code

C++
#include<bits/stdc++.h>
namespace fast_IO{
	#define IOSIZE 100000
		char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
	#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
	#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
	#define isdigit(ch) (ch>47&&ch<58)
	#define isspace(ch) (ch<33)
		template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
		template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
		template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
		inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
		inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
		inline void print(char x) { putchar(x); }
		inline void print(char *x) { while (*x) putchar(*x++); }
		inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
		inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
		inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
		inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
		inline void print(bool b) { putchar(b+48); }
		template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
		template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
		struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io;
		template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
		template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
	#define cout io
	#define cin io
}using namespace fast_IO;
using namespace std;
const int N=2005;
#define pii  pair<int,int>
#define mk make_pair
int n,k,a[N][N];
pii pos[N*N];
bool win[N][N];
int ma[5];//(x0-x,y0-y) (x-x0,y0-y) (x0-x,y-y0) (x-x0,y-y0)
signed main(){
	cin>>n>>k;
	for(int i=1;i<=n;++i)
	for(int j=1;j<=n;++j){
	  cin>>a[i][j];
	  pos[a[i][j]]=mk(i,j);
	}
	for(int i=0;i<5;++i)ma[i]=-2e9;
	for(int i=n*n;i>=1;--i){
	  int x=pos[i].first,y=pos[i].second;
	  if(ma[0]-x-y>k)win[x][y]=1;
	  if(ma[1]+x-y>k)win[x][y]=1;
	  if(ma[2]-x+y>k)win[x][y]=1;
	  if(ma[3]+x+y>k)win[x][y]=1;
	  if(!win[x][y]){
		ma[0]=max(ma[0],x+y);
		ma[1]=max(ma[1],-x+y);
		ma[2]=max(ma[2],x-y);
		ma[3]=max(ma[3],-x-y);
	  }
	}
	for(int i=1;i<=n;++i){
	  for(int j=1;j<=n;++j){
		if(win[i][j])cout<<"G";
		else cout<<"M";
	  }
	  cout<<"\n";
	}
	return 0;
}
原文