题目描述

http://codeforces.com/contest/1339/problem/B

有一个长度为 \(n(3\le n \le 10^5)\) 的整数序列 \(a_1,a_2,...,a_n(-10^9\le a_i\le 10^9)\) 。

将序列重排序使得 \(|a_1-a_2|\le|a_2-a_3|\le...\le|a_{n-1}-a_n|\) 。

输出任意一种满足上述条件的排序方式。

解题

这里采用一种类似贪心的策略:

  • 序列 \(a\) 中的最大值与最小值差的绝对值(\(|a_{max}-a_{min}|\))一定是序列中最大的;
  • 最大值和次小值的差的绝对值一定是第二大的;
  • 次大值和次小值的差的绝对值一定是第三大的;
  • 以此类推…

我们先将数组按升序排序,使得 \(a_1\le a_2\le...\le a_n\) 。

然后按照 \(a_{(n-k+1)},a_k,...,a_{n-1},a_2,a_n,a_1\) 顺序输出,即为所求。

#include<bits/stdc++.h>
#define ll long long #define fr(i,n) for(int i=0;i<n;i++)
#define frs(i,n,flag) for(int i=0;i<n&&flag;i++) #define frr(i,j,n) for(int i=j;i<n;i++)
#define r_frr(i,j,n) for(int i=n-1;i>=j;i--) #define frrs(i,j,n,flag) for(int i=j;i<n&&flag;i++)
#define r_frrs(i,j,n,flag) for(int i=n-1;i>=j&&flag;i--) #define arend(i,n) ((i!=n-1)?" ":"\n")
#define memset0(dp) memset(dp,0,sizeof(dp))
#define print_arr(begin,end) for(auto it = begin;it!=end;it++) cout<<*it<<arend(it,end);
#define log_this(name,value) cout<<name<<": "<<value<<endl;
#define e4 10004
#define e5 100005
#define e6 1000006
#define e7 10000007
#define e9 1000000000
#define INF 9999999
using namespace std;
int to_int(string s) {stringstream ss;ss<<s;int a;ss>>a;return a;}
string to_str(double a) {stringstream ss;ss<<a;return ss.str();} ll a[1*e5];
ll ans[1*e5]; int main(){ cin.tie(0);
//ios::sync_with_stdio(false);
//cout<<setiosflags(ios::fixed)<<setprecision(0); //freopen("1.out","w",stdout);
int t;
while(cin>>t){
while(t--){
int n;
cin>>n;
fr(i,n){
cin>>a[i];
}
sort(a,a+n);
int tail = 0;
int b = 0,e = n-1;
while(b<=e){
ans[tail++] = a[b];
if(b!=e) ans[tail++] = a[e];
b++,e--;
}
r_frr(i,0,n){
cout<<ans[i]<<" ";
}
cout<<endl;
}
} return 0;
}

