题意:有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(迷之贪心...)的更多相关文章

  1. POJ 1083 &amp;&amp; HDU 1050 Moving Tables (贪心)

    Moving Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  2. --hdu 1050 Moving Tables(贪心)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1050 AC code: #include<stdio.h> #include<str ...

  3. HDU 1050 Moving Tables (贪心)

    题意:在一个走廊两边都有对称分布的连续房间,现在有n张桌子需要从a移动到b房间.每次移动需要10分钟, 但是如果两次移动中需要经过相同的走廊位置,则不能同时进行,需要分开移动.最后求最少需要多长时间移 ...

  4. hdu 1050 Moving Tables 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1050 这道题目隔了很久才做出来的.一开始把判断走廊有重叠的算法都想错了.以为重叠只要满足,下一次mov ...

  5. hdu 1050 Moving Tables

    http://acm.hdu.edu.cn/showproblem.php?pid=1050 这个题我首先直接用的常规贪心,用的和那个尽可能看更多完整节目那种思路.但是.......一直WA....T ...

  6. HDU – 1050 Moving Tables

    http://acm.hdu.edu.cn/showproblem.php?pid=1050 当时这道题被放在了贪心专题,我又刚刚做了今年暑假不AC所以一开始就在想这肯定是个变过型的复杂贪心,但是后来 ...

  7. hdu 1050 Moving Tables (Greedy)

    Problem - 1050 过两天要给12的讲贪心,于是就做一下水贪心练习练习. 代码如下: #include <cstdio> #include <iostream> #i ...

  8. hdoj 1050 Moving Tables【贪心区间覆盖】

    Moving Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  9. HDOJ 1050 Moving Tables

    Moving Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

随机推荐

  1. python中的深拷贝和浅拷贝(面试题)

    一.浅拷贝 定义:浅拷贝只是对另外一个变量的内存地址的拷贝,这两个变量指向同一个内存地址的变量值. 浅拷贝的特点: 公用一个值: 这两个变量的内存地址一样: 对其中一个变量的值改变,另外一个变量的值也 ...

  2. Java_Web三大框架之Struts2

    今天正式接触Java_Web三大框架之Struts2框架.对于初学者来说,先来了解什么是框架技术: 一.“框架技术”帮我们更快更好地构建程序: 1.是一个应用程序的半成品 2.提供可重用的公共结构 3 ...

  3. C#鸡翁百鸡

    一个for static void Main(string[] args) { int x, y, z; ; x <= ; x++) { - * x) % == ) { y = ( - * x) ...

  4. java关于工作,跳槽之总结

    关于工作中: 如何展示自己项目中的亮点,技术或者难点: 总结我的经历和技术倒是可以,但是我做的项目和我会的技术都很平庸,实在找不到亮点怎么办? 如果知道了你没有亮点,也就是知道了你自己欠缺什么,那么下 ...

  5. js生成web安全色

    256色里有40种颜色在Macintosh和Windows里显示的效果不一样,所以能安全使用的只有216色. <!DOCTYPE HTML> <html> <head&g ...

  6. C# 后台POST提交方式

    1.第一种方式:用最新框架,但是针对IIS服务器的操作系统有关系,非R2的收不到数据: using (var reqConts = new MultipartFormDataContent()) { ...

  7. react 子组件调用父组件方法

    import React from 'react'import '../page1/header.css'import { Table } from 'antd'import Child from ' ...

  8. chrony配置介绍

    rhel7 文档https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/System_Adminis ...

  9. 点击 table 单元格 取值

    function Test() { var rows = document.getElementById("tbDetail").rows; if (rows.length > ...

  10. [C++] muParser 的简单使用方法

    关于 muParser 库 许多应用程序需要解析数学表达式.该库的主要目的是提供一种快速简便的方法. muParser是一个用C ++编写的可扩展的高性能数学表达式解析器库. 它的工作原理是将数学表达 ...