链接:https://ac.nowcoder.com/acm/contest/5668/D
来源:牛客网
题目描述
Imagine you have an infinite 2D plane with Cartesian coordinate system. Initially, all the integral points are painted as white. You are given two integers
n
n
n, and
m
m
m. Please paint exactly
n
n
n integral points to black such that there are exactly
m
m
m pairs of points which satisfy the following conditions:
-
The two points are colored by different colors.
-
the two points are adjacent. We call two integral points ( x 1 , y 1 ) (x_1, y_1) (x1,y1) and ( x 2 , y 2 ) (x_2, y_2) (x2,y2) being adjacent if and only if ∣ x 1 − x 2 ∣ + ∣ y 1 − y 2 ∣ = 1 |x_1 - x_2| + |y_1 - y_2| = 1 ∣x1−x2∣+∣y1−y2∣=1. ( ∣ v ∣ |v| ∣v∣ means the absolute value of v v v.)
-
The x x x and y y y coordinates of all black points are in the range [ − 1 0 9 , 1 0 9 ] [-10^9, 10^9] [−109,109].
输入描述:
The first line contains one integer
t
t
t
(
1
≤
t
≤
1
0
3
)
(1 \le t \le 10^3)
(1≤t≤103) — the number of test cases.
The only line of each test case contains two integers
n
n
n and
m
m
m
(
1
≤
n
≤
50
,
1
≤
m
≤
200
)
(1 \le n \le 50, 1 \le m \le 200)
(1≤n≤50,1≤m≤200).
输出描述:
For each test, if there exists at least one configuration to choose
n
n
n points to satisfy the conditions given by statement, you should print
n
+
1
n+1
n+1 line for this test. The first line contains one string “Yes”. And the following
n
n
n lines contain the coordinator of these
n
n
n points which is colored as black. If there are no solution, please print one line containing only one string “No”.
示例1
输入
6
5 20
1 2
1 3
1 4
1 5
3 8
输出
Yes
1 1
2 2
3 3
4 4
5 5
No
No
Yes
1 1
No
Yes
1 1
1 2
2 1
说明
In the first test and fourth test, each black point in the sample output is adjacent to exactly
4
4
4 white points.
In the sixth test, the second and third black points in the sample output are both adjacent to
3
3
3 white points and the forst black point is adjacent to
2
2
2 white points.
对于一个
n
n
n,首先找出
m
m
m的范围。
m
m
m的上界是
4
⋅
n
4·n
4⋅n,下界为
min
m
n
x
⋅
m
n
y
≥
n
(
2
⋅
(
m
n
x
+
m
n
y
)
)
\min\limits_{mnx·mny≥n}(2·(mnx+mny))
mnx⋅mny≥nmin(2⋅(mnx+mny))。
显然
m
m
m只能为偶数。因为
m
m
m只能变化偶数个,且初始只有一个方格时
m
=
4
m=4
m=4为偶数。
因此如果
m
m
m不在合理范围或
m
m
m为偶数的时候一定无解。
然后剩余情况一定有解。
因为如果初始化为下界,如图
n
=
5
n=5
n=5的情况。
初始时
m
m
m为最小值
10
10
10。
显然总是存在策略使得
m
m
m每次增加
2
2
2直到最大值
20
20
20。
因为每次可以将一个与两个已涂色的格子相邻的涂色的格子移到与一个已涂色的格子相邻的未涂色的格子上,或将与一个已涂色的格子相邻的涂色的格子移到不与涂色格子相邻的未涂色格子上。这两种方法都可以使
m
m
m增加
2
2
2。
策略
1
1
1:
策略
2
2
2:
因此先按下界方案涂色,然后先每次将与两个涂色格子相邻的涂色格子移走单独一个。
然后如果还是达不到
m
m
m,则采取策略
2
2
2,直到达到
m
m
m。
如果与
m
m
m相差等于
2
2
2且没有一个已涂色的格子相邻的涂色的格子,则采取策略
1
1
1。
#include<bits/stdc++.h>
#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define sc(a) scahf("%c",&a);
#define ss(a) scanf("%s",a)
#define pi(a) printf("%d\n",a)
#define pl(a) printf("%lld\n",a)
#define pc(a) putchar(a)
#define ms(a) memset(a,0,sizeof(a))
#define repi(i, a, b) for(register int i=a;i<=b;++i)
#define repd(i, a, b) for(register int i=a;i>=b;--i)
#define reps(s) for(register int i=head[s];i;i=Next[i])
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define pii pair<int,int>
#define mii unordered_map<int,int>
#define msi unordered_map<string,int>
#define lowbit(x) ((x)&(-(x)))
#define ce(i, r) i==r?'\n':' '
#define pb push_back
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define pr(x) cout<<#x<<": "<<x<<endl
using namespace std;
inline int qr() {
int f = 0, fu = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')fu = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
f = (f << 3) + (f << 1) + c - 48;
c = getchar();
}
return f * fu;
}
const int N = 55, base = 100;
int T, n, m;
int num[N];
vector<pii > tmp;
inline void move(int x, int y) {
tmp.pb({x, (tmp.size() + 1) * base + y});
}
int main() {
T = qr();
while (T--) {
n = qr(), m = qr();
if (m & 1 || m > 4 * n) {
puts("No");
continue;
}
int mnx, mny, mn = INF;
repi(i, 1, n)repi(j, n / i, n) {
if (i * j < n)continue;
if (i + j >= mn)break;
mn = i + j, mnx = i, mny = j;
}
if (mn * 2 > m) {
puts("No");
continue;
}
int d4 = (m - mn * 2) / 4, d2 = ((m - mn * 2) % 4) / 2;
repi(i, 1, mnx)num[i] = mny;
num[1] -= (mnx * mny - n);
tmp.clear();
repi(i, 1, mnx - 1) {
if (!d4)break;
while (num[i] > 1) {
if (!d4)break;
d4--, move(i, num[i]), num[i]--;
}
}
if (d4)d2 += d4 * 2;
repi(i, 1, mnx - 1) {
if (!d2 || num[i] != 1)break;
d2--, move(i, num[i]), num[i] = 0;
}
if (d2 && (mnx == 1 || !num[mnx - 1]))
while (d2 && (num[mnx] > 1))d2--, move(mnx, num[mnx]), num[mnx]--;
if (d2) num[1]--, tmp.pb({mnx + 1, 1});
puts("Yes");
repi(i, 1, mnx)repi(j, 1, num[i])printf("%d %d\n", i, j);
for (auto it:tmp)printf("%d %d\n", it.fi, it.se);
}
return 0;
}