Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

留念,又学会了新的知识

Posted by lvweihua at 2017-08-09 18:51:21 on Problem 3190
#include<cstdio>                        //借助于堆进行实现
#include<queue>
#include<algorithm>
using namespace std;
const int MAXN=5005;
int N,use[MAXN];                        //use是定义的下标,因为输入不是严格按照顺序来的,排序后的下标肯定会变化,所以用这种方法更好
struct Cow{
    int l,r,pos;                        //开始、结束时间,机器编号
    bool operator<(const Cow &a)const{  //重载运算符,越早结束,优先级越高
        if(r==a.r)return l>a.l;
        return r>a.r;
    }
}cow[MAXN];
priority_queue<Cow>Q;
int cmp(const Cow &a,const Cow &b){
    if(a.l==b.l)return a.r<b.r;
    return a.l<b.l;
}
void solve(){
    sort(cow,cow+N,cmp);
    while(!Q.empty())Q.pop();   //使用前清空数据
    int stall=1;                //最终设备的编号
    use[cow[0].pos]=1;
    Q.push(cow[0]);
    for(int i=1;i<N;i++){
        Cow tmp=Q.top();
        if(cow[i].l>tmp.r){     //能跟着上一个继续用
            use[cow[i].pos]=use[tmp.pos]; //使用上一个的设备
            Q.pop();
            Q.push(cow[i]);
        }
        else{
            stall++;            //新增设备
            use[cow[i].pos]=stall;
            Q.push(cow[i]);
        }
    }
    printf("%d\n",stall);
    for(int i=0;i<N;i++)
        printf("%d\n",use[i]); //注意输出的下标
}
int main()
{
    while(scanf("%d",&N)!=EOF){
        for(int i=0;i<N;i++){
            scanf("%d%d",&cow[i].l,&cow[i].r);
            cow[i].pos=i;
        }
        solve();
    }
    return 0;
}


Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator