Stall Reservations POJ - 3190

结构体+优先队列+贪心

#include <iostream>
#include <cstdio>
#include <cstring>
#include <limits>
//#include <stack>
#include<queue>
#include <algorithm>
#define endl ‘\n‘
#define _for(i,a,b) for(int i=a;i<b;i++)
using namespace std;
const int N = 1e5+5;
typedef long long ll;
int n;
struct Node{
    int id,l,r,i;
    bool operator < (const Node &o){
        return i<o.i;
    }
}a[N];
struct Stall{
    int id,time;
    bool operator<(const Stall &o)const{
        return time>o.time;
    }
}b[N];
bool cmp(Node aa,Node bb){
    if( aa.l!=bb.l ) return aa.l<bb.l;
    else return aa.r<bb.r;
}
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n;
    _for(i,1,n+1){
        a[i].i = i;
        cin>>a[i].l>>a[i].r;
        if( a[i].r<a[i].l ) swap( a[i].r,a[i].l );
    }
    sort(a+1,a+1+n,cmp);
    priority_queue<Stall> pq;
    a[1].id = 1;
    Stall tems; tems.id = 1,tems.time = a[1].r;
    pq.push( tems );
    int res_num = 1;
    _for(i,2,n+1){
        int Best = pq.top().time,ID = pq.top().id; pq.pop();
//        cout<<"cow "<<i<<":  l,r =" <<a[i].l<<" "<<a[i].r<<"  best choice is "<<ID<<endl;
        if( a[i].l > Best ){
            a[i].id = ID;
            Stall tem ; tem.id = ID,tem.time = a[i].r;
            pq.push(tem);
        }
        else{//要加新的
            Stall tem ; tem.id = ID,tem.time = Best;
            pq.push(tem);
            tem.id = ++res_num ; a[i].id = tem.id;
            tem.time = a[i].r ;
            pq.push(tem);
        }
    }
    sort(a+1,a+n+1);
    cout<<res_num<<endl;
    _for(i,1,n+1) cout<<a[i].id<< endl;
    return 0;
}

原文地址:https://www.cnblogs.com/SunChuangYu/p/12542025.html

时间: 2024-10-28 18:37:21

Stall Reservations 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

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

挑战程序设计竞赛2.2习题:Stall Reservations POJ - 3190

Stall Reservations 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

Greedy:Stall Reservations(POJ 3190)

牛挤奶 题目大意:一群牛很挑剔,他们仅在一个时间段内挤奶,而且只能在一个棚里面挤,不能与其他牛共享地方,现在给你一群牛,问你如果要全部牛都挤奶,至少需要多少牛棚? 这一题如果把时间区间去掉,那就变成装箱子问题了(装箱子要用Splay维护),但是现在规定了区间和时间,我们只用贪婪算法就可以了,每次只用找到最小就可以了(用堆维护). PS:一开始我想到用AVL去维护,的都不知道我在想什么,简直浪费时间 1 #include <iostream> 2 #include <functional&

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贪心

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 3190 Stall Reservations (优先队列)C++

Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7646   Accepted: 2710   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 3190 Stall Reservations(贪心)

Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3590   Accepted: 1284   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 3190 Stall Reservations【贪心】

POJ 3190 题意: 一些奶牛要在指定的时间内挤牛奶,而一个机器只能同时对一个奶牛工作.给你每头奶牛的指定时间的区间(闭区间),问你最小需要多少机器.思路:先按奶牛要求的时间起始点进行从小到大排序,然后维护一个优先队列,里面以已经开始挤奶的奶牛的结束时间早为优先.然后每次只需要检查当前是否有奶牛的挤奶工作已经完成的机器即可,若有,则换那台机器进行工作.若没有,则加一台新的机器.注意:利用priority_queue<Node,vector<Node>,less<Node>