Educational Codeforces Round 7 D. Optimal Number Permutation 构造题
D. Optimal Number Permutation
题目连接:
http://www.codeforces.com/contest/622/problem/D
Description
You have array a that contains all integers from 1 to n twice. You can arbitrary permute any numbers in a.
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 number i. Permute the numbers in array a to minimize the value of the sum .
Input
The only line contains integer n (1 ≤ n ≤ 5·105).
Output
Print 2n integers — the permuted array a that minimizes the value of the sum s.
Sample Input
2
Sample Output
1 1 2 2
Hint
题意
构造题,让构造一个1-n中每个数出现两次的一个序列,使得这个序列的权值和最小
权值 = (n-i)(di+i-n),di是指第i个数出现两次的距离
题解:
我们猜想一下,我们能不能让di = n-i,这样权值和就是0了
于是就这样构造吧,我们分奇数偶数去摆放,这样就可以错开了,就不会重叠在一起
因为n这个数可以使得(n-i)=0,所以就用它来调整这个序列
唔,如果我题解不是很清楚,可以跑跑我的代码,可以发现出来的结果是两个类似回文串的东西~
喵。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
int a[maxn];
int d[maxn];
int main()
{
int n;
scanf("%d",&n);
int tot = 1;
for(int i=1;i<=n;i+=2)
a[tot++]=i;
int p = n;
if(p%2==0)p--;
else p-=2;
for(int i=p;i>=1;i-=2)
{
a[tot++]=i;
}
for(int i=2;i<=n;i+=2)
a[tot++]=i;
p=n;
if(p%2==1)p--;
else p-=2;
for(int i=p;i>=1;i-=2)
a[tot++]=i;
a[tot++]=n;
int Ans=0;
/*
for(int j=1;j<=2*n;j++)
{
if(d[a[j]])
Ans+=(n-a[j])*abs(j-d[a[j]]+a[j]-n);
else
d[a[j]]=j;
}
cout<<Ans<<endl;
*/
for(int i=1;i<=2*n;i++)
cout<<a[i]<<" ";
cout<<endl;
}
Educational Codeforces Round 7 D. Optimal Number Permutation 构造题的更多相关文章
- [Educational Codeforces Round 16]B. Optimal Point on a Line
[Educational Codeforces Round 16]B. Optimal Point on a Line 试题描述 You are given n points on a line wi ...
- Educational Codeforces Round 14 D. Swaps in Permutation 并查集
D. Swaps in Permutation 题目连接: http://www.codeforces.com/contest/691/problem/D Description You are gi ...
- Educational Codeforces Round 14 D. Swaps in Permutation (并查集orDFS)
题目链接:http://codeforces.com/problemset/problem/691/D 给你n个数,各不相同,范围是1到n.然后是m行数a和b,表示下标为a的数和下标为b的数可以交换无 ...
- Educational Codeforces Round 14 D. Swaps in Permutation(并查集)
题目链接:http://codeforces.com/contest/691/problem/D 题意: 题目给出一段序列,和m条关系,你可以无限次互相交换这m条关系 ,问这条序列字典序最大可以为多少 ...
- Educational Codeforces Round 14 D. Swaps in Permutation
题目链接 分析:一些边把各个节点连接成了一颗颗树.因为每棵树上的边可以走任意次,所以不难想出要字典序最大,就是每棵树中数字大的放在树中节点编号比较小的位置. 我用了极为暴力的方法,先dfs每棵树,再用 ...
- codeforces 622C. Optimal Number Permutation 构造
题目链接 假设始终可以找到一种状态使得值为0, 那么两个1之间需要隔n-2个数, 两个2之间需要隔n-3个数, 两个3之间隔n-4个数. 我们发现两个三可以放到两个1之间, 同理两个5放到两个3之间. ...
- Educational Codeforces Round 4 A. The Text Splitting 水题
A. The Text Splitting 题目连接: http://www.codeforces.com/contest/612/problem/A Description You are give ...
- Codeforces Educational Codeforces Round 3 B. The Best Gift 水题
B. The Best Gift 题目连接: http://www.codeforces.com/contest/609/problem/B Description Emily's birthday ...
- Codeforces Educational Codeforces Round 3 A. USB Flash Drives 水题
A. USB Flash Drives 题目连接: http://www.codeforces.com/contest/609/problem/A Description Sean is trying ...
随机推荐
- IOS-多视图控制器之间的切换
1. 创建个单视图应用程序 2. 在向Main.storyboard中拖一个ViewController控制器 3. 在第一个viewController中添加一个按钮 4. 按着control键,推 ...
- leetcode:Roman to Integer(罗马数字转化为罗马数字)
Question: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the rang ...
- “内部类” 大总结(Java)
(本文整理自很久以前收集的资料(我只是做了排版修改),作者小明,链接地址没有找到,总之感谢,小明) (后面也对"静态内部类"专门做了补充) 内部类的位置: 内部类可以作用在方法里以 ...
- 封装cookie.js、EventUtil.js、
最近学习了javascript,封装好的东西看起来舒服,以备需要的时候拉出来,jquery对javascript做了很好的封装!以后会多用jquery多些 var CookieUtil = { get ...
- extjs笔记
1. ExtJs 结构树.. 2 2. 对ExtJs的态度.. 3 3. Ext.form概述.. 4 4. Ext.TabPanel篇.. 5 5. Functio ...
- Azure杂七杂八系列(二) - 如何在Azure上重新配置VM
我们经常遇到这样的问题, 对于已经建立的VM进行性能提升, 比如需要更好的虚拟机或者需要迁移到其他的虚拟网络 那么我们可以使用以下的方法进行修改. 1. 如图所示, TESTVMXX位于North ...
- 【CLR】奇妙的String
- 一.背景 1. 以下代码的HashCode是否相同,它们是否是同个对象: var A = "ab" + "c"; var B = "abc&quo ...
- geeksforgeeks@ Sorting Elements of an Array by Frequency (Sort)
http://www.practice.geeksforgeeks.org/problem-page.php?pid=493 Sorting Elements of an Array by Frequ ...
- Latex 横排图片
\begin{figure} \begin{minipage}[t]{0.5\linewidth} \centering \includegraphics[width=2.2in]{figure/an ...
- 转】MyEclipse使用总结——在MyEclipse中设置jsp页面为默认utf-8编码
原博文出自于:http://www.cnblogs.com/xdp-gacl/p/3496161.html 感谢! 在MyEclispe中创建Jsp页面,Jsp页面的默认编码是"ISO-88 ...