zoukankan      html  css  js  c++  java
  • 洛谷 2048 BZOJ 2006 [NOI2010]超级钢琴

    【题解】

      贪心题。设五元组(mx,pos,l,r1,r2)表示最大值为mx,取得最大值的区间右端点为pos,区间左端点为l,区间右端点的可选区间为[r1,r2]. 每次从堆里拎出最大值,然后把这个区间拆了,因为选了[l,pos]这个区间之后就不能再选它了。我们得往堆了丢俩新的五元组(mx',pos',l,r1,pos-1)以及(mx'',pos'',l,pos+1,r2),至于mx',mx'',pos',pos''的获得,用ST表即可。

      

     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<cmath>
     4 #include<cstring>
     5 #define N 1000010
     6 #define rg register
     7 #define LL long long
     8 using namespace std;
     9 int n,k,L,R,tot,pos[20][N],f[20][N];
    10 LL ans;
    11 struct heap{
    12     LL d,p,l,r1,r2;
    13 }h[N];
    14 struct rec{
    15     LL d,p;
    16 };
    17 inline int read(){
    18     int k=0,f=1; char c=getchar();
    19     while(c<'0'||c>'9')c=='-'&&(f=-1),c=getchar();
    20     while('0'<=c&&c<='9')k=k*10+c-'0',c=getchar();
    21     return k*f;
    22 }
    23 
    24 inline void up(int x){
    25     int fa;
    26     while((fa=x>>1)&&h[fa].d<h[x].d) swap(h[fa],h[x]),x=fa;
    27 }
    28 inline void down(int x){
    29     int son;
    30     while((son=x<<1)<=tot){
    31         if(son<tot&&h[son+1].d>h[son].d) son++;
    32         if(h[son].d>h[x].d) swap(h[son],h[x]),x=son;
    33         else return;
    34     }
    35 }
    36 inline rec query(int l,int r){
    37     r=min(r,n); l=max(1,l);
    38     int k=log2(r-l+1); rec tmp; tmp.d=tmp.p=0;
    39     if(l>r) return tmp;
    40     if(f[k][l]>f[k][r-(1<<k)+1]){
    41         tmp.d=f[k][l]; tmp.p=pos[k][l];
    42         return tmp;
    43     }
    44     else{
    45         tmp.d=f[k][r-(1<<k)+1]; tmp.p=pos[k][r-(1<<k)+1];
    46         return tmp;
    47     }
    48 }
    49 int main(){
    50     memset(f,128,sizeof(f)); f[0][0]=0;
    51     n=read(); k=read(); L=read()-1; R=read()-1;
    52     for(rg int i=1;i<=n;i++) f[0][i]=read()+f[0][i-1],pos[0][i]=i;
    53     for(rg int i=1;i<=log2(n);i++)
    54         for(rg int j=1;j<=n-(1<<i)+1;j++)
    55             if(f[i-1][j]>f[i-1][j+(1<<(i-1))])
    56                 f[i][j]=f[i-1][j],pos[i][j]=pos[i-1][j];
    57             else f[i][j]=f[i-1][j+(1<<(i-1))],pos[i][j]=pos[i-1][j+(1<<(i-1))];
    58     for(rg int i=1;i<=n-L;i++){
    59         rec tmp=query(i+L,i+R);
    60         h[++tot]=(heap){tmp.d-f[0][i-1],tmp.p,i,i+L,min(n,i+R)};
    61         up(tot);
    62     }
    63     for(rg int i=1;i<=k;i++){
    64         heap now=h[1];
    65         ans+=now.d;
    66         h[1]=h[tot--]; if(tot) down(1);
    67         if(now.p-1>=now.r1){
    68             rec tmp=query(now.r1,now.p-1);
    69             h[++tot]=(heap){tmp.d-f[0][now.l-1],tmp.p,now.l,now.r1,now.p-1};
    70             up(tot);
    71         }
    72         if(now.r2>=now.p+1){
    73             rec tmp=query(now.p+1,now.r2);
    74             h[++tot]=(heap){tmp.d-f[0][now.l-1],tmp.p,now.l,now.p+1,now.r2};
    75             up(tot);
    76         }
    77     }
    78     printf("%lld
    ",ans);
    79     return 0;
    80 }
    View Code
  • 相关阅读:
    Ubuntu18.04 server 双网卡,一个设置为静态IP, 一个设置为动态IP
    【转载】 Ubuntu 中开机自动执行脚本的两种方法
    Ubuntu18.04server 双网卡,开机自动设置路由并启动校园网网络认证程序(Ubuntu开机自动设置路由,开机自启动应用程序)
    mpi4py 官方使用手册
    【转转】 Huber Loss
    【转载】 MPP大规模并行处理架构详解
    并行强化学习设计的一些想法
    人工智能必备数学基础-回归方程定义
    tf.gather()、tf.gather_nd()、tf.batch_gather()、tf.where()和tf.slice()
    C# 计算时间间隔
  • 原文地址:https://www.cnblogs.com/DriverLao/p/8921521.html
Copyright © 2011-2022 走看看