CF1968E 题解

· · 题解

思路

赛时始终百思不得其解,赛后看了他人代码恍然大悟。

先看一下我画的 n=5 时的做法(x 表示选择的点):

xx...
.....
..x..
...x.
....x

首先距离为 0 是一定存在的,然后距离为 1 就在第一行,2 在 a_{i,i},a_{i+1,i+1}(3\le i<n),3 在 a_{1,2},a_{3,3},4 在 a_{1,1},a_{3,3},2i-3 在 a_{1,2},a_{i,i},2i-2 在 a_{1,1},a_{i,i},这一来,0\sim 2n-2 都凑齐了(n=2,3 除外,但也是最优解)。

代码

# include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
int t, n;
int main () {
    ios::sync_with_stdio (0);
    cin.tie (0);
    cout.tie (0);
    cin >> t;
    while (t --) {
        cin >> n;
        cout << "1 1\n1 2\n";
        for (int i = 3; i <= n; ++ i)
            cout << i << ' ' << i << '\n';
        cout << '\n';
    }
    return 0;
}