CF633(div.2)B. Sorted Adjacent Differences的更多相关文章

  1. Sorted Adjacent Differences(CodeForces - 1339B)【思维+贪心】

    B - Sorted Adjacent Differences(CodeForces - 1339B) 题目链接 算法 思维+贪心 时间复杂度O(nlogn) 1.这道题的题意主要就是让你对一个数组进 ...

  2. B. Sorted Adjacent Differences(思维构造)

    \(给出n个数字,要求构造一个由这n个数组成的序列,使得|a_1-a_2|<=|a_2-a_3|...<=|a_{n-1}-a_n|\) \(排序后,从数列中间取个数,然后从左右分别循环取 ...

  3. Educational Codeforces Round 37 (Rated for Div. 2)C. Swap Adjacent Elements (思维,前缀和)

    Educational Codeforces Round 37 (Rated for Div. 2)C. Swap Adjacent Elements time limit per test 1 se ...

  4. CF633(div.2)A. Filling Diamonds

    题目描述 http://codeforces.com/contest/1339/problem/A 给定一个 \(n(1\le n \le 10^9)\) ,问用一个由两个三角形组成的菱形,填充下面这 ...

  5. CF633(div.2)C. Powered Addition

    题目描述 http://codeforces.com/contest/1339/problem/C 给定一个长度为 \(n\) 的无序数组,你可以在第 \(x\) 秒进行一次下面的操作. 从数组选取任 ...

  6. Codeforces Round #719 (Div. 3) C. Not Adjacent Matrix

    地址 Problem - C - Codeforces 题意 每个格子,该格子和相邻的格子的值不能相同 题解 思维题, 先从1~n输出奇数,再输出偶数 代码 #include <iostream ...

  7. Codeforces Round #633 (Div. 2)

    Codeforces Round #633(Div.2) \(A.Filling\ Diamonds\) 答案就是构成的六边形数量+1 //#pragma GCC optimize("O3& ...

  8. Codeforces Round #633 div2 A~C

    A. Filling Diamonds 题意:给你n个菱形方块,问能构成图示形状的有多少种 题解:自己画几个不难发现答案是n 代码: 1 #include <iostream> 2 #in ...

  9. woj1019 Curriculum Schedule 输入输出 woj1020 Adjacent Difference 排序

    title: woj1019 Curriculum Schedule 输入输出 date: 2020-03-19 10:43:00 categories: acm tags: [acm,woj] 水题 ...

随机推荐

  1. 树莓派上搭建唤醒词检测引擎 Snowboy

    Snowboy 是一款高度可定制的唤醒词检测引擎,可以用于实时嵌入式系统,并且始终监听(即使离线).当前,它可以运行在 Raspberry Pi.(Ubuntu)Linux 和 Mac OS X 系统 ...

  2. Java反射之Class类

    接下来的几章,我们谈一谈java的反射机制. 反射就是从一个java类中映射出一个java类或是一个实例.通常在很多框架中都用到反射,比如常用的ssm框架,在配置文件中总是会写到类的全名,框架通过读取 ...

  3. python基础学习day02

    pycharm的安装以及简单使用 辅助开发软件,代码逐行调试,设计高端 python的种类: ​ CPython:官方推荐可以转换成c能够识别的字节码. ​ JPython:可以转化为Java语言能够 ...

  4. 按 ctrl 时会触发 shift ,导致不能正常使用 ctrl + c 复制

    问题描述 不能使用 ctrl 功能键组合,原因是按 ctrl 时 shift 也会被触发. win 10 电脑为例,可以打开虚拟键盘查看按键情况. 当我按下 ctr时 可见 shift 也被触发了. ...

  5. hdu(杭电oj)输入输出练习题目总结

    1000.1001 .1089.1090.1091.1092.1093.1094.1095.1096

  6. Spring Boot(十三):整合Redis哨兵,集群模式实践

    前面的两篇文章(Redis的持久化方案, 一文掌握Redis的三种集群方案)分别介绍了Redis的持久化与集群方案 -- 包括主从复制模式.哨兵模式.Cluster模式,其中主从复制模式由于不能自动做 ...

  7. 国际惯例,Hello World。

    c语言: #include<stdio.h> int main() { printf("Hello World!\n"); ; } C++: #include<i ...

  8. 子序列宽度求和 Sum of Subsequence Widths

    2019-10-14 17:00:10 问题描述: 问题求解: 如果暴力求解,时间复杂度是exponational的,因为这里是子序列而不是子数组.显然,直接枚举子序列是不太现实的了,那么可以怎么做呢 ...

  9. 什么是CPU load

    最近经常收到告警,CPU load大于阈值告警.查看系统的CPU是12核,告警阈值设置的是8.对于CPU load一直有个模糊的概念,具体是什么意思还真搞不明白,趁这个机会好好搞搞究竟. 1.查看CP ...

  10. OpenCV-Python 直方图-3:二维直方图 | 二十八

    目标 在本章中,我们将学习查找和绘制2D直方图.这将在以后的章节中有所帮助. 介绍 在第一篇文章中,我们计算并绘制了一维直方图. 之所以称为一维,是因为我们仅考虑一个特征,即像素的灰度强度值. 但是在 ...