Submission #1038898

# Submission time Handle Problem Language Result Execution time Memory
1038898 2024-07-30T08:47:33 Z vjudge1 Domino (COCI15_domino) C++17
30 / 160
4000 ms 196744 KB
#include <bits/stdc++.h>
using namespace std;

#define F first
#define S second

typedef pair<int, int> pii;

const int N = 2e3 + 10, K = 8;
int n, k, mat[N][N], exist[N][N], mark[N][N];
vector<pii> cells, my_cells;

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

bool done;

long long ans, total;

void dfs(int i){
    if (i >= my_cells.size()){
        done = 1;
        return;
    }
    auto p = my_cells[i];
    if (mark[p.F][p.S]){
        dfs(i + 1);
        return;
    }

    mark[p.F][p.S] = 1;
    for (int i = 0; i < 4; i ++){
        int nx = p.F + dx[i];
        int ny = p.S + dy[i];

        if (!exist[nx][ny]) continue;
        if (mark[nx][ny]) continue;

        mark[nx][ny] = 1;
        dfs(i + 1);        
        mark[nx][ny] = 0;
    }
    mark[p.F][p.S] = 0;
}

int main(){
    cin >> n >> k;
    for (int i = 1; i <= n; i ++)
        for (int j = 1; j <= n; j ++)
            cin >> mat[i][j], total += mat[i][j];

    vector<pair<int, pair<pii, pii>>> vec;
    for (int i = 1; i <= n; i ++){
        for (int j = 1; j <= n; j ++){
            if (j + 1 <= n)
                vec.push_back({mat[i][j] + mat[i][j + 1], {{i, j}, {i, j + 1}}});
            if (i + 1 <= n)
                vec.push_back({mat[i][j] + mat[i + 1][j], {{i, j}, {i + 1, j}}});
        }
    }
    sort(vec.begin(), vec.end());

    int mn = 4 * k;
    set<pii> st;
    while (vec.size() and st.size() < mn){
        st.insert((vec.back()).S.F);
        st.insert((vec.back()).S.S);
        vec.pop_back();
    }

    for (auto p : st){
        cells.push_back(p);
    }

    ans = total;
    int sz = cells.size();
    for (int mask = 0; mask < (1 << sz); mask ++){
        int c = __builtin_popcount(mask);
        if (c != 2 * k) continue;

        my_cells.clear();
        long long sm = 0;
        for (int i = 0; i < sz; i ++){
            if ((1 << i) & mask){
                my_cells.push_back(cells[i]);
                sm += mat[cells[i].F][cells[i].S];
            }
        }

        for (auto p : my_cells)
            exist[p.F][p.S] = 1;

        done = 0;
        dfs(0);

        for (auto p : my_cells)
            exist[p.F][p.S] = 0;

        if (done)
            ans = min(ans, total - sm);
    }

    cout << ans << endl;
}

Compilation message

domino.cpp: In function 'void dfs(int)':
domino.cpp:21:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   21 |     if (i >= my_cells.size()){
      |         ~~^~~~~~~~~~~~~~~~~~
domino.cpp: In function 'int main()':
domino.cpp:65:37: warning: comparison of integer expressions of different signedness: 'std::set<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   65 |     while (vec.size() and st.size() < mn){
      |                           ~~~~~~~~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 89 ms 14716 KB Output is correct
2 Correct 83 ms 19368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Incorrect 1 ms 860 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1642 ms 195488 KB Output is correct
2 Incorrect 1194 ms 196112 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Incorrect 0 ms 344 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1112 ms 185072 KB Output is correct
2 Incorrect 893 ms 183784 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 379 ms 53940 KB Output is correct
2 Incorrect 343 ms 53944 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1619 ms 196324 KB Output is correct
2 Incorrect 1458 ms 192652 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 531 ms 4912 KB Output is correct
2 Incorrect 281 ms 4664 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1758 ms 196240 KB Output is correct
2 Incorrect 1609 ms 196744 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 4061 ms 5156 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 4053 ms 196168 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 2824 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 372 ms 56440 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 714 ms 4440 KB Output is correct
2 Correct 703 ms 4548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1516 ms 196192 KB Output isn't correct
2 Halted 0 ms 0 KB -