Codeforces Round #851 (Div. 2)C. Matching Numbers

题目描述:

大意:

给你一个整数n。将整数1到2n配对(即每个整数应该正好在一对中),这样匹配对的每个和都是连续的和不同的。

正式地说,让(ai,bi)是你匹配的那对。{a1,b1,a2,b2,…,an,bn}应该是{1,2,…,2n}的置换。设{a1+b1,a2+b2,…,an+bn}的排序列表为s1<s2<…<sn。对于1≤i≤n−1,我们必须有s(i+1)−si=1。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤500). The description of the test cases follows.

For each test case, a single integer n (1≤n≤10^5) is given.

It is guaranteed that the sum of n over all test cases doesn't exceed 10^5.

每个测试包含多个测试用例。第一行包含测试用例数t(1≤t≤500)。测试用例的描述如下。

对于每个测试用例,给出单个整数n(1≤n≤10^5)。

保证所有测试用例的n之和不超过10^5。

输出格式:

For each test case, if it is impossible to make such a pairing, print "No".

Otherwise, print "Yes" followed by n lines.

At each line, print two integers that are paired.

You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.

If there are multiple solutions, print any.

对于每个测试用例,如果无法进行这样的配对,请打印“否”。

否则,打印“是”,后跟n行。

每行打印两个成对的整数。

您可以在任何情况下输出答案(上限或下限)。例如,字符串“yEs”、“yEs”、“yEs”和“yEs”将被识别为肯定响应。

如果有多种解决方案,请打印任何解决方案。

输入输出样例

输入 #1

4

1

2

3

4

输出 #1

Yes

1 2

No

Yes

1 6

3 5

4 2

No

思路:

通过观察可以得出:n个组合中,中间的那一个是1+n,才能满足题目要求,那么,当n为偶数时,一定不满足。接下来只用找到一个合适的方式排序。根据样例可以猜测,(1+2*i,2*n-i)0<=i<=n/2,可表示大于等于1+n的组合,(2*i,2*n-n/2-i)1<=i<=n/2,可表示小于1+n的组合。

代码:

#include<bits/stdc++.h>
using namespace std;


int main(){
	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		int mid=n+1;
		if(n%2==0){
			printf("NO\n");
		}
		else{
			printf("YES\n");
			for(int i=0;i<=n/2;i++){
				printf("%d %d\n",1+2*i,2*n-i);
			}
			for(int i=1;i<=n/2;i++){
				printf("%d %d\n",2*i,2*n-n/2-i);
			}
		}
	}

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值