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。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define LL long long
int num[];
struct node{int l,r;}P[];
bool vis[];
int main()
{
int T,N,M,K,i,j,k;
cin>>T;
while(T--){
int a,b,c,d;
cin>>N>>K;
memset(num,,sizeof(num));
memset(vis,,sizeof(vis));
for(i=;i<=N;++i)
{
scanf("%d%d%d%d",&a,&b,&c,&d);
if(a>c) swap(a,c);
P[i].l=a;
P[i].r=c;
for(j=a;j<=c;++j)
num[j]++;
}
int ans=;
for(i=;i<=;++i)
{ while(num[i]>K){
int u=-,w=-;
for(j=;j<=N;++j)
{
if(!vis[j]&&i>=P[j].l&&i<=P[j].r&&w<P[j].r){
w=P[j].r;
u=j;
}
}
vis[u]=;
for(j=P[u].l;j<=P[u].r;++j)
num[j]--;
ans++;
}
}
printf("%d\n",ans);
}
return ;
}
zoj 1375 贪心的更多相关文章
- Heap Partition ZOJ - 3963(贪心)
ZOJ - 3963 贪心做一下就好了 反正别用memset #include <iostream> #include <cstdio> #include <sstrea ...
- zoj 1375||poj 1230(贪心)
Pass-Muraille Time Limit: 2 Seconds Memory Limit: 65536 KB In modern day magic shows, passing t ...
- ZOJ - 3715贪心
ZOJ - 3715KindergartenElection 题目大意:幼儿园里正在举办班长选举,除1号小朋友外每个人都会投他最好的朋友,但1号小朋友可以贿赂别人(小伙子有丶想法),被贿赂的小朋友就会 ...
- ZOJ 38727(贪心)
这道题真心坑.越想越远 想的飞起来了. 最后纠结起后缀表达式的定义来了. 题意: 就是给你一个串 , 让你用最少改动次数来实它变成一个合法的后缀表达式, 改动方式有两种, 一种是直接加入数字或者 ...
- ZOJ 3607贪心算法
http://blog.csdn.net/ffq5050139/article/details/7832991 http://blog.watashi.ws/1944/the-8th-zjpcpc/ ...
- ZOJ 3829 贪心 思维题
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题.自己智商不够.不敢搞,想着队友智商 ...
- ZOJ Problem Set - 3829Known Notation(贪心)
ZOJ Problem Set - 3829Known Notation(贪心) 题目链接 题目大意:给你一个后缀表达式(仅仅有数字和符号),可是这个后缀表达式的空格不幸丢失,如今给你一个这种后缀表达 ...
- 贪心+模拟 ZOJ 3829 Known Notation
题目传送门 /* 题意:一串字符串,问要最少操作数使得成为合法的后缀表达式 贪心+模拟:数字个数 >= *个数+1 所以若数字少了先补上在前面,然后把不合法的*和最后的数字交换,记录次数 岛娘的 ...
- 【贪心+一点小思路】Zoj - 3829 Known Notation
借用别人一句话,还以为是个高贵的dp... ... 一打眼一看是波兰式的题,有点懵还以为要用后缀表达式或者dp以下什么什么的,比赛后半阶段才开始仔细研究这题发现贪心就能搞,奈何读错题了!!交换的时候可 ...
随机推荐
- boost之时间timer
C++一直缺乏对时间和日期的处理能力,一般借助于C的struct tm和time():timer包含三个类其中timer,progress_timer是计时器类,进度指示类是progress_disp ...
- 【jenkins】jenkins实时显示python脚本输出
jenkins在构建shell脚本时可以实时输出结果,但是在构建python脚本时,是等到python执行完成以后,才显示结果,这个对于我们判断脚本执行状态非常不利 这里介绍一种方法,能够实时显示py ...
- start() vs. run()
I'm reading a Blog. But a rather familiar question occurred to me, "What's the difference ...
- vim插件快捷键
@1:winmanager: #1:打开winmanager的快捷键在.vimrc中配置,默认为":WMToggle",使用nmap可以将其映射到其他的命令. #2:netrw快捷 ...
- 【LeetCode】【定制版排序】Sort Colors
之前转载过一篇STL的sort方法底层详解的博客:https://www.cnblogs.com/ygh1229/articles/9806398.html 但是我们在开发中会根据自己特定的应用,有新 ...
- Oracle 9i Unix Manager
在Unix上被迫终止ORACLE进程时,必须做以下事情: (1) 杀掉所有Oracle进程. ps -ef|grep $ORACLE_SID|grep -v grep|awk '{print $ ...
- 【TopCoder】SRM151 DIV2 练习总结
第一次做完整的SRM题,刷完感觉萌萌哒,不过自己对java中很多细节不熟练,还要边做题边google. 250分的题:判断字符串序列是否是前缀码,如果不是,返回第一个违反前缀码规则的字符串. 简单的暴 ...
- 20145230《Java程序设计》第4周学习总结
20145230<Java程序设计>第4周学习总结 教材学习内容总结 继承共同行为 本周学习的首先是Java语言中的继承与多态.何为我们的继承呢?在我们面向对象中,子类继承父类,避免重复的 ...
- matplotlib模块之子图画法
一般化的子图布局 首先要创建各个子图的坐标轴,传入一个四元列表参数:[x,y,width,height],用来表示这个子图坐标轴原点的x坐标.y坐标,以及宽和高.值得注意的是,这四个值的取值范围都是[ ...
- MySQL数据库基本操作(三)
MySQL补充: mysql是关系型数据库,关系数据库,是建立在关系模型基础上的数据库,现实世界中的各种实体,以及实体之间的各种联系,均用关系模型(table)来表示.#关系模型就是指二维表格模型,因 ...