답안 #758185

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
758185 2023-06-14T08:48:22 Z Sam_a17 카멜레온의 사랑 (JOI20_chameleon) C++17
100 / 100
58 ms 412 KB
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
// #include "chameleon.h"
// #include <atcoder/all>
#include <cstdio>
using namespace std;
 
#ifndef ONLINE_JUDGE
#define dbg(x) cerr << #x <<" "; print(x); cerr << endl;
#else
#define dbg(x)
#endif
 
#define sz(x) (int((x).size()))
#define len(x) (int)x.length()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define clr(x) (x).clear()
#define uniq(x) x.resize(unique(all(x)) - x.begin());
#define blt(x) __builtin_popcount(x)

#define pb push_back
#define popf pop_front
#define popb pop_back
#define ld long double
#define ll long long
 
void print(long long t) {cerr << t;}
void print(int t) {cerr << t;}
void print(string t) {cerr << t;}
void print(char t) {cerr << t;}
void print(double t) {cerr << t;}
void print(unsigned long long t) {cerr << t;}
void print(long double t) {cerr << t;}
 
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define nl '\n'

// Indexed Set
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

template <class T, class V> void print(pair <T, V> p);
template <class T> void print(vector <T> v);
template <class T> void print(set <T> v);
template <class T, class V> void print(map <T, V> v);
template <class T> void print(multiset <T> v);
template <class T> void print(T v[],T n) {cerr << "["; for(int i = 0; i < n; i++) {cerr << v[i] << " ";} cerr << "]";}
template <class T, class V> void print(pair <T, V> p) {cerr << "{"; print(p.first); cerr << ","; print(p.second); cerr << "}";}
template <class T> void print(vector <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(Tree <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(deque <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(set <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(multiset <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void print(map <T, V> v) {cerr << "[ "; for (auto i : v) {print(i); cerr << " ";} cerr << "]";}
 
// for random generations
mt19937 myrand(chrono::steady_clock::now().time_since_epoch().count());
// mt19937 myrand(131);
 
// for grid problems
int dx[8] = {-1,0,1,0,1,-1,1,-1};
int dy[8] = {0,1,0,-1,1,1,-1,-1};
 
long long ka() {
	long long x = 0;
	bool z = false;
	while (1)
	{
		char y = getchar();
		if (y == '-')
			z = true;
		else if ('0' <= y && y <= '9')
			x = x * 10 + y - '0';
		else
		{
			if (z)
				x *= -1;
			return x;
		}
	}
}
 
// lowest / (1 << 17) >= 1e5 / (1 << 18) >= 2e5 / (1 << 21) >= 1e6
void fastIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr); cout.tie(nullptr);
}
// file in/out
void setIO(string str = "") {
  fastIO();
 
  if (str != "") {
    freopen((str + ".in").c_str(), "r", stdin);
    freopen((str + ".out").c_str(), "w", stdout);
  } else if(str == "input") {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  }
}

const int maxN = 1005;
bool vis[maxN];
vector<int> bro[maxN], adj[maxN];
int n, p[maxN], iran[maxN];

int Query(const std::vector<int> &p);
void Answer(int a, int b);

vector<int> first, second;
bool vis2[maxN];

vector<int> g[5];

void Solve(int N) {
  n = N;

  g[0].push_back(1);
  for(int i = 2; i <= 2 * n; i++) {
    bool added = false;
    for(int j = 0; j <= 3; j++) {
      g[j].push_back(i);
      if(Query(g[j]) != sz(g[j])) {
        g[j].pop_back();

        int lst = -1;
        while(true) {
          if(lst != -1) {
            vector<int> vi{i};
            for(int k = lst + 1; k <= sz(g[j]) - 1; k++) {
              vi.push_back(g[j][k]);
            }

            if(Query(vi) == sz(vi)) {
              break;
            }
          }

          int ina = lst + 1, inb = sz(g[j]) - 1, ans = -1;
          while(ina <= inb) {
            int mid = (ina + inb) / 2;
            vector<int> toask{i};
            for(int k = lst + 1; k <= mid; k++) {
              toask.push_back(g[j][k]);
            }

            if(Query(toask) != sz(toask)) {
              ans = mid;
              inb = mid - 1;
            } else {
              ina = mid + 1;
            }
          }

          bro[g[j][ans]].push_back(i);
          bro[i].push_back(g[j][ans]);
          lst = ans;
        }
      } else {
        if(added) {
          g[j].pop_back();
        }
        added = true;
      }
    }
  }


  vector<bool> done(2 * n + 1);
  for(int i = 1; i <= 2 * n; i++) {
    if(done[i]) continue;
    vector<int> which = bro[i];
    if(sz(which) == 3) {
      bro[i] = which;
      queue<int> q;
      q.push(i);

      while(!q.empty()) {
        auto u = q.front();
        q.pop();


        if(iran[u] == 0) {
          for(int j = 0; j < (1 << 3); j++) {
            if(blt(j) != 2) continue;

            vector<int> ask{u};
            int to = -1;
            for(int k = 0; k < 3; k++) {
              if(j & (1 << k)) {
                ask.push_back(which[k]);
              } else {
                to = which[k];
              }
            }

            if(Query(ask) == 1) {
              p[u] = to;
              iran[to] = u;
              done[u] = true;
              q.push(to);
              break;
            }
          }
        } else {
          set<int> st;
          for(auto j: bro[u]) {
            st.insert(j);
          }
          st.erase(iran[u]);
          vector<int> ask{u, iran[u]};
          int ls = *st.rbegin();
          for(auto j: st) {
            ask.push_back(j);
            if(Query(ask) == 1) {
              p[u] = ls;
              iran[ls] = u;

              if(!done[u]) {
                done[u] = true;
                q.push(ls);
              }
              break;
            }
            ask.pop_back();
            ls = j;
          }
        }
      }
    } else {
      if(!vis[i]) {
        vis[i] = vis[which[0]] = true;
        Answer(i, which[0]);
      }
    }
  }

  for(int i = 1; i <= 2 * n; i++) {
    if(vis[i]) continue;
    if(bro[i].empty()) continue;

    int harazat = -1;
    for(auto j: bro[i]) {
      if(j == p[i]) continue;
      if(j == iran[i]) continue;
      assert(harazat == -1);
      harazat = j;
    }
    assert(harazat != -1);

    vis[i] = vis[harazat] = true;
    Answer(i, harazat);
  }
}

Compilation message

chameleon.cpp: In function 'void setIO(std::string)':
chameleon.cpp:94:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   94 |     freopen((str + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
chameleon.cpp:95:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   95 |     freopen((str + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
chameleon.cpp:97:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   97 |     freopen("input.txt", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
chameleon.cpp:98:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   98 |     freopen("output.txt", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 27 ms 396 KB Output is correct
4 Correct 29 ms 336 KB Output is correct
5 Correct 28 ms 400 KB Output is correct
6 Correct 32 ms 380 KB Output is correct
7 Correct 30 ms 400 KB Output is correct
8 Correct 27 ms 336 KB Output is correct
9 Correct 27 ms 392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 0 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 0 ms 336 KB Output is correct
8 Correct 0 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 0 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 0 ms 336 KB Output is correct
8 Correct 0 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 376 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 336 KB Output is correct
2 Correct 0 ms 336 KB Output is correct
3 Correct 48 ms 400 KB Output is correct
4 Correct 52 ms 404 KB Output is correct
5 Correct 43 ms 400 KB Output is correct
6 Correct 45 ms 336 KB Output is correct
7 Correct 47 ms 336 KB Output is correct
8 Correct 53 ms 392 KB Output is correct
9 Correct 58 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 27 ms 396 KB Output is correct
4 Correct 29 ms 336 KB Output is correct
5 Correct 28 ms 400 KB Output is correct
6 Correct 32 ms 380 KB Output is correct
7 Correct 30 ms 400 KB Output is correct
8 Correct 27 ms 336 KB Output is correct
9 Correct 27 ms 392 KB Output is correct
10 Correct 0 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 0 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 0 ms 336 KB Output is correct
17 Correct 0 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 376 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 1 ms 336 KB Output is correct
27 Correct 1 ms 336 KB Output is correct
28 Correct 0 ms 336 KB Output is correct
29 Correct 0 ms 336 KB Output is correct
30 Correct 48 ms 400 KB Output is correct
31 Correct 52 ms 404 KB Output is correct
32 Correct 43 ms 400 KB Output is correct
33 Correct 45 ms 336 KB Output is correct
34 Correct 47 ms 336 KB Output is correct
35 Correct 53 ms 392 KB Output is correct
36 Correct 58 ms 336 KB Output is correct
37 Correct 42 ms 412 KB Output is correct
38 Correct 26 ms 400 KB Output is correct
39 Correct 42 ms 384 KB Output is correct
40 Correct 41 ms 404 KB Output is correct
41 Correct 41 ms 404 KB Output is correct
42 Correct 26 ms 412 KB Output is correct
43 Correct 45 ms 336 KB Output is correct
44 Correct 40 ms 392 KB Output is correct
45 Correct 42 ms 336 KB Output is correct