solve 4/11

A Erase Numbers II

Code:KK

Thinking :KK

用ans表示当前最优答案,maxx表示遍历到的最大数字,一开始ans肯定等于a[ 1 ]+a[ 2 ],然后每次往后找,都把当前的a [ j ]拼到maxx后面,然后和答案比较,每次也更新maxx,时间复杂度o(n)

注意数据是1e19,会爆long long,用unsigned long long 就可以过。

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#define CLR(a,b) memset(a,b,sizeof(a));
const int inf=0x3f3f3f3f;
using namespace std;
typedef unsigned long long ll;
const int maxn= 6010;
int  T;
int n;
ll a[maxn],maxx,ans;
int d[maxn];
void cut(int i,ll val){
    d[i]=0;
    while(val>0)
    {
        val/=10;
        d[i]++;
    }
    if(d[i]==0)d[i]=1;
}
int main(){
    cin>>T;
    int cat=1;
    while(T--)
    {
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            //scanf("%lld",&a[i]);
            cin>>a[i];
            cut(i,a[i]);
        }
        maxx=max(a[1],a[2]);
        ans=a[1];
        int time=d[2];
        while(time--){
            ans*=10;
        }
        ans+=a[2];
        for(int j=3;j<=n;j++)
        {
            ll temp=0;
            temp =maxx;
            time=d[j];
            while(time--)
            {
                temp*=10;
            }
            temp+=a[j];
            ans=max(ans,temp);
            maxx=max(maxx,a[j]);
        }
        printf("Case #%d: ",cat++);
        cout<<ans;
        printf("\n");
    }
}
View Code

相关文章:

  • 2021-11-16
  • 2021-08-25
  • 2021-08-15
  • 2021-10-15
  • 2021-10-09
  • 2021-06-12
  • 2021-04-19
猜你喜欢
  • 2021-08-09
  • 2021-11-06
  • 2021-07-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案