题目链接: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. [AI开发]深度学习如何选择GPU?

    机器推理在深度学习的影响下,准确性越来越高.速度越来越快.深度学习对人工智能行业发展的贡献巨大,这得益于现阶段硬件计算能力的提升.互联网海量训练数据的出现.本篇文章主要介绍深度学习过程中如何选择合适的 ...

  2. 335. Self Crossing

    /* * 335. Self Crossing * 2016-7-10 by Mingyang */ // Categorize the self-crossing scenarios, there ...

  3. Ubuntu 16.04下使用Wine安装正则表达式工具RegexBuddy 4

    说明: 1.使用的Wine版本是深度出品(Deepin),已经精简了很多没用的配置,使启动能非常快,占用资源小. 2.关于没有.wine文件夹的解决方法:在命令行上运行winecfg: 下载: (链接 ...

  4. zip 压缩文件夹

    import java.io.*; import java.util.zip.*; /** * @author Dana·Li * <p> * 程序实现了ZIP压缩[compression ...

  5. Spring HTTP Service

    基于Spring MVC, 使用Http Service Invoke远程调用方法 (参考: http://blog.csdn.net/hanqunfeng/article/details/43031 ...

  6. 编写自己的UDTF

    1. UDTF介绍 UDTF(User-Defined Table-Generating Functions) 用来解决 输入一行输出多行(On-to-many maping) 的需求. 2. 编写自 ...

  7. windows平台是上的sublime编辑远程linux平台上的文件

    sublime是个跨平台的强大的代码编辑工具,不多说. 想使用sublime完毕linux平台下django网站的代码编辑工作以提高效率(原来使用linux下的vim效率较低,适合编辑一些小脚本). ...

  8. hdu 1081 &amp; poj 1050 To The Max(最大和的子矩阵)

    转载请注明出处:http://blog.csdn.net/u012860063 Description Given a two-dimensional array of positive and ne ...

  9. C# 知识点随手学习网站推荐

    http://www.studyofnet.com/news/list-8881.2-1-1.html

  10. Mataplotlib绘图和可视化

    Mataplotlib是一个强大的python绘图和数据可视化工具包 安装方法:pip install matplotlib 引用方法:import matplotlib.pyplot as plt ...