Stall Reservations
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7646   Accepted: 2710   Special Judge

Description

Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time interval A..B (1 <= A <= B <= 1,000,000), which includes both times A and B. Obviously, FJ must create a reservation system to determine which stall each cow can be assigned for her milking time. Of course, no cow will share such a private moment with other cows.

Help FJ by determining:

  • The minimum number of stalls required in the barn so that each cow can have her private milking period
  • An assignment of cows to these stalls over time

Many answers are correct for each test dataset; a program will grade your answer.

Input

Line 1: A single integer, N

Lines 2..N+1: Line i+1 describes cow i's milking interval with two space-separated integers.

Output

Line 1: The minimum number of stalls the barn must have.

Lines 2..N+1: Line i+1 describes the stall to which cow i will be assigned for her milking period.

Sample Input

  1. 5
  2. 1 10
  3. 2 4
  4. 3 6
  5. 5 8
  6. 4 7

Sample Output

  1. 4
  2. 1
  3. 2
  4. 3
  5. 2
  6. 4

Hint

Explanation of the sample:

Here's a graphical schedule for this output:

  1. Time 1 2 3 4 5 6 7 8 9 10

  2. Stall 1 c1>>>>>>>>>>>>>>>>>>>>>>>>>>>

  3. Stall 2 .. c2>>>>>> c4>>>>>>>>> .. ..

  4. Stall 3 .. .. c3>>>>>>>>> .. .. .. ..

  5. Stall 4 .. .. .. c5>>>>>>>>> .. .. ..

Other outputs using the same number of stalls are possible.

 
 
这道大水题真的坑,不少人想的都是贪心,背包问题。比赛时候甚至队友讨论过树状数组。。。。。。。
比赛结束鹏哥说C题E题都是水题,E题优先队列。   喵喵喵???
仔细一想真的是啊。根本没往这方面想过!!
每次看完题解都是这么简单怎么没想出来,允悲。。。
 
 
 
这道题是修改了队列的优先级,因为在同一槽中要下一只牛的话, 当前r值必须小于l值。所以优先级r值优先。
所以本题的难点就在于,怎样处理当前槽,怎样构造队列优先级!!
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<queue>
  4. #include<algorithm>
  5. using namespace std;
  6. struct node
  7. {
  8. int l,r,n;
  9. bool operator <(const node &b)const
  10. {
  11. return r>b.r||(r==b.r&&l>b.l);
  12. }
  13. }a[50005];
  14. priority_queue <node> q;
  15. int u[50005];
  16. bool cmp(node a,node b)
  17. {
  18.  
  19. return a.l<b.l||(a.l==b.l&&a.r<b.r);
  20. }
  21. int main()
  22. {
  23. int n,m;
  24. while(~scanf("%d",&n))
  25. {
  26. int ans=1;
  27. for(int i=1;i<=n;i++)
  28. {
  29. scanf("%d%d",&a[i].l,&a[i].r);
  30. a[i].n=i;
  31. }
  32. sort(a+1,a+n+1,cmp);
  33. q.push(a[1]);
  34. u[a[1].n]=1;
  35.  
  36. for(int i=2;i<=n;i++)
  37. {
  38. if(!q.empty()&&q.top().r<a[i].l){
  39. u[a[i].n]=u[q.top().n];
  40. q.pop();
  41. }
  42. else{
  43. ans++;
  44. u[a[i].n]=ans;
  45. }
  46. q.push(a[i]);
  47. }
  48. printf("%d\n",ans);
  49. for(int i =1;i<=n;i++){
  50. printf("%d\n",u[i]);
  51. }
  52. while(!q.empty()) // 尤其重要,排空队列
  53. q.pop();
  54. }
  55. }

  

