답안 #1050247

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1050247 2024-08-09T08:13:20 Z 정민찬(#11048) 카멜레온의 사랑 (JOI20_chameleon) C++17
100 / 100
41 ms 4440 KB
#include "chameleon.h"
#include <bits/stdc++.h>

using namespace std;

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

int col[1010];
vector<int> same[1010];
int adj[1010][1010];
int vis[1010];

void change(int x) {
    col[x] = 3 - col[x];
    vis[x] = 1;
    for (auto &y : same[x]) {
        if (!vis[y])
            change(y);
    }
}

void Solve(int N) {
    for (int i=1; i<=2*N; i++) {
        col[i] = 0;
        same[i].clear();
        vis[i] = 0;
    }
    col[1] = 1;
    for (int i=2; i<=2*N; i++) {
        vector<int> L, R;
        for (int j=1; j<=i-1; j++) {
            if (col[j] == 1) L.push_back(j);
            else R.push_back(j);
        }
        vector<int> La, Ra;
        int p = -1;
        for (int rep=0; rep<3; rep ++) {
            if (p+1 >= L.size()) break;
            vector<int> qry;
            for (int j=p+1; j<L.size(); j++) {
                qry.push_back(L[j]);
            }
            int ret1 = qry.size();
            qry.push_back(i);
            int ret2 = Query(qry);
            if (ret1 < ret2) break;
            int lo = p + 1, hi = L.size() - 1;
            while (lo < hi) {
                int mid = lo + hi >> 1;
                qry.clear();
                for (int j=p+1; j<=mid; j++) {
                    qry.push_back(L[j]);
                }
                int ret1 = qry.size();
                qry.push_back(i);
                int ret2 = Query(qry);
                if (ret1 < ret2) lo = mid + 1;
                else hi = mid;
            }
            La.push_back(L[lo]);
            p = lo;
        }
        p = -1;
        for (int rep=0; rep<3; rep ++) {
            if (p+1 >= R.size()) break;
            vector<int> qry;
            for (int j=p+1; j<R.size(); j++) {
                qry.push_back(R[j]);
            }
            int ret1 = qry.size();
            qry.push_back(i);
            int ret2 = Query(qry);
            if (ret1 < ret2) break;
            int lo = p + 1, hi = R.size() - 1;
            while (lo < hi) {
                int mid = lo + hi >> 1;
                qry.clear();
                for (int j=p+1; j<=mid; j++) {
                    qry.push_back(R[j]);
                }
                int ret1 = qry.size();
                qry.push_back(i);
                int ret2 = Query(qry);
                if (ret1 < ret2) lo = mid + 1;
                else hi = mid;
            }
            Ra.push_back(R[lo]);
            p = lo;
        }
        if (La.empty()) {
            col[i] = 1;
        }
        else if (Ra.empty()) {
            col[i] = 2;
        }
        else {
            col[i] = 1;
            memset(vis, 0, sizeof(vis));
            for (auto &x : La) {
                if (!vis[x])
                    change(x);
            }
        }
        for (auto &x : La) {
            same[i].push_back(x);
            same[x].push_back(i);
            adj[x][i] = 1;
            adj[i][x] = 1;
        }
        for (auto &x : Ra) {
            same[i].push_back(x);
            same[x].push_back(i);
            adj[x][i] = 1;
            adj[i][x] = 1;
        }
    }
    vector<int> L, R;
    for (int i=1; i<=2*N; i++) {
        if (col[i] == 1) L.push_back(i);
        else R.push_back(i);
    }
    assert(L.size() == N);
    for (int i=0; i<N; i++) {
        int x = L[i];
        int p = -1;
        for (int rep=0; rep<3; rep++) {
            if (same[x].size() == 3) break;
            int lo = p+1, hi = N;
            while (lo < hi) {
                int mid = lo + hi >> 1;
                vector<int> qry;
                for (int j=p+1; j<=mid; j++) {
                    if (adj[x][R[j]]) continue;
                    qry.push_back(R[j]);
                }
                int ret1 = qry.size();
                qry.push_back(x);
                int ret2 = Query(qry);
                if (ret1 < ret2) lo = mid + 1;
                else hi = mid;
            }
            if (lo == N) break;

            same[x].push_back(R[lo]);
            same[R[lo]].push_back(x);
            adj[x][R[lo]] = 1;
            adj[R[lo]][x] = 1;
            p = lo;
        }
    }
    for (int i=1; i<=2*N; i++) {
        assert(same[i].size() % 2 == 1);
    }
    vector<int> chk(2*N+1, 0);
    for (int i=1; i<=2*N; i++) {
        if (chk[i]) continue;
        if (same[i].size() == 1) {
            Answer(i, same[i][0]);
            chk[same[i][0]] = 1;
        }
        else {
            bool flag = false;
            for (auto &j : same[i]) {
                if (same[j].size() == 1) {
                    Answer(i, j);
                    chk[j] = 1;
                    flag = true;
                    break;
                }
            }
            if (flag) continue;
            int in = -1;
            for (auto &j : same[i]) {
                bool zero = false;
                for (auto &k : same[j]) {
                    if (k == i) continue;
                    vector<int> qry = {i, j, k};
                    if (Query(qry) == 1) {
                        zero = true;
                        break;
                    }
                }
                if (!zero) {
                    in = j;
                    break;
                }
            }
            for (auto &j : same[i]) {
                if (j == in) continue;
                vector<int> qry = {in, i, j};
                if (Query(qry) == 1) {
                    Answer(i, j);
                    chk[j] = 1;
                    break;
                }
            }
        }
    }
}
/*
namespace {

using std::exit;
using std::fprintf;
using std::printf;
using std::scanf;

constexpr int Q_MAX = 20000;
constexpr int N_MAX = 500;

int N;
int Y[N_MAX * 2 + 1], C[N_MAX * 2 + 1], L[N_MAX * 2 + 1];

int query_count = 0;
int answer_count = 0;
bool finishes[N_MAX * 2 + 1];

void WrongAnswer(int code) {
  printf("Wrong Answer [%d]\n", code);
  exit(0);
}

}  // namespace

int Query(const std::vector<int> &p) {
  if (++query_count > Q_MAX) WrongAnswer(3);
  bool presents[N_MAX * 2 + 1];
  for (int i = 1; i <= N * 2; ++i) presents[i] = false;
  for (const int k : p) {
    if (k <= 0 || k > N * 2) WrongAnswer(1);
    if (presents[k]) WrongAnswer(2);
    presents[k] = true;
  }
  bool colors[N_MAX + 1];
  for (int j = 1; j <= N; ++j) colors[j] = false;
  int color_count = 0;
  for (int i = 1; i <= N * 2; ++i) {
    if (!presents[i]) continue;
    const int color = presents[L[i]] ? C[L[i]] : C[i];
    if (!colors[color]) {
      ++color_count;
      colors[color] = true;
    }
  }
  return color_count;
}

void Answer(int a, int b) {
  ++answer_count;
  if (a <= 0 || a > N * 2) WrongAnswer(4);
  if (b <= 0 || b > N * 2) WrongAnswer(4);
  if (finishes[a]) WrongAnswer(5);
  finishes[a] = true;
  if (finishes[b]) WrongAnswer(5);
  finishes[b] = true;
  if (C[a] != C[b]) WrongAnswer(6);
}

int main() {
  if (scanf("%d", &N) != 1) {
    fprintf(stderr, "Error while reading input.\n");
    exit(1);
  }
  for (int i = 1; i <= N * 2; ++i) {
    if (scanf("%d", &Y[i]) != 1) {
      fprintf(stderr, "Error while reading input.\n");
      exit(1);
    }
  }
  for (int i = 1; i <= N * 2; ++i) {
    if (scanf("%d", &C[i]) != 1) {
      fprintf(stderr, "Error while reading input.\n");
      exit(1);
    }
  }
  for (int i = 1; i <= N * 2; ++i) {
    if (scanf("%d", &L[i]) != 1) {
      fprintf(stderr, "Error while reading input.\n");
      exit(1);
    }
  }
  for (int i = 1; i <= N * 2; ++i) finishes[i] = false;
  Solve(N);
  if (answer_count != N) WrongAnswer(7);
  printf("Accepted: %d\n", query_count);
  return 0;
}
*/

Compilation message

chameleon.cpp: In function 'void Solve(int)':
chameleon.cpp:39:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 |             if (p+1 >= L.size()) break;
      |                 ~~~~^~~~~~~~~~~
chameleon.cpp:41:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |             for (int j=p+1; j<L.size(); j++) {
      |                             ~^~~~~~~~~
chameleon.cpp:50:30: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   50 |                 int mid = lo + hi >> 1;
      |                           ~~~^~~~
chameleon.cpp:66:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   66 |             if (p+1 >= R.size()) break;
      |                 ~~~~^~~~~~~~~~~
chameleon.cpp:68:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |             for (int j=p+1; j<R.size(); j++) {
      |                             ~^~~~~~~~~
chameleon.cpp:77:30: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   77 |                 int mid = lo + hi >> 1;
      |                           ~~~^~~~
In file included from /usr/include/c++/10/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
                 from chameleon.cpp:2:
chameleon.cpp:123:21: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  123 |     assert(L.size() == N);
      |            ~~~~~~~~~^~~~
chameleon.cpp:131:30: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  131 |                 int mid = lo + hi >> 1;
      |                           ~~~^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 22 ms 3888 KB Output is correct
4 Correct 39 ms 3672 KB Output is correct
5 Correct 22 ms 3672 KB Output is correct
6 Correct 23 ms 4192 KB Output is correct
7 Correct 41 ms 3928 KB Output is correct
8 Correct 30 ms 3672 KB Output is correct
9 Correct 22 ms 3672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 1 ms 2648 KB Output is correct
11 Correct 1 ms 856 KB Output is correct
12 Correct 1 ms 856 KB Output is correct
13 Correct 1 ms 856 KB Output is correct
14 Correct 1 ms 856 KB Output is correct
15 Correct 1 ms 856 KB Output is correct
16 Correct 1 ms 856 KB Output is correct
17 Correct 1 ms 856 KB Output is correct
18 Correct 1 ms 856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 21 ms 4224 KB Output is correct
4 Correct 25 ms 4440 KB Output is correct
5 Correct 22 ms 4204 KB Output is correct
6 Correct 21 ms 4440 KB Output is correct
7 Correct 22 ms 4328 KB Output is correct
8 Correct 21 ms 4268 KB Output is correct
9 Correct 21 ms 4440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 22 ms 3888 KB Output is correct
4 Correct 39 ms 3672 KB Output is correct
5 Correct 22 ms 3672 KB Output is correct
6 Correct 23 ms 4192 KB Output is correct
7 Correct 41 ms 3928 KB Output is correct
8 Correct 30 ms 3672 KB Output is correct
9 Correct 22 ms 3672 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 1 ms 2648 KB Output is correct
20 Correct 1 ms 856 KB Output is correct
21 Correct 1 ms 856 KB Output is correct
22 Correct 1 ms 856 KB Output is correct
23 Correct 1 ms 856 KB Output is correct
24 Correct 1 ms 856 KB Output is correct
25 Correct 1 ms 856 KB Output is correct
26 Correct 1 ms 856 KB Output is correct
27 Correct 1 ms 856 KB Output is correct
28 Correct 0 ms 344 KB Output is correct
29 Correct 0 ms 344 KB Output is correct
30 Correct 21 ms 4224 KB Output is correct
31 Correct 25 ms 4440 KB Output is correct
32 Correct 22 ms 4204 KB Output is correct
33 Correct 21 ms 4440 KB Output is correct
34 Correct 22 ms 4328 KB Output is correct
35 Correct 21 ms 4268 KB Output is correct
36 Correct 21 ms 4440 KB Output is correct
37 Correct 21 ms 4440 KB Output is correct
38 Correct 23 ms 3672 KB Output is correct
39 Correct 22 ms 4440 KB Output is correct
40 Correct 22 ms 4432 KB Output is correct
41 Correct 40 ms 4432 KB Output is correct
42 Correct 22 ms 4184 KB Output is correct
43 Correct 25 ms 4432 KB Output is correct
44 Correct 23 ms 4432 KB Output is correct
45 Correct 21 ms 4440 KB Output is correct