题解:P10709 [NOISG2024 Prelim] Party
一道贪心题。
根据题目的描述,说明两个人不能坐在相邻的座位上,那么我们最多只能选择
那么我们按照
时间复杂度为
参考代码如下:
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define ll long long
#define writes(x) write(x), putchar(' ')
#define writeln(x) write(x), putchar('\n');
static char buf[100000], * pa(buf), * pb(buf);
#define gc pa == pb && (pb = (pa = buf) + fread(buf, 1, 100000, stdin), pa == pb) ? EOF : *pa++
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
template <typename T> void read(T& x) {
T t = 0, sgn = 1;
char ch = gc;
while (!isdigit(ch)) {
if (ch == '-') sgn = -sgn;
ch = gc;
}
while (isdigit(ch)) {
t = (t << 3) + (t << 1) + (ch ^ 48);
ch = gc;
}
x = sgn * t;
}
template <typename T, typename ...Args> void read(T& tmp, Args &...tmps) {
read(tmp); read(tmps...);
}
template <typename T> void write(T x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
template <typename T, typename ...Args> void write(T& tmp, Args &...tmps) {
writes(tmp);
writes(tmps...);
}
template <typename T> T rand(T l, T r) {
return rnd() % (r - l + 1) + l;
}
ll n, a[200005];
int main() {
read(n);
for (int i = 1; i <= n; i++) {
read(a[i]);
}
sort(a + 1, a + n + 1, greater<>());
int rg = ceil(n / 2.0);
ll ans = 0;
for (int i = 1; i <= rg; i++) {
if (a[i] < 0) break;
ans += a[i];
}
write(ans);
}