codeforces622D Optimal Number Permutation
来源:互联网 发布:网络与生活作文2000字 编辑:程序博客网 时间:2024/05/24 06:46
原题链接:http://codeforces.com/problemset/problem/622/D
题意:
长度为2*N的序列,包含1~N的数字各两个,要求找到一个排列,使得最小。。
思路:
只有di可变,所以使di+i-n的绝对值最小(di表示相同两数之间的距离),那么di=n-i;
所以1之间需要隔n-2个数字,2之间需要n-3个数字,3之间需要n-4个数字;
那么当1放完之后,其之间的n-2个空间(首尾相隔n-4)可以放3,以此类推;
所以我们可以把奇数和偶数分开放。
代码:
#include <cstdio>#include <cstring>int a[500005*2];int main(){int i, j, x, y,n;while (~scanf("%d", &n)){memset(a, 0, sizeof(a));if (n == 1){printf("1 1\n");continue;}x = 1;y = n;a[x] = a[y] = 1;i = 3;x++;y--;while (x < y){a[x] = a[y] = i;x++;y--;i += 2;}x = n+1;y = 2 * n - 1;a[x] = a[y] = 2;i = 4;x++;y--;while (x < y){a[x] = a[y] = i;x++;y--;i += 2;}for (i = 1; i <= 2 * n; i++){if (a[i] == 0)printf(i == 1 ? "%d" : " %d", n);elseprintf(i == 1 ? "%d" : " %d", a[i]);}printf("\n");}return 0;}
阅读全文
0 0
- codeforces622D Optimal Number Permutation
- Optimal Number Permutation CodeForces
- Codeforces 622D Optimal Number Permutation 【贪心】
- codeforces_622D. Optimal Number Permutation(构造)
- D. Optimal Number Permutation(构造)
- codeforces 622D Optimal Number Permutation 思维
- Educational Codeforces Round 7 D. Optimal Number Permutation(构造)
- Educational Codeforces Round 7--D. Optimal Number Permutation
- Codeforces edu 7 D. Optimal Number Permutation 构造
- cf#ecr7-D - Optimal Number Permutation-构造+脑洞
- Codeforces 622D Optimal Number Permutation(构造)
- codeforces edu round7 D Optimal Number Permutation构造
- CodeForces 622D Optimal Number Permutation(构造)
- Educational Codeforces Round 7-D. Optimal Number Permutation
- CodeForces 622 D.Optimal Number Permutation(构造)
- Codeforces 622D Optimal Number Permutation【贪心+构造】
- hdu3664 Permutation Counting (具体数学Eulerian Number)
- Number of permutation with K inversions
- 关于Callback的一些浅见
- input各种属性的使用
- Hbase 集群部署
- android 目录结构
- vs插件 自定义注释说明
- codeforces622D Optimal Number Permutation
- MongoDB 3.x+Java 官方学习资料
- 浅谈神经网络发展史:从莫克罗-彼特氏神经模型到深层神经网络
- 科目一考试
- Java的格式化输出
- 学习 ui-router (四): 路由控制
- :before 和 :after的多用途实践 — 特效篇(1)
- 最长回文
- TextView控件常用属性