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]\),一个牛棚每个时间点只能被一头牛占用,问最少新建多少个牛棚,并且每头牛 ...
随机推荐
- Java单元测试工具:JUnit4(一)(二)(三)(四)
Java单元测试工具:JUnit4(一)--概述及简单例子 Java单元测试工具:JUnit4(二)--JUnit使用详解 Java单元测试工具:JUnit4(三)--JUnit详解之运行流程及常用注 ...
- Learning Lua Programming (3) iMac下搭建Lua脚本最好的编码环境(代码补全,编译运行)
这篇文章参考自http://blog.sina.com.cn/s/blog_991afe570101rdgf.html,十分感谢原作者的伟大创造,本人亲测可行. 这篇文章记录一下如何在MAC系统环境下 ...
- JQuery.ajax一解
关于JQuery.ajax方法,好处也不用多说了,主要是想记下ajax中的一些参数: url:请求的目标地址,为一个字符串,格式为:http://localhost:端口号/User/方法名.eg:现 ...
- IEquatable(T) interface in .Net
原文:http://weblogs.asp.net/pawanmishra/iequatable-t-interface-in-net 泛型方法: public static bool AreEqua ...
- js判断浏览器类型以及版本
你知道世界上有多少种浏览器吗?除了我们熟知的IE, Firefox, Opera, Safari四大浏览器之外,世界上还有近百种浏览器. 几天前,浏览器家族有刚诞生了一位小王子,就是Google推出的 ...
- (转)[老老实实学WCF] 第四篇 初探通信--ChannelFactory
第四篇 初探通信--ChannelFactory 通过前几篇的学习,我们简单了解了WCF的服务端-客户端模型,可以建立一个简单的WCF通信程序,并且可以把我们的服务寄宿在IIS中了.我们不禁感叹WCF ...
- C++类中的静态成员变量与静态成员函数的使用
代码: #include <iostream> #include <string> #include <cstdio> using namespace std; c ...
- Shell脚本中单引号(‘)和双引号(“)的使用区别[转载]
shell可以识别4种不同类型的引字符号: 单引号字符' 双引号字符" 反斜杠字符\ 反引号字符` 1. 单引号 ( '' ) # grep Susan phonebook Susan Go ...
- c#快捷键设置和text输入限制
快捷键 使用KeyDonw事件 输入限制使用 KeyPress 事件 1.注意:如果是整个窗体的快捷键,一定要把窗体属性中的KeyPreview改为true private void textbox_ ...
- uva 10061 How many zero's and how many digits ?
How many zeros and how many digits? Input: standard input Output: standard output Given a decimal in ...