#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <cassert>
#include <string>
#include <cstring>
#include <bitset>
#include <random>
#include <chrono>
#include <iomanip>
#pragma GCC optimize ("Ofast")
#pragma GCC target("fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("unroll-loops")
#define FOR(i, a, b) for(int i = a; i < (int) b; i++)
#define F0R(i, a) FOR (i, 0, a)
#define ROF(i, a, b) for(int i = a; i >= (int) b; i--)
#define R0F(i, a) ROF(i, a, 0)
#define GO(i, a) for (auto i : a)
#define rsz resize
#define eb emplace_back
#define pb push_back
#define sz(x) (int) x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define f first
#define s second
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef vector<vpii> vvpii;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef vector<pi64> vpi64;
const int dr[] = {+1, -1, +0, +0, +1, -1, +1, -1};
const int dc[] = {+0, +0, +1, -1, +1, -1, -1, +1};
const int ms[] = {+31, +29, +31, 30, +31, +30, +31, +31, +30, +31, +30, +31};
const int N = 5e5 + 5;
vvi g (N);
int dwarf [N];
int elf [N];
int main () {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
F0R (i, n) {
int foo;
cin >> foo;
--foo;
g[foo].eb(i);
}
F0R (i, n) cin >> dwarf[i];
F0R (i, n) cin >> elf[i];
/*
* This problem is solved using a Greedy algorithm
* Similar to the 40pts solution, there is a location k where no elf will ever go through
* the edge k - k + 1
* If we start the search from this edge and run the 40pts solution idea, we will get full credit.
*/
// Find location
int m = -1;
set<pii> edge_list;
F0R (i, n - 1) edge_list.insert({i, i + 1});
edge_list.insert({n - 1, 0});
int current_elf = 0;
int vezes = 0;
for (int i = 0; ; i = (i + 1) % n) {
vezes += (i == 0);
if (vezes == 5) break;
current_elf += sz(g[i]);
if (current_elf) --current_elf;
if (current_elf) {
if (i != n - 1) edge_list.erase({i, i + 1});
else edge_list.erase({n - 1, 0});
}
}
m = (*edge_list.begin()).s;
// Run greedy algorithm
assert (m != -1);
int vis = 0, res = 0;
multiset<int> Get;
for (int i = m; vis != n; i = (i + 1) % n) {
vis++;
// Add all the new ones
GO (to, g[i]) {
Get.insert(elf[to]);
}
assert (Get.empty() == false);
int cur = dwarf[i];
auto best = Get.upper_bound(cur);
// If there is no way to beat this dwarf, simply play the weakest one
if (best == Get.end()) {
Get.erase(Get.begin());
continue;
}
// Play the one which is just slightly better
++res;
Get.erase(best);
}
cout << res << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
839 ms |
44140 KB |
Output is correct |
2 |
Correct |
539 ms |
42988 KB |
Output is correct |
3 |
Correct |
704 ms |
50540 KB |
Output is correct |
4 |
Correct |
727 ms |
51180 KB |
Output is correct |
5 |
Correct |
535 ms |
51836 KB |
Output is correct |
6 |
Correct |
487 ms |
51320 KB |
Output is correct |
7 |
Correct |
647 ms |
56480 KB |
Output is correct |
8 |
Correct |
524 ms |
52728 KB |
Output is correct |
9 |
Correct |
680 ms |
58872 KB |
Output is correct |
10 |
Correct |
580 ms |
55032 KB |
Output is correct |