Codeforces 622D Optimal Number Permutation【贪心+构造】
来源:互联网 发布:虫虫群发软件 编辑:程序博客网 时间:2024/06/07 01:15
You have array a that contains all integers from1 to n twice. You can arbitrary permute any numbers ina.
Let number i be in positions xi, yi (xi < yi) in the permuted array a. Let's define the value di = yi - xi — the distance between the positions of the numberi. Permute the numbers in array a to minimize the value of the sum .
The only line contains integer n (1 ≤ n ≤ 5·105).
Print 2n integers — the permuted array a that minimizes the value of the sum s.
2
1 1 2 2
1
1 1
题目大意:
让你构造出一个长度为2*n的序列。
设定序列必须包含1~n每个数各两次。
设定一个数i出现的两个位子为xi,yi(xi<yi),那么对应di=yi-xi.
现在希望.尽可能的小,让你构造出一个序列。
思路:
1、现在我们希望整体最优,因为每个部分都是独立不相互影响的,那么我们考虑局部最优可以导致整体最优。
那么对于第i个数来讲,(n-i)是不必考虑的,因为其值是固定的,我们考虑|di+i-n|尽可能的小,我们又知道,绝对值的最小值就是0.那么我们希望di=n-i..
而又有当i==n的时候(n-i)已经是0.那么我们问题转换变成:
对于1~n这些数来讲,我们希望数字i两个位子的差距为n-i那么长。
2、接下来问题就变成了构造,手写两组数组我们不难想到:
将数组分成两个长度为N的部分,i%2==1的部分放在左侧,i%2==0的部分放在右侧,然后贪心的将位子放置好即可。
具体细节参考代码。
Ac代码:
#include<stdio.h>#include<string.h>#include<queue>using namespace std;int ans[1080000];int main(){ int n; while(~scanf("%d",&n)) { memset(ans,-1,sizeof(ans)); int nex=-1; int cnt=1; for(int i=1;i<=n-1;i++) { int want=n-i; if(i%2==1) { ans[cnt]=i; ans[cnt+want]=i; } else { ans[n+cnt]=i; ans[n+cnt+want]=i; cnt++; } } for(int i=1;i<=2*n;i++) { if(ans[i]==-1)printf("%d ",n); else printf("%d ",ans[i]); } printf("\n"); }}
- Codeforces 622D Optimal Number Permutation【贪心+构造】
- Codeforces 622D Optimal Number Permutation 【贪心】
- Codeforces 622D Optimal Number Permutation(构造)
- CodeForces 622D Optimal Number Permutation(构造)
- CodeForces 622 D.Optimal Number Permutation(构造)
- codeforces 622D Optimal Number Permutation 思维
- Educational Codeforces Round 7 D. Optimal Number Permutation(构造)
- Codeforces edu 7 D. Optimal Number Permutation 构造
- codeforces edu round7 D Optimal Number Permutation构造
- D. Optimal Number Permutation(构造)
- Optimal Number Permutation CodeForces
- cf#ecr7-D - Optimal Number Permutation-构造+脑洞
- Educational Codeforces Round 7--D. Optimal Number Permutation
- Educational Codeforces Round 7-D. Optimal Number Permutation
- codeforces_622D. Optimal Number Permutation(构造)
- codeforces622D Optimal Number Permutation
- Codeforces Round #436 (Div. 2)-贪心-D. Make a Permutation!
- CodeForces 864D Make a Permutation! 【贪心】【模拟】
- Maven 常用配置
- Android
- Linux 下 PATH与LS_LIBRARY_PATH 环境变量设置相关问题
- java算法(二)
- 程序员的10个谎言,你听过几个?
- Codeforces 622D Optimal Number Permutation【贪心+构造】
- 第一天整理的笔记
- 在Mac OS X上安装 dnsmasq 来搭建DNS服务器
- 1019. 数字黑洞
- 使用maven构建 ssm项目 tomcat7插件运行报错, Invalid byte tag in constant pool: 60
- jsp:bean与el表达式笔记
- 计算机图形学(八)几何图形变换讲解与源代码
- echarts图表显示问题
- Android面试题-与性能优化相关面试题三