题解:Pass-Muraille(贪心)

本文介绍了一种现代魔术表演中常见的穿墙魔术,墙 passeur(Pass-Muraille)拥有有限能量,最多能穿过 k 堵墙。给定墙的位置和墙 passeur 的能量 k,目标是确定最小的墙体拆除数量,使得表演者可以从任一列成功穿越所有墙。题目提供输入输出格式,并说明了贪心策略,即从左到右依次拆除右侧最多墙的策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

图,里有n堵墙,一个魔术师从上往下走,可穿过k堵墙,要求每行都可以到底,最少拆多少,
贪心:从左到右,每次拆掉右边数最多的墙

#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int G[105][105];
struct node{
 int x,flag;
}tmp[105];
bool cmp(node a,node b)
{
 return a.x>b.x;
}
int main() 
{
 int sum,t,n,k,x1,y1,x2,y2,maxx,maxy;
 scanf("%d",&t);
 while(t--)
 {
  scanf("%d%d",&n,&k);
  memset(G,0,sizeof(G));
  maxx=0;maxy=0;sum=0;
  for(int i=1;i<=n;i++)
  {
   scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
   maxx=max(maxx,x1);
   maxx=max(maxx,x2);
   maxy=max(maxy,y1);
    if(x1>x2) swap(x1,x2);//坑 
   int t=x2-x1+1;
   for(int j=x1;j<=x2;j++)//右边有多少墙 
   {
    G[y1][j]=t;t--; 
 }
  }
  for(int i=0;i<=maxx;i++)
  {
   int tip=0;
   for(int j=0;j<=maxy;j++)//该行有多少墙 
    if(G[j][i]) 
    {
     tmp[++tip].x=G[j][i];
   tmp[tip].flag=j; 
  }
    if(tip>k) //拆掉右边墙最多的 
    {
     sum+=tip-k;
     sort(tmp+1,tmp+tip+1,cmp);
     for(int p=1;p<=tip-k;p++)
     {
       for(int ii=i;ii<i+tmp[p].x;ii++)
       G[tmp[p].flag][ii]=0;
  }
 }
  }
  printf("%d\n",sum);
 }
 return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值