把每一列墙的数目求出,再从左到右遍历每一列,若该列中墙的数目n大于k,则除去n-k个对该列右边影响最大的墙。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
struct Walls{
int x1, y1, x2, y2;
int s;
}wall[105];
int num[105];
int main()
{
// freopen("in.txt", "r", stdin);
int t;
cin >> t;
while(t--)
{
int n, k;
cin >> n >> k;
int maxs = 0;
memset(num, 0, sizeof(num));
for(int i = 0; i < n; i++)
{
cin >> wall[i].x1 >> wall[i].y1 >> wall[i].x2 >> wall[i].y2;
if(wall[i].x1 > wall[i].x2)
swap(wall[i].x1, wall[i].x2);
wall[i].s = 0;
maxs = max(maxs, wall[i].x2);
for(int j = wall[i].x1; j <= wall[i].x2; j++)
num[j]++;
}
int sum = 0;
for(int i = 0; i <= maxs; i++)
{
int p = num[i] - k;
for(int j = 0; j < p; j++)
{
sum++;
int m1 = -1, m2;
for(int h = 0; h < n; h++)
{
// cout << wall[h].x2 - i << endl;
if(wall[h].s == 0 && wall[h].x1 <= i && wall[h].x2 >= i && wall[h].x2 - i > m1)
{
m2 = h;
m1 = wall[h].x2 - i;
}
}
wall[m2].s = 1;
for(int h = i+1; h <= wall[m2].x2; h++)
num[h]--;
}
}
cout << sum << endl;
}
return 0;
}