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|这个 ...
随机推荐
- Vue 中 $attrs 的使用
名词解释: $attrs--继承所有的父组件属性(除了prop传递的属性.class 和 style ) inheritAttrs:默认值true,继承所有的父组件属性(除props的特定绑定)作为普 ...
- C++多线程基础学习笔记(六)
condition_variable.wait.notifiy_one.notify_all的使用方式 condition_variable:条件变量 wait:等待被唤醒 notify_one:随机 ...
- C++学习 之 指针及动态内存分配(笔记)
1. 指针 1.1 指针的含义: 简单来说,指针是存储内存地址的变量.当我们声明了一个指针变量后,使用该指针时系统会根据指针内的地址索引内存块,读取内存内的值.指针因为是用来存地址的,所以一般固定长度 ...
- Luogu P5068 [Ynoi2015]我回来了
题目 Ynoi难得的水题. 首先我们可以\(O(n^2)\)地求出任意两点之间的距离. 然后我们可以\(O(n^3)\)地求出对于任意一个点\(u\),跟它距离\(\le d\)的点的集合. 然后对于 ...
- python网络爬虫(3)python爬虫遇到的各种问题(python版本、进程等)
import urllib2 源地址 在python3.3里面,用urllib.request代替urllib2 import urllib.request as urllib2 import coo ...
- SQL----Scalar 函数
UCASE() 函数 UCASE 函数把字段的值转换为大写. SQL UCASE() 语法 SELECT UCASE(column_name) FROM table_name SQL UCASE() ...
- 定制ListView的界面(让列表中不仅有文字还有图片fruitImage.setImageResource(fruit.getImageId());)
1.定义自己的实体类来作为ListView的适配类型如: public class Fruit { private String name;//水果的名字 private int imageId;// ...
- 关于redis的几件小事(七)redis缓存雪崩与穿透
1.缓存雪崩 (1)什么是缓存雪崩 缓存雪崩指的是在同一时刻,缓存大量失效,导致大量的请求直接到了数据库,数据库压力剧增,引起系统崩溃.可能出现的情况有: ①大量的key设置了相同的过期时间,导致在缓 ...
- N4_75条语法
1. コ/ソ/ア/ド体系 -(こ.そ.あ.ど)れ/-(こ.そ.あ.ど)の A:-(こ.そ.あ.ど)れ 接续: 指示代词和场所代词,分近称.中称.远称.疑问称. 意思: 这个,那个,那个,哪个 例:これ ...
- pt-archiver配置自动归档
Mysql的数据归档通常使用percona的pt-archiver.通过shell脚本加crontab可以应对大多数场景下的数据自动归档. 安装 Percona Toolkit的安装不再赘述,请自行搜 ...