Submission #303623

# Submission time Handle Problem Language Result Execution time Memory
303623 2020-09-20T13:33:27 Z alex_velea Counting Mushrooms (IOI20_mushrooms) C++17
10 / 100
1735 ms 416 KB
#include <bitset>
#include <array>
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

#define DEBUG if(0)

int count_mushrooms(int n);

int use_machine(vector<int> x);

const int kMaxLen = 8;

struct Solver {
    int start, n;
    bool valid[1 << kMaxLen];

    Solver(int start, int n) : start(start), n(n) {
        for (int i = 0; i < (1 << n); i += 1) {
            valid[i] = true;
        }
    }

    void FixFirst() {
        int firstEl = use_machine({0, start});
        for (int i = 0; i < (1 << n); i += 1) {
            valid[i] = (i & 1) == firstEl;
        }
    }
    
    int GetCount(int mask) {
        int ans = 0;
        for (int i = 0; i < n; i += 1) {
            cerr << (!!(mask & (1 << i)));
            ans += (mask & (1 << i)) == 0;
        }
        return ans;
    }

    int GetCost(int state, vector<int> order) {
        int cost = 0;
        for (int i = 1; i < (int)order.size(); i += 1) {
            cost += (!!(state & (1 << order[i - 1]))) != (!!(state & (1 << order[i]))); 
        }
        return cost;
    }

    vector<int> GetBestMove() {
        vector<int> best;
        int best_cost = (1 << n);
        vector<int> current_order = {};
        for (int i = 0; i < n; i += 1) {
            current_order.push_back(i);
        }

        for (int i = 0; i < (50); i += 1) {
            random_shuffle(current_order.begin(), current_order.end());
            
            array<int, kMaxLen> freq;
            for (int i = 0; i < kMaxLen; i += 1) {
                freq[i] = 0;
            }

            for (int i = 0; i < (1 << n); i += 1) {
                if (valid[i]) {
                    int cost = GetCost(i, current_order);
                    DEBUG cerr << cost << ' ';
                    freq[cost] += 1;
                }
            }

            int mx = 0;
            for (auto& itr : freq) {
                mx = max(mx, itr);
            }

            DEBUG cerr << "Current max = " << mx << '\n';

            if (best_cost > mx) {
                mx = best_cost;
                best = current_order;
            }
        }
        return best;
    }

    int Solve() {
        DEBUG cerr << "Solve .. " << start << '\t' << n << '\n';
        FixFirst();
        vector<int> available;
        while (1) {
            DEBUG cerr << "Run .. \n";
            available.clear();
            for (int i = 0; i < (1 << n); i += 1) {
                if (valid[i]) {
                    bitset<kMaxLen> b(i);
                    DEBUG cerr << "Ok .. " << b << "\n";
                    available.push_back(i);
                }
            }

            if (available.size() == 1) {
                return GetCount(available[0]); 
            }

            auto best = GetBestMove();
            DEBUG {
                cerr << "Best = \t";
                for (auto& itr : best) {
                    cerr << itr << '\t';
                }
                cerr << '\n';
            }
            auto q = best;
            for (auto& itr : q) {
                itr += start;
            }

            int ans = use_machine(q);
            DEBUG cerr << "Ans = " << ans << '\n';
            for (int i = 0; i < (1 << n); i += 1) {
                if (valid[i] and GetCost(i, best) != ans) {
                    valid[i] = false;
                }
            }
        }
    }
};

