Submission #220748

# Submission time Handle Problem Language Result Execution time Memory
220748 2020-04-08T15:06:36 Z Haunted_Cpp Kralj (COCI16_kralj) C++17
0 / 140
2000 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;
  F0R (times, 2) {
    for (int i = 0; ; i = (i + 1) % n) {
      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 Execution timed out 2088 ms 35696 KB Time limit exceeded
2 Execution timed out 2085 ms 35088 KB Time limit exceeded
3 Execution timed out 2082 ms 40720 KB Time limit exceeded
4 Execution timed out 2092 ms 41452 KB Time limit exceeded
5 Execution timed out 2087 ms 39928 KB Time limit exceeded
6 Execution timed out 2085 ms 40200 KB Time limit exceeded
7 Execution timed out 2083 ms 44140 KB Time limit exceeded
8 Execution timed out 2100 ms 41720 KB Time limit exceeded
9 Execution timed out 2085 ms 45944 KB Time limit exceeded
10 Execution timed out 2080 ms 43256 KB Time limit exceeded