• 进位


    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
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <cmath>
    #include <vector>
    #include <queue>
    #include <algorithm>
    #include <set>
    using namespace std;
    
    #define MM(a) memset(a,0,sizeof(a))
    
    typedef long long LL;
    typedef unsigned long long ULL;
    const int maxn = 2*1e5+5;
    const int mod = 1000000007;
    const double eps = 1e-7;
    const double pi = 3.1415926;
    const int e =  2.718281828;
    
    int cont[15];
    int arr[maxn];
    int data[2][maxn];
    int num[maxn];
    void Init()
    {
        cont[0] = 1;
        for(int i=1; i<10; i++)
            cont[i] = cont[i-1]*10;
    }
    int main()
    {
        int m;
        Init();
        while(~scanf("%d",&m))
        {
            for(int i=0; i<m; i++)
                scanf("%d",&arr[i]);
            LL ans=0, ret=0;
            for(int i=1; i<10; i++)
            {
                int sum = 0;
                for(int j=0; j<m; j++)
                {
                    data[0][j] = arr[j]%cont[i];
                    data[1][j] = cont[i] -data[0][j];
                    num[sum++] = data[0][j];
                    if(data[0][j] >= data[1][j])
                        ans++;
                }
                sort(num, num+sum);
                for(int i=0; i<m; i++)
                {
                    int temp = lower_bound(num, num+sum,data[1][i])-num;
                    ret += sum - temp;
                }
            }
            cout<<(ret-ans)/2<<endl;
        }
        return 0;
    }
  • 相关阅读:
    JavaWeb--基本概念
    启动Tomcat错误:The JRE_HOME environment variable is not defined correctly
    时间管理-1-总有一种情况你经历过
    时间管理
    非对称加密-支付宝 堆成加密
    TreeMap底层实现和原理-红黑树
    @Autowired报错原因分析和4种解决方案!
    布隆过滤器的设计原理
    springboot 日期参数前后台转换问题
    设计模式
  • 原文地址:https://www.cnblogs.com/chen9510/p/4852072.html
Copyright © 2020-2023  润新知