Submission #757749

# Submission time Handle Problem Language Result Execution time Memory
757749 2023-06-13T17:36:52 Z Sam_a17 Chameleon's Love (JOI20_chameleon) C++17
40 / 100
48 ms 348 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];
int n, p[maxN], iran[maxN];

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

void Solve(int N) {
  n = N;

  if(n <= 50) {
    for(int i = 1; i <= 2 * n; i++) {
      vector<int> which;
      for(int j = 1; j <= 2 * n; j++) {
        if(i == j) continue;
        
        vector<int> ask;
        ask.push_back(i);
        ask.push_back(j);
        if(Query(ask) == 1) {
          which.push_back(j);
        }

      }

      if(sz(which) == 3) {
        bro[i] = which;
        for(int j = 0; j < (1 << 3); j++) {
          if(blt(j) != 2) continue;

          vector<int> ask{i};
          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[i] = to;
            iran[to] = i;
            break;
          }
        }
      }
    }

    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);
    }
  } else {
    for(int i = 1; i <= n; i++) {
      int ina = n + 1, inb = 2 * n, ans = -1;
      vector<int> which;

      // first
      while(ina <= inb) {
        int mid = (ina + inb) / 2;
        vector<int> ask{i};
        for(int j = n + 1; j <= mid; j++) {
          ask.push_back(j);
        }

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

      if(ans != -1) {
        which.push_back(ans);
        bro[i] = which;
      } else {
        continue;
      }

      // second
      int ans2 = -1;
      ina = ans + 1, inb = 2 * n;
      while(ina <= inb) {
        int mid = (ina + inb) / 2;
        vector<int> ask{i};

        for(int j = ans + 1; j <= mid; j++) {
          ask.push_back(j);
        }

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

      if(ans2 != -1) {
        which.push_back(ans2);
        bro[i] = which;
      } else {
        continue;
      }

      // third if have
      int ans3 = -1;
      ina = ans2 + 1, inb = 2 * n;
      while(ina <= inb) {
        int mid = (ina + inb) / 2;
        vector<int> ask{i};

        for(int j = ans2 + 1; j <= mid; j++) {
          ask.push_back(j);
        }

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

      if(ans3 != -1) {
        which.push_back(ans3);
        bro[i] = which;
      } else {
        continue;
      }
    }

    for(int i = n + 1; i <= 2 * n; i++) {
      int ina = 1, inb = n, ans = -1;
      vector<int> which;

      // first
      while(ina <= inb) {
        int mid = (ina + inb) / 2;
        vector<int> ask{i};
        for(int j = 1; j <= mid; j++) {
          ask.push_back(j);
        }

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

      if(ans != -1) {
        which.push_back(ans);
        bro[i] = which;
      } else {
        continue;
      }

      // second
      int ans2 = -1;
      ina = ans + 1, inb = n;
      while(ina <= inb) {
        int mid = (ina + inb) / 2;
        vector<int> ask{i};

        for(int j = ans + 1; j <= mid; j++) {
          ask.push_back(j);
        }

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

      if(ans2 != -1) {
        which.push_back(ans2);
        bro[i] = which;
      } else {
        continue;
      }

      // third if have
      int ans3 = -1;
      ina = ans2 + 1, inb = n;
      while(ina <= inb) {
        int mid = (ina + inb) / 2;
        vector<int> ask{i};

        for(int j = ans2 + 1; j <= mid; j++) {
          ask.push_back(j);
        }

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

      if(ans3 != -1) {
        which.push_back(ans3);
        bro[i] = which;
      } else {
        continue;
      }
    }

    for(int i = 1; i <= 2 * n; i++) {
      vector<int> which = bro[i];
      if(sz(which) == 3) {
        bro[i] = which;
        for(int j = 0; j < (1 << 3); j++) {
          if(blt(j) != 2) continue;

          vector<int> ask{i};
          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[i] = to;
            iran[to] = i;
            break;
          }
        }
      }
    }

    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);
    }
  }

  // special case
  vector<int> toask;
  for(int i = 1; i <= 2 * n; i++) {
    if(vis[i]) continue;
    toask.push_back(i);

    if(sz(toask) >= 2) {
      int ss = Query(toask);
      if(ss == sz(toask) - 1) {
        int ina = 0, inb = sz(toask) - 2, ans = -1;
        while(ina <= inb) {
          int mid = (ina + inb) / 2;
          vector<int> newask{i};

          for(int j = 0; j <= mid; j++) {
            newask.push_back(toask[j]);
          }

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

        assert(ans != -1);

        vis[toask[ans]] = true;
        vis[i] = true;


        Answer(toask[ans], i);

        vector<int> newask;
        for(auto j: toask) {
          if(vis[j]) continue;
          newask.push_back(j);
        }
        toask.swap(newask);
      }
    }
  }


}

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);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 336 KB Output is correct
2 Correct 0 ms 336 KB Output is correct
3 Incorrect 33 ms 348 KB Wrong Answer [3]
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 336 KB Output is correct
2 Correct 0 ms 336 KB Output is correct
3 Correct 0 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 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 0 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 336 KB Output is correct
2 Correct 0 ms 336 KB Output is correct
3 Correct 0 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 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 0 ms 336 KB Output is correct
10 Correct 3 ms 340 KB Output is correct
11 Correct 2 ms 336 KB Output is correct
12 Correct 2 ms 336 KB Output is correct
13 Correct 3 ms 336 KB Output is correct
14 Correct 3 ms 336 KB Output is correct
15 Correct 3 ms 336 KB Output is correct
16 Correct 3 ms 336 KB Output is correct
17 Correct 3 ms 336 KB Output is correct
18 Correct 3 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 0 ms 336 KB Output is correct
3 Incorrect 48 ms 348 KB Wrong Answer [3]
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 336 KB Output is correct
2 Correct 0 ms 336 KB Output is correct
3 Incorrect 33 ms 348 KB Wrong Answer [3]
4 Halted 0 ms 0 KB -