弱校联萌十一大决战之强力热身 B.Carries (二分 数学)

78 篇文章 0 订阅


B. Carries

Time Limit: 1000ms
Memory Limit: 65536KB

frog has n integers a1,a2,,an , and she wants to add them pairwise.
Unfortunately, frog is somehow afraid of carries (进位). She defines \emph{hardness} h(x,y) for adding x and y the number of carries involved in the calculation. For example, h(1,9)=1,h(1,99)=2 .
 Find the total hardness adding n integers pairwise. In another word, find
1i<jnh(ai,aj) .

Input

The input consists of multiple tests. For each test:
The first line contains 1 integer n ( 2n105 ). The second line contains n integers a1,a2,,an . ( 0ai109 ).

Output

For each test, write 1 integer which denotes the total hardness.

Sample Input

2
5 5
10
0 1 2 3 4 5 6 7 8 9

Sample Output

1
20

题目链接:http://www.bnuoj.com/v3/contest_show.php?cid=6865#problem/B

题目大意:h(a, b)表示计算a+b时进位的次数,要求题目给的那个公式的答案

题目分析:可以把每个数位分开讨论,比如现在求个位的时候,把每个数字对10取余的得到b数组再排序,然后对于b[i]我要找数组中大于等于10-b[i]的数字的个数,就是个位进十位的次数,这里找的时候直接二分,十位进百位,百位进千位也是同样的方法,n的范围是1e5,数字最多10位,所以总的复杂度位10n*log(n)

#include <cstdio>
#include <algorithm>
#define ll long long
using namespace std;
int const MAX = 1e5 + 5;
int a[MAX], b[MAX];

int main()
{
    int n;
    while(scanf("%d", &n) != EOF)
    {
        int ten = 1;
        ll ans = 0;
        for(int i = 0; i < n; i++)
            scanf("%d", &a[i]);
        for(int i = 1; i <= 9; i++)
        {
            ten *= 10;
            for(int j = 0; j < n; j++)
                b[j] = a[j] % ten;
            sort(b, b + n);
            for(int j = 0; j < n; j++)
                ans += (ll) ((b + n) - lower_bound(b + j + 1, b + n, ten - b[j]));
        }
        printf("%lld\n", ans);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值