2017 清北济南考前刷题Day 4 morning

考场思路:

倒着算就是

可以对一个数-1

可以合并两个数

可以证明只有0和0才能执行合并操作

然后模拟

 

#include<cstdio>
#include<iostream>
#include<algorithm>

using namespace std;

#define N 1000001

void read(int &x)
{
    x=0; char c=getchar();
    while(!isdigit(c)) c=getchar();
    while(isdigit(c)) { x=x*10+c-'0'; c=getchar(); }
}

int a[N];

int main()
{
    //freopen("multiset.in","r",stdin);
//    freopen("multiset.out","w",stdout);
    int n;
    read(n); 
    int sum0=0,cnt=0,x;
    for(int i=1;i<=n;i++) 
    {
        read(x);
        if(!x) sum0++;
        else a[++cnt]=x;
    }
    sort(a+1,a+cnt+1);
    long long ans=0,gather=0;
    for(int i=1;i<=cnt;i++)
    {
        if(!a[i]) break;
        a[i]-=gather;
        x=a[i]; gather+=x; ans+=x;
        while(x) 
        {
            x--;
            if(sum0>1) sum0=sum0+1>>1;
            else break;
        }
        sum0++;
        while(i<cnt && a[i+1]-gather==0)
        {
            sum0++;
            a[++i]-=gather;
        }
    }
    while(sum0>1) ans++,sum0=sum0+1>>1;
    cout<<ans;
}
View Code

相关文章:

  • 2022-01-24
  • 2021-07-13
  • 2021-11-09
  • 2021-09-23
  • 2021-08-13
  • 2021-11-11
  • 2021-11-05
  • 2021-06-14
猜你喜欢
  • 2022-02-24
  • 2021-10-27
  • 2021-11-13
  • 2022-01-29
  • 2021-10-03
  • 2021-11-05
  • 2022-02-20
相关资源
相似解决方案