题解 P2859 【[USACO06FEB]摊位预订Stall Reservations】
题目链接:
思路:
首先大家会想到这是典型的贪心,类似区间覆盖问题的思路,我们要将每段时间的左端点从小到大排序,然后一个个插入,插入时比较是否先前的牛棚中已经有牛挤完了奶,如果没有就新增一个牛棚,否则用挤完奶的牛棚。
如果插入时扫描一遍找可用的牛棚的话肯定是会超时的,那么我们就要用一个priority_queue(当然你手写堆也可以)维护先前插入的牛棚中最早挤完奶,即右端点最小的那个。
Tips:
注意排序后奶牛的顺序可能是混乱的,而输出是要按原来输入的顺序。我们可以用一个index记录每头奶牛先前的数组下标,以便在记录摊位位置时不会出错。
如何判断每头奶牛在哪个摊位???
- 虽然感觉第一篇题解的二分+树状数组好神奇但是我看不懂
- 感觉第三篇的差分好神奇但我还是看不懂
于是就用了土方法
我们用一个be数组记录每个奶牛对应的摊位,如果前面没有可插入的奶牛,则新增一个。如果前面有一个奶牛挤完了奶,那么我们就用它的牛棚,具体请看代码实现。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <vector>
#include <queue>
using namespace std;
const int maxn=50005;
int n,be[maxn],cnt=0;
struct Cow{
int s,e,index;
}cow[maxn];
struct Num{ //优先队列记录下标
int x;
bool operator < (const Num &a) const{
return cow[x].e>cow[a.x].e;
}
};
inline bool cmp1(const Cow &a,const Cow &b){
return a.s<b.s;
}
priority_queue< Num,vector<Num>>poi;
int main()
{
cin>>n;
for(register int i=1;i<=n;i++){
scanf("%d %d",&cow[i].s,&cow[i].e);
cow[i].index=i;
}
sort(cow+1,cow+1+n,cmp1);
for(register int i=1;i<=n;i++){
Num k;k.x=i;
if(!poi.empty()){
Num p=poi.top();//取出队首时间最短的
if(cow[p.x].e>=cow[i].s){
cnt++;
poi.push(k);
be[cow[i].index]=cnt; //注意是index值
}
else {//如果前面有挤完奶的
poi.pop();//注意要pop出来
poi.push(k);
be[cow[i].index]=be[cow[p.x].index]; //注意这句话,仔细想一想为什么
}
}
else {//如果是第一头牛
poi.push(k);
cnt++;
be[cow[i].index]=cnt;
}
}
cout<<cnt<<endl;
for(register int i=1;i<=n;i++){
printf("%d\n",be[i]);
}
return 0;
}
总结:
思维难度一般,考验一些小技巧的使用
题解 P2859 【[USACO06FEB]摊位预订Stall Reservations】的更多相关文章
- bzoj1651 / P2859 [USACO06FEB]摊位预订Stall Reservations
P2859 [USACO06FEB]摊位预订Stall Reservations 维护一个按右端点从小到大的优先队列 蓝后把数据按左端点从小到大排序,顺序枚举. 每次把比右端点比枚举线段左端点小的数据 ...
- 洛谷P2859 [USACO06FEB]摊位预订Stall Reservations
P2859 [USACO06FEB]摊位预订Stall Reservations 题目描述 Oh those picky N (1 <= N <= 50,000) cows! They a ...
- [USACO06FEB]摊位预订Stall Reservations(贪心)
[USACO06FEB]摊位预订Stall Reservations 题目描述 Oh those picky N (1 <= N <= 50,000) cows! They are so ...
- [USACO06FEB] Stall Reservations 贪心
[USACO06FEB] Stall Reservations 贪心 \(n\)头牛,每头牛占用时间区间\([l_i,r_i]\),一个牛棚每个时间点只能被一头牛占用,问最少新建多少个牛棚,并且每头牛 ...
- BZOJ1651: [Usaco2006 Feb]Stall Reservations 专用牛棚
1651: [Usaco2006 Feb]Stall Reservations 专用牛棚 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 509 Sol ...
- BZOJ 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚
题目 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 553 ...
- 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚
1651: [Usaco2006 Feb]Stall Reservations 专用牛棚 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 566 Sol ...
- POJ 3190 Stall Reservations (优先队列)C++
Stall Reservations Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7646 Accepted: 271 ...
- Stall Reservations POJ - 3190 (贪心+优先队列)
Stall Reservations Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11002 Accepted: 38 ...
随机推荐
- Flutter移动电商实战 --(3)底部导航栏制作
1.cupertino_IOS风格介绍 在Flutter里是有两种内置风格的: material风格: Material Design 是由 Google 推出的全新设计语言,这种设计语言是为手机.平 ...
- ThinkPHP空操作与命名空间
命名空间:相当于一个虚拟的目录 正常管理文件使用文件夹--物理区分 TP框架的初始命名空间是:ThinkPHP\Library 在TP框架下命名空间里面使用\代表的是初始命名空间(ThinkPHP\L ...
- 对String的内存解析
@Test public void stringTest(){ /* * str1和str2地址指向字符串常量池 * 解析: str1 在字符串常量池中创建出java 地址例如为:0x456 * st ...
- mvc导出数据到pdf
using iTextSharp;using iTextSharp.text;using iTextSharp.text.pdf; , , ") { System.Collections.A ...
- 为什么HashMap继承了AbstractMap还要实现Map?
前言 之前看源码一直忽略了这个现象,按理说HashMap的父类AbstractMap已经实现了Map,它为什么还要实现一次呢?遂上网查了一下,背后原因让人大跌眼镜. 原因 这是类库设计者的拼写错误,其 ...
- maven settings.xml详解
setting.xml配置文件 http://blog.csdn.net/u012152619/article/details/51485152 maven的配置文件settings.xml存在于两个 ...
- IDEA如何构建mybatis
任何一个软件都要和数据库关联,软件需要的数据都存储在数据库中. 对于经常使用的数据库相关的代码就出现了很多冗余的代码,持久层框架也随之出现. 目前使用比较流程的持久层框架有hibernate和myba ...
- ajax基础------备忘
1:register.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" ...
- jquery简单入门1
前端 html:展示 form: 属性: action和method 子标签: input(10种) text password radio checkbox file submit button r ...
- python连接mysql数据库实例demo(银行管理系统数据库版)
主函数: import adminView import os import pickle from bankFunction import BankFunction import time def ...