zoukankan      html  css  js  c++  java
  • POJ3190

    POJ3190

    将所有牛从小到大排序然后用优先队列(小根堆)依次记录插入的牛的结束时间,如果插入牛时起始时间大于首元素,ans不增加并弹出首元素。

    挺简单的。那么为什么我会写(水)这篇博客呢?

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<queue>
    #include<utility>
    #include<vector>
    #include<ctype.h>
    using namespace std;
    const int maxn=5e4+10;
    inline int read()
    {
        int w=0,x=0;char c=getchar();
        while(!isdigit(c))w|=c=='-',c=getchar();
        while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=getchar();
        return w?-x:x;
    }
    pair<pair<int,int>,int> a[maxn];
    int n,id[maxn];
    int main()
    {
        n=read();
        priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > q;
        for(int i=1;i<=n;i++)
        {
            a[i].first.first=read(),a[i].first.second=read();
            a[i].second=i;
        }
        int ans=0,tmp;
        sort(a+1,a+1+n);
        for(int i=1;i<=n;i++)
        {
            if(q.empty() or q.top().first>=a[i].first.first)
                tmp=++ans;
            else
                tmp=q.top().second,q.pop();
            q.push(make_pair(a[i].first.second,tmp));
            id[a[i].second]=tmp;
        }
        printf("%d
    ",ans);
        for(int i=1;i<=n;i++)
        printf("%d
    ",id[i]);
        return 0;
    }
    /*
    5
    1 10
    2 4
    3 6
    5 8
    4 7
    */

    如果您勇于实践,您会发现这个代码tle了。初步判定是我pair套多了。因为您随便在其他博客扒一个代码都会过。

    这是一个教训。

    还有就是注意priority_queue的排序方式。

    如果queue中没有元素而您用访问top的话会挂。

  • 相关阅读:
    axios baseURL
    TP5 nginx 配置
    Vue
    key
    curl openssl error
    vue use bulma
    《平凡的世界》
    《听听那冷雨》余光中
    心烦意乱
    祝你19岁生日快乐
  • 原文地址:https://www.cnblogs.com/BrotherHood/p/13304732.html
Copyright © 2011-2022 走看看