[Explanations] Music Festival (Fenwick tree optimization dp)

[Explanations] Music Festival (Fenwick tree optimization dp)

Gym - 101908F

Meaning of the questions: There \ (n \) kinds of programs, each program has a start time and end time and weight. The case of a moment, only to see a show (not the border), at least once have seen all kinds of maximum benefit

Set \ (dp (s, i) \) said it had seen \ (s \) in the collection program, and have seen the end time of the program is \ (i \) of maximum benefit.

Transfer:
\ [DP (S, E [T] .r) = \ max (DP (S, K), DP (SE [T] .id, K)) + E [T] .val, K \ E Le [t] .l \]
Since \ (O (m ^ 3) \) can not be over 1000, it can be seen that the transfer was a prefix, so the direct optimization Fenwick tree like.

Note that the order of transfer, can be found in \ (R & lt \) can be used as transfer state. R may be the same situation, but does not affect the answer.

Complexity \ (O (m ^ 2 \ log m) \)

//@winlere
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;  typedef long long ll; 
inline int qr(){
      register int ret=0,f=0;
      register char c=getchar();
      while(!isdigit(c))f|=c==45,c=getchar();
      while(isdigit(c)) ret=ret*10+c-48,c=getchar();
      return f?-ret:ret;
}
const int maxn=86405;
const int inf=0x3f3f3f3f;
int dp[1<<10|1][maxn];

struct E{
      int l,r,id,val;
      inline bool operator <(const E&a)const{return r<a.r;}
}e[1001];
int n,cnt,len;
inline int que(const int&pos,const int*a){
      int ret=-inf;
      for(int t=pos;t>0;t-=t&-t) ret=max(ret,a[t]);
      return ret;
}

inline void upd(const int&pos,const int&tag,int*a){
      for(int t=pos;t<=len;t+=t&-t) a[t]=max(a[t],tag);
}

int main(){
#ifndef ONLINE_JUDGE
      freopen("in.in","r",stdin);
      //freopen("out.out","w",stdout);
#endif
      n=qr();
      memset(dp,0xcc,sizeof dp);
      for(int t=1;t<=n;++t){
        int m=qr();
        for(int i=1,t1,t2,t3;i<=m;++i) t1=qr(),t2=qr(),t3=qr(),e[++cnt]={t1,t2,t,t3};
      }
      sort(e+1,e+cnt+1);
      len=e[cnt].r+1;
      memset(dp[0],0,sizeof dp[0]);
      for(int t=1;t<=cnt;++t){
        for(int s=0;s<1<<n;++s){
          if(s&(1<<e[t].id>>1)){
            int g=s^(1<<e[t].id>>1);
            int f=max(que(e[t].l,dp[s]),que(e[t].l,dp[g]))+e[t].val;
            upd(e[t].r,f,dp[s]);
          }
        }
      }
      int g=que(len,dp[(1<<n)-1]);
      cout<<max(g,-1)<<endl;
      return 0;
}

Guess you like

Origin www.cnblogs.com/winlere/p/11725245.html