#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.rbegin()).fs){
sd.erase(*sd.rbegin());
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.rbegin()).fs){
sd.erase(*sd.rbegin());
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;
}
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 |
32 ms |
8784 KB |
Output is correct |
2 |
Correct |
31 ms |
8776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
4432 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
503 ms |
19820 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
2384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
283 ms |
17016 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
133 ms |
12904 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
508 ms |
19964 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
11 ms |
4432 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
512 ms |
19828 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
104 ms |
6736 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2808 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
584 ms |
20068 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1263 ms |
4688 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1502 ms |
12872 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
2384 KB |
Output is correct |
2 |
Correct |
34 ms |
2384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1914 ms |
20072 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |