题解:CF2000F Color Rows and Columns
_little_duck_ · · 题解
CF2000F Color Rows and Columns 题解
题意
给定
每当完整涂满任意一行或一列时,你将获得
1 分。思路
- 预处理出每种矩形达到
x \le k 分需要的最小步数。- 对于每个矩形,对于每种得分,计算答案。
易错点
- 赋值时注意
memset函数是按位赋值。
代码
#include<bits/stdc++.h>
using namespace std;
const int N = 1010, M = 110, INF = 1e9;
int t, n, k;
int u[N][M];
int dp[N][M];
struct P{
int x, y;
}a[N];
int main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> t;
while(t--){
cin >> n >> k;
for(int i = 1; i <= n; i++){
cin >> a[i].x >> a[i].y;
if(a[i].x > a[i].y){
swap(a[i].x, a[i].y);
}
}
memset(u, 0x3f3f3f3f, sizeof u);
for(int o = 1; o <= n; o++){
for(int i = 0; i <= a[o].x; i++){
for(int j = 0; j <= a[o].y; j++){
int t = i + j;
if(t <= k){
u[o][t] = min(u[o][t], i * a[o].y + j * a[o].x - i * j);
}
}
}
}
memset(dp, 0x3f3f3f3f, sizeof dp);
dp[0][0] = 0;
for(int i = 1; i <= n; i++){
for(int j = 0; j <= k; j++){
for(int v = 0; v <= j; v++){
dp[i][j] = min(dp[i][j], dp[i - 1][j - v] + u[i][v]);
}
}
}
cout << (dp[n][k] >= 0x3f3f3f3f ? -1 : dp[n][k]) << "\n";
}
return 0;
}