Submission #220749

# Submission time Handle Problem Language Result Execution time Memory
220749 2020-04-08T15:08:47 Z Haunted_Cpp Kralj (COCI16_kralj) C++17
140 / 140
783 ms 45944 KB
#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
  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 == 3) 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});
    }
  }
  assert (!edge_list.empty());
  int m = (*edge_list.begin()).s;
  // Run greedy algorithm
  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 783 ms 35692 KB Output is correct
2 Correct 541 ms 35180 KB Output is correct
3 Correct 683 ms 40684 KB Output is correct
4 Correct 689 ms 41452 KB Output is correct
5 Correct 525 ms 39800 KB Output is correct
6 Correct 471 ms 40440 KB Output is correct
7 Correct 607 ms 44152 KB Output is correct
8 Correct 546 ms 41720 KB Output is correct
9 Correct 684 ms 45944 KB Output is correct
10 Correct 577 ms 43256 KB Output is correct