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

相关文章

[leetcode] 515. Find Largest Value in Each Tree Row

Find Largest Value in Each Tree Row 描述 You need to find the largest value in each row of a binary tree. Example: Input: 1/ \3 2/ \ \ 5 3 9 Output: [1, 3, 9] 我的代码 简单的dfs。 要使

OSS报错The difference between the request time and the current time is too large

目录 一、问题描述二、问题原因三、解决方法 一、问题描述 文件上传阿里云 OSS 报错: The difference between the request time and the current time is too large 二、问题原因 请求发起的时间超过 OSS 服务器当前时间 15 分钟,OSS 判定该请求无效,返回报错。 三、解决方法 OSS

515. Find Largest Value in Each Tree Row 在每个树行中找最大值

https://leetcode-cn.com/problems/find-largest-value-in-each-tree-row/description/ 思路: 和637. Average of Levels in Binary Tree(https://www.jianshu.com/p/814d871c5f6d)的思路基本相同.即层遍历二叉树,然后在每层中分别找最大的. vec

Eigen中 Row-Major 和 Column-Major 存储顺序的区别

Eigen中 Row-Major 和 Column-Major 存储顺序的区别 flyfish Eigen::RowMajor 是 Eigen 库中用于指定矩阵存储顺序的一种选项 理解 Row-Major 和 Column-Major 存储顺序的区别,绘制一个单一的图来显示内存中的元素访问顺序,在图中用箭头表示访问顺序. import numpy as npimport matplotl

Minimum Absolute Difference in BST问题及解法

问题描述: Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes. 示例: nput:1\3/2Output:1Explanation:The minimum absolute differenc

el-table表头修改文字或者背景颜色,通过header-row-style设置样式

方式一 <el-table :header-cell-style="{'text-align': 'center'}" /> 方式二 <template><el-table :header-cell-style="tableHeaderColor" /></template><script>export default {methods: {tableHeaderColor ({ro

关于Mysql 中 Row size too large (> 8126) 错误的解决和理解

提示:啰嗦一嘴 ,数据库的任何操作和验证前,一定要记得先备份!!!不会有错; 文章目录 问题发现一、问题导致的可能原因1、页大小2、行格式2.1 compact格式2.2 Redundant格式2.3 Dynamic格式2.4 Compressed格式 3、BLOB和TEXT列 二、解决办法1、修改页大小(不推荐)2、修改行格式3、修改数据类型为BLOB和TEXT列4、其他优化方式(

Codeforces Round #236 (Div. 2) B. Trees in a Row

B. Trees in a Row time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output The Queen of England has n trees growing in a row i

get row object by rowindex

var rows = $('#tt').datagrid('getRows'); // get current page rowsvar row = rows[rowIndex]; // your row data

Compressed Sparse Row (CSR)行压缩存储相关错误概念纠正

之前关于矩阵压缩存储的知识都是网上看的,比如这篇 稀疏矩阵存储格式总结+存储效率对比:COO,CSR,DIA,ELL,HYB 里面对ia数组,即行偏移数组的表述是这样的。强调了ia数组中存的是某行的第一个元素的偏移。 今天测试一个国外数学教授开源的AGMG库时才发现如果按之前的理解,还原后的矩阵将和理论上的无法统一。 翻看维基发现,CSR的ia定义并没有说其存储的是第一个元素的偏移信息