Educational Codeforces Round 7 D. Optimal Number Permutation 构造题

时间:2023-01-30 15:22:45

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. &lbrack;Educational Codeforces Round 16&rsqb;B&period; 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&period; 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&period; Swaps in Permutation &lpar;并查集orDFS&rpar;

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

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

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

  5. Educational Codeforces Round 14 D&period; Swaps in Permutation

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

  6. codeforces 622C&period; Optimal Number Permutation 构造

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

  7. Educational Codeforces Round 4 A&period; 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&period; 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&period; USB Flash Drives 水题

    A. USB Flash Drives 题目连接: http://www.codeforces.com/contest/609/problem/A Description Sean is trying ...

随机推荐

  1. Ifvisible&period;js – 判断网页中的用户是闲置还是活动状态

    ifvisible.js 是一个跨浏览器.轻量级的方式,用户检查用户在浏览页面或正在与它进行交互.它可以处理活动状态,如在页面上空闲或活跃.您还可以使用 ifvisible.js 智能设置您的间隔,如 ...

  2. zookeeper典型应用场景之一:master选举

    对于zookeeper这种东西,仅仅知道怎么安装是远远不够的,至少要对其几个典型的应用场景进行了解,才能比较全面的知道zk究竟能干啥,怎么玩儿,以后的日子里才能知道这货如何能为我所用.于是,有了如下的 ...

  3. 慕课linux学习笔记(七)常用命令(4)

    帮助命令 Man命令 Man的级别 命令帮助 可被内核调用的函数帮助 函数和函数库帮助 特殊文件帮助 /dev 配置文件帮助 游戏的帮助 其他杂项的帮助 管理员可用命令帮助 内核相关文件的帮助 -f ...

  4. &lbrack;转&rsqb;前端利器:SASS基础与Compass入门

    [转]前端利器:SASS基础与Compass入门 SASS是Syntactically Awesome Stylesheete Sass的缩写,它是css的一个开发工具,提供了很多便利和简单的语法,让 ...

  5. 2016 ASC 移动物联网安全高峰论坛 万物互联时代的安全与隐私

    互联网的发展已经迈入了"万物互联"时代.移动设备作为人.物连接的主要入口,让人们享受高效.便利的"互联生活"的同时,也给用户的安全和隐私带来了前所未有的挑战.正 ...

  6. Swift资料大全

    简单看了一下swift的语法,个人感觉相对于object-c很是简单明了.Swift的出现并不能说明iOS开发简单了很多,有可能会变得复杂,你需要学习两门编程语言,因为在程序员的世界里,务实并不是最重 ...

  7. java实现截屏

    import java.awt.Dimension; import java.awt.Rectangle; import java.awt.Robot; import java.awt.Toolkit ...

  8. asp&period;net网页上获取其中表格中的数据(爬数据)

    下面的方法获取页面中表格数据,每个页面不相同,获取的方式(主要是正则表达式)不一样,只是提供方法参考.大神勿喷,刚使用了,就记下来了. 其中数据怎么存,主要就看着怎么使用了.只是方便记录就都放在lis ...

  9. devmem读写物理内存和devkmem读取内核虚拟内存

    关键词:/dev/mem./dev/kmem.mmap.__va.__pa.remap_pfn_range等等. 在日常工作中常有直接操作寄存器或者某一物理地址的需求,busybox中提供了devme ...

  10. HTML5 script 标签的 crossorigin 和integrity属性的作用

    Bootstrap 4 依赖的基础库中出现了两个新的属性 <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.slim.m ...