hdu 1050 Moving Tables(迷之贪心...)
题意:有400间房间按题目中图片所给的方式排列,然后给出要移动的n张桌子所要移动的范围,每张桌子要移动的范围不能出现重叠的区域;问最少要多少次才能移动完所有的桌子。
题解思路:把题目转换下,就是有n个区间,每次可以去除掉k个没有重叠部分的区间,最少要多少次能去掉所有区间。妥妥的,,贪心。可能会有人联想到经典的“区间调度问题”。但很遗憾,在这里行不通;区间调度问题是按区间右端排序后尽可能选取结束时间早的,但这种贪心只是一次性选取尽可能多的区间,而本题是要求选取完所有区间所要花费次数最少。从整体上看,如果只是每次找到尽可能多的区间,对一次的遍历从房间尾号排序确实达到了这样的目的,但整体目的却不是这样的,因为最终是要把所有的区间都找完的,并不是说仅仅找最多的一次,你还要顾及后面的让每一次都尽可能找得更多的房间。所以不管前几次怎样,你现在要找的这一次需要越靠前开始找。 因此正解应该是按区间左端排序,然后每次找最靠前的。 虽然看完上面的解释可能还是会不怎么明白为什么第一种贪心是错的,那就测试下面这组数据琢磨琢磨吧:
Input Output
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <utility>
#include <vector>
#include <map>
#include <queue>
#include <stack>
const int inf=0x3f3f3f3f;
const double PI=acos(-1.0);
const double EPS=1e-;
using namespace std;
typedef long long ll;
typedef pair<int,int> P; int T,n;
typedef struct node
{
int l,r;
} node;
node a[];
bool cmp(node a,node b)
{
return a.l<b.l;
}
int book[];
int main()
{
//freopen("input.txt","r",stdin);
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
int l,r;
for(int i=; i<=n; i++)
{
scanf("%d%d",&l,&r);
a[i].l=(l%)?(l/+):l/;
a[i].r=(r%)?(r/+):r/;
if(a[i].l>a[i].r) swap(a[i].l,a[i].r);
}
//
sort(a+,a++n,cmp);
//
memset(book,,sizeof(book));
int ans=;
int N=n;
while(n)
{
//printf("%d\n",n);
ans++;
int temp=;
while(book[temp]) temp++;
book[temp]=;
n--;
for(int i=temp+; i<=N; i++)
{
if(!book[i]&&a[i].l>a[temp].r)
{
n--;
book[i]=;
temp=i;
}
}
}
//
printf("%d\n",ans*);
}
return ;
}
还有另一种贪心方法更直接明了:计算出所有区间中最大的重叠次数,即答案。因为假设有块地方被k个区间所重叠,那么无论如何选择,这块地方肯定都是至少要k次才能移动完重叠在这上面的区间;而其他地方肯定也都能在k次以内移动完的。 计算重叠部分的话,,这题数据略水可以暴力过去...=_=
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <utility>
#include <vector>
#include <map>
#include <queue>
#include <stack>
const int inf=0x3f3f3f3f;
const double PI=acos(-1.0);
const double EPS=1e-;
using namespace std;
typedef long long ll;
typedef pair<int,int> P; int T,n;
typedef struct node
{
int l,r;
} node;
node a[];
int book[];
int main()
{
//freopen("input.txt","r",stdin);
scanf("%d",&T);
while(T--)
{
memset(book,,sizeof(book));
scanf("%d",&n);
int l,r;
for(int i=; i<=n; i++)
{
scanf("%d%d",&l,&r);
a[i].l=(l%)?(l/+):l/;
a[i].r=(r%)?(r/+):r/;
//
if(a[i].l>a[i].r) swap(a[i].l,a[i].r);
//
for(int j=a[i].l;j<=a[i].r;j++) book[j]++; }
int Max=-inf;
for(int i=;i<=;i++) Max=max(Max,book[i]);
printf("%d\n",Max*);
}
return ;
}
hdu 1050 Moving Tables(迷之贪心...)的更多相关文章
- POJ 1083 && HDU 1050 Moving Tables (贪心)
Moving Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- --hdu 1050 Moving Tables(贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1050 AC code: #include<stdio.h> #include<str ...
- HDU 1050 Moving Tables (贪心)
题意:在一个走廊两边都有对称分布的连续房间,现在有n张桌子需要从a移动到b房间.每次移动需要10分钟, 但是如果两次移动中需要经过相同的走廊位置,则不能同时进行,需要分开移动.最后求最少需要多长时间移 ...
- hdu 1050 Moving Tables 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1050 这道题目隔了很久才做出来的.一开始把判断走廊有重叠的算法都想错了.以为重叠只要满足,下一次mov ...
- hdu 1050 Moving Tables
http://acm.hdu.edu.cn/showproblem.php?pid=1050 这个题我首先直接用的常规贪心,用的和那个尽可能看更多完整节目那种思路.但是.......一直WA....T ...
- HDU – 1050 Moving Tables
http://acm.hdu.edu.cn/showproblem.php?pid=1050 当时这道题被放在了贪心专题,我又刚刚做了今年暑假不AC所以一开始就在想这肯定是个变过型的复杂贪心,但是后来 ...
- hdu 1050 Moving Tables (Greedy)
Problem - 1050 过两天要给12的讲贪心,于是就做一下水贪心练习练习. 代码如下: #include <cstdio> #include <iostream> #i ...
- hdoj 1050 Moving Tables【贪心区间覆盖】
Moving Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- HDOJ 1050 Moving Tables
Moving Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
随机推荐
- Android json 数据解析
1.json格式 2.json解析 3.gson解析 4.fastjson解析 一.Json格式 json一种轻量级的数据交换格式.在网络上传输交换数据一般用xml, json. 两种结构: 1)对象 ...
- [转]Android ListView的Item高亮显示的办法
本文转自:http://www.cnblogs.com/dyllove98/archive/2013/07/31/3228601.html 在我们使用ListView的时候,经常会遇到某一项(Item ...
- 新认知之WinForm窗体程序
Windows应用程序和控制台应用程序有很大的区别 >Form1.cs :窗体文件,程序员对窗体编写的代码一般都存放在这个文件中. >Form1.Designer.cs :窗体设计文件, ...
- Android常用的Dialog对话框用法
Android的版本有很多通常开发的时候对话框大多数使用自定义或是 Google提供的V4, V7 兼容包来开发保持各个版本的对话框样式统一,所以这里使用的是V7 包里的AlertDialog. im ...
- React Native真机调试安卓版
React Native真机调试安卓版 一.准备工作 1.1 环境搭建 React Native中文网的文档已经非常清晰地描述了,按照步骤即可.http://reactnative.cn/docs/0 ...
- 02--Java Socket编程--IO方式
一.基础知识 1. TCP状态转换知识,可参考: http://www.cnblogs.com/qlee/archive/2011/07/12/2104089.html 2. 数据传输 3. TCP/ ...
- Json——一般应用
引用命名空间 using Newtonsoft.Json; 序列化类或者类的集合 string jsonData1 = JsonConvert.SerializeObject(p1);//序列化类 s ...
- MyEclipse中VSS的使用详解
本文系转载,原文地址http://hi.baidu.com/yi88cheng/blog/item/13dd862f765e6b5c4fc226e5.html
- Linux Shell 小知识
${} ——变量替换 通常 $var 与 ${var} 没有区别,但是用 ${} 会比较精确的界定变量名称的范围. name='Ace' echo "result1: my name is ...
- Ubuntu 关闭guest用户
Ubuntu 关闭guest用户 ca0gu0@ub:~$ cat /etc/lightdm/lightdm.conf [SeatDefaults]autologin-user=falseallow- ...