题解:先整体考虑可以推出和顺序无关,对于当前i考虑bit(a[i]+a[j])比bit(a[i])大一#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1E5 + 7;
ll bit(ll x)
{
if(!x) return 1;
ll cnt = 0;
while(x) {
x /= 10;
cnt ++;
} return cnt;
}
ll a[N];
int main()
{
int n;
ll res = 0;
scanf("%d",&n);
for(int i = 1;i <= n;i ++) {
scanf("%lld",&a[i]);
res -= (n-i) * bit(a[i]);
}
sort(a+1, a+1+n, greater<ll>());
for(int i = 1;i <= n-1;i ++) {
int l = i+1, r = n;
ll now = bit(a[i]);
while(l <= r) {
int j = (l + r) / 2;
if(bit(a[i]+a[j]) > now) l = j + 1;
else r = j - 1;
}
res += (r - i) * (now + 1) + (n - r) * now;
}
printf("%lld\n", res);
return 0;
}
的那个分界点,二分找。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1E5 + 7;
ll bit(ll x)
{
if(!x) return 1;
ll cnt = 0;
while(x) {
x /= 10;
cnt ++;
} return cnt;
}
ll a[N];
int main()
{
int n;
ll res = 0;
scanf("%d",&n);
for(int i = 1;i <= n;i ++) {
scanf("%lld",&a[i]);
res -= (n-i) * bit(a[i]);
}
sort(a+1, a+1+n, greater<ll>());
for(int i = 1;i <= n-1;i ++) {
int l = i+1, r = n;
ll now = bit(a[i]);
while(l <= r) {
int j = (l + r) / 2;
if(bit(a[i]+a[j]) > now) l = j + 1;
else r = j - 1;
}
res += (r - i) * (now + 1) + (n - r) * now;
}
printf("%lld\n", res);
return 0;
}
题目链接:https://www.nowcoder.com/acm/contest/20/C
题目描述
给一个数组
{a},定义
h(a,b)为在十进制下
a + b 与
a 的位数差,求
,0的位数为1。
![](https://i-blog.csdnimg.cn/blog_migrate/25e322fec137d024b2eb918e2ad502b1.png)
输入描述:
第一行读入一个正整数 n (1 <= n <= 105)。
第二行读入 n 个非负整数,第 i 个表示a[i] (0 <= a[i] <= 108)。
输出描述:
一行表示答案。
示例1
输入
10 0 1 2 3 4 5 6 7 8 9
输出
20