题目链接:http://codeforces.com/gym/100283/problem/K

题解:

要使其相邻两项的差值之和最小,那么越靠中间,其数值越小。

那么剩下的问题就是如何放数字了。一开始的想法是从中间开始放,然后:左右左右……, 后来发现当为偶数个时,这种放法的字典序并非最小,应该右左右左地放。所以从中间向两边扩散的放法需要分奇偶讨论(不太好写)。那有没有其他放法不用分类讨论,且一步过的?有的,就是从两边开始,往中间靠近,即右左右左一直放到中间没有剩余位置。这种放法保证了字典序大的一定被放在后面的位置。

两道与此题思想相近的题:

http://blog.csdn.net/dolfamingo/article/details/54999938

http://blog.csdn.net/dolfamingo/article/details/62887448

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define ms(a, b) memset((a), (b), sizeof(a))
#define LOCAL
#define eps 0.0000001
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int maxn = 100+10;
const int mod = 1000000007; int a[maxn], ans[maxn]; int main()
{
#ifdef LOCAL
freopen("cubes.in", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif // LOCAL int T;
int cnt = 1;
scanf("%d", &T);
while(T--)
{
int n;
scanf("%d", &n); for(int i = 1; i<=n; i++)
scanf("%d", &a[i]);
sort(a+1, a+1+n); int l = 1, r = n, now = n;
while(now)
{
ans[r--] = a[now--]; if(now)
ans[l++] = a[now--];
} printf("Case %d:\n", cnt++);
for(int i = 1; i<=n; i++)
{
printf("%d", ans[i]);
if(i<n)
putchar(' ');
}
printf("\n"); }
return 0;
}

Gym - 100283K K. Cubes Shuffling —— 贪心的更多相关文章

  1. gym/102021/K GCPC18 背包dp算不同数和的可能

    gym/102021/K 题意: 给定n(n<=60)个直线 ,长度<=1000; 可以转化为取 计算 ans = (sum  + 10 - g) / ( n + 1)  在小于5的条件下 ...

  2. GYM 101755 K.Video Reviews 【贪心】+【二分】

    <题目链接> 题目大意: 一家公司想让n个人给他们的产品评论,所以依次去找这n个人,第i个人会评论当且仅当已经有ai个人评论或他确实对这个产品感兴趣,但是这n个人都不对这个产品感兴趣,问这 ...

  3. codeforces Gym 100187J J. Deck Shuffling dfs

    J. Deck Shuffling Time Limit: 2   Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...

  4. 【Gym - 101164I】Cubes(dfs,剪枝)

    BUPT2017 wintertraining(15) #4 A - I.Cubes Gym - 101164I 题意 将n拆成最少个立方数相加的形式. 题解 根据n的范围,立方数最大不超过400的立 ...

  5. Codeforces Gym 100187K K. Perpetuum Mobile 构造

    K. Perpetuum Mobile Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...

  6. Gym 101873K - You Are Fired - [贪心水题]

    题目链接:http://codeforces.com/gym/101873/problem/K 题意: 现在给出 $n(1 \le n \le 1e4)$ 个员工,最多可以裁员 $k$ 人,名字为 $ ...

  7. codeforces gym 100971 K Palindromization 思路

    题目链接:http://codeforces.com/gym/100971/problem/K K. Palindromization time limit per test 2.0 s memory ...

  8. CF gym 101933 K King's Colors —— 二项式反演

    题目:http://codeforces.com/gym/101933/problem/K 其实每个点的颜色只要和父亲不一样即可: 所以至多 i 种颜色就是 \( i * (i-1)^{n-1} \) ...

  9. codeforces 680D D. Bear and Tower of Cubes(dfs+贪心)

    题目链接: D. Bear and Tower of Cubes time limit per test 2 seconds memory limit per test 256 megabytes i ...

随机推荐

  1. BZOJ——1620: [Usaco2008 Nov]Time Management 时间管理

    Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 920  Solved: 569[Submit][Status][Discuss] Description ...

  2. ML | Naive Bayes

    what's xxx In machine learning, naive Bayes classifiers are a family of simple probabilistic classif ...

  3. spring事物,在service层如果进行了异常处理,则不会回滚

    今天进行了事物处理的验证,发现如果在在service层如果进行了异常处理,则不会回滚. 看来异常的处理还是统一放在controller层比较好,service如果是查询方法,出现了异常,就不要做处理了 ...

  4. emacs 打开文件乱码

    emacs在win下写的文件在linux打开乱码 M-x set-language-environment RET   chinese-gb    改变当前编码 用当前编码重新打开 M-x rever ...

  5. vcl.Forms等与VCL界面有关的单元不支持跨平台

    vcl.Forms等与VCL界面有关的单元不支持跨平台 midaslib也不支持

  6. Ubuntu下添加开机启动项的2种方法

    1.方法一,编辑rc.loacl脚本 Ubuntu开机之后会执行/etc/rc.local文件中的脚本,所以我们可以直接在/etc/rc.local中添加启动脚本.当然要添加到语句:exit 0 前面 ...

  7. VirtualApp技术黑产利用研究报告

    一. 前言 VirtualApp(以下称VA)是一个App虚拟化引擎(简称VA).VirtualApp创建了一个虚拟空间,你可以在虚拟空间内任意的安装.启动和卸载APK,这一切都与外部隔离,如同一个沙 ...

  8. 树莓派 Zero作为飞控图传

    前言 原创文章,转载引用务必注明链接,水平有限,如有疏漏,欢迎指正. 本文使用Markdown写成,为获得更好的阅读体验和正常的链接.图片显示,请访问我的博客原文: http://www.cnblog ...

  9. enter键触发的函数

    enter键触发的函数示例: <input type="text" onkeydown="fun();"> function fun() { if( ...

  10. Hdu-1565 方格取数(1) (状态压缩dp入门题

    方格取数(1) Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...