답안 #544072

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
544072 2022-04-01T01:11:19 Z Olympia Domino (COCI15_domino) C++17
110 / 160
2657 ms 251708 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 <cstdio>
#include <limits.h>
#pragma GCC target ("avx2")
#pragma GCC optimization ("Ofast")
#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<vector<int>> adj;
    vector<int64_t> dp;
    void add_edge (int u, int v) {
        adj[u][v] = adj[v][u] = 1;
    }
    Graph (int n) {
        adj.resize(n);
        for (int i = 0; i < n; i++) {
            adj[i].assign(n, 0);
        }
    }
    void solve (int k, bool b) {
        dp.clear();
        dp.assign((1 << (int)adj.size()), 0);
        for (int i = 0; i < dp.size(); i++) {
            if (__builtin_popcount(i) > k) {
                continue;
            }
            vector<int> nodes;
            for (int j = 0; j < adj.size(); j++) {
                if (i & (1 << j)) {
                    nodes.push_back(j);
                }
            }
            bool fine = true;
            for (int x: nodes) {
                for (int y: nodes) {
                    if (x == y) continue;
                    if (!adj[x][y]) {
                        fine = false;
                    }
                }
            }
            if (!fine) {
                continue;
            }
            for (int x: nodes) {
                dp[i] += weight[x];
            }
        }
        if (b) return;
        for (int i = 1; i < dp.size(); i++) { 
            for (int j = 0; j < adj.size(); j++) {
                if (i & (1 << j)) {
                    dp[i] = max(dp[i], dp[i - (1 << j)]); 
                }
            }
        }
    }
};
 
int main() {
    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;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            for (int dx = -1; dx <= 1; dx++) {
                for (int dy = -1; dy <= 1; dy++) {
                    if (i + dx < 0 || j + dy < 0) continue;
                    if (i + dx == n || j + dy == n) continue;
                    if (abs(dx) + abs(dy) != 1) continue;
                    if (dx < dy) continue;
                    dominoes.push_back({{i, j}, {i + dx,j + dy}, grid[i][j] + grid[i + dx][j + dy]});
                }
            }
        }
    }
    sort(dominoes.begin(), dominoes.end());
    while (dominoes.size() > min(7 * (m - 1) + 5, 40)) {
        dominoes.pop_back();
    }
    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;
            //cout << d1.from.first << " " << d1.from.seco
            edges.push_back({d1.cost + d2.cost, {d1, d2}});
        }
    }
    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][k]) {
                        fine = false;
                        break;
                    }
                }
                if (fine) {
                    tot += (1 << j);
                }
            }
            myMax = max(gr2.dp[tot] + gr1.dp[i], myMax);
        }
    }
    gr2.solve(m, false), gr1.solve(m, false);
    myMax = max(myMax, max(gr1.dp.back(), gr2.dp.back()));
    cout << sm - myMax;
}

Compilation message

domino.cpp:18: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
   18 | #pragma GCC optimization ("Ofast")
      | 
domino.cpp:19: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
   19 | #pragma GCC optimization ("unroll-loops")
      | 
domino.cpp: In member function 'void Graph::solve(int, bool)':
domino.cpp:51:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |         for (int i = 0; i < dp.size(); i++) {
      |                         ~~^~~~~~~~~~~
domino.cpp:56:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   56 |             for (int j = 0; j < adj.size(); j++) {
      |                             ~~^~~~~~~~~~~~
domino.cpp:78:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   78 |         for (int i = 1; i < dp.size(); i++) {
      |                         ~~^~~~~~~~~~~
domino.cpp:79:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   79 |             for (int j = 0; j < adj.size(); j++) {
      |                             ~~^~~~~~~~~~~~
domino.cpp: In function 'int main()':
domino.cpp:116:28: warning: comparison of integer expressions of different signedness: 'std::vector<Domino>::size_type' {aka 'long unsigned int'} and 'const int' [-Wsign-compare]
  116 |     while (dominoes.size() > min(7 * (m - 1) + 5, 40)) {
      |            ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
domino.cpp:151:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  151 |     for (int i = 1; i < gr1.dp.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~
domino.cpp:158:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  158 |             for (int j = 0; j < gr1.adj.size(); j++) {
      |                             ~~^~~~~~~~~~~~~~~~
domino.cpp:164:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  164 |             for (int j = 0; j < gr2.adj.size(); j++) {
      |                             ~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 95 ms 14648 KB Output is correct
2 Correct 73 ms 14660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 596 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1795 ms 228680 KB Output is correct
2 Correct 1332 ms 228640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1048 ms 215072 KB Output is correct
2 Correct 733 ms 214924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 433 ms 57472 KB Output is correct
2 Correct 320 ms 57472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1796 ms 228724 KB Output is correct
2 Correct 1349 ms 228836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 544 ms 17336 KB Output is correct
2 Correct 528 ms 17164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2255 ms 236296 KB Output is correct
2 Correct 1852 ms 235912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 688 ms 18280 KB Output is correct
2 Incorrect 662 ms 17480 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 440 KB Output is correct
2 Correct 6 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2452 ms 236676 KB Output is correct
2 Incorrect 1983 ms 235988 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 884 ms 18164 KB Output is correct
2 Incorrect 843 ms 17324 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1295 ms 73560 KB Output is correct
2 Incorrect 1140 ms 75624 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 842 ms 16928 KB Output is correct
2 Correct 812 ms 17036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2657 ms 237852 KB Output is correct
2 Incorrect 2173 ms 251708 KB Output isn't correct
3 Halted 0 ms 0 KB -