结构体+优先队列+贪心
#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