답안 #1079563

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1079563 2024-08-28T17:08:24 Z bleahbleah 식물 비교 (IOI20_plants) C++17
30 / 100
4000 ms 68052 KB
#include "plants.h"
#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()

using namespace std;

using ll = long long;
#define sz(x) ((int)(x).size())

int K, N;
const int nmax = 2e5 + 5;

namespace Queue {
   int next[nmax];
   int inside[nmax];
   
   set<int> heads;
   void insert(int node) {
      heads.emplace(node);
      inside[node] = 1;
      next[node] = -1;
      for(int i = (node + 1) % N, d = 1; i != node && d < K; d++, i = (i + 1) % N) {
         if(inside[i]) {
            if(heads.count(i))
               heads.erase(i);
            next[node] = i;
            break;
         }
      }
      for(int i = (node - 1 + N) % N, d = 1; i != node && d < K; d++, i = (i - 1 + N) % N) {
         if(inside[i]) {
            heads.erase(node);
            next[i] = node;
            break;
         }
      }
   }
   
   vector<int> prune() {
      vector<int> rez;
      for(auto x : heads)
         rez.emplace_back(x), inside[x] = 0;
      heads.clear();
      for(auto x : rez)
         if(next[x] != -1) heads.emplace(next[x]);
      return rez;
   }
}

vector<int> topsort;
#define left ofia
#define right goa
int left[nmax][18], jumpl[nmax][18];
int right[nmax][18], jumpr[nmax][18];

void init(int k__, std::vector<int> r) {
   auto T = r;
   K = k__;
   N = sz(r);
   topsort.resize(N);
   
   vector<int> erased(N, 0);
   for(int i = 0; i < sz(r); i++) 
      if(r[i] == 0) erased[i] = 1, Queue::insert(i);
   
   int color = 0;
   while(sz(Queue::heads)) {
      auto rem = Queue::prune();
      ++color;
      for(auto x : rem) topsort[x] = color;
      
      for(auto x : rem) {
         for(int i = (x - 1 + N) % N, d = 1; d < K; d++, i = (i - 1 + N) % N) {
            if(erased[i]) continue;
            r[i]--;
            if(r[i] == 0) erased[i] = 1, Queue::insert(i);
         }
      }
   }
   
   for(int x = 0; x < sz(r); x++) {
      int mn = -1;
      jumpl[x][0] = 0;
      for(int i = (x - 1 + N) % N, d = 1; d < K; d++, i = (i - 1 + N) % N) {
         if(topsort[i] > topsort[x])
            mn = (mn == -1? i : topsort[mn] < topsort[i]? mn : i);
      }
      left[x][0] = mn == -1? x : mn;
      jumpl[x][0] = left[x][0] <= x? x - left[x][0] : x + N - left[x][0];
      
      mn = -1;
      jumpr[x][0] = 0;
      for(int i = (x + 1 + N) % N, d = 1; d < K; d++, i = (i + 1 + N) % N) {
         if(topsort[i] > topsort[x])
            mn = (mn == -1? i : topsort[mn] < topsort[i]? mn : i);
      }
      right[x][0] = mn == -1? x : mn;
      jumpr[x][0] = (right[x][0] >= x? right[x][0] - x : N - x + right[x][0]);
      //cerr << x << ": " << left[x][0] << ' ' << right[x][0] << '\t' << jumpl[x][0] << ' ' << jumpr[x][0] << '\n';
   }
   for(int step = 1; step < 18; step++)
      for(int i = 0; i < N; i++)
         left[i][step] = left[left[i][step - 1]][step - 1], jumpl[i][step] = jumpl[i][step - 1] + jumpl[left[i][step - 1]][step - 1],
         right[i][step] = right[right[i][step - 1]][step - 1], jumpr[i][step] = jumpr[i][step - 1] + jumpr[right[i][step - 1]][step - 1];
   
   //for(auto x : topsort) cerr << x << ' ';
   //cerr << '\n';
   
   
   //for(int i= 0; i < N; i++) {
      //cerr << i << "/";
      //if(erased[i]) cerr << -1;
      //else cerr << r[i] << "/" << T[i];;
      //cerr << ' ';
   //}
   //cerr << '\n';
      
   
	return;
}

