Topcoder SRM 662 DIV1
2015-09-24 12:13
483 查看
FoxesOfTheRoundTable
题意:
给你一个序列,让你生成一个排列,并且这个排列是循环的。使得任意两个相邻数的差的最大值尽量小。题解:
先排个序,然后从小到大逐个插入。这时容易发现,若要得到最优的策略,只能将现在的这个数插在当前排列中最大的那个数的旁边,这是因为,要插入的这个数是大于当前排列中的任何一个数,故而插入只会产生一个更大的差距,为了使这个差距尽量小,就不得不在最大的数旁边插入。假如现在的序列就是0 1 2 3 4 5
首先前三个数是没有影响的:0 1 2
插入3:0 1 3 2
插入4:0 1 3 4 2
插入5:0 1 3 5 4 2
根据前面的推论,我们知道我们总是将当前的数插在整个序列的中间位置,因为上个数也是在中间,而且是奇偶交替的。故而可先生成这个置换,然后将原序列排序后,进行置换,然后返回结果。
代码:
#include<iostream> #include<vector> #include<cstring> #include<algorithm> #define MAX_N 55 using namespace std; int n; vector<int> res; struct node{ public: int val,pos; }; bool cmp(node a,node b){ return a.val<b.val; } node x[MAX_N]; class FoxesOfTheRoundTable { public: vector<int> minimalDifference(vector<int> h) { n = h.size(); res.push_back(0); if (n == 1)return res; res.push_back(1); if (n == 2)return res; res.push_back(2); if (n == 3)return res; res.clear(); for (int i = 0; i < n; i++)x[i].val = h[i], x[i].pos = i; sort(x, x + n, cmp); res.push_back(x[0].pos); for (int i = 1; i < n; i += 2)res.push_back(x[i].pos); for (int i = ((n - 1) >> 1) << 1; i > 0; i -= 2)res.push_back(x[i].pos); return res; } };
View Code
相关文章推荐
- Jcrop使用心得
- dockerfile 多端口映射 expose
- LoadRunner监控Tomcat
- 简单讲解Linux的文件系统及相关的挂载命令
- ubuntu添加用户未指定shell,ll别名等无法是使用
- Openstack节点网卡连Cisco交换机出现环路的处理
- 学习日志---初次接触mapreduce
- linux 测试端口是否可通
- openwrt默认不开启wifi
- 配置apache的工作模式
- ECshop彻底去版权(同时适用于2.7.3)
- 解决myeclipse或者tomcat内存不足
- 配置nginx worker 进程数
- 上午所学(jvm内存分配和shell排序算法的深入理解)
- Keep-Alive简介及在Tomcat中配置
- centos6.5安装php
- Opencv 源码解读之 ImageEncoder
- CentOS安装中文支持
- MapReduce 规划 系列的12 使用Hadoop Streaming技术集成newLISP文字
- nginx配置二级域名访问集群tomcat和nginx windows版安装 启动