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]\),一个牛棚每个时间点只能被一头牛占用,问最少新建多少个牛棚,并且每头牛 ...
随机推荐
- JDBC中Statement接口提供的execute、executeQuery和executeUpdate之间的区别
Statement 接口提供了三种执行 SQL 语句的方法:executeQuery.executeUpdate 和 execute.使用哪一个方法由 SQL 语句所产生的内容决定. 方法execut ...
- Ajax属性和函数以及 返回值之XML格式和文本格式(二)
(一) client请求文本之json格式:接收到json格式,再有js解析(详细先eval成对象,然后.就可以) var text = this.responseText; var book = e ...
- OLE操作Excel编译错误处理
Excel在公司用的很多,而这个东西我用的不是很好,就想用程序来处理,遇到很多错误.这几天研究了下OLE操作Excel.环境:VS2008 SP1+Excel 2007 加入OLE Type Li ...
- WPF DataGrid 之数据绑定--实例2
1.前台Grid定义 <!--数据绑定--> <DataGrid Grid.Row="1" Name="gridOne" Margin=&qu ...
- C#调用C++的DLL 数据类型转换
/C++中的DLL函数原型为 //extern "C" __declspec(dllexport) bool 方法名一(const char* 变量名1, unsig ...
- linux环境下jdk 安装以及maven私服搭建
1:准备资源 linux服务器,jdk和nexus 安装包 2:网络通畅,保持windows端和linux服务器端网络通畅. 3: 安装jdk和配置环境变量 进入到 ...
- js中的两个数字a,b求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。一共b个数字相加,例如用户输入2,5 s=2+22+222+2222+22222
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- hdu 2642 Stars
Problem Description Yifenfei is a romantic guy and he likes to count the stars in the sky. To make t ...
- 用C++写出hanoi
汉诺塔(港台:河內塔)是根据一个传说形成的數學问题有三根杆子A,B,C.A杆上有N个(N>1)穿孔圆盘,盘的尺寸由下到上依次变小.要求按下列规则将所有圆盘移至C杆:-每次只能移动一个圆盘-大的盘 ...
- centos mysql 数据存储目录安装位置
rpm -ql mysql查看安装位置 MYSQL默认的数据文件存储目录为/var/lib/mysql.假如要把目录移到/home/data下需要进行下面几步: 1.home目录下建立data目录 c ...