int count_mushrooms(int n) {
    int ans = 1;
    for (int i = 1; i < n; i += kMaxLen) {
        Solver s(i, min(kMaxLen, n - i));
        ans += s.Solve();
    }
    DEBUG cerr << "ans = " << ans << '\n';
    cerr << '\n';
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 14 ms 256 KB Output is correct
6 Partially correct 145 ms 256 KB Output is partially correct
7 Partially correct 888 ms 388 KB Output is partially correct
8 Partially correct 1000 ms 392 KB Output is partially correct
9 Partially correct 1166 ms 388 KB Output is partially correct
10 Partially correct 1402 ms 392 KB Output is partially correct
11 Partially correct 1446 ms 388 KB Output is partially correct
12 Partially correct 1459 ms 388 KB Output is partially correct
13 Partially correct 1163 ms 388 KB Output is partially correct
14 Partially correct 738 ms 380 KB Output is partially correct
15 Partially correct 1570 ms 396 KB Output is partially correct
16 Partially correct 1735 ms 392 KB Output is partially correct
17 Partially correct 681 ms 380 KB Output is partially correct
18 Partially correct 1180 ms 392 KB Output is partially correct
19 Partially correct 1502 ms 392 KB Output is partially correct
20 Partially correct 1504 ms 380 KB Output is partially correct
21 Partially correct 1515 ms 392 KB Output is partially correct
22 Partially correct 1499 ms 384 KB Output is partially correct
23 Partially correct 1434 ms 388 KB Output is partially correct
24 Partially correct 745 ms 380 KB Output is partially correct
25 Partially correct 1622 ms 396 KB Output is partially correct
26 Partially correct 1717 ms 396 KB Output is partially correct
27 Partially correct 1490 ms 380 KB Output is partially correct
28 Partially correct 1484 ms 396 KB Output is partially correct
29 Partially correct 1454 ms 396 KB Output is partially correct
30 Partially correct 1429 ms 392 KB Output is partially correct
31 Partially correct 1437 ms 396 KB Output is partially correct
32 Partially correct 1527 ms 392 KB Output is partially correct
33 Partially correct 1590 ms 392 KB Output is partially correct
34 Partially correct 1605 ms 400 KB Output is partially correct
35 Partially correct 1502 ms 392 KB Output is partially correct
36 Partially correct 1555 ms 392 KB Output is partially correct
37 Partially correct 1398 ms 392 KB Output is partially correct
38 Partially correct 1520 ms 392 KB Output is partially correct
39 Partially correct 1523 ms 392 KB Output is partially correct
40 Partially correct 1490 ms 396 KB Output is partially correct
41 Partially correct 1600 ms 400 KB Output is partially correct
42 Partially correct 1454 ms 392 KB Output is partially correct
43 Partially correct 1447 ms 400 KB Output is partially correct
44 Partially correct 1560 ms 392 KB Output is partially correct
45 Partially correct 1459 ms 388 KB Output is partially correct
46 Partially correct 1551 ms 388 KB Output is partially correct
47 Partially correct 1569 ms 392 KB Output is partially correct
48 Partially correct 1436 ms 396 KB Output is partially correct
49 Partially correct 1465 ms 400 KB Output is partially correct
50 Partially correct 1492 ms 388 KB Output is partially correct
51 Partially correct 1454 ms 392 KB Output is partially correct
52 Partially correct 1484 ms 392 KB Output is partially correct
53 Partially correct 1615 ms 400 KB Output is partially correct
54 Partially correct 1434 ms 388 KB Output is partially correct
55 Partially correct 1444 ms 392 KB Output is partially correct
56 Partially correct 1529 ms 392 KB Output is partially correct
57 Partially correct 1435 ms 396 KB Output is partially correct
58 Partially correct 1458 ms 392 KB Output is partially correct
59 Partially correct 1615 ms 400 KB Output is partially correct
60 Partially correct 1233 ms 392 KB Output is partially correct
61 Partially correct 1165 ms 384 KB Output is partially correct
62 Correct 1 ms 256 KB Output is correct
63 Correct 1 ms 256 KB Output is correct
64 Correct 1 ms 256 KB Output is correct
65 Correct 1 ms 256 KB Output is correct
66 Correct 1 ms 256 KB Output is correct
67 Correct 1 ms 360 KB Output is correct
68 Correct 1 ms 256 KB Output is correct
69 Correct 1 ms 380 KB Output is correct
70 Correct 1 ms 256 KB Output is correct
71 Correct 1 ms 256 KB Output is correct
72 Correct 1 ms 256 KB Output is correct
73 Correct 1 ms 256 KB Output is correct
74 Correct 1 ms 256 KB Output is correct
75 Correct 1 ms 384 KB Output is correct
76 Correct 1 ms 256 KB Output is correct
77 Correct 1 ms 256 KB Output is correct
78 Correct 1 ms 256 KB Output is correct
79 Correct 1 ms 256 KB Output is correct
80 Correct 1 ms 256 KB Output is correct
81 Correct 1 ms 256 KB Output is correct
82 Correct 1 ms 256 KB Output is correct
83 Correct 1 ms 416 KB Output is correct
84 Correct 1 ms 256 KB Output is correct
85 Correct 1 ms 384 KB Output is correct
86 Correct 1 ms 384 KB Output is correct
87 Correct 1 ms 256 KB Output is correct
88 Correct 1 ms 256 KB Output is correct
89 Correct 1 ms 256 KB Output is correct
90 Correct 1 ms 256 KB Output is correct
91 Correct 1 ms 256 KB Output is correct