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

5100 -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;
}
时间: 2024-08-05 19:30:07

codefroces Round #201.a--Difference Row的相关文章

Codeforces Round #201 (Div. 1) / 346A Alice and Bob

#include <cstdio> #include <algorithm> using namespace std; int gcd(int a,int b) { return b?gcd(b,a%b):a; } int n[100+10]; int main() { int t,maxn = 0; scanf("%d",&t); for(int i=0; i<t; i++) { scanf("%d",&n[i]);

CodeForces 347A Difference Row (水题)

题意:给定 n 个数,让你找出一个排列满足每个数相邻作差之和最大,并且要求字典序最小. 析:这个表达式很简单,就是把重新组合一下,就成了x1-xn,那么很简单,x1是最大的,xn是最小的,中间排序就好. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <algorithm> #include <cstring> using namespace std;

Codeforces Round#201(div1) D. Lucky Common Subsequence

题意:给定两个串,求出两个串的最长公共子序列,要求该公共子序列不包含virus串. 用dp+kmp实现 dp[i][j][k]表示以i结尾的字符串和以j结尾的字符串的公共子序列的长度(其中k表示该公共子序列的与virus的匹配程度)很显然,当k==strlen(virus)时,该公共子序列不是我们所求得 当添加一个字符时,如果失配,这时不能让k直接等于0,而是要用kmp给k一个合理的值. 1 #include <stdio.h> 2 #include <string.h> 3 co

Codeforces Round #201 (Div. 2) C 数论

Problem: 给你n(n<=100)个正数,每次从中取任意两个数,他们的差值如果在集合中没有就加入集合中,问最后集合中元素个数的奇偶性? Analyse: 首先考虑只有两个数的情况,发现两个数x,y(x>y),可以观察到连续执行操作,可以生成的数的集合是 {k?gcd(x,y) | k=1,2,..且 k?gcd(x,y)<=y} 然后考虑有第三个数Z的情况,因为前面的两个数,极其生成的所有数的最大公约数都是gcd(x, y), 所以加入第三个数之后这个集合的新的最大公约数是gcd(

【iScroll源码学习01】准备阶段 - 叶小钗

[iScroll源码学习01]准备阶段 - 叶小钗 时间 2013-12-29 18:41:00 博客园-原创精华区 原文  http://www.cnblogs.com/yexiaochai/p/3496369.html 主题 iScroll HTML JavaScript ① viewport相关知识点(device-width等) ②  CSS3硬件加速 ③ 如何暂停CSS动画 ④ e.preventDefault导致文本不能获取焦点解决方案 ...... 当然,我们写的demo自然不能和

自定义表头Datagrid

自定义的一个表头 1 <bp:BasePage x:Class="NetReform.Pages.RealProbabiTableCompare" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:mc="http:/

js实现2048小游戏

这是学完javascript基础,编写的入门级web小游戏 游戏规则:在玩法规则也非常的简单,一开始方格内会出现2或者4等这两个小数字,玩家只需要上下左右其中一个方向来移动出现的数字,所有的数字就会想滑动的方向靠拢,而滑出的空白方块就会随机出现一个数字,相同的数字相撞时会叠加靠拢,然后一直这样,不断的叠加最终拼凑出2048这个数字就算成功.但是我们的程序没有终点. 一.HTML部分 1 <body> 2 <!-- 分数行 --> 3 <p id="scorePane

Oracle 12c 的新功能:模式匹配查询

模式匹配SQL 在一系列的数据行中辨识出某种模式是一种广泛需求的功能,但在以前的SQL中是不可能的.有许多解决方法,但这些都很难写,很难理解,而且执行效率低下.在Oracle数据库中,从12c开始,你可以使用原生SQL中执行效率很高的 MATCH_RECOGNIZE 子句来实现这种功能.本章讨论如何做到这一点,并包括以下几个部分: .模式匹配的概述.模式匹配中的基本主题.模式匹配详细信息.模式匹配中的高级主题.模式匹配中的规则与限制.模式匹配中的例子 模式匹配的概述 SQL中的模式匹配是用MAT

051 题库

序号 OCP 051题库 1 :  OCP-1Z0-051 第1题 CTAS语句建表注意事项 2 :  OCP-1Z0-051 第2题 视图的WITH CHECK OPTION选项 3 :  OCP-1Z0-051 第3题 转义字符 4 :  OCP-1Z0-051 第4题 单行函数 5 :  OCP-1Z0-051 第5题 to_char的数字的格式 6 :  OCP-1Z0-051 第6题 case when和decode的用法 7 :  OCP-1Z0-051 第7题 ON和USING子句