codefroces Round #201.a--Difference Row
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Description
You want to arrange n integers a1, a2, ..., an in some order in a row. Let's define the value of an arrangement as the sum of differences between all pairs of adjacent integers.
More formally, let's denote some arrangement as a sequence of integers x1, x2, ..., xn, where sequence x is a permutation of sequence a. The value of such an arrangement is (x1 - x2) + (x2 - x3) + ... + (xn - 1 - xn).
Find the largest possible value of an arrangement. Then, output the lexicographically smallest sequence x that corresponds to an arrangement of the largest possible value.
Input
The first line of the input contains integer n (2 ≤ n ≤ 100). The second line contains n space-separated integers a1, a2, ..., an (|ai| ≤ 1000).
Output
Print the required sequence x1, x2, ..., xn. Sequence x should be the lexicographically smallest permutation of a that corresponds to an arrangement of the largest possible value.
Sample Input
Input
5
100 -100 50 0 -50
Output
100 -50 0 50 -100
Hint
In the sample test case, the value of the output arrangement is (100 - ( - 50)) + (( - 50) - 0) + (0 - 50) + (50 - ( - 100)) = 200. No other arrangement has a larger value, and among all arrangements with the value of 200, the output arrangement is the lexicographically smallest one.
Sequence x1, x2, ... , xp is lexicographically smaller than sequence y1, y2, ... , yp if there exists an integer r(0 ≤ r < p) such that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1.
题意:
给定一个序列,求使得(x1 - x2) + (x2 - x3) + ... + (xn - 1 - xn)最大且字典序最小的排列
化简式子,得到:x1-xn 即求出x1-xn最大的即可
然后按字典序最小的输出
代码:
#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
int a[130];
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
}
int x,y,res=-INF;
int xl,yl;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(a[i]-a[j]>res){
x=a[i],y=a[j];
xl=i,yl=j;
res=a[i]-a[j];
}
if(a[j]-a[i]>res){
y=a[i],x=a[j];
xl=j,yl=i;
res=a[j]-a[i];
}
}
}
a[xl]=-INF,a[yl]=-INF;
sort(a,a+n);
cout<<x<<" ";
for(int i=0;i<n;i++){
if(a[i]!=-INF){
cout<<a[i]<<" ";
}
}
cout<<y<<endl;
}
codefroces Round #201.a--Difference Row的更多相关文章
- codefroces Round #201.B--Fixed Points
B. Fixed Points time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- A. Difference Row
A. Difference Row time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- codeforces round #201 Div2 A. Difference Row
#include <iostream> #include <vector> #include <algorithm> using namespace std; in ...
- Codeforces Round #201 (Div. 2)C,E
数论: C. Alice and Bob time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codeforces Round #201.C-Alice and Bob
C. Alice and Bob time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- codeforces A. Difference Row
link:http://codeforces.com/contest/347/problem/A 开始看起来很复杂的样子,但是刚写下样例,就发现因为中间的都消去了,其实起作用的就是最大值和最小值=_= ...
- codeforces A. Difference Row 解题报告
题目链接:http://codeforces.com/problemset/problem/347/A 题目意思:给出一个序列 a1, a2, ..., an , 通过重排序列,假设变成 x1, x2 ...
- codeforce Codeforces Round #201 (Div. 2)
cf 上的一道好题: 首先发现能生成所有数字-N 判断奇偶 就行了,但想不出来,如何生成所有数字,解题报告 说是 所有数字的中最大的那个数/所有数字的最小公倍数,好像有道理:纪念纪念: #incl ...
- Codeforces Round #201 (Div. 2) - C. Alice and Bob
题目链接:http://codeforces.com/contest/347/problem/C 题意是给你一个数n,然后n个数,这些数互不相同.每次可以取两个数x和y,然后可以得到|x - y|这个 ...
随机推荐
- powerdesigner去掉网格线
powerdesigner去掉网格线 去掉网格线
- [LGP5115] Check,Check,Check one two!
神奇的思路,还是要学习一个. 题意:给你一个字符串,并定义两个前缀的lcs.两个后缀的lcp,求式子膜\(2^{64}\)的值. \[ \sum_{1\le i<j\le n} lcp(i,j) ...
- windows terminal编译实录
直接甩个大佬链接吧 https://www.bilibili.com/video/av52032233?t=835 安装过程中如果出问题了,靠搜索引擎解决下,微软或者vs的问题可以用biying搜索 ...
- python基础之 线程_进程关系
上图
- java 给定一个日期期间 返回形如Mar 2015 3/20-3/31的数据
最近一个项目中有个前台对于表头要求: 给定一个日期期间返回形如 Mar 2015 3/20-3/31Apr 2015 4/1-4/30 这样的月年数据,简单的写了下代码,暂时没想到更好的办法 例如传进 ...
- wex5 页面跳转
页面交互: 3种方法: 1.使用Shell提供的方法 打开另一个页面不需要等待页面返回 功能树上打开 2. 用windowDialog组件 需要等待页面返回 3.内嵌页 windowContainer ...
- CentOS下安装DockerCE
title: CentOS下安装DockerCE comments: false date: 2019-09-04 09:47:58 description: 在CentOS下安装社区版Docker ...
- API接口之安全篇
APP.前后端分离项目都采用API接口形式与服务器进行数据通信,传输的数据被偷窥.被抓包.被伪造时有发生,那么如何设计一套比较安全的API接口方案呢? 一般的解决方案如下: 1.Token授权认证,防 ...
- python的加密方式
MD5加密 这是一种使用非常广泛的加密方式,不可逆的,在日常字符串加密中经常会用到,下面我简单介绍一下这种方式,主要用到Python自带的模块hashlib,测试代码如下,先创建一个md5对象,然后直 ...
- JS异步上传文件
直接调用Upload(option)方法,即可上传文件,不需要额外的插件辅助,采用原生js编写. /* *异步上传文件 *option参数 **url:上传路径 **data:上传的其他数据{id:& ...