Pass-Muraille


Time Limit: 2 Seconds      Memory Limit: 65536 KB

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[][];
int t,n,m,x1,x2,y,y0,max_x,max_y,sum_s=;
cin>>t;
while(t--){
cin>>n>>m;
memset(mapp,,sizeof(mapp));
max_x=;
max_y=;
sum_s=;
for(int i=;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=;i<max_x;i++){
int tt=;
for(int j=;j<=max_y;j++)
if(mapp[i][j]>) tt++;
int offset;offset=tt-m;
if(offset>){
sum_s+=offset;
while(offset--){
int max_s=,max_bh;
for(int k=;k<=max_y;k++){
if(mapp[i][k]>){
int tem_s=;
for(int z=i+;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]=;
}
}
}
cout<<sum_s<<endl;
}
return ;
}

zoj 1375||poj 1230(贪心)的更多相关文章

  1. ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法

    题目连接:problemId=542" target="_blank">ZOJ 1542 POJ 1861 Network 网络 Network Time Limi ...

  2. POJ 1862 &amp; ZOJ 1543 Stripies(贪心 | 优先队列)

    题目链接: PKU:http://poj.org/problem?id=1862 ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?proble ...

  3. zoj 1375 贪心

    https://vjudge.net/problem/ZOJ-1375 In modern day magic shows, passing through walls is very popular ...

  4. POJ - 2349 ZOJ - 1914 Arctic Network 贪心+Kru

    Arctic Network The Department of National Defence (DND) wishes to connect several northern outposts ...

  5. (贪心5.1.1)POJ 1230 Pass-Muraille

    /* * POJ_1230.cpp * * Created on: 2013年10月9日 * Author: Administrator */ #include <iostream> #i ...

  6. POJ 1230 Pass-Muraille#贪心+vector迭代器用法

    (- ̄▽ ̄)-* (注意下面代码中关于iterator的用法,此代码借鉴某大牛) #include<iostream> #include<cstdio> #include< ...

  7. POJ - 1017 贪心训练

    Packets Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 59725   Accepted: 20273 Descrip ...

  8. poj 3100 (zoj 2818)||ZOJ 2829 ||ZOJ 1938 (poj 2249)

    水题三题: 1.给你B和N,求个整数A使得A^n最接近B 2. 输出第N个能被3或者5整除的数 3.给你整数n和k,让你求组合数c(n,k) 1.poj 3100 (zoj 2818) Root of ...

  9. POJ 2376 贪心

    题意:FJ希望它的牛做一些清洁工作.有N只牛和T个时间段,每只牛可以承担一段时间内的工作.FJ希望让最小数量的牛覆盖整个T,求出其数量.若无法覆盖整个T,则输出-1. 分析:首先要注意T表示T个时间段 ...

随机推荐

  1. SVG 基本绘图方法总结

    基本内容:   * SVG并不属于HTML5专有内容    * HTML5提供有关SVG原生的内容   * 在HTML5出现之前,就有SVG内容   * SVG,简单来说就是矢量图   * SVG文件 ...

  2. Redis 模糊匹配 SearchKeys

    语法:KEYS pattern说明:返回与指定模式相匹配的所用的keys.该命令所支持的匹配模式如下:(1)?:用于匹配单个字符.例如,h?llo可以匹配hello.hallo和hxllo等:(2)* ...

  3. 【uva12232/hdu3461】带权并查集维护异或值

    题意: 对于n个数a[0]~a[n-1],但你不知道它们的值,通过逐步提供给你的信息,你的任务是根据这些信息回答问题: I P V :告诉你a[P] = V I P Q V:告诉你a[P] XOR a ...

  4. 【51NOD】数据流中的算法

    [算法]数学 [题解] 1.平均数:累加前缀和.//听说要向下取整? 2.中位数:双堆法,大于中位数存入小顶堆,小于中位数存入大顶堆,保证小顶堆内数字数量≥大顶堆,奇数则取小堆顶,偶数则取两堆顶/2. ...

  5. Atlantis(POJ1151+线段树+扫描线)

    题目链接:http://poj.org/problem?id=1151 题目: 题意:求所有矩形的面积,重合部分只算一次. 思路:扫描线入门题,推荐几篇学扫描线的博客: 1.http://www.cn ...

  6. LESS使用简介!

    我真的真的极度痛苦. 原本用了那么久的LESS,一直都是用编译工具(考拉)进行编译的,今天试了试用less.js来搞,按官网的都一毛一样,然而!就是编译不出来! 我用来擦鼻涕的卫生纸都一下午用了大半卷 ...

  7. LCD实验学习笔记(九):UART

    s3c2440包含三个通用异步收发器,可工作于中断模式或DMA模式.每个UART包含两个64字节的FIFOs用于接收和发送数据.可编程设置波特率.1或2个停止位,5/6/7/8个数据位和奇偶校验状态. ...

  8. SQL注入之逗号拦截绕过

    目前所知博主仅知的两个方法 1.通过case when then 2.join [一]case when then mysql,,,,,,, ) ) end; +----+-----------+-- ...

  9. CentOS在ssh下远程重装系统

    CentOS在ssh下远程重装系统 http://www.zxsdw.com/index.php/archives/913/ 国外VPS服务器一般都有控制面板,有很多种系统可自行安装,但国内有些IDC ...

  10. HDU 6188 Duizi and Shunzi 贪心

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6188 题意:给了n个数,然后现在问我们最多构成多少个对子和顺子,其中对子是2个相同的牌,顺子是3个连续 ...