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以下什么什么的,比赛后半阶段才开始仔细研究这题发现贪心就能搞,奈何读错题了!!交换的时候可 ...
随机推荐
- spring下配置shiro
1.web.xml中加入shiro的过滤器: <!-- Spring --> <!-- 配置Spring配置文件路径 --> <context-param> < ...
- zabbix-2.4.8-1添加MySQL状态监控
1.安装zabbix-agentyum -y install zabbix-2.4.8-1.el6.x86_64.rpm zabbix-agent-2.4.8-1.el6.x86_64.rpm 安装以 ...
- Autonomous System
自治系统(Autonomous System).在互联网中,一个自治系统(AS)是一个有权自主地决定在本系统中应采用何种路由协 议的小型单位.这个网络单位可以是一个简单的网络也可以是一个由一个 ...
- 字节流和字符流 in Java
@0: 深入理解Java中的流(Stream) @1:字节流:java.io.InputStream:public abstract class InputStreamThis abstract cl ...
- admin 模块功能
class CustomModelAdmin(admin.ModelAdmin): def has_module_permission(self, request): 是否会显示model def h ...
- windows下docker toolbox无法下载boot2docker.iso
GitHub连不上导致自动更新失败.(网络形势严峻!) 通过别的途径手动下载了指定的最新的boot2docker.iso文件.(比方说迅雷!比方说迅雷!比方说迅雷!) https://github.c ...
- 17南宁区域赛 I - Rake It In 【DFS】
题目链接 https://nanti.jisuanke.com/t/19975 题意 Alice 和 Bob 玩游戏 在一个4x4 的方格上 每个人 每次选择2x2的区域 将里面的四个值求和加到最后的 ...
- xml转换为对象 微信接口
public sealed class XMLSerilizable { /// <summary> /// XML转换为对象 /// </summary> /// <t ...
- iOS获取设备IP地址
项目用到要获取iOS设备的IP地址,有2种方法: 1)第一种比较简单,但是只有当你的设备连接到WIFI时才能获取到IP地址,倘若你的设备用的是流量,那就不行.代码如下: #import <ifa ...
- CMD 删除脚本
CMD 删除脚本 forfiles /p D:\BACKUP\WindowsImageBackup /s /m *.* /d -14 /c "cmd /c del @file"; ...