当前位置:网站首页>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;
}
}
边栏推荐
- Application of state mode in JSF source code
- ANSI / nema- mw- 1000-2020 magnetic iron wire standard Latest original
- Mysql database (2)
- redis的持久化方式-RDB和AOF 两种持久化机制
- Guojingxin center "APEC investment +": some things about the Internet sector today | observation on stabilizing strategic industrial funds
- nacos-微服务网关Gateway组件 +Swagger2接口生成
- 云原生应用开发之 gRPC 入门
- 腾讯游戏客户端开发面试 (Unity + Cocos) 双重轰炸 社招6轮面试
- Qml 字体使用pixelSize来自适应界面
- Call (import) in Jupiter notebook ipynb . Py file
猜你喜欢
2022 R1 fast opening pressure vessel operation test question bank and R1 fast opening pressure vessel operation free test questions
Macro definition and multiple parameters
Redux使用
用户之声 | 对于GBase 8a数据库学习的感悟
Redis master-slave replication
Matlab code about cosine similarity
Common fault analysis and Countermeasures of using MySQL in go language
2、TD+Learning
Js中forEach map无法跳出循环问题以及forEach会不会修改原数组
qt-使用自带的应用框架建立--hello world--使用min GW 32bit
随机推荐
Guojingxin center "friendship and righteousness" - the meta universe based on friendship and friendship, and the parallel of "honguniverse"
云原生应用开发之 gRPC 入门
5. Contrôle discret et contrôle continu
Matlab code on error analysis (MAE, MAPE, RMSE)
About snake equation (5)
2、TD+Learning
npm 內部拆分模塊
Euler Lagrange equation
3、多智能体强化学习
Tapdata 的 2.0 版 ,开源的 Live Data Platform 现已发布
Different methods for setting headers of different pages in word (the same for footer and page number)
如何让导电滑环信号更好
STM32GPIO口的工作原理
从Starfish OS持续对SFO的通缩消耗,长远看SFO的价值
Plot function drawing of MATLAB
Chapter 7 behavior level modeling
快速熟知XML解析
4. Strategic Learning
2022 chemical automation control instrument examination summary and chemical automation control instrument simulation examination questions
子矩阵的和