Codeforces 1419D1: Sage's Birthday (easy version)

Codeforces 1419D1 C++ 一解.

分析

本题是简单版本, 各个冰淇淋球的价格都不相同. 我们只需要构造出数量最多的 “谷” 即可. 与上篇 Codeforces 1312B: Bogosort 类似, 这次相当于对一个排序后的数组同时从两边取数输出, 例如对输入数据 $[7,6,5,4,3,2,1]$, 两边取数得到 $[7,1,6,2,5,3,4]$, 这样得到的新数列的 “谷” 数量最多, 且为 $\left[\dfrac{n-1}{2}\right]$.

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <algorithm>
#include <iostream>

using namespace std;

int a[100000];

int main()
{
int n, p = 0;

cin >> n;
for (int i = 0; i < n; ++i)
{
cin >> a[i];
}

cout << (n - 1) / 2 << endl;
sort(a, a + n);
while (p++ < n / 2)
{
cout << a[n - p] << ' ' << a[p - 1] << ' ';
}
if (n % 2)
{
cout << a[p - 1];
}
cout << endl;

return 0;
}

Codeforces 1419D1: Sage's Birthday (easy version)

https://blog.tamako.work/acmoi/codeforces/1419d1/

Posted on

2022-07-31

Updated on

2022-07-31

Licensed under

Comments