poj3190区间类贪心+优先队列
题意:每个奶牛产奶的时间为A到B,每个奶牛产奶时要占用一间房子,问n头奶牛产奶共需要多少房子,并输出每头奶牛用哪间房子
分析:这题就是一个裸的贪心,将奶牛按开始时间进行排序即可,但考虑一下数据范围,我们可以用一个优先队列来进行维护,在优先队列中我们按照奶牛的结束时间最小构造小顶堆,然后判断新进来的元素的开始时间是否比最小的结束时间大,若是,加入队列,修改队列,若不是,加入对列,并且计数加1
注意自定义类型的优先队列的维护方法。详见:http://blog.sina.com.cn/s/blog_4e5157120100vn7b.html
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <queue>
#include <stack>
using namespace std;
const int maxn=;
//注意自定义类型在优先队列中的用法
typedef struct P
{
int start,over,id;
friend bool operator<(P a,P b)
{
return a.over>b.over; //小顶堆
}
}P;
P point[maxn];
bool cmp(P a,P b)
{
return a.start<b.start;
}
int t[maxn];
int main()
{
int n;
while(cin>>n)
{
for(int i=;i<n;i++)
{
scanf("%d%d",&point[i].start,&point[i].over);
point[i].id=i;
}
memset(t,,sizeof(t));
sort(point,point+n,cmp);
priority_queue<P> que;
int r=; //统计个数
que.push(point[]);
t[point[].id]=++r;
for(int i=;i<n;i++)
{
P node=que.top();
if(node.over<point[i].start){
t[point[i].id]=t[node.id];
que.pop();
que.push(point[i]);
}
else{
t[point[i].id]=++r;
que.push(point[i]);
}
}
cout<<r<<endl;
for(int i=;i<n;i++)
printf("%d\n",t[i]);
}
return ;
}
poj3190区间类贪心+优先队列的更多相关文章
- poj3190 Stall Reservations (贪心+优先队列)
Cleaning Shifts Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) To ...
- UVA 11134 - Fabled Rooks(贪心+优先队列)
We would like to place n rooks, 1 ≤ n ≤ 5000, on a n×n board subject to the following restrict ...
- CodeForces 137C【贪心+优先队列】
这种区间的贪心好像都出"烂"了? 不过还是想写一下... 先按照区间左端点排序一下,然后搞个优先队列维护当前最小的右端点. #include <bits/stdc++.h&g ...
- hihoCoder 1309:任务分配 贪心 优先队列
#1309 : 任务分配 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定 N 项任务的起至时间( S1, E1 ), ( S2, E2 ), ..., ( SN, ...
- C. Playlist Educational Codeforces Round 62 (Rated for Div. 2) 贪心+优先队列
C. Playlist time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- HDU 6438 网络赛 Buy and Resell(贪心 + 优先队列)题解
思路:维护一个递增队列,如果当天的w比队首大,那么我们给收益增加 w - q.top(),这里的意思可以理解为w对总收益的贡献而不是真正获利的具体数额,这样我们就能求出最大收益.注意一下,如果w对收益 ...
- 贪心+优先队列 HDOJ 5360 Hiking
题目传送门 /* 题意:求邀请顺序使得去爬山的人最多,每个人有去的条件 贪心+优先队列:首先按照l和r从小到大排序,每一次将当前人数相同的被邀请者入队,那么只要能当前人数比最多人数条件小,该人能 被邀 ...
- [POJ1456]Supermarket(贪心 + 优先队列 || 并查集)
传送门 1.贪心 + 优先队列 按照时间排序从前往后 很简单不多说 ——代码 #include <queue> #include <cstdio> #include <i ...
- Painting The Fence(贪心+优先队列)
Painting The Fence(贪心+优先队列) 题目大意:给 m 种数字,一共 n 个,从前往后填,相同的数字最多 k 个在一起,输出构造方案,没有则输出"-1". 解题思 ...
随机推荐
- Food on the Plane
Food on the Plane time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- JAVA-面向对象--封装
1. 如果一个类中没有定义构造函数,会自动加上一个空参的默认构造函数 如果定义了一个构造函数,默认的空参构造函数就没有了. 2. this 当成员变量与局部变量重名的时候,可以使用this来区分.th ...
- 给Notepad++ 加带图标右键菜单
给Notepad++ 加带图标右键菜单 方式一: 拷贝以下代码建立一个reg文件,替换相关路径,保存,双击运行加入注册表 Windows Registry Editor Version 5.00 [H ...
- monkeyrunner对比屏幕局部图像.getSubImage()
monkeyrunner对比屏幕局部图像.getSubImage() monkeyrunner执行测试时使用.takeSnapshot()截图,默认截取整个屏幕,包含了系统的状态栏.真实手机状态栏中包 ...
- asp 正则替换函数
Function RegExpTest(patrn,str1,strng) Dim regEx',str1 ' 建立变量 'str1 = "The quick brown fox jumpe ...
- VIJOS P1647 不差钱 SBT
[描述] 同学们一起看了小品<不差钱>,LX神突发奇想,想刁难一下十八居士,他让十八居士模拟一下点菜的过程. [输入格式] 输入第一行为一个数price,表示价钱大于price的菜赵本山都 ...
- Nginx配置-伪静态,隐藏index.php大入口
server { listen ; server_name ; root E:/www/wvtuan/; index index.php index.html; log_not_found off; ...
- kill -QUIT <pid>
On Solaris and Linux a thread dump is also printed if the J2SE process receives a QUIT signal. So ki ...
- 未出现的子串(unapeared)
未出现的子串(unapeared) 题目描述 有一个长度为n的数字串,其中会出现数字1,2,3,…,q(5≤q≤9).现在的问题是,需要求出一个长度最小的串(出现的数字也是1-q),使得该串不是这个数 ...
- Android客户端通过socket与服务器通信
android端--Client package com.sec.chatroomandroid; import java.io.BufferedReader; import java.io.Buff ...