CF 723 A

#CF 723 A
链接: link.
题意:给定一个n,之后输入2n个数通过交换2n个数的位置形成的环(相当于首尾相接)满足任意x a[x] != (a[x - 1] + a[x + 1]) / 2(不下取整).

题解:
首先对于这种构造题,找到关键点,直接看能否凑出反例特例。
任意 a[i] 若不成立则 相当于 a[i - 1] = a[i] - x , a[i + 1] = a[i] + x,可以看出a[i]的左右一定是一个比他大一个比他小,所以当左右都比它小,或者都比它大的时候就不成立了。
由此可以想到sort一下从中间分开,左半边放一下,右半边放一下,就满足小大小的结构了。

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <set>
#include <queue>
#include <vector>
#include <map>
#include <unordered_map>
#include <cmath> 
#include <stack>
#define x first
#define y second
 
using namespace std;
 
typedef long long LL;
typedef pair<int, int> PII;
typedef unsigned long long ULL;
const int N = 100010, M = 1100, INF = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
int n, m, x, y, k, cnt, t;
int a[N];
int main() {
		cin >> t;
	while (t --) {
		int x;
		cin >> x;
		n = 2 * x;
		for (int i = 0; i < n; i ++ )	cin >> a[i];
		sort(a, a + n);
		for (int i = 0; i < n / 2; i ++ ) cout << a[i] << ' ' << a[i + x] << ' ';
		cout << endl;
	} 
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值