Points Construction Problem

链接: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:

  1. The two points are colored by different colors.

  2. 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 x1x2+y1y2=1. ( ∣ v ∣ |v| v means the absolute value of v v v.)

  3. 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) (1t103) — 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) (1n50,1m200).
输出描述:
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 4n,下界为 min ⁡ m n x ⋅ m n y ≥ n ( 2 ⋅ ( m n x + m n y ) ) \min\limits_{mnx·mny≥n}(2·(mnx+mny)) mnxmnynmin(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:
策略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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_sky123_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值