题意:给定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 贪心 + 优先队列的更多相关文章

  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 贪心+自定义优先级的优先队列(求含不重叠子序列的多个序列最小值问题)

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

  3. POJ 3190 Stall Reservations (优先队列)C++

    Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7646   Accepted: 271 ...

  4. poj 3190 Stall Reservations

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

  5. POJ 3190 Stall Reservations【贪心】

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

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

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

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

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

  8. poj3190 Stall Reservations (贪心+优先队列)

    Cleaning Shifts Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) To ...

  9. [USACO06FEB] Stall Reservations 贪心

    [USACO06FEB] Stall Reservations 贪心 \(n\)头牛,每头牛占用时间区间\([l_i,r_i]\),一个牛棚每个时间点只能被一头牛占用,问最少新建多少个牛棚,并且每头牛 ...

随机推荐

  1. pyqt lineedit右边显示按钮效果

    from PyQt4 import QtGui, QtCore class ButtonLineEdit(QtGui.QLineEdit): buttonClicked = QtCore.pyqtSi ...

  2. python lcd 时间显示

    #!/usr/bin/python # QStopWatch -- a very simple stop watch # Copyright (C) 2006 Dominic Battre <d ...

  3. Hibernate中Criteria的用法

    概念 Criterion 是 Criteria 的查询条件.Criteria 提供了 add(Criterion criterion) 方法来添加查询条件. Criterion 接口的主要实现包括: ...

  4. AngularJs学习笔记3——自定义指令

    指令 概述: 前面也说过一些常用指令,用于快速入门.现在详细总结一下:指令用于实现各种页面的操作,是对于底层DOM操作的封装,扩展了HTML的行为,实现页面交互以及数据绑定. 指令是一种执行的信号,一 ...

  5. 用Scertify Professional实现代码审查

    用Scertify Professional实现代码审查 作者:chszs,转载需注明.博客主页: http://blog.csdn.net/chszs Scertify Professional是一 ...

  6. Opencv 简单的图片显示

    #include <opencv\cv.h> #include <opencv\highgui.h> #include <opencv\cxcore.h> int ...

  7. install samba on crux without net.

    1. 解压文件 tar -xvfz samba-.tar.gz 2. 进入目录 cd samba- 3. 配置 ./configure 4. 编译 make 5. 安装 make install

  8. Javascript进阶篇——(DOM—getAttribute()、setAttribute()方法)—笔记整理

    getAttribute()方法通过元素节点的属性名称获取属性的值.语法: elementNode.getAttribute(name) 1. elementNode:使用getElementById ...

  9. GDI+编程的10个基本技巧(转)

    创建绘图表面 创建绘图表面有两种常用的方法.下面设法得到PictureBox的绘图表面. private void Form1_Load(object sender, System.EventArgs ...

  10. Jquery:强大的选择器<二>

    今天跟着资料做了一个示例,为什么我感觉自己做的没书上的好看呢?好吧,我承认自己对css样式只懂一点皮毛,我也不准备深度的去学习它,因为……公司有美工嘛! 这个小示例只是实现了元素的隐藏和显示.元素cl ...