位数差(二分)

 

 

 

 AC_Code

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int maxn = 1e5+5;
 5 const int inf=0x3f3f3f3f;
 6 #define rep(i,first,last) for(int i=first;i<=last;i++)
 7 #define dep(i,first,last) for(int i=first;i>=last;i--)
 8 
 9 int a[maxn];
10 int bit(int x){
11     int ans=0;
12     if( x==0 ) return 1;
13     while(x){
14         ans++;
15         x/=10;
16     }
17     return ans;
18 }
19 bool cmp(int x, int y){ return x>y; }
20 
21 int main()
22 {
23     int n;
24     scanf("%d",&n);
25     ll ans=0;
26     rep(i,1,n){
27         scanf("%d",&a[i]);
28         ans-=(n-i)*bit(a[i]);
29     }
30     sort(a+1,a+1+n,cmp);
31     rep(i,1,n-1){
32         int l=i+1,r=n;
33         ll now=bit(a[i]);
34         while( l<=r ){
35             int mid=(l+r)>>1;
36             if( bit(a[i]+a[mid])>now ) l=mid+1;
37             else r=mid-1;
38         }
39         ans += (r-i)*(now+1)+(n-r)*now;
40     }
41     printf("%lld\n",ans);
42     return 0;
43 }

 

posted @ 2020-02-20 10:19  swsyya  阅读(237)  评论(0编辑  收藏  举报

回到顶部