Stall Reservations POJ - 3190
Posted sunchuangyu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Stall Reservations POJ - 3190相关的知识,希望对你有一定的参考价值。
结构体+优先队列+贪心
#include <iostream> #include <cstdio> #include <cstring> #include <limits> //#include <stack> #include<queue> #include <algorithm> #define endl ‘ ‘ #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的主要内容,如果未能解决你的问题,请参考以下文章
POJ--3176 Stall Reservations(DP)