Luogu1090合并果子[单调队列]

/*
ID:hh826532
PROB:
LANG:C++
*/
#define _FILE_ ""
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<queue>
#include<time.h>
#include<fstream>
#include<string>
#include<set>
#include<list>
#include<stdlib.h>
#define fr(i,a,b) for(int i=a,_end_=b;i<=_end_;i++)
#define fd(i,a,b) for(int i=a,_end_=b;i>=_end_;i--)
#define frei(s) freopen(s,"r",stdin)
#define freo(s) freopen(s,"w",stdout)
#define ll long long
#define u unsigned
using namespace std;
#define rt return
#define inf 0x3f3f3f3f
#define infll 4557430888798830399ll
#define pc(x) putchar(x)
#define spc putchar(' ')
#define gc getchar()
#define ln pc('\n')
#define writeint(x) printf("%d",x)
#define lowbit(x) (x&(-x))
int readuint(){
    int s=0;
    char c=getchar();
    while(c<'0'||c>'9')c=gc;
    while(c>=48&&c<='9'){
        s=s*10+c-48;
        c=gc;
    }
    rt s;
}
int readint(){
    int s=0,k=1;
    char c=getchar();
    while((c<'0'||c>'9')&&c!='-')c=gc;
    if(c=='-'){
        k=-1;
        c=gc;
    }
    while(c>=48&&c<='9'){
        s=s*10+c-48;
        c=gc;
    }
    rt s*k;
}
void OPENFILE(){
    char FILENAME[50];
    if(strlen(_FILE_)==0)rt;
    sprintf(FILENAME,"%s.in",_FILE_);
    frei(FILENAME);
    sprintf(FILENAME,"%s.out",_FILE_);
    freo(FILENAME);
}
int alen,al,bl,blen,a[100010],b[100010],ans;
int main(){
    OPENFILE();
    alen=readuint();
    if(alen<2)
    {
        printf("0\n");
        rt 0;
    }
    fr(i,1,alen)
        a[i]=readuint();
    sort(a+1,a+alen+1);
    blen=1;
    al=3;
    bl=1;
    a[alen+1]=inf;
    ans=b[1]=a[1]+a[2];
    b[2]=inf;
    fr(i,1,alen-2)
    {
        blen++;
        b[blen]=0;
        fr(j,1,2)
            if(a[al]>b[bl]&&bl!=blen)
            {
                b[blen]+=b[bl];
                bl++;
            }
            else
            {
                b[blen]+=a[al];
                al++;
            }
        ans+=b[blen];
        b[blen+1]=inf;
    }
    printf("%d\n",ans);
    rt 0;
}

大家可以去codevs做一下
本人题目

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值