답안 #1043212

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1043212 2024-08-04T04:52:19 Z model_code Light Bulbs (EGOI24_lightbulbs) C++17
100 / 100
984 ms 1100 KB
#include <bits/stdc++.h>
#include <random>
using namespace std;

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define debug(...) // ignore
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long double ld;

const int MAXN = 105;
int cnt[MAXN*MAXN];
mt19937 rng(31);

int main() {
  int n;
  cin >> n;

  auto printgrid = [&](vector<pii> v) {
    vector<string> grid(n,string(n,'0'));
    for(auto [a,b] : v) grid[a][b] = '1';
    for(auto g : grid) cout << g << endl;
  };
  auto query = [&](vector<pii> v) {
    cout << "?" << endl;
    printgrid(v);
    int x;
    cin >> x;
    return x;
  };
  auto answer = [&](vector<pii> v) {
    assert(sz(v) == n);
    cout << "!" << endl;
    printgrid(v);
    exit(0);
  };


  vector<pii> h, v;
  vi rows(n), cols(n);
  iota(all(rows),0);
  iota(all(cols),0);
  auto know = [&](pii p, char c) {
    if(c == 'h' && count(all(rows),p.first)) {
      h.emplace_back(p);
      rows.erase(find(all(rows),p.first));
    }
    if(c == 'v' && count(all(cols),p.second)) {
      v.emplace_back(p);
      cols.erase(find(all(cols),p.second));
    }
    if(sz(rows) == 0) answer(h);
    if(sz(cols) == 0) answer(v);
  };



  using bs = bitset<205>;
  int k = 0;

  vector<pii> ca;
  vector<bs> valid;
  valid.emplace_back(0);
  vi active_row(n), active_col(n);

  while(true) {
    rep(b,0,k) {
      int c0 = 0, c1 = 0;
      for(auto a : valid) {
        if(a[b]) ++c1;
        else ++c0;
      }
      assert(c0 + c1);
      if(c1 == 0) know(ca[b],'v');
      if(c0 == 0) know(ca[b],'h');
      if(c0 == 0 || c1 == 0) {
        --active_row[ca[b].first];
        --active_col[ca[b].second];
        ca.erase(begin(ca)+b);
        for(auto &a : valid) rep(i,b,k) a[i] = a[i+1];
        --k;
        --b;
        unordered_set<bs> vv(all(valid));
        valid = vector<bs>(all(vv));
      }
    }


    while(sz(valid) < 1e3 && k < sz(rows) + sz(cols)) {
      shuffle(all(rows),rng);
      shuffle(all(cols),rng);
      //sort(all(rows),[&](int a, int b){return active_row[a] < active_row[b];});
      //sort(all(cols),[&](int a, int bd){return active_col[a] < active_col[b];});
      if(count(all(ca),pii(rows[0],cols[0]))) break;
      ca.emplace_back(rows[0],cols[0]);
      ++active_row[rows[0]];
      ++active_col[cols[0]];
      vector<bs> valid2;
      for(auto a : valid) {
        a[k] = 0;
        valid2.emplace_back(a);
        a[k] = 1;
        valid2.emplace_back(a);
      }
      swap(valid,valid2);
      ++k;
    }

    auto simulate = [&](bs a, bs on, int use_h) {
      vi vert(n), horz(n);
      rep(i,0,k) if(on[i]) {
        if(a[i]) horz[ca[i].first] = 1;
        else vert[ca[i].second] = 1;
      }
      if(use_h) for(auto [r,c] : h) horz[r] = 1;
      else for(auto [r,c] : v) vert[c] = 1;
      int vv = count(all(vert),1), hh = count(all(horz),1);
      return n*(vv+hh) - vv*hh;
    };

    ll best = 1e18;
    bs bestmsk = 0;
    int best_use_h = 0;
    vi probs = {100,90,80,70,60,40,30,20,10};
    while(sz(probs) < 50) probs.emplace_back(rng()%70+20);
    for(int prob : probs) {
      int use_h = rng()%2;
      bs on;
      rep(i,0,k) on[i] = (rng()%100 < prob);
      memset(cnt,0,sizeof cnt);
      for(auto a : valid) ++cnt[simulate(a,on,use_h)];
      ll mx = *max_element(all(cnt)); // try different heuristic, something like entropy?
      if(mx < best) {
        best = mx;
        bestmsk = on;
        best_use_h = use_h;
      }
    }
    vector<pii> ca2 = (best_use_h ? h : v);
    rep(i,0,k) if(bestmsk[i]) ca2.emplace_back(ca[i]);
    int t = query(ca2);
    vector<bs> valid2;
    for(auto a : valid) if(simulate(a,bestmsk,best_use_h) == t) valid2.emplace_back(a);
    swap(valid,valid2);
    assert(sz(valid));
  }
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:132:37: warning: comparison of integer expressions of different signedness: 'std::mersenne_twister_engine<long unsigned int, 32, 624, 397, 31, 2567483615, 11, 4294967295, 7, 2636928640, 15, 4022730752, 18, 1812433253>::result_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  132 |       rep(i,0,k) on[i] = (rng()%100 < prob);
      |                           ~~~~~~~~~~^~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 344 KB Output is correct
2 Correct 2 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 3 ms 344 KB Output is correct
5 Correct 3 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 2 ms 344 KB Output is correct
8 Correct 3 ms 344 KB Output is correct
9 Correct 3 ms 480 KB Output is correct
10 Correct 2 ms 344 KB Output is correct
11 Correct 2 ms 344 KB Output is correct
12 Correct 2 ms 344 KB Output is correct
13 Correct 3 ms 344 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 2 ms 344 KB Output is correct
16 Correct 4 ms 344 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 2 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 3 ms 344 KB Output is correct
5 Correct 3 ms 480 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 2 ms 472 KB Output is correct
8 Correct 4 ms 344 KB Output is correct
9 Correct 2 ms 344 KB Output is correct
10 Correct 2 ms 344 KB Output is correct
11 Correct 3 ms 344 KB Output is correct
12 Correct 2 ms 344 KB Output is correct
13 Correct 2 ms 344 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 2 ms 344 KB Output is correct
16 Correct 2 ms 344 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 9 ms 600 KB Output is correct
19 Correct 4 ms 344 KB Output is correct
20 Correct 13 ms 664 KB Output is correct
21 Correct 16 ms 640 KB Output is correct
22 Correct 13 ms 672 KB Output is correct
23 Correct 14 ms 600 KB Output is correct
24 Correct 10 ms 572 KB Output is correct
25 Correct 18 ms 600 KB Output is correct
26 Correct 8 ms 580 KB Output is correct
27 Correct 16 ms 600 KB Output is correct
28 Correct 21 ms 652 KB Output is correct
29 Correct 19 ms 640 KB Output is correct
30 Correct 4 ms 344 KB Output is correct
31 Correct 12 ms 676 KB Output is correct
32 Correct 8 ms 600 KB Output is correct
33 Correct 11 ms 600 KB Output is correct
34 Correct 9 ms 648 KB Output is correct
35 Correct 4 ms 344 KB Output is correct
36 Correct 8 ms 580 KB Output is correct
37 Correct 19 ms 652 KB Output is correct
38 Correct 11 ms 600 KB Output is correct
39 Correct 13 ms 600 KB Output is correct
40 Correct 15 ms 600 KB Output is correct
41 Correct 19 ms 676 KB Output is correct
42 Correct 18 ms 600 KB Output is correct
43 Correct 21 ms 636 KB Output is correct
44 Correct 18 ms 680 KB Output is correct
45 Correct 18 ms 672 KB Output is correct
46 Correct 7 ms 344 KB Output is correct
47 Correct 5 ms 344 KB Output is correct
48 Correct 9 ms 600 KB Output is correct
49 Correct 8 ms 344 KB Output is correct
50 Correct 5 ms 568 KB Output is correct
51 Correct 6 ms 344 KB Output is correct
52 Correct 9 ms 508 KB Output is correct
53 Correct 14 ms 648 KB Output is correct
54 Correct 10 ms 648 KB Output is correct
55 Correct 15 ms 664 KB Output is correct
56 Correct 15 ms 344 KB Output is correct
57 Correct 26 ms 664 KB Output is correct
58 Correct 19 ms 648 KB Output is correct
59 Correct 10 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 4 ms 344 KB Output is correct
5 Correct 4 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 3 ms 344 KB Output is correct
8 Correct 2 ms 344 KB Output is correct
9 Correct 2 ms 344 KB Output is correct
10 Correct 3 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 2 ms 596 KB Output is correct
13 Correct 4 ms 344 KB Output is correct
14 Correct 2 ms 344 KB Output is correct
15 Correct 2 ms 344 KB Output is correct
16 Correct 2 ms 344 KB Output is correct
17 Correct 2 ms 344 KB Output is correct
18 Correct 11 ms 676 KB Output is correct
19 Correct 2 ms 344 KB Output is correct
20 Correct 18 ms 644 KB Output is correct
21 Correct 17 ms 600 KB Output is correct
22 Correct 14 ms 600 KB Output is correct
23 Correct 15 ms 612 KB Output is correct
24 Correct 14 ms 600 KB Output is correct
25 Correct 18 ms 656 KB Output is correct
26 Correct 11 ms 576 KB Output is correct
27 Correct 18 ms 644 KB Output is correct
28 Correct 21 ms 600 KB Output is correct
29 Correct 17 ms 652 KB Output is correct
30 Correct 5 ms 600 KB Output is correct
31 Correct 14 ms 672 KB Output is correct
32 Correct 8 ms 600 KB Output is correct
33 Correct 14 ms 600 KB Output is correct
34 Correct 13 ms 652 KB Output is correct
35 Correct 5 ms 344 KB Output is correct
36 Correct 9 ms 500 KB Output is correct
37 Correct 20 ms 656 KB Output is correct
38 Correct 12 ms 648 KB Output is correct
39 Correct 13 ms 672 KB Output is correct
40 Correct 16 ms 656 KB Output is correct
41 Correct 21 ms 676 KB Output is correct
42 Correct 18 ms 584 KB Output is correct
43 Correct 21 ms 652 KB Output is correct
44 Correct 16 ms 600 KB Output is correct
45 Correct 14 ms 676 KB Output is correct
46 Correct 6 ms 344 KB Output is correct
47 Correct 6 ms 344 KB Output is correct
48 Correct 11 ms 648 KB Output is correct
49 Correct 8 ms 344 KB Output is correct
50 Correct 5 ms 344 KB Output is correct
51 Correct 5 ms 568 KB Output is correct
52 Correct 7 ms 344 KB Output is correct
53 Correct 18 ms 664 KB Output is correct
54 Correct 11 ms 600 KB Output is correct
55 Correct 14 ms 580 KB Output is correct
56 Correct 19 ms 344 KB Output is correct
57 Correct 27 ms 600 KB Output is correct
58 Correct 16 ms 648 KB Output is correct
59 Correct 116 ms 584 KB Output is correct
60 Correct 129 ms 588 KB Output is correct
61 Correct 410 ms 664 KB Output is correct
62 Correct 501 ms 788 KB Output is correct
63 Correct 693 ms 696 KB Output is correct
64 Correct 813 ms 672 KB Output is correct
65 Correct 872 ms 680 KB Output is correct
66 Correct 984 ms 700 KB Output is correct
67 Correct 927 ms 680 KB Output is correct
68 Correct 928 ms 796 KB Output is correct
69 Correct 748 ms 680 KB Output is correct
70 Correct 400 ms 660 KB Output is correct
71 Correct 421 ms 668 KB Output is correct
72 Correct 335 ms 680 KB Output is correct
73 Correct 198 ms 560 KB Output is correct
74 Correct 303 ms 668 KB Output is correct
75 Correct 246 ms 668 KB Output is correct
76 Correct 314 ms 668 KB Output is correct
77 Correct 208 ms 648 KB Output is correct
78 Correct 537 ms 840 KB Output is correct
79 Correct 602 ms 680 KB Output is correct
80 Correct 280 ms 660 KB Output is correct
81 Correct 912 ms 916 KB Output is correct
82 Correct 838 ms 700 KB Output is correct
83 Correct 834 ms 732 KB Output is correct
84 Correct 863 ms 720 KB Output is correct
85 Correct 814 ms 696 KB Output is correct
86 Correct 190 ms 612 KB Output is correct
87 Correct 146 ms 656 KB Output is correct
88 Correct 156 ms 652 KB Output is correct
89 Correct 124 ms 580 KB Output is correct
90 Correct 222 ms 1100 KB Output is correct
91 Correct 138 ms 568 KB Output is correct
92 Correct 806 ms 888 KB Output is correct
93 Correct 903 ms 680 KB Output is correct
94 Correct 862 ms 672 KB Output is correct
95 Correct 920 ms 704 KB Output is correct
96 Correct 839 ms 716 KB Output is correct
97 Correct 6 ms 344 KB Output is correct