CodeForces 622D Optimal Number Permutation
是一个简单构造题。
请观察公式:

绝对值里面的就是 |di-(n-i)|,即di与(n-i)的差值的绝对值。
事实上,对于任何n,我们都可以构造出来每一个i的di与(n-i)的差值为0。
换句话说,就是这个最小值一定可以构造出来是0。
假设输入是6:
那么可以这样构造:1 3 5 5 3 1 2 4 6 4 2 6
假设输入是7:
那么可以这样构造:1 3 5 7 5 3 1 2 4 6 6 4 2 7
从上面就能看出怎么构造了,n最后放空缺的两个位置,剩下的从小到大一个一个放,奇数放左边构造,偶数放右边构造。
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <queue>
#include <stack>
#include <map>
#include <vector>
using namespace std; const int maxn = * + ;
int n;
int ans[maxn]; int main()
{
scanf("%d", &n);
memset(ans, , sizeof ans);
int left = , right = n + ;
for (int i = ; i <= n - ; i++)
{
if (i % == )
{
ans[left] = i;
ans[left + n - i] = i;
left++;
}
else
{
ans[right] = i;
ans[right + n - i] = i;
right++;
}
}
for (int i = ; i <=* n; i++) if (!ans[i]) ans[i] = n;
for (int i = ; i <= *n; i++) printf("%d ", ans[i]);
printf("\n");
return ;
}
CodeForces 622D Optimal Number Permutation的更多相关文章
- codeforces 622C. Optimal Number Permutation 构造
题目链接 假设始终可以找到一种状态使得值为0, 那么两个1之间需要隔n-2个数, 两个2之间需要隔n-3个数, 两个3之间隔n-4个数. 我们发现两个三可以放到两个1之间, 同理两个5放到两个3之间. ...
- codeforces 622D D. Optimal Number Permutation(找规律)
D. Optimal Number Permutation time limit per test 1 second memory limit per test 256 megabytes input ...
- Educational Codeforces Round 7 D. Optimal Number Permutation 构造题
D. Optimal Number Permutation 题目连接: http://www.codeforces.com/contest/622/problem/D Description You ...
- Codeforces 55D Beautiful Number
Codeforces 55D Beautiful Number a positive integer number is beautiful if and only if it is divisibl ...
- [Codeforces 1214A]Optimal Currency Exchange(贪心)
[Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的 ...
- Codeforces 691D Swaps in Permutation
Time Limit:5000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submit Status Prac ...
- [Codeforces 864D]Make a Permutation!
Description Ivan has an array consisting of n elements. Each of the elements is an integer from 1 to ...
- Codeforces 40 E. Number Table
题目链接:http://codeforces.com/problemset/problem/40/E 妙啊... 因为已经确定的格子数目严格小于了$max(n,m)$,所以至少有一行或者一列是空着的, ...
- Codeforces 124A - The number of positions
题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't ...
随机推荐
- javaScript 新学习:Array.contains 函数
Array.contains 函数 确定指定对象是否是 Array 对象中的元素. 此函数是静态的,可在不创建对象实例的情况下调用. var itemExists = Array.contains(a ...
- 【转】dmidecode命令详解
Dmidecode 这款软件允许你在 Linux 系统下获取有关硬件方面的信息.Dmidecode 遵循 SMBIOS/DMI 标准,其输出的信息包括 BIOS.系统.主板.处理器.内存.缓存等等. ...
- Webdriver中实现区域截图的方式以及如何截取frame中的图片
import java.awt.Rectangle;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOE ...
- jquery 中 $('div','li')
要搞清楚$('div','li') 和 $('div , li') 和 $('div li') 区别$('div','li')是$(子,父),是从父节点里找子,而不是找li外面的div $('div ...
- gameUnity 0.15alpha 网络游戏框架
在重要版本 0.2之前,先 出一个 0.15alpha 版本热热身. 0.15主要是添加了 动画事件 和一些 动画特效的类,比如快进,慢进,(类似被冰冻效果),但这些都不在这个 alpha版本中出现 ...
- HDU1548:A strange lift
A strange lift Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tota ...
- NGUI 添加回调函数
//缓动完成 TweenPosition tweenPos=GetComponent<TweenPosition>(); tweenPos.AddOnFinished(complete); ...
- thinkPHP17---操作绑定到类
首先要配置: "ACTION_BIND_CLASS"=>"TRUE"; 控制器类的定义如下: namespace Home\Controller\Inde ...
- Linux -- ls只显示目录
ls没有直接显示目录的选项, 不过根据目录和文件显示的差异,可以搭配grep来实现 方法1: ll | grep "^d" 方法2: ls -F | grep$ "/$& ...
- CodeForces 190D Non-Secret Cypher
双指针. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...