A - Difference Row

2023-12-28 08:32
文章标签 row difference

本文主要是介绍A - Difference Row,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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 sequencea. 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 a1a2...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.

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.

题意描述:

给你n个数,然后求字典序最小,排完序之后,求两两相邻元素差和最大的序列。

解题思路:

根据所给的公式可知,中间元素被抵消,所以只需a1-an最大即可,所以先整体从小到大排列,然后将最后一个与第一个元素互换位置。

解题细节:

运用sort排序,先输出a[n-1],最后输出a[0].

代码:

#include <iostream>
#include <algorithm>
using namespace std;
int main()
{int a[101];int n;while(cin>>n){int i;for(int i=0;i<n;i++)cin>>a[i];sort(a,a+n);cout<<a[n-1]<<' ';for(i=1;i<n-1;i++){cout<<a[i]<<' ';}cout<<a[0]<<endl;}return 0;
}
思路:

做题数量还不够,一开始未想到用这种办法,多做题,积累经验。

这篇关于A - Difference Row的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/545431

相关文章

MySQL 中 ROW_NUMBER() 函数最佳实践

《MySQL中ROW_NUMBER()函数最佳实践》MySQL中ROW_NUMBER()函数,作为窗口函数为每行分配唯一连续序号,区别于RANK()和DENSE_RANK(),特别适合分页、去重... 目录mysql 中 ROW_NUMBER() 函数详解一、基础语法二、核心特点三、典型应用场景1. 数据分

如何解决mysql出现Incorrect string value for column ‘表项‘ at row 1错误问题

《如何解决mysql出现Incorrectstringvalueforcolumn‘表项‘atrow1错误问题》:本文主要介绍如何解决mysql出现Incorrectstringv... 目录mysql出现Incorrect string value for column ‘表项‘ at row 1错误报错

INDEX+SMALL+IF+ROW函数组合使用解…

很多人在Excel中用函数公式做查询的时候,都必然会遇到的一个大问题,那就是一对多的查找/查询公式应该怎么写?大多数人都是从VLOOKUP、INDEX+MATCH中入门的,纵然你把全部的多条件查找方法都学会了而且运用娴熟,如VLOOKUP和&、SUMPRODUCT、LOOKUP(1,0/....,但仍然只能对这种一对多的查询望洋兴叹。   这里讲的INDEX+SMALL+IF+ROW的函数组合,

SQLSERVER排名函数RANK,DENSE_RANK,NTILE,ROW_NUMBER

SQL SERVER排名函数RANK,DENSE_RANK,NTILE,ROW_NUMBER 前言 本文意于用实例数据帮助理解SQL SERVER排名函数RANK,DENSE_RANK,NTILE,ROW_NUMBER。 准备工作 创建测试表:   ? 1 2 3 4 5 create table test( id int identity(1,1)

ValueError: could not convert string ‘date‘ to float64 at row 0, column 1.

ValueError:无法将字符串“date”转换为第0行第1列的float64。 解决办法: 通过以下代码检查自己的数据,查找问题,逐一改正。 import csvdef find_blank_or_invalid_cells(file_path):blank_cells = []invalid_cells = []with open(file_path, 'r', newline=

DDR的Controller、Channel、Chip、Rank、Bank、Row、Column、Sided、Dimm

目录 概览 1.概览             先从半导体生产开始,生产出来还没切割的叫晶圆(wafer)。切割出来还没封装的叫裸die(bare die)。封装好的叫颗粒(component)。做成内存条后叫模组(module)。下文我们也会按这样的称呼去区分。 2.Controller(内存控制器)         一开始内存控制器在主板上有独立的芯片;在英特尔微处理器

如何得到自定义UITableViewCell中的按钮所在的cell的indexPath.row

在创建button的时候可以把indexpath的值给button.tag。点击的时候响应sender.tag. 在自定义UITableViewCell中创建了一个按钮。 想在点击该按钮时知道该按钮所在的cell在TableView中的行数。就是cell的 indexPath.row 两种方法都很好。 -(IBAction):(id)sender {

Ajax, SPA, Client-Side VS Server-Side code difference

What is Ajax: http://www.seguetech.com/what-is-ajax-and-where-is-it-used-in-technology/ Single Page Application:  http://www.seguetech.com/what-is-a-single-page-application/ Client-Side vs. Server

Flutter 小技巧之 Row/Column 即将支持 Flex.spacing

事实上这是一个相当久远的话题,如果对于前因后果不管兴趣,直接看最后就行。 这个需求最早提及应该是 2018 年初在 #16957 被人提起,因为在 Flutter 上 Wrap 有 runSpacing 和 spacing 用于配置垂直和水平间距,而为什么 Colum 和 Row 这样更通用的控件居然没有 spacing 支持? 而后在 2020 年,Flutter 在 #55378 用户希望

【python因果推断库2】使用 PyMC 模型进行差分-in-差分(Difference in Differences, DID)分析

目录  使用 PyMC 模型进行差分-in-差分(Difference in Differences, DID)分析 导入数据 分析 使用 PyMC 模型建模银行业数据集 导入数据  分析 1 - 经典 2×2 差分-in-差分 (DiD) 分析 2 - 具有多个干预前后观测值的差分-in-差分 (DiD) 分析   使用 PyMC 模型进行差分-in-差分(Differe