Reducir enteros (pensar + codicioso)

https://ac.nowcoder.com/acm/contest/11163/C


La idea es muy buena ... Pero el error se ha modificado durante mucho tiempo.

Al revés es codicioso, el número actual puede tomar como máximo, y al menos 1 debe tomarse por cada número al frente. El número es log, hasta 30, y el número de veces es hasta 30. La violencia está bien.

Se trata principalmente del procesamiento del código. Primero, primero tome -1 para cada parte que deba tomarse para evitar algunos problemas de límites. Entonces tómalo codicioso.

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=50;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL p[maxn];
int main(void)
{
  LL t;t=read();
  p[0]=1;
  for(LL i=1;i<=40;i++) p[i]=p[i-1]*2;
  while(t--){
    LL H=read();
    if(H==2){
        printf("2\n");
        continue;
    }
    LL ans=1;
    H--;
    for(LL i=30;i>=0;i--){
        if(p[i]>H) continue;
        LL res=0;
        LL temp=H;
        LL cnt=0;

        for(LL j=1;j<=30;j++){///枚举次数
            if(temp-p[i]*j>=(1LL<<i)-1-1 ){

                cnt=max(cnt,j);

            }
            else{
                break;
            }
        }

        if(cnt==0) continue;

        res+=cnt;
        temp-=p[i]*cnt;

        for(LL j=i-1;j>=1;j--){
            temp-=p[j]*1;
            res++;
        }

        for(LL j=i-1;j>=0;j--){
            if(temp>0&&temp>=p[j]){
                res+=temp/p[j],temp%=p[j];
            }
        }
        ans+=res;
        break;
    }
    printf("%lld\n",ans);
  }
return 0;
}

 

Supongo que te gusta

Origin blog.csdn.net/zstuyyyyccccbbbb/article/details/115037584
Recomendado
Clasificación