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 构造题的更多相关文章

  1. [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 ...

  2. Educational Codeforces Round 14 D. Swaps in Permutation 并查集

    D. Swaps in Permutation 题目连接: http://www.codeforces.com/contest/691/problem/D Description You are gi ...

  3. Educational Codeforces Round 14 D. Swaps in Permutation (并查集orDFS)

    题目链接:http://codeforces.com/problemset/problem/691/D 给你n个数,各不相同,范围是1到n.然后是m行数a和b,表示下标为a的数和下标为b的数可以交换无 ...

  4. Educational Codeforces Round 14 D. Swaps in Permutation(并查集)

    题目链接:http://codeforces.com/contest/691/problem/D 题意: 题目给出一段序列,和m条关系,你可以无限次互相交换这m条关系 ,问这条序列字典序最大可以为多少 ...

  5. Educational Codeforces Round 14 D. Swaps in Permutation

    题目链接 分析:一些边把各个节点连接成了一颗颗树.因为每棵树上的边可以走任意次,所以不难想出要字典序最大,就是每棵树中数字大的放在树中节点编号比较小的位置. 我用了极为暴力的方法,先dfs每棵树,再用 ...

  6. codeforces 622C. Optimal Number Permutation 构造

    题目链接 假设始终可以找到一种状态使得值为0, 那么两个1之间需要隔n-2个数, 两个2之间需要隔n-3个数, 两个3之间隔n-4个数. 我们发现两个三可以放到两个1之间, 同理两个5放到两个3之间. ...

  7. Educational Codeforces Round 4 A. The Text Splitting 水题

    A. The Text Splitting 题目连接: http://www.codeforces.com/contest/612/problem/A Description You are give ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Nginx - Windows下Nginx基本安装和配置

    Nginx 是一个轻量级的高性能 Http WebServer,以事件驱动方式编写,因此相比 Apache 而言,Nginx 更加稳定.性能更好,而且配置简单,资源占用较低. 1. 安装 Nginx ...

  2. Dzz任务板初版完成笔记-仿trello私有部署的一款轻量团队任务协作工具。

    刚完成了第一个版本Dzz任务板的设计开发,记录下设计思路和完成情况. Dzz任务板是DzzOffice中的团队协作套件中的一款应用,它需要安装在DzzOffice中使用. 主界面中需要能够快速简单的创 ...

  3. effective c++:virtual函数在构造函数和析构函数中的注意事项

    如不使用自动生成函数要明确拒绝 对于一个类,如果你没有声明,c++会自动生成一个构造函数,一个析构函数,一个copy构造函数和一个copy assignment操作符. class Empty { p ...

  4. _margin和margin的区别

    _margin和margin的区别 _margin和margin的区别 Question: margin:15px 300px 0px 100px; height:72px; width:188px; ...

  5. ios8 下请求读取通讯录权限 (网上众多资料漏下得一个坑)

    读取通讯录权限网上都有,不再叙述. 当第一次授权时用户假如拒绝授权, 第二次就无法再次弹出提示框授权. 刚开始时完全按照网上例子写的,第一次拒绝授权后,在设置里面无法找到对应的更改读取权限的选项. 后 ...

  6. 第二百一十四天 how can I 坚持

    今天肯定有需要记录的东西. html表格. table tr rd colspan rowspan, 还有呢.没了. 睡觉,明天石洋要来泛华. 睡觉.

  7. ResultMap

    Mybatis 数据库字段和对象属性的映射 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE ...

  8. C++11 语法记录

    转自:http://blog.csdn.net/crayondeng/article/details/18563121 一.Lambda表达式 C++ 11中的Lambda表达式用于定义并创建匿名的函 ...

  9. 制作Andriod程序的数字签名需要使用JDK

    原文地址:制作Andriod程序的数字签名需要使用JDK作者:黄金大劫案88  制作Andriod程序的数字签名需要使用JDK,先确认本机是否安装了JDK,在JDK目录下有一个KEYTOOL工具,这个 ...

  10. vim之grep

    [vim之grep] :vimgrep 用于多文件搜索,如 1):vim[grep] start_stage *   在当前目录下(不包括子目录)搜索 2)  :vim[grep] start_sta ...