Codeforces Round #275 (Div. 2) C Diverse Permutation
来源:互联网 发布:金华网络教育 编辑:程序博客网 时间:2024/06/06 21:41
C Diverse Permutation
题意是,1到n的全排列,记为p1, p2, ..., pn.,让你找出一种排列,这种排列满足|p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| ,这n-1个数有k个不同的数。其中1 ≤ k < n ≤ 105
我是用构造的方法做的,1到k+1的全排列一定有一个可以使得上式有k个不同的数,为1到k,我们使第一个数为1,|pi - pi+1|等于k-i+1,就可以构造出来,然后k+2到n依次排列就可以了,只有|pk+1 - pk+2|这个地方才可能超过k,其他地方都是1,因为第k+2个数是k+2,所以第k+1个数为1时才能超过k,但是1已经放在了第一个位置,所以这个数一定小于等于k。这就构造出来了答案。
但我在写的时候没有考虑前k+1个数必须是1到k+1的排列,我忽略了这个条件,错了好多次,最后才发现了。
代码如下:
#include <cstdio>#include <cstring>#define N 100010int ans[N], n, k, vis[N];int main(void){while (scanf("%d%d", &n, &k) != EOF) {memset(ans, 0, sizeof(ans));memset(vis, 0, sizeof(ans));ans[1] = 1;vis[1] = 1;for (int i = 2; i <= k + 1; ++i) {int m = k - i + 2;if (ans[i - 1] + m > k + 1) {ans[i] = ans[i - 1] - m;} else {int tmp = ans[i - 1] + m;if (vis[tmp] == 0) {ans[i] = ans[i - 1] + m;} else {ans[i] = ans[i - 1] - m;}}vis[ans[i]] = 1;}int cnt = k + 2;for (int i = cnt; i <= n; ++i) {ans[i] = i;}for (int i = 1; i <= n; ++i) {printf("%d%c", ans[i], (i == n) ? '\n' : ' ');}}return 0;}
0 0
- Codeforces Round #275 (Div. 2) C - Diverse Permutation (构造)
- Codeforces Round #275 (Div. 2) C Diverse Permutation
- Codeforces Round #275 (Div. 2) --C Diverse Permutation
- Codeforces Round #275 (Div. 2) C. Diverse Permutation 构造
- C. Diverse Permutation(Codeforces Round #275(div2)
- Codeforces Round #275 (Div. 1)A. Diverse Permutation (水)
- codeforces C. Diverse Permutation
- 【CODEFORCES】C. Diverse Permutation
- codeforces 483C.Diverse Permutation
- CodeForces-483C Diverse Permutation
- CodeForces 483C Diverse Permutation
- CodeForces-483C Diverse Permutation(构造)
- Codeforces 483C Diverse Permutation【思维构造】
- Codeforces Round #183 (Div. 2)-C. Lucky Permutation Triple
- Codeforces Round #209 (Div. 2) B. Permutation
- Codeforces:Diverse Permutation(找规律)
- CodeForces 482A Diverse Permutation
- CodeForces 482A Diverse Permutation
- 上机作业2
- Linux文件类型与扩展名
- ubuntu 12.04 qq
- Centos 6.5 x64上安装redis
- Cannot refer to a non-final variable image inside an inner class 关于android 的final
- Codeforces Round #275 (Div. 2) C Diverse Permutation
- 捷速ocr文字识别软件巧运用 对工作压力say no
- DirectShow基础编程 最简单transform filter 编写步骤
- 创建Windows类
- 解剖 Nginx ·自动脚本篇(2)设置初始变量脚本 auto/init
- Leetcode: Insertion Sort List
- linux ps命令
- 和字符串数组
- C语言---给定年、月、日,输出其为这一年的第几天(多版本)