CF1283E New Year Parties
\(\large{题目链接}\)
\(\\\)
\(\Large\textbf{Solution: } \large{考虑贪心。对于最少的,如果当前位置有人,那么把他往左移一定不会更差,所以就往左更优。\\对于最多的,首先在确保当前位置有人的情况下,优先往右更优,如果右边扩展了,那么再考虑向左扩展。}\)
\(\\\)
\(\Large\textbf{Code: }\)
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, a[N], b1[N], c[N], b2[N];
inline int read() {
int x = 0;
char ch = getchar();
while (!isdigit(ch)) ch = getchar();
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
return x;
}
int main() {
n = read();
int x;
for (int i = 1; i <= n; ++i) x = read(), ++a[x], ++c[x];
int Min = 0, Max = 0, flg1 = 0, flg2 = 0;
for (int i = 1; i <= n; ++i) {
if (!flg1) {
if (a[i] && !b1[i - 1]) ++b1[i - 1], --a[i];
if (a[i]) ++b1[i], --a[i];
if (a[i]) flg1 = 1, ++b1[i + 1];
} else {
if (a[i]) ++b1[i + 1];
else flg1 = 0;
}
if (!flg2) {
if (c[i] && b2[i - 1]) c[i] = 0;
if (c[i] && c[i + 1]) flg2 = 1, c[i] = 0, ++b2[i + 1];
if (c[i]) ++b2[i + 1], c[i] = 0;
} else c[i + 1] = 0, flg2 = 0;
}
for (int i = 1; i <= n + 1; ++i)
if (b1[i]) ++Max;
if (b2[i]) ++Min;
if (b1[0]) ++Max;
printf("%d %d\n", Min, Max);
return 0;
}