题意:给定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. 详细介绍如何使用kindEditor编辑器

    今天群里的朋友问我能不能写个kindEditor编辑器的使用教程,说是弄了半天没有搞定.由于PHP啦后台正好用了这个编辑器,我有写经验,正好教他的同时写出来分享给大家. kindEditor编辑器是一 ...

  2. (转)直接保存对象的数据库——db4o

    在实际开发中,数据的存储是必不可少的,常用的有数据库存储和文件存储.数据库目前有关系型数据库和文档型数据库(No-SQL).关系型数据库以字段.类型.约束.表关系来存储和管理数据,比较常见的比如Ora ...

  3. MySql索引原理与使用大全

    林炳文Evankaka原创作品. 转载请注明出处http://blog.csdn.net/evankaka 一.索引介绍 索引是对数据库表中一列或多列的值进行排序的一种结构.在关系数据库中,索引是一种 ...

  4. python 间谍程序传输文件 socket编程

    本程序实现了,把目标机器的某个目录(可控)的所有的某种类型文件(可控)全部获取并传到己方的机器上. 1.用了base64的encode(infile,outfile)加密,以及decode(infil ...

  5. C语言随记-1

    涉及指针.数组.函数指针 几种声明形式 int *a[5]; // a是一个有5个元素的数组,每个元素是整数类型指针(int *) int *a[] = {0x100, 0x104, 0x108, 0 ...

  6. SSH公钥私钥安全通讯原理

    客户端在访问服务器的时候,防止通讯信息被截取,进行加密处理通讯. 在服务器上会有两把钥匙,公钥和私钥.公钥可以对所有公开,私钥只有服务器自己知道, 并且公钥产生的密文只能通过私钥才能解开 1:客户端发 ...

  7. MD5 Tool 工具类

    package com.example.secret.tools; import java.io.UnsupportedEncodingException; import java.security. ...

  8. oc随笔四:NSString、NSNumber

    #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { ...

  9. sdsdd

    while(scanf("%d",&n)!=EOF) { res=-; level(tmp,n,res,); printf("%d/n",res); }

  10. 在shell中运行以不同方式运行脚本

    在shell当中,可以有3中方式运行脚本: 1 . ./script_name 或者source ./script_name 2 直接./script_name 3 ./script_name &am ...