题解 AT5222 【[ABC140F] Many Slimes】
这个题可以直接根据题意模拟就行......
贪心一下,可以发现一定是先生成大的再生成小的,并且大的数一定去生成刚好小于它的那个数。我们就可以根据这个贪心来模拟。
然后模拟过程就需要一些乱搞了......比如我用了两个 multiset 和一个 set,一个 multiset 用来记录用来存还剩哪些数没生成,另一个用来存已经生成了哪些数,然后后面放数的时候就枚举第二个 multiset 来生成新的数。两个 multiset 都是默认从大到小排序的。
然后我那个 set 就是用来存还有哪几种数没放,因为根据贪心,我们需要找到第一个刚好小于它的数来生成,就可以在 set 上面二分一下即可。然后如果这个数在第一个 multiset 已经没了,那么我们就在这个 set 中把这个数删掉即可。
时间复杂度大概为
渣代码轻喷QAQ
#include <bits/stdc++.h>
#define ll long long
#define ls id << 1
#define rs id << 1 | 1
#define mem(array, value, size, type) memset(array, value, ((size) + 5) * sizeof(type))
#define memarray(array, value) memset(array, value, sizeof(array))
#define pb(x) push_back(x)
#define st(x) (1LL << (x))
#define pii pair<int, int>
#define mp(a, b) make_pair((a), (b))
#define Flush fflush(stdout)
using namespace std;
const int N = 1000050;
const int inf = 0x3f3f3f3f;
const ll mod = 998244353LL;
clock_t TIME_START, TIME_END;
void program_end()
{
#ifdef ONLINE
printf("\nTime used: %.6lf(s)\n", ((double)TIME_END - TIME_START) / CLOCKS_PER_SEC);
system("pause");
#endif
}
int n;
int s[N];
multiset<int, greater<int>> S1, S2;
set<int> vis;
int id[N];
int tot;
inline int Query(int x)
{
auto it = vis.lower_bound(x);
if (it != vis.begin())
{
it--;
return *it;
}
return -1;
}
void solve()
{
cin >> n;
for (int i = 1; i <= st(n); ++i)
scanf("%d", &s[i]);
for (int i = 1; i <= st(n); ++i)
vis.insert(s[i]);
for (int i = 1; i <= st(n); ++i)
S1.insert(s[i]);
sort(s + 1, s + st(n) + 1, greater<int>());
S2.insert(s[1]);
S1.erase(S1.find(s[1]));
if (S1.count(s[1]) == 0)
vis.erase(s[1]);
int tim = 1;
vector<int> tmp;
while (tim <= n)
{
// puts("flag");
tmp.clear();
for (auto &i : S2)
{
int x = Query(i);
if (S1.empty())
return puts("Yes"), void();
if (x == -1 || S1.find(x) == S1.end())
return puts("No"), void();
S1.erase(S1.find(x));
if (S1.find(x) == S1.end())
vis.erase(x);
if (S1.empty())
return puts("Yes"), void();
tmp.push_back(x);
}
for (auto &i : tmp)
S2.insert(i);
tim++;
}
puts("No");
}
int main()
{
TIME_START = clock();
int Test = 1;
// cin >> Test;
while (Test--)
solve();
TIME_END = clock();
program_end();
return 0;
}
/*
3
5 4 4 4 3 3 2 1
*/