#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;
short n, g[MAXN][MAXN], k;
int m;
set<pair<ll, pair<pair<short, short>, bool>>> sd;
vector<pair<ll, pair<pair<short, short>, bool>>> d;
bool v[MAXN][MAXN];
void recurse(short itr = 0, short 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;
}
Compilation message
domino.cpp: In function 'int main()':
domino.cpp:47:26: warning: comparison of integer expressions of different signedness: 'std::set<std::pair<long long int, std::pair<std::pair<short int, short int>, bool> > >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
47 | if(sd.size() < (k - 1) * 7 + 1){
| ~~~~~~~~~~^~~~~~~~~~~~~~~~~
domino.cpp:55:26: warning: comparison of integer expressions of different signedness: 'std::set<std::pair<long long int, std::pair<std::pair<short int, short int>, bool> > >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
55 | if(sd.size() < (k - 1) * 7 + 1){
| ~~~~~~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
6748 KB |
Output is correct |
2 |
Correct |
30 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 |
492 ms |
12144 KB |
Output is correct |
2 |
Correct |
475 ms |
12104 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 |
283 ms |
10840 KB |
Output is correct |
2 |
Correct |
235 ms |
10860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
123 ms |
8808 KB |
Output is correct |
2 |
Correct |
131 ms |
8776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
484 ms |
12116 KB |
Output is correct |
2 |
Correct |
425 ms |
12140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
2384 KB |
Output is correct |
2 |
Correct |
7 ms |
2556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
523 ms |
12104 KB |
Output is correct |
2 |
Correct |
530 ms |
12104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
274 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 |
816 ms |
11988 KB |
Output is correct |
2 |
Correct |
548 ms |
12140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3799 ms |
2620 KB |
Output is correct |
2 |
Correct |
735 ms |
2384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4035 ms |
8776 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
2384 KB |
Output is correct |
2 |
Correct |
29 ms |
2556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4062 ms |
12140 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |