Codeforces Round #540 (Div. 3) E. Yet Another Ball Problem 构造

题解

题目大意,要求构造一个(ai, bi)序列,ab的值在[1, k]内,要求同一个(ai, bi)内aibi不能相同,相邻的两个ai或bi不能相同,所有的a或b不能相同,没有两个完全一样的(ai, bi)。

当n大于k*(k-1)时则无解(注意相乘炸int),否则按照完全图的方式构建相邻的两个顺序反转一下(1, 2)(1, 3)(1, 4)(2, 3)(2, 4)(3, 4)

AC代码

#include <stdio.h>
#include <bits/stdc++.h>
#define fst first
#define sed second
using namespace std;
typedef long long ll;

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;

int main()
{
#ifdef LOCAL
	//freopen("C:/input.txt", "r", stdin);
#endif
	int n, k;
	cin >> n >> k;
	if (n > 1LL * k * (k - 1))
		cout << "NO" << endl, exit(0);
	cout << "YES" << endl;
	for (int i = 1; i <= k; i++)
		for (int j = i + 1; j <= k; j++)
		{
			printf("%d %d\n", i, j);
			if (--n == 0) exit(0);
			printf("%d %d\n", j, i);
			if (--n == 0) exit(0);
		}

	return 0;
}

猜你喜欢

转载自blog.csdn.net/CaprYang/article/details/87870620