[Usaco2006 Open] County Fair Events participate in festivals

Original title link https://www.lydsy.com/JudgeOnline/problem.php?id=1664

Can be found that if participated holiday \ (I \) then can participate holiday \ (J \) , if and only if \ (I \) end time holiday \ (l [i] \) or less \ (J \) holiday start time \ (t [J] \) . So we have for each \ (i \) , from \ (i \) departure to every legal \ (j \) connected to an edge, expressed attending \ (i \) then you can then participate \ (j \) .

Up to several questions asked to participate in the festival, so we set up the right side to 1, we built out \ (DAG \) to calculate the length of the answer is the longest on the road. Then we can build a super source, carried out with topological sorting \ (dp \) .

Time complexity is built FIG worst \ (O (\ FRAC {N (N-. 1)} {2}) \) , the scale also FIG \ (\ frac {N (N -1)} {2} \ ) , it is consistent with the complexity and map building topological sorting.

But we can a wholemetaphysicsOptimization: we can do for each \ (i \) are complete enumeration \ (1 \) ~ \ (the n-\) all festivals. Our first holiday in accordance with the start time \ (t \) from small to large sort, then for the first \ (i \) festivals, we find the first half of a start time greater than or equal \ (i \) holiday end time \ (J \) , then we \ (i \) is connected to the \ (J \) ~ \ (the n-\) can be. So generally do not complete enumeration of all the number 1 ~ n.

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#define maxn 10010
using namespace std;
 
inline int read(){
    register int x(0),f(1); register char c(getchar());
    while(c<'0'||'9'<c){ if(c=='-') f=-1; c=getchar(); }
    while('0'<=c&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
    return x*f;
}
 
struct node{ int t,l,id; }a[maxn];
vector<int> to[maxn];
int n,tmp[maxn];
int ind[maxn],dp[maxn],ans;
 
inline bool cmp(const node &x,const node &y){ return x.t<y.t; }
 
inline int binary(const int &val){
    if(a[n].t<val) return -1;
    int l=1,r=n,mid;
    while(l<r){
        mid=l+r>>1;
        if(a[mid].t<val) l=mid+1;
        else r=mid;
    }
    return l;
}
 
queue<int> q;
inline void topo(){
    q.push(0),dp[0]=0;
    while(q.size()){
        int u=q.front(); q.pop();
        for(register int i=0;i<to[u].size();i++){
            int v=to[u][i];
            dp[v]=max(dp[v],dp[u]+1),ind[v]--;
            if(!ind[v]) q.push(v);
        }
    }
}
 
int main(){
    n=read();
    for(register int i=1;i<=n;i++) a[i].t=read(),a[i].l=read(),a[i].id=i;
    sort(a+1,a+1+n,cmp);
     
    for(register int i=1;i<=n;i++){
        int end=a[i].l+a[i].t;
        int j=binary(end);
        if(j==-1) continue;
        for(register int k=j;k<=n;k++){
            to[a[i].id].push_back(a[k].id),ind[a[k].id]++;
        }
    }
    for(register int i=1;i<=n;i++) to[0].push_back(i),ind[i]++;
     
    topo();
    for(register int i=1;i<=n;i++) ans=max(ans,dp[i]);
    printf("%d\n",ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/akura/p/11309779.html
Recommended