洛谷 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\n",ans);
79     return 0;
80 }
View Code

 

转载于:https://www.cnblogs.com/DriverLao/p/8921521.html

Stkcd [股票代码] ShortName [股票简称] Accper [统计截止日期] Typrep [报表类型编码] Indcd [行业代码] Indnme [行业名称] Source [公告来源] F060101B [净利润现金净含量] F060101C [净利润现金净含量TTM] F060201B [营业收入现金含量] F060201C [营业收入现金含量TTM] F060301B [营业收入现金净含量] F060301C [营业收入现金净含量TTM] F060401B [营业利润现金净含量] F060401C [营业利润现金净含量TTM] F060901B [筹资活动债权人现金净流量] F060901C [筹资活动债权人现金净流量TTM] F061001B [筹资活动股东现金净流量] F061001C [筹资活动股东现金净流量TTM] F061201B [折旧摊销] F061201C [折旧摊销TTM] F061301B [公司现金流1] F061302B [公司现金流2] F061301C [公司现金流TTM1] F061302C [公司现金流TTM2] F061401B [股权现金流1] F061402B [股权现金流2] F061401C [股权现金流TTM1] F061402C [股权现金流TTM2] F061501B [公司自由现金流(原有)] F061601B [股权自由现金流(原有)] F061701B [全部现金回收率] F061801B [营运指数] F061901B [资本支出与折旧摊销比] F062001B [现金适合比率] F062101B [现金再投资比率] F062201B [现金满足投资比率] F062301B [股权自由现金流] F062401B [企业自由现金流] Indcd1 [行业代码1] Indnme1 [行业名称1] 季度数据,所有沪深北上市公司的 分别包含excel、dta数据文件格式及其说明,便于不同软件工具对数据的分析应用 数据来源:基于上市公司年报及公告数据整理,或相关证券交易所、各部委、省、市数据 数据范围:基于沪深北证上市公司 A股(主板、中小企业板、创业板、科创板等)数据整理计算
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值