Pass-Muraille

Description

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.

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

Hint

Walls are parallel to X.

把二维的问题转换为一维的问题:删除最少的线段,使得每个点最多被k条线段覆盖。贪心,每次删除覆盖这条线段的最右面的一条。刚开始算法错了,本来是应该先算出所有点被几条线段覆盖,然后再一条一条删,窝却是边算边删,这样当然不能保证删去的是对后面 影响最大的线段,wa......后来发现之后,又一直tle,....醉了。最后发现是因为两个线段可能右端点一样,而我之前为了标记已经删除的线段用的是mark[maxright],maxright是最右面的坐标,这样当然会tle,因为可能两个线段右端点一样,然后就是死循环了.....我怎么这么弱啊,还是要多敲代码....最后终于ac了..........
代码:
#include<stdio.h>
#include<string.h>
int max(int a,int b) {return a>b?a:b;}
int min(int a,int b) {return a<b?a:b;}
int main(){
    int wall[105],l[105],r[105],mark[105],n,k,t,i,j,ans,tp1,tp2,tp3,maxright,m,mr;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&k);
        ans=0;
        memset(wall,0,sizeof(wall));
        memset(mark,0,sizeof(mark));
        for(i=0;i<n;i++){
            scanf("%d%d%d%d",&l[i],&tp1,&r[i],&tp2);
            if(l[i]>r[i]){
                tp3=l[i];
                l[i]=r[i];
                r[i]=tp3;
            }
        }
        for(j=0;j<n;j++){
            for(i=0;i<100;i++){
                if(l[j]<=i&&r[j]>=i)
                    wall[i]++;
            }
        }
        for(i=0;i<100;i++){
            while(wall[i]>k){
                maxright=0;
                for(j=0;j<n;j++){
                    if(l[j]<=i&&r[j]>=i&&!mark[j]){
                        if(maxright<r[j]){
                            maxright=r[j];
                            mr=j;//有可能两个墙的maxright相等,方了;
                        }
                    }
                }
                mark[mr]=1;
                ans++;
                for(m=i;m<=maxright;m++)
                    wall[m]--;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值