답안 #544111

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
544111 2022-04-01T04:42:15 Z Olympia Domino (COCI15_domino) C++17
10 / 160
1382 ms 524288 KB
#include <vector>
#include <algorithm>
#include <iostream>
#include <set>
#include <cmath>
#include <map>
#include <random>
#include <cassert>
#include <ctime>
#include <bitset>
#include <stack>
#include <cstdlib>
#include <queue>
#include <stdint.h>
#include <vector>
#include <cassert>
#include <numeric>
#include <iostream>
#include <algorithm>
#include <functional>
#include <cstdio>
#include <limits.h>

#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")

using namespace std;

struct Domino {
    pair<int, int> from, to;
    int64_t cost;

    bool operator<(const Domino &d1) const {
        if (this->cost != d1.cost) return (this->cost > d1.cost);
        if (this->from != d1.from) return (this->from < d1.from);
        if (this->to != d1.to) return (this->to < d1.to);
        return false;
    }
};

class Graph {
public:
    vector<int64_t> weight;
    vector<int> adj;
    vector<int64_t> dp;

    void add_edge(int u, int v) {
        //assert(!(adj[u] & (1 << v)));
        adj[u] += (1 << v);
        //adj[v] += (1 << u);
    }

    Graph(int n) {
        adj.resize(n);
        for (int i = 0; i < n; i++) {
            adj[i]= 0;
        }
    }

    void solve(int k, bool b) {
        dp.assign((1 << (int) adj.size()), 0);
        for (int i = 1; i < dp.size(); i++) {
            if (__builtin_popcount(i) > k) {
                continue;
            }
            int x = i;
            bool fine = true;
            dp[0] = true;
            while (x != 0) {
                if (!dp[i - (1 << __builtin_ctzll(x))]) {
                    fine = false;
                    break;
                }
                x = x & (x - 1);
            }
            dp[0] = false;
            if (!fine) {
                continue;
            }
            if (__builtin_popcount(i) == 2) {
                if (!(adj[(int)log2(i)] & (i & -i))) {
                    continue;
                }
            }
            dp[i] = dp[i - (1 << (int)log2(i))] + weight[(int)log2(i)];
        }
        if (b) return;
        for (int i = 1; i < dp.size(); i++) {
            int x = i;
            while (x != 0) {
                dp[i] = max(dp[i], dp[i - (1 << __builtin_ctzll(x))]);
                x = x & (x - 1);
            }
        }
    }
};

