码迷,mamicode.com
首页 > 其他好文 > 详细

Stall Reservations POJ - 3190

时间:2020-03-21 21:23:32      阅读:65      评论:0      收藏:0      [点我收藏+]

标签:priority   turn   struct   color   str   typedef   std   tac   time   

结构体+优先队列+贪心

#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;
}

 

Stall Reservations POJ - 3190

标签:priority   turn   struct   color   str   typedef   std   tac   time   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!