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 ...
随机推荐
- boost库之geometry
环境:win732位旗舰版.VS2010旗舰版.boost 1.55.0版本.坐标系为MM_TEXT Geometry是一个开源的几何计算库,包含了几何图形最基本的操作(也支持复杂的操作),下面我们看 ...
- sqlserver2008行锁
select * from tablename WITH (UPDLOCK) where Id=#value#
- 使用ADO对象添加、修改、删除数据
使用ADO对象对数据库中的数据进行添加.修改和删除等操作.首先创建一个ADO类,通过ADO类连接数据库,并打开记录集.例如,使用ADO对象添加.修改.删除数据,程序设计步骤如下:(1)创建一个基于对话 ...
- AndroidStudio使用注意事项
今天在引入GitHUb上的开源框架时,写好依赖后编译时,报以下错误: Error:Execution failed for task ':app:processDebugResources'.> ...
- Servlet程序开发-Helloworld
D:\Workspace\WEB-INF\classes下新建HelloServlet.java文件: package org.lxh.servletdemo ; import java.io.* ; ...
- 转 图片缓存之内存缓存技术LruCache,软引用
每当碰到一些大图片的时候,我们如果不对图片进行处理就会报OOM异常,这个问题曾经让我觉得很烦恼,后来终于得到了解决,那么现在就让我和大家一起分享一下吧.这篇博文要讲的图片缓存机制,我接触到的有两钟,一 ...
- js中将 整数转成字符,,将unicode 编码后的字符还原出来的方法。
一.将整数转成字符: String.fromCharCode(17496>>8,17496&0xFF,19504>>8,19504&0xFF,12848> ...
- centos 7.1搭建docker本地私有仓库返回500错误
之前有一篇写到在ubuntu14.04系统上安装私有仓库,遇到了两个问题,本次在centos7上遇到了另外一个问题. 安装完仓库并运行registry镜像之后发现push和pull操作都会返回一个50 ...
- UISegmentedControl 踩坑
@interface JLMyContactsViewController () @property (nonatomic, strong) UIImageView *navImageView; ...
- android 1.6 launcher研究之自定义ViewGroup (转 2011.06.03(二)——— android 1.6 launcher研究之自定义ViewGroup )
2011.06.03(2)——— android 1.6 launcher研究之自定义ViewGroup2011.06.03(2)——— android 1.6 launcher研究之自定义ViewG ...