poj 3190 Stall Reservations 贪心 + 优先队列
题意:给定N头奶牛,每头牛有固定的时间[a,b]让农夫去挤牛奶,农夫也只能在对应区间对指定奶牛进行挤奶,
求最少要多少个奶牛棚,使得在每个棚内的奶牛的挤奶时间不冲突。
思路:1、第一个想法就是贪心,对每头牛的挤奶时间[a,b]按a和b都从小排序,接着从左边开始找地一头牛,
然后再往右边找能够不冲突的牛再一个奶牛棚内。这个算法事件复杂度为n*n,由于最多5000头牛
所以后面还是TLE了。
2、还是自己太弱了,原来可以用优先队列进行优化,可以把当前冲突的牛放入优先队列,然后每次都能够冲优先队列
里面取出a最小的奶牛,所以自然就不用如算法1那样,要进行一个n的循环。所以算法最终复杂度就是nlogn。
AC代码:
#include <cstdio>
#include <cstring>
#include <queue>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
#define LL long long
const int N = 50005;
int t,d[N],n,k,ans[N];
struct node{
int s,e,id,stall;
bool operator < (const node &a) const
{
return a.e < e;
}
}w[N]; bool cmp(node n1, node n2){
return n1.s < n2.s;
}
void solve(){
priority_queue<node > Q;
sort(w,w+n, cmp);
k = 0;
int S = 2; node now;
now.e= 0;
now.stall = 1;
Q.push(now); for(int i = 0; i < n; i++)
{
now = Q.top();
if(w[i].s > now.e)
{
Q.pop();
w[i].stall = now.stall;
ans[w[i].id] = now.stall;
Q.push(w[i]);
}else
{
w[i].stall = S;
ans[w[i].id] = S++;
Q.push(w[i]);
}
}
printf("%d\n", S-1);
for(int i = 0; i < n; i++)
printf("%d\n", ans[i]);
}
int main(){
//freopen("in.txt", "r", stdin);
while(~scanf("%d", &n)){
for(int i = 0; i < n; i++){
scanf("%d %d", &w[i].s, &w[i].e);
w[i].id =i;
}
solve();
}
return 0;
}
TLE代码:
#include <cstdio>
#include <cstring>
#include <queue>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
#define LL long long
const int N = 50005;
const int INF = 0X3F3F3F3F3F3F3F3F;
int t,ans,d[N],n;
struct node{
int s,e,id;
}w[N];
bool cmp(node n1, node n2){
if(n1.s != n2.s) return n1.s < n2.s;
else return n1.e > n2.e;
}
void init(){
}
void solve(){
int k = 0;
ans = 0;
sort(w,w+n, cmp);
bool vis[n];
memset(vis, false, sizeof vis);
for(int i = 0; i < n; i++){ //这个循环n*n,所以TLE了,之前没留意,还以为因为sort函数
if(!vis[i]){
int end = w[i].e;
int id = w[i].id;
d[id] = ++k;
for(int j = i+1; j < n; j++)
if(!vis[j] && w[j].s > end){
vis[j] = true;
d[w[j].id] = k;
end = w[j].e;
}
}
}
printf("%d\n", k);
for(int i = 0; i < n; i++)
printf("%d\n", d[i]);
}
int main(){
//freopen("in.txt", "r", stdin);
while(~scanf("%d", &n)){
for(int i = 0; i < n; i++)
scanf("%d %d", &w[i].s, &w[i].e),w[i].id =i;
solve();
}
return 0;
}
poj 3190 Stall Reservations 贪心 + 优先队列的更多相关文章
- POJ 3190 Stall Reservations贪心
POJ 3190 Stall Reservations贪心 Description Oh those picky N (1 <= N <= 50,000) cows! They are s ...
- POJ - 3190 Stall Reservations 贪心+自定义优先级的优先队列(求含不重叠子序列的多个序列最小值问题)
Stall Reservations Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one w ...
- POJ 3190 Stall Reservations (优先队列)C++
Stall Reservations Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7646 Accepted: 271 ...
- poj 3190 Stall Reservations
http://poj.org/problem?id=3190 Stall Reservations Time Limit: 1000MS Memory Limit: 65536K Total Su ...
- POJ 3190 Stall Reservations【贪心】
POJ 3190 题意: 一些奶牛要在指定的时间内挤牛奶,而一个机器只能同时对一个奶牛工作.给你每头奶牛的指定时间的区间(闭区间),问你最小需要多少机器.思路:先按奶牛要求的时间起始点进行从小到大排序 ...
- POJ -3190 Stall Reservations (贪心+优先队列)
http://poj.org/problem?id=3190 有n头挑剔的奶牛,只会在一个精确时间挤奶,而一头奶牛需要占用一个畜栏,并且不会和其他奶牛分享,每头奶牛都会有一个开始时间和结束时间,问至少 ...
- POJ 3190 Stall Reservations 【贪心 优先队列】
题意:给出n头牛必须单独占用一台机器的时间段,问至少需要多少台机器 先按照每头牛的时间的x来排序,然后用一个优先队列(优先选取最小的)维护已经喂好的牛的最小的结束时间 比如现在优先队列里面有m头牛已经 ...
- poj3190 Stall Reservations (贪心+优先队列)
Cleaning Shifts Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) To ...
- [USACO06FEB] Stall Reservations 贪心
[USACO06FEB] Stall Reservations 贪心 \(n\)头牛,每头牛占用时间区间\([l_i,r_i]\),一个牛棚每个时间点只能被一头牛占用,问最少新建多少个牛棚,并且每头牛 ...
随机推荐
- 学习phpcms模板方法:
1.改官方模板,读里面的代码,改改它,看看有什么变化,如果不明白,去官方论 坛.查手册.专业人士还可以看数据库.2.复制实例代码,整理笔记,到实战的时候,就直接复制,改改参数即可.
- [WPF] 将普通的Library工程,改造成WPF Custom Control 的Library
1. 添加References PresentationCore PresentationFramework System.Xaml WindowsBase2. 修改AssemblyInfo.xsus ...
- Java操作属性文件,支持新增或更新多个属性
Java操作属性文件.支持新增或更新多个属性 一.更新或新增单个属性的方法 /** * 写入properties信息 * @param filePath 绝对路径(包含文件名称和后缀名) * @par ...
- Window vagrant 安装部署【转】
回想以前,想要安装个虚拟机是多么的麻烦.先要费尽心机找到想要的操作系统镜像文件,然后安装虚拟化软件,按照其提供的GUI界面操作一步步创建,整个过程费时费力.但是,自从使用了Vagrant以后,咱腰不酸 ...
- php:兄弟连之面向对象版图形计算器1
曾经看细说PHP的时候就想做这个,可是一直没什么时间,这次总算忙里偷闲搞了代码量比較多的project. 首先,文档结构,都在一个文件夹下就好了,我的就例如以下. 一開始,进入index.php文件. ...
- Web App 前端构建(纯净版)
asp.net 母版页: <!DOCTYPE html> <html> <head> <meta charset="utf-8" name ...
- 视图中的Layout使用(转)
1.母板页_Layout.cshtml 类似于传统WebForm中的.master文件,起到页面整体框架重用的目地 1.母板页代码预览 1 <!DOCTYPE html> 2 <ht ...
- silverlight 生成图表 WCF 解析XML代码.svc.cs 文件
silverlight 调用wcf 文件代码 private ListItem AnalyzeXML(string XMLCode, string Reportdate, string ChartNa ...
- keil中查看内存数据
1.工具栏中 view->Memory Windows 然后 c:0 表示读取0地址开始的代码区数据 d:0 表示读取0地址开始的数据区数据 x:0表示读取0地址开始的外部数据区
- windows安装Apache HTTP服务器报错:无法启动,因为应用程序的并行配置不正确
Apache HTTP服务器安装后报:无法启动,因为应用程序的并行配置不正确-(已解决) 0条评论 [摘要:本创做品,出自 “深蓝的blog” 专客,迎接转载,转载时请务必说明出处,不然有权穷究版 ...