题解:这是个贪心题,一列一列找,找到不符合的那一列找出这一列中对应右边最长的哪一行即可。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=150;
int mapp[maxn][maxn];
int main()
{
int t;
scanf("%d",&t);
while(t--){
memset(mapp,0,sizeof(mapp));
int n,k;
scanf("%d%d",&n,&k);
int y1,x1,y2,x2,maxx=-1,maxy=-1;
for(int i=1;i<=n;i++){
scanf("%d%d%d%d",&y1,&x1,&y2,&x2);
maxx=max(maxx,x2);
maxy=max(maxy,max(y1,y2));
if(y1<y2){
for(int j=y1;j<=y2;j++){
mapp[x1][j]=i;
}
}else{
for(int j=y2;j<=y1;j++){
mapp[x1][j]=i;
}
}
}
// for(int i=0;i<=maxx;i++){
// for(int j=0;j<=maxy;j++){
// printf("%d ",mapp[i][j]);
// }
// printf("\n");
// }
// cout<<endl;
int temp1,ans=0;
for(int i=0;i<=maxy;i++){
temp1=0;
for(int j=0;j<=maxx;j++){
if(mapp[j][i]){
temp1++;
}
}
//cout<<temp1<<endl;
int temp2=temp1-k;
if(temp2>0){
ans+=temp2;
while(temp2--){
int tempcnt=0,tempr,tempmax=-1;
for(int k=0;k<=maxx;k++){
tempcnt=0;
if(mapp[k][i]>0){
for(int l=i+1;l<=maxy;l++){//此处l=i+1有个坑
if(mapp[k][l]==mapp[k][i]){
tempcnt++;
}else{
break;
}
}
}
if(tempcnt>tempmax){
tempmax=tempcnt;
tempr=k;
}
}
//cout<<tempr<<endl;
for(int m=i;m<=i+tempmax;m++){
mapp[tempr][m]=0;
}
}
}
}
// for(int i=0;i<=maxx;i++){
// for(int j=0;j<=maxy;j++){
// cout<<mapp[i][j]<<" ";
// }
// cout<<endl;
// }
// cout<<endl;
printf("%d\n",ans);
}
return 0;
}