样例过了38.89%,求佬help
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 5e5+10;
int a[N];int n,m;
//判断一个数的位数
signed wei(int x)
{
int cnt=1;
while(x/10>0)
{
cnt++;
x/=10;
}
return cnt;
}
//将当前a数组里面的位数和之和计算出来
int add()
{
int sum = 0;
for(int i = 0; i < n;i ++ )
{
int x=wei(a[i]);
int t = a[i];
while(x--)
{
sum+=t%10;
t/=10;
}
}
return sum;
}
int res=1e6;
signed main()
{
cin >> n>> m;
for(int i = 0;i < n;i ++ ) cin>>a[i];
//m次,每次减一
while(m--)
{
int y = 1;
for(int i = 1;i <= 9;i ++ )
{
y*=10; //y==10是个位减一,y==100是十位往下减一 ,y==...
int t = 0;
for(int j = 0;j < n;j ++ )
{
int x=a[j]%y; //如1001和121两位数,如果各位减到0那么就是1000和120,那么接下来比较十位,一个是0,一个是2
//那么就用含2的那个数继续减一
if(x >= 1) { t=1;a[j]--;int sum=add();res=min(res,sum);break; }
//上面这一步是储存最小的位数
}
if(t==1) break;
}
}
cout<<res;
return 0;
}
全部评论
(1) 回帖