zoukankan      html  css  js  c++  java
  • zoj 1375 贪心

    https://vjudge.net/problem/ZOJ-1375

    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

         一开始我贪心是优先选择覆盖目标列最多的墙进行拆除然后统计总个数,然后一直WA意识到贪心方案不对,正解是从0-最后一列遍历发现不满足的列之后优先选择一个之前未选择过得且包含这一列且往右延伸的距离尽可能大的那个进行拆除就AC。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 using namespace std;
     5 #define LL long long
     6 int num[115];
     7 struct node{int l,r;}P[115];
     8 bool vis[115];
     9 int main()
    10 {
    11     int T,N,M,K,i,j,k;
    12     cin>>T;
    13     while(T--){
    14         int a,b,c,d;
    15         cin>>N>>K;
    16         memset(num,0,sizeof(num));
    17         memset(vis,0,sizeof(vis));
    18         for(i=1;i<=N;++i)
    19         {
    20             scanf("%d%d%d%d",&a,&b,&c,&d);
    21             if(a>c) swap(a,c);
    22             P[i].l=a;
    23             P[i].r=c;
    24             for(j=a;j<=c;++j)
    25                 num[j]++;
    26         }
    27         int ans=0;
    28         for(i=0;i<=100;++i)
    29         {
    30             
    31             while(num[i]>K){
    32                     int u=-1,w=-1;
    33                 for(j=1;j<=N;++j)
    34                 {
    35                     if(!vis[j]&&i>=P[j].l&&i<=P[j].r&&w<P[j].r){
    36                         w=P[j].r;
    37                         u=j;
    38                     }
    39                 }
    40                 vis[u]=1;
    41                 for(j=P[u].l;j<=P[u].r;++j)
    42                     num[j]--;
    43                 ans++;
    44             }
    45         }
    46         printf("%d
    ",ans);
    47     }
    48     return 0;
    49 }
  • 相关阅读:
    CSP -- 运营商内容劫持(广告)的终结者
    vue学习:解决Apycharm的 * is only available in ES6(use 'esversion: 6') 问题
    nvm、node、npm安装以及pycharm配置eslint
    python中的daemon守护进程实现方法
    python语言基础问题汇总
    python -- DNS处理模块dnspython
    Python ping 模块
    如何把Python脚本导出为exe程序
    从实际案例聊聊Java应用的GC优化
    Python类(六)-静态方法、类方法、属性方法
  • 原文地址:https://www.cnblogs.com/zzqc/p/7495498.html
Copyright © 2011-2022 走看看