zoukankan      html  css  js  c++  java
  • NOI2010 航空管制

    http://www.lydsy.com/JudgeOnline/problem.php?id=2535

    贪心。

    对于第1个问,我们先建立拓扑图,对于如果a必须在b前起飞,那么连有向边b->a,并求出点的入度。

    将所有入度为0的点放在一个优先队列里,按最大起飞编号从大到小排序。

    我们从后往前考虑起飞的航班。

    取出优先队列中最大起飞编号最大的点,作为最后一个航班,并删去拓扑图中与他相连的边,如果有新的点的入度变成0,继续加入优先队列里。

    重复操作次即可。

    如果问第i个航班最早可以什么时候起飞,我们可以在优先队列中,如果发现最大起飞编号最大的点是第i号航班,我们看看最大起飞编号第二大的点能不能先放,如果能就放最大起飞编号第二大的点;否则这个位置就是第i个航班最早可以起飞的位置了。

    时间复杂度O(NM+N^2logN)。

    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    #include<fstream>
    #include<algorithm>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<queue>
    #include<stack>
    #include<map>
    #include<utility>
    #include<set>
    #include<bitset>
    #include<vector>
    #include<functional>
    #include<deque>
    #include<cctype>
    #include<climits>
    #include<complex>
    //#include<bits/stdc++.h>适用于CF,UOJ,但不适用于poj
      
    using namespace std;
     
    typedef long long LL;
    typedef double DB;
    typedef pair<int,int> PII;
    typedef complex<DB> CP;
     
    #define mmst(a,v) memset(a,v,sizeof(a))
    #define mmcy(a,b) memcpy(a,b,sizeof(a))
    #define fill(a,l,r,v) fill(a+l,a+r+1,v)
    #define re(i,a,b)  for(i=(a);i<=(b);i++)
    #define red(i,a,b) for(i=(a);i>=(b);i--)
    #define ire(i,x) for(typedef(x.begin()) i=x.begin();i!=x.end();i++)
    #define fi first
    #define se second
    #define m_p(a,b) make_pair(a,b)
    #define SF scanf
    #define PF printf
    #define two(k) (1<<(k))
     
    template<class T>inline T sqr(T x){return x*x;}
    template<class T>inline void upmin(T &t,T tmp){if(t>tmp)t=tmp;}
    template<class T>inline void upmax(T &t,T tmp){if(t<tmp)t=tmp;}
     
    const DB EPS=1e-9;
    inline int sgn(DB x){if(abs(x)<EPS)return 0;return(x>0)?1:-1;}
    const DB Pi=acos(-1.0);
     
    inline int gint()
      {
            int res=0;bool neg=0;char z;
            for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
            if(z==EOF)return 0;
            if(z=='-'){neg=1;z=getchar();}
            for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar());
            return (neg)?-res:res; 
        }
    inline LL gll()
      {
        LL res=0;bool neg=0;char z;
            for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
            if(z==EOF)return 0;
            if(z=='-'){neg=1;z=getchar();}
            for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar());
            return (neg)?-res:res; 
        }
     
    const int maxn=2000;
    const int maxm=10000;
     
    int n,m;
    int last[maxn+10];
     
    int du[maxn+10];
    int now,first[maxn+10];
    struct Tedge{int v,next;}edge[maxm+100];
    int used[maxn+10];
    int out[maxn+10];
     
    inline void addedge(int u,int v){now++;edge[now].v=v;edge[now].next=first[u];first[u]=now;}
     
    struct cmp{inline bool operator ()(PII a,PII b){return a.se<b.se;}};
    priority_queue<PII,vector<PII>,cmp> Q;
     
    int main()
      {
          freopen("plane.in","r",stdin);
            freopen("plane.out","w",stdout);
            int i,j;
            n=gint();m=gint();
            re(i,1,n)last[i]=gint();
            mmst(first,-1);now=-1;
            re(i,1,m){int a=gint(),b=gint();addedge(b,a);}
             
            re(i,1,n)du[i]=used[i]=0;
            re(i,0,now)du[edge[i].v]++;
            re(i,1,n)if(du[i]==0)Q.push(PII(i,last[i]));
            red(j,n,1)
              {
                int id=Q.top().fi,v;Q.pop();
                used[j]=id;
                for(i=first[id],v=edge[i].v;i!=-1;i=edge[i].next,v=edge[i].v){du[v]--;if(du[v]==0)Q.push(PII(v,last[v]));}
              }
            re(i,1,n)PF("%d ",used[i]);PF("
    ");
             
            int t;
            re(t,1,n)
              {
                re(i,1,n)du[i]=used[i]=0;
                while(!Q.empty())Q.pop();
                    re(i,0,now)du[edge[i].v]++;
                    re(i,1,n)if(du[i]==0)Q.push(PII(i,last[i]));
                    red(j,n,1)
                      {
                        int id=Q.top().fi,v;Q.pop();
                        if(id==t)
                              {  
                            if(!Q.empty() && j<=Q.top().se)
                              {
                                int newid=Q.top().fi;Q.pop();
                                used[j]=newid;
                                Q.push(PII(id,last[id]));
                              }
                            else
                              {
                                out[t]=j;
                                break;
                              }
                          }
                        else
                          used[j]=id;
                            for(i=first[used[j]],v=edge[i].v;i!=-1;i=edge[i].next,v=edge[i].v){du[v]--;if(du[v]==0)Q.push(PII(v,last[v]));}
                      }
              }
            re(i,1,n)PF("%d ",out[i]);PF("
    ");
            return 0;
      }
    View Code
  • 相关阅读:
    组合模式扩展,有选择的递归
    SQL分页查询【转】
    facade外观模式
    C#:几种数据库的大数据批量插入 faib
    装饰模式的扩展
    yeild之我理解
    数据库操作 sqlserver查询存储过程+分页
    SQL Server 索引结构及其使用(二)[转]
    SQL索引使用初步,(转)
    解决多集成,多子类,扩展等 装饰模式
  • 原文地址:https://www.cnblogs.com/maijing/p/4709861.html
Copyright © 2011-2022 走看看