int main() {
    //freopen("balancing.in", "r", stdin);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n, m;
    cin >> n >> m;
    int64_t grid[n][n];
    int64_t sm = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> grid[i][j];
            sm += grid[i][j];
        }
    }
    vector<Domino> dominoes;
    vector<pair<int, int>> pos;
    pos.emplace_back(1, 0);
    pos.emplace_back(0, 1);
    for (int i = 0; i < n; i++) {
        for (auto &p: pos) {
            if (i + p.first < 0 || i + p.first == n) continue;
            for (int j = 0; j < n; j++) {
                if (j + p.second < 0 || j + p.second == n) continue;
                dominoes.push_back({{i, j}, {i + p.first, j + p.second}, grid[i][j] + grid[i + p.first][j + p.second]});

            }
        }
    }
    nth_element(dominoes.begin(), dominoes.begin() + min(max(7 * (m - 1) + 1, 7), (int)dominoes.size()), dominoes.end());
    dominoes.resize(min(max(7 * (m - 1) + 1, 7), (int)dominoes.size()));
    vector<pair<int64_t, pair<Domino, Domino>>> edges;
    for (auto &d1: dominoes) {
        for (auto &d2: dominoes) {
            set<pair<int, int>> s;
            s.insert(d1.from), s.insert(d1.to), s.insert(d2.from), s.insert(d2.to);
            if (s.size() != 4) continue;
            edges.push_back({d1.cost + d2.cost, {d1, d2}});
        }
    }
    dominoes.clear();
    set<Domino> mySet;
    for (auto &p: edges) {
        mySet.insert(p.second.first), mySet.insert(p.second.second);
    }
    map<Domino, int> myMap;
    int cntr = 0;
    vector<int64_t> weights;
    for (Domino d: mySet) {
        weights.push_back(d.cost);
        myMap[d] = cntr++;
    }
    Graph gr(cntr), gr1(cntr / 2), gr2(cntr - cntr / 2);
    gr.weight = weights;
    for (int i = 0; i < cntr / 2; i++) gr1.weight.push_back(weights[i]);
    for (int i = cntr / 2; i < cntr; i++) gr2.weight.push_back(weights[i]);
    for (auto &e: edges) {
        gr.add_edge(myMap[e.second.first], myMap[e.second.second]);
        if (myMap[e.second.first] < cntr / 2 && myMap[e.second.second] < cntr / 2)
            gr1.add_edge(myMap[e.second.second], myMap[e.second.first]);
        if (myMap[e.second.first] >= cntr / 2 && myMap[e.second.second] >= cntr / 2)
            gr2.add_edge(myMap[e.second.second] - cntr / 2, myMap[e.second.first] - cntr / 2);

    }
    gr1.solve(m, true);
    int64_t myMax = 0;
    vector<int> cnt[m + 1];
    for (int i = 1; i < gr1.dp.size(); i++) {
        if (gr1.dp[i] != 0) cnt[__builtin_popcount(i)].push_back(i);
    }
    for (int pc = 1; pc <= m; pc++) {
        gr2.solve(m - pc, false);
        for (int i: cnt[pc]) {
            vector<int> nodes;
            for (int j = 0; j < gr1.adj.size(); j++) {
                if (i & (1 << j)) {
                    nodes.push_back(j);
                }
            }
            int tot = 0;
            for (int j = 0; j < gr2.adj.size(); j++) {
                bool fine = true;
                for (int k: nodes) {
                    if (!gr.adj[j + cntr / 2] & (1 << k)) {
                        fine = false;
                        break;
                    }
                }
                if (fine) {
                    tot += (1 << j);
                }
            }
            myMax = max(gr2.dp[tot] + gr1.dp[i], myMax);
        }
    }
    gr1.solve(m, false);
    myMax = max(myMax, gr1.dp.back());
    cout << sm - myMax;
}

Compilation message

domino.cpp:25: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
   25 | #pragma GCC optimization ("O3")
      | 
domino.cpp:26: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
   26 | #pragma GCC optimization ("unroll-loops")
      | 
domino.cpp: In member function 'void Graph::solve(int, bool)':
domino.cpp:63:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   63 |         for (int i = 1; i < dp.size(); i++) {
      |                         ~~^~~~~~~~~~~
domino.cpp:89:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   89 |         for (int i = 1; i < dp.size(); i++) {
      |                         ~~^~~~~~~~~~~
domino.cpp: In function 'int main()':
domino.cpp:165:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  165 |     for (int i = 1; i < gr1.dp.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~
domino.cpp:172:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  172 |             for (int j = 0; j < gr1.adj.size(); j++) {
      |                             ~~^~~~~~~~~~~~~~~~
domino.cpp:178:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  178 |             for (int j = 0; j < gr2.adj.size(); j++) {
      |                             ~~^~~~~~~~~~~~~~~~
domino.cpp:181:25: warning: suggest parentheses around operand of '!' or change '&' to '&&' or '!' to '~' [-Wparentheses]
  181 |                     if (!gr.adj[j + cntr / 2] & (1 << k)) {
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 14648 KB Output is correct
2 Correct 32 ms 14672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 596 KB Output is correct
2 Incorrect 1 ms 596 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 564 ms 228632 KB Output is correct
2 Incorrect 481 ms 228636 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Incorrect 1 ms 340 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 330 ms 214940 KB Output is correct
2 Incorrect 289 ms 215016 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 133 ms 57412 KB Output is correct
2 Incorrect 128 ms 57500 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 522 ms 228624 KB Output is correct
2 Incorrect 451 ms 228628 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 45 ms 4756 KB Output is correct
2 Incorrect 42 ms 4756 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 584 ms 228708 KB Output is correct
2 Incorrect 526 ms 228648 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 817 ms 51528 KB Output is correct
2 Incorrect 753 ms 50316 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1382 ms 270068 KB Output is correct
2 Incorrect 1261 ms 268832 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 395 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 525 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 227 ms 16928 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 868 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -