洛谷 CF482A Diverse Permutation

CF482A

题目大意 :

构造一种n的序列,让着n个数中间差值的绝对值有k种.

解题思路:

显然我们有n个数最多可以组成n-1种不同的差值(感性的意会一下)

然后我们可以知道前k + 1个人可以组成k种不同的差值,然后我们可以第一个放上1

第二个,放上k + 1,第三个放到2,第四个放到k.....

然后我们我们就可以得到一个k,k-1,k-2...的差值,最后的时候放完了k个数之后.

我们就把没放上的数从小到大依次放上,我们可以发现此时,剩下的数

组成的差值一定在上边放上的出现过,而且剩下的那些依次放的时候

可以发现后边的差值都是1.

code:

#include <map>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

#define N 100010
#define M 1010

using namespace std;
int n, m, num[N], cnt;
map<int, bool> ma;

int read() {
	int s = 0, f = 0; char ch = getchar();
	while (!isdigit(ch)) f |= (ch == '-'), ch = getchar();
	while (isdigit(ch)) s = s * 10 + (ch ^ 48), ch = getchar();
	return f ? -s : s;
}

int main() {
	n = read(), m = read();
	if (m == 1) {//特殊情况
		for (int i = 1; i <= n; i++) cout << i << " ";
		return 0;
	}
	cnt = 1, num[1] = 1;
	int j = ++m, i = 1; ma[1] = 1;
	while (cnt <= m) {
		if (ma[j - 1]) break;
		num[++cnt] = j--;
		ma[num[cnt]] = 1;
		if (ma[i + 1]) break;
		if (cnt < m) num[++cnt] = ++i, ma[i] = 1;
	}
	if (cnt >= n) 
		for (int i = 1; i <= n; i++) cout << num[i] << " ";
	else {
		int last = 2;
		for (int i = cnt + 1; i <= n; i++)
			for (int j = last; j <= n; j++)
				if(ma[j] == 0) {
					num[i] = j;
					last = j;
					ma[j] = 1;
					break;
				}
		for (int i = 1; i <= n; i++)
			cout << num[i] << " ";
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章