Submission #1114915

# Submission time Handle Problem Language Result Execution time Memory
1114915 2024-11-19T18:32:43 Z codexistent Domino (COCI15_domino) C++14
140 / 160
4000 ms 35680 KB
#include <bits/stdc++.h>
using namespace std;
#define MAXN 2005
#define ll long long
#define FOR(i, a, b) for(int i = a; i <= b; ++i)
#define fs first
#define sc second

ll r, s = 0;
int n, k, g[MAXN][MAXN], m;
set<pair<ll, pair<pair<int, int>, bool>>> sd;
vector<pair<ll, pair<pair<int, int>, bool>>> d;
bool v[MAXN][MAXN];

void recurse(int itr = 0, int i = -1, ll sm = 0){
    if(itr == k){
        r = min(r, s - sm);
        return;
    }

    FOR(i2, i + 1, m - 1){
        if(v[d[i2].sc.fs.fs][d[i2].sc.fs.sc] || v[d[i2].sc.fs.fs + !d[i2].sc.sc][d[i2].sc.fs.sc + d[i2].sc.sc]){
            continue;
        }
        v[d[i2].sc.fs.fs][d[i2].sc.fs.sc] = true;
        v[d[i2].sc.fs.fs + !d[i2].sc.sc][d[i2].sc.fs.sc + d[i2].sc.sc] = true;

        recurse(itr + 1, i2, sm + d[i2].fs);

        v[d[i2].sc.fs.fs][d[i2].sc.fs.sc] = false;
        v[d[i2].sc.fs.fs + !d[i2].sc.sc][d[i2].sc.fs.sc + d[i2].sc.sc] = false;
    }
}

int main(){
    cin >> n >> k;
    FOR(i, 1, n) FOR(j, 1, n) {
        cin >> g[i][j];
        v[i][j] = false;
        s += g[i][j];
    }
    r = s;

    FOR(i, 1, n) FOR(j, 1, n) {
        if(i != n) {
            if(sd.size() < (k - 1) * 7 + 1){
                sd.insert({g[i][j] + g[i + 1][j], {{i, j}, 0}});
            }else if(g[i][j] + g[i + 1][j] > (*sd.begin()).fs){
                sd.erase(*sd.begin());
                sd.insert({g[i][j] + g[i + 1][j], {{i, j}, 0}});
            }
        }
        if(j != n) {
            if(sd.size() < (k - 1) * 7 + 1){
                sd.insert({g[i][j] + g[i][j + 1], {{i, j}, 1}});
            }else if(g[i][j] + g[i][j + 1] > (*sd.begin()).fs){
                sd.erase(*sd.begin());
                sd.insert({g[i][j] + g[i][j + 1], {{i, j}, 1}});
            }
        }
    }
    for(auto i : sd) {
        d.push_back(i);
    }

    m = sd.size();
    recurse();

    cout << r << endl;
}

/*
5
6
6
8
9
9
9
10
*/

Compilation message

domino.cpp: In function 'int main()':
domino.cpp:46:26: warning: comparison of integer expressions of different signedness: 'std::set<std::pair<long long int, std::pair<std::pair<int, int>, bool> > >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   46 |             if(sd.size() < (k - 1) * 7 + 1){
      |                ~~~~~~~~~~^~~~~~~~~~~~~~~~~
domino.cpp:54:26: warning: comparison of integer expressions of different signedness: 'std::set<std::pair<long long int, std::pair<std::pair<int, int>, bool> > >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   54 |             if(sd.size() < (k - 1) * 7 + 1){
      |                ~~~~~~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 35 ms 9764 KB Output is correct
2 Correct 33 ms 9812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4432 KB Output is correct
2 Correct 1 ms 4432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 496 ms 35144 KB Output is correct
2 Correct 478 ms 35656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2384 KB Output is correct
2 Correct 1 ms 2384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 272 ms 25308 KB Output is correct
2 Correct 282 ms 23368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 120 ms 16712 KB Output is correct
2 Correct 123 ms 16784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 511 ms 35044 KB Output is correct
2 Correct 444 ms 31692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 4432 KB Output is correct
2 Correct 9 ms 4432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 562 ms 35052 KB Output is correct
2 Correct 580 ms 35668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 262 ms 6736 KB Output is correct
2 Correct 63 ms 6736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2384 KB Output is correct
2 Correct 1 ms 2384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 805 ms 35040 KB Output is correct
2 Correct 591 ms 35680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3745 ms 4692 KB Output is correct
2 Correct 723 ms 4688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 4040 ms 16480 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 31 ms 2384 KB Output is correct
2 Correct 28 ms 2384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 4069 ms 35144 KB Time limit exceeded
2 Halted 0 ms 0 KB -