POJ 3190 Stall Reservations (优先队列)C++的更多相关文章

  1. POJ 3190 Stall Reservations贪心

    POJ 3190 Stall Reservations贪心 Description Oh those picky N (1 <= N <= 50,000) cows! They are s ...

  2. poj 3190 Stall Reservations

    http://poj.org/problem?id=3190 Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Su ...

  3. POJ - 3190 Stall Reservations 贪心+自定义优先级的优先队列(求含不重叠子序列的多个序列最小值问题)

    Stall Reservations Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one w ...

  4. POJ 3190 Stall Reservations【贪心】

    POJ 3190 题意: 一些奶牛要在指定的时间内挤牛奶,而一个机器只能同时对一个奶牛工作.给你每头奶牛的指定时间的区间(闭区间),问你最小需要多少机器.思路:先按奶牛要求的时间起始点进行从小到大排序 ...

  5. POJ -3190 Stall Reservations (贪心+优先队列)

    http://poj.org/problem?id=3190 有n头挑剔的奶牛,只会在一个精确时间挤奶,而一头奶牛需要占用一个畜栏,并且不会和其他奶牛分享,每头奶牛都会有一个开始时间和结束时间,问至少 ...

  6. poj 3190 Stall Reservations 贪心 + 优先队列

    题意:给定N头奶牛,每头牛有固定的时间[a,b]让农夫去挤牛奶,农夫也只能在对应区间对指定奶牛进行挤奶, 求最少要多少个奶牛棚,使得在每个棚内的奶牛的挤奶时间不冲突. 思路:1.第一个想法就是贪心,对 ...

  7. POJ 3190 Stall Reservations 【贪心 优先队列】

    题意:给出n头牛必须单独占用一台机器的时间段,问至少需要多少台机器 先按照每头牛的时间的x来排序,然后用一个优先队列(优先选取最小的)维护已经喂好的牛的最小的结束时间 比如现在优先队列里面有m头牛已经 ...

  8. Stall Reservations POJ - 3190 (贪心+优先队列)

    Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11002   Accepted: 38 ...

  9. POJ:3190-Stall Reservations

    传送门:http://poj.org/problem?id=3190 Stall Reservations Time Limit: 1000MS Memory Limit: 65536K Total ...

随机推荐

  1. 采用SmartQQ 协议可制作聊天机器人

    采用.NET CORE可运行在 Linux . Windows 和 Mac OSX 平台下. SmartQQ可以: 收发文字消息 获取好友.群.讨论组.好友分组和最近会话的列表 SmartQQ不可以: ...

  2. SpringMVC的REST风格的四种请求方式

    一. 在HTTP 协议里面,四个表示操作方式的动词:GET.POST.PUT.DELETE. ·它们分别对应四种基本操作: 1.GET  ====== 获 取资源 2.POST ======新建资源 ...

  3. BotVS配置托管者-基于新浪云

    1. 创建SAE应用 登录新浪云平台,点击创建新应用 2. SAE环境部署 在新应用中选择自定义 相应选项如下 开发语言:自定义 运行环境:云容器 语言版本:自定义 部署方式:手工部署 操作系统:系统 ...

  4. 【Java学习笔记之二十九】Java中的"equals"和"=="的用法及区别

    Java中的"equals"和"=="的用法及区别 在初学Java时,可能会经常碰到下面的代码: String str1 = new String(" ...

  5. MSF初体验 - kali linux 入侵XP系统

    最近做某安全竞赛平台的比赛,真正开始接触Metasploit这一渗透神器,同时也是装逼神器(2333-.),下面记录一下初步使用Metasploit的过程.首先在百度百科摘录了一段关于Metasplo ...

  6. Windows下JNI的使用教程

    JNI的使用大致有以下4个步骤: 一.在Java中写native方法 二.用javah命令生成C/C++头文件 三.写对应的C/C++程序实现头文件中声明的方法,并编译成库文件 四.在Java中加载这 ...

  7. python专题-读取xml文件

    关于python读取xml文章很多,但大多文章都是贴一个xml文件,然后再贴个处理文件的代码.这样并不利于初学者的学习,希望这篇文章可以更通俗易懂的教如何使用python 来读取xml 文件. 什么是 ...

  8. 安装oracle后登录时出现 ERROR: ORA-01031 insufficient privileges

    运行环境:在自己笔记本电脑(win10)上安装测试 操作系统版本:64位win8.1 Oracle版本:64位 oracle 11g 安装oracle 成功后//以管理员身份登录oracle 在cmd ...

  9. python appium 操作app

    下面是一些Python脚本中操作app的用法: 检查app安装情况(返回true/false), driver.is_app_installed(package_name) 安装app driver. ...

  10. java值传递与引用传递实例

    public class Test2 { public static void main(String[] args) { int[] arr=new int[5]; arr[0]=10; arr[1 ...