In modern day magic shows, passing through walls is very popular in which a magician performer passes through several walls in a predesigned stage show. The wall-passer (Pass-Muraille) has a limited wall-passing energy to pass through at most k walls in each wall-passing show. The walls are placed on a grid-like area. An example is shown in Figure 1, where the land is viewed from above. All the walls have unit widths, but different lengths. You may assume that no grid cell belongs to two or more walls. A spectator chooses a column of the grid. Our wall-passer starts from the upper side of the grid and walks along the entire column, passing through every wall in his way to get to the lower side of the grid. If he faces more than k walls when he tries to walk along a column, he would fail presenting a good show. For example, in the wall configuration shown in Figure 1, a wall-passer with k = 3 can pass from the upper side to the lower side choosing any column except column 6.
Figure 1. Shaded cells represent the walls.
Given a wall-passer with a given energy and a show stage, we want to remove the minimum number of walls from the stage so that our performer can pass through all the walls at any column chosen by spectators.
Input
The first line of the input file contains a single integer t (1 <= t <=
10), the number of test cases, followed by the input data for each test case.
The first line of each test case contains two integers n (1 <= n <= 100),
the number of walls, and k (0 <= k <= 100), the maximum number of walls
that the wall-passer can pass through, respectively. After the first line, there
are n lines each containing two (x, y) pairs representing coordinates of the
two endpoints of a wall. Coordinates are non-negative integers less than or
equal to 100. The upper-left of the grid is assumed to have coordinates (0,
0). The second sample test case below corresponds to the land given in Figure
1.
Output
There should be one line per test case containing an integer number which is
the minimum number of walls to be removed such that the wall-passer can pass
through walls starting from any column on the upper side.
Sample Input
2
3 1
2 0 4 0
0 1 1 1
1 2 2 2
7 3
0 0 3 0
6 1 8 1
2 3 6 3
4 4 6 4
0 5 1 5
5 6 7 6
1 7 3 7
Sample Output
1
1
思路:由左往右扫每一列,若当前的墙数tem<=K,则不处理;若tem>=K,则需要拆tem-K堵墙。至于拆除哪些墙,采取贪心策略:在当前列所有的有墙格中,选择右方最长的tem-K堵墙拆除。
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<cstdlib> #include<iomanip> #include<cmath> #include<vector> #include<queue> #include<stack> using namespace std; #define PI 3.141592653589792128462643383279502 int main(){ //#ifdef CDZSC_June //freopen("in.txt","r",stdin); //#endif //std::ios::sync_with_stdio(false); int mapp[105][105]; int t,n,m,x1,x2,y,y0,max_x,max_y,sum_s=0; cin>>t; while(t--){ cin>>n>>m; memset(mapp,0,sizeof(mapp)); max_x=0; max_y=0; sum_s=0; for(int i=1;i<=n;i++){ cin>>x1>>y>>x2>>y0; if(x1>max_x) max_x=x1; if(x2>max_x) max_x=x2; if(y>max_y) max_y=y; if(x2<x1){ for(int j=x2;j<=x1;j++) mapp[j][y]=i; } else{ for(int j=x1;j<=x2;j++) mapp[j][y]=i; } } for(int i=0;i<max_x;i++){ int tt=0; for(int j=0;j<=max_y;j++) if(mapp[i][j]>0) tt++; int offset;offset=tt-m; if(offset>0){ sum_s+=offset; while(offset--){ int max_s=0,max_bh; for(int k=0;k<=max_y;k++){ if(mapp[i][k]>0){ int tem_s=0; for(int z=i+1;z<=max_x;z++) if(mapp[z][k]==mapp[i][k]) tem_s++; else break; if(max_s<tem_s){ max_s=tem_s;max_bh=k; } } } for(int a=i;a<=i+max_s;a++) mapp[a][max_bh]=0; } } } cout<<sum_s<<endl; } return 0; }