bool compare_smaller(int x, int y) {
   auto rdist = [&](int d) {
      return d < x? N - x + d : d - x;
   };
   auto ldist = [&](int d) {
      return d <= x? x - d : x + N - d;
   };
   
   int st = x, buffer = rdist(y);
   for(int i = 17; i >= 0; i--) {
      if(buffer < jumpr[st][i]) continue;
      buffer -= jumpr[st][i];
      st = right[st][i];
   }
   
   
   //cerr << "rightmost: " << x << ' ' << st << ' ' << y << "\t\t" << rdist(y) << ' ' << rdist(st) << ' ' << topsort[st] << ' ' << topsort[x] << '\n';
   
   if(st == y) return 1;
   if(rdist(y) - rdist(st) < K && topsort[y] > topsort[st]) return 1;
   
   st = x;
   buffer = ldist(y);
   for(int i = 17; i >= 0; i--) {
      if(buffer < jumpl[st][i]) continue;
      buffer -= jumpl[st][i];
      st = left[st][i];
   }
   
   //cerr << "leftmost: " << x << ' ' << st << ' ' << y << "\t\t" << ldist(y) << ' ' << ldist(st) << ' ' << topsort[st] << ' ' << topsort[x] << '\n';
   
   
   if(st == y) return 1;
   if(ldist(y) - ldist(st) < K && topsort[y] > topsort[st]) return 1;
   return 0;
}

 
int compare_plants(int x, int y) {
   return compare_smaller(x, y)? 1 : compare_smaller(y, x)? -1 : 0;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 67 ms 4180 KB Output is correct
7 Correct 93 ms 11320 KB Output is correct
8 Correct 242 ms 68052 KB Output is correct
9 Correct 230 ms 67296 KB Output is correct
10 Correct 257 ms 67176 KB Output is correct
11 Correct 234 ms 67152 KB Output is correct
12 Correct 258 ms 67152 KB Output is correct
13 Correct 188 ms 67156 KB Output is correct
14 Correct 317 ms 67184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 520 KB Output is correct
6 Correct 18 ms 724 KB Output is correct
7 Correct 527 ms 6776 KB Output is correct
8 Correct 3 ms 604 KB Output is correct
9 Correct 18 ms 860 KB Output is correct
10 Correct 474 ms 6740 KB Output is correct
11 Correct 279 ms 6484 KB Output is correct
12 Correct 338 ms 6736 KB Output is correct
13 Correct 631 ms 6640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 520 KB Output is correct
6 Correct 18 ms 724 KB Output is correct
7 Correct 527 ms 6776 KB Output is correct
8 Correct 3 ms 604 KB Output is correct
9 Correct 18 ms 860 KB Output is correct
10 Correct 474 ms 6740 KB Output is correct
11 Correct 279 ms 6484 KB Output is correct
12 Correct 338 ms 6736 KB Output is correct
13 Correct 631 ms 6640 KB Output is correct
14 Execution timed out 4017 ms 9668 KB Time limit exceeded
15 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 66 ms 5452 KB Output is correct
4 Correct 339 ms 67512 KB Output is correct
5 Correct 689 ms 67660 KB Output is correct
6 Execution timed out 4053 ms 62544 KB Time limit exceeded
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 2 ms 600 KB Output is correct
7 Correct 15 ms 1372 KB Output is correct
8 Correct 12 ms 1372 KB Output is correct
9 Correct 15 ms 1312 KB Output is correct
10 Correct 12 ms 1368 KB Output is correct
11 Correct 15 ms 1372 KB Output is correct
12 Correct 14 ms 1476 KB Output is correct
13 Correct 12 ms 1372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 3 ms 604 KB Output is correct
6 Correct 668 ms 66868 KB Output is correct
7 Execution timed out 4077 ms 54868 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 67 ms 4180 KB Output is correct
7 Correct 93 ms 11320 KB Output is correct
8 Correct 242 ms 68052 KB Output is correct
9 Correct 230 ms 67296 KB Output is correct
10 Correct 257 ms 67176 KB Output is correct
11 Correct 234 ms 67152 KB Output is correct
12 Correct 258 ms 67152 KB Output is correct
13 Correct 188 ms 67156 KB Output is correct
14 Correct 317 ms 67184 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 520 KB Output is correct
20 Correct 18 ms 724 KB Output is correct
21 Correct 527 ms 6776 KB Output is correct
22 Correct 3 ms 604 KB Output is correct
23 Correct 18 ms 860 KB Output is correct
24 Correct 474 ms 6740 KB Output is correct
25 Correct 279 ms 6484 KB Output is correct
26 Correct 338 ms 6736 KB Output is correct
27 Correct 631 ms 6640 KB Output is correct
28 Execution timed out 4017 ms 9668 KB Time limit exceeded
29 Halted 0 ms 0 KB -