SCU 4437 Carries 想法题

题目大意:

就是现在给出10万个1e9以内的非负整数, 问他们两两相加一共会有多少次进位(10进制加法), 例如99 + 1有2次进位, 10 + 19没有进位


大致思路:

其实就是一个简单题, 比赛的时候想了半天一直束缚在按位考虑的范围内没有想到以一整段后缀位来考虑...数位DP做傻了么...

这个题考虑进位的位置即可, 首先枚举进位是因为达到了10, 100, 1000..., 1e9

对于每一次枚举10^t, 就是在10万个数中对于每一个数x % 10^t, 所有的数模10^t之后, 二分以下不小于10^t - x的数有多少个即可

总体时间复杂度O(9*n*logn)


代码如下:

Result  :  Accepted     Memory  :  2684 KB     Time  :  644 ms

/*
 * Author: Gatevin
 * Created Time:  2015/10/1 19:33:14
 * File Name: A.cpp
 */
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;
int n;
int a[100010];
int rem[100010];
int get(int x)
{
    int ans = 0;
    int L = 1, R = n;
    while(L <= R)
    {
        int mid = (L + R) >> 1;
        if(rem[mid] < x)
        {
            ans = mid;
            L = mid + 1;
        }
        else R = mid - 1;
    }
    return n - ans; 
}
int main()
{
    while(~scanf("%d", &n))
    {
        for(int i = 1; i <= n; i++)
            scanf("%d", &a[i]);
        lint ans = 0;
        for(int i = 10; i <= 1e9 && i > 0; i *= 10)
        {
            for(int j = 1; j <= n; j++)
                rem[j] = a[j] % i;
            sort(rem + 1, rem + n + 1);
            for(int j = 1; j <= n; j++)
            {
                int now = a[j] % i;
                ans += get(i - now);
                if((now << 1) >= i) ans--;
            }
        }
        printf("%lld\n", ans / 2);
    }
    return 0;
}


你可能感兴趣的:(scu,4437,Carries)