#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
typedef long long ll;
typedef pair<ll, ll> pii;
const ll N = 2e3 + 10, K = 8;
ll n, k, mat[N][N], exist[N][N], mark[N][N];
vector<pii> cells, my_cells;
ll dx[4] = {0, 1, 0, -1};
ll dy[4] = {1, 0, -1, 0};
bool done;
long long ans, total;
void dfs(ll 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 (ll i = 0; i < 4; i ++){
ll nx = p.F + dx[i];
ll 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 (ll i = 1; i <= n; i ++)
for (ll j = 1; j <= n; j ++)
cin >> mat[i][j], total += mat[i][j];
vector<pair<ll, pair<pii, pii>>> vec;
for (ll i = 1; i <= n; i ++){
for (ll 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());
ll mn = 2 * 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;
ll sz = cells.size();
for (ll mask = 0; mask < (1 << sz); mask ++){
ll c = __builtin_popcount(mask);
if (c != 2 * k) continue;
my_cells.clear();
long long sm = 0;
for (ll 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(ll)':
domino.cpp:22:11: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | if (i >= my_cells.size()){
| ~~^~~~~~~~~~~~~~~~~~
domino.cpp: In function 'int main()':
domino.cpp:66:37: warning: comparison of integer expressions of different signedness: 'std::set<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
66 | while (vec.size() and st.size() < mn){
| ~~~~~~~~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
91 ms |
33444 KB |
Output is correct |
2 |
Correct |
84 ms |
33720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6808 KB |
Output is correct |
2 |
Incorrect |
2 ms |
6804 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1657 ms |
375652 KB |
Output is correct |
2 |
Incorrect |
1466 ms |
376128 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
963 ms |
363316 KB |
Output is correct |
2 |
Incorrect |
1014 ms |
361460 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
371 ms |
104608 KB |
Output is correct |
2 |
Incorrect |
401 ms |
104880 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2336 ms |
375560 KB |
Output is correct |
2 |
Incorrect |
1899 ms |
372404 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6808 KB |
Output is correct |
2 |
Incorrect |
2 ms |
6808 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1648 ms |
375508 KB |
Output is correct |
2 |
Incorrect |
1671 ms |
374432 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7372 KB |
Output is correct |
2 |
Incorrect |
5 ms |
7372 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1591 ms |
373748 KB |
Output is correct |
2 |
Incorrect |
1849 ms |
377532 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6992 KB |
Output is correct |
2 |
Incorrect |
3 ms |
6996 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
376 ms |
104308 KB |
Output is correct |
2 |
Incorrect |
437 ms |
104868 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4440 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1579 ms |
370092 KB |
Output is correct |
2 |
Incorrect |
2140 ms |
377552 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |