poj 3190 贪心 + 优先队列

题意:一群牛,每头牛有固定的时间[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;
}

  

时间: 2024-10-29 16:15:02

poj 3190 贪心 + 优先队列的相关文章

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

Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4434   Accepted: 1588   Special Judge Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time in

poj 3253 贪心+优先队列【哈夫曼思想】

题目链接 题目大意: 大意:需要把一根长木棍锯成一些短木棍短木棍的长度是告诉你的每一次锯的花费为要锯的改段的长度问最小花费比如n个小木棍长度分别5 8 8也就是相当于你有一根21的木棍 现在需要把它锯成 上述的三段每次只能把一个木棍锯成两段比如21可以锯成13 和 8 但是由于选择的是21 所以花费为21第二次把13 锯成5和8 花费 为13总花费为21 + 13 = 34 分析:其实我们可以逆向思维想在有n跟木棍现在想要把他们拼成一跟每一次的花费就是这两段的和那么我们根据贪心的思想肯定是每次选

Sunscreen (poj 3614 贪心+优先队列)

Language: Default Sunscreen Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4499   Accepted: 1565 Description To avoid unsightly burns while tanning, each of the C (1 ≤ C ≤ 2500) cows must cover her hide with sunscreen when they're at th

POJ 2431 贪心+优先队列

题意:一辆卡车距离重点L,现有油量P,卡车每前行1米耗费油量1,途中有一些加油站,问最少在几个加油站加油可使卡车到达终点或到达不了终点. 思路:运用优先队列,将能走到的加油站的油量加入优先队列中,油不够时加入优先队列中数值最大的油,如果油不够时队列里为空则到达不了. #include<cstdio> #include<queue> #include<algorithm> using namespace std; priority_queue<int> que

Stall Reservations (poj 3190 贪心)

Language: Default Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3394   Accepted: 1215   Special Judge Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over so

POJ 3190 Stall Reservations(贪心+优先队列优化)

Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time interval A..B (1 <= A <= B <= 1,000,000), which includes both times A and B. Obviously, FJ must create a reserv

POJ 3190 Stall Reservations-奶牛分栏(区间贪心,优先队列)

http://poj.org/problem?id=3190 题目大意:每一只奶牛要求在时间区间[A,B]内独享一个牛栏.问最少需要多少个牛栏. 贪心策略是优先满足A最小的奶牛,维持一个牛栏B最小堆,将新来的奶牛塞进B最小的牛栏里. <p><span style="color: rgb(51, 51, 51); font-family: 'Microsoft Yahei', 'Helvetica Neue', Helvetica, Arial, sans-serif; line

POJ 3190 Stall Reservations贪心

POJ 3190 Stall Reservations贪心 Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time interval A..B (1 <= A <= B <= 1,000,000), which includes both times A and B. Obvi

poj 2431 Expedition (贪心+优先队列)

Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6890   Accepted: 2065 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poor drivers, the cows unfortunately managed to