当前位置:网站首页>Codeforces Round #633 (Div. 2) B. Sorted Adjacent Differences
Codeforces Round #633 (Div. 2) B. Sorted Adjacent Differences
2022-07-08 00:02:00 【非长】
题目链接:https://codeforces.com/problemset/problem/1339/B
解题思路: 思维+贪心
开始没转过弯来,一直想怎么把相等的数放在前面,后面看了大佬题解才知道方法从刚开始就不对。
使数字形成一种特殊的排列,使得数组中相邻两个数,前一个减后一个的差有非递减趋势。
可以先反过来想,怎么使得数组中相邻两个数的差呈非递增趋势(便于理解分析很多)之后反过来输出就行。
非递增情况(大的在前)最大的情况:自然是最大数和最小数的差,由于是按差的绝对值排列,因此后一个为最小和次大值,再后为次小值和次大值…最后是中间部分(奇数个数据最后为中间那个,偶数个照规律排列完即可)
由此:对于题目要求情况,反过来输出即可。没有前面情况的过渡可能较难理解为什么从中间开始向两边输出。
AC代码:
#include<iostream>
#include<algorithm>
using namespace std;
int num[100005];
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
for (int i = 0; i < n; i++)
cin >> num[i];
sort(num, num + n);
if (n % 2 == 1)
cout << num[n / 2] << ' ';
for (int i = n / 2 - 1; i >= 0; i--)
cout << num[i] << ' ' << num[n - i - 1] << ' ';
cout << endl;
}
}
边栏推荐
- The beauty of Mathematics -- the principle of fine Fourier transform
- About snake equation (1)
- Transportation, new infrastructure and smart highway
- The usage of rand function in MATLAB
- The difference between distribution function and probability density function of random variables
- 能力贡献 GBASE三大解决方案入选“金融信创生态实验室-金融信创解决方案(第一批)”
- Kaptcha generates verification code on Web page
- Anaconda3 download address Tsinghua University open source software mirror station
- A little experience from reading "civilization, modernization, value investment and China"
- How does Matplotlib generate multiple pictures in turn & only save these pictures without displaying them in the compiler
猜你喜欢
Introduction to grpc for cloud native application development
5、离散控制与连续控制
Gnuradio operation error: error thread [thread per block [12]: < block OFDM_ cyclic_ prefixer(8)>]: Buffer too small
Four digit nixie tube display multi digit timing
5. Contrôle discret et contrôle continu
Redis 主从复制
Chapter 7 behavior level modeling
Application of state mode in JSF source code
Redis master-slave replication
Gnuradio transmits video and displays it in real time using VLC
随机推荐
ROS problems (topic types do not match, topic datatype/md5sum not match, MSG XXX have changed. rerun cmake)
QT build with built-in application framework -- Hello World -- use min GW 32bit
2022 new examination questions for crane driver (limited to bridge crane) and question bank for crane driver (limited to bridge crane) operation examination
Understanding of expectation, variance, covariance and correlation coefficient
Matlab code on error analysis (MAE, MAPE, RMSE)
子矩阵的和
Arm bare metal
Redis cluster
Kindle operation: transfer downloaded books and change book cover
滑环使用如何固定
About snake equation (5)
LaTeX 中 xcolor 颜色的用法
Frequency probability and Bayesian probability
Transportation, new infrastructure and smart highway
common commands
Gnuradio3.9.4 create OOT module instances
Talk about smart Park
pb9.0 insert ole control 错误的修复工具
Redis master-slave replication
腾讯游戏客户端开发面试 (Unity + Cocos) 双重轰炸 社招6轮面试