Carries


题目描述:

B. Carries

Time Limit: 1000ms

Memory Limit: 65536KB
64-bit integer IO format: %lld Java class name: Main

Submit Status

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
∑1≤i

题解:

当时真是傻..
首先肯定是按照位来计算,然后麻烦的是进位的计算.
但是但是!! 进位是一个很好搞的东西,排个序,那么比一个特定数大的值都可以进位!!!! 之后就很好写了呗. 可以每次排序,然后二分找,也可以用两个指针o(n)搞

重点:

想到了进位不会,那么继续探索怎么搞定进位?大于一个数就会进位,那么后面的一段都会进位

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <ctype.h>
#include <limits.h>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <bitset>
#define CLR(a) memset(a, 0, sizeof(a))
#define REP(i, a, b) for(ll i = a;i < b;i++)
#define REP_D(i, a, b) for(ll i = a;i <= b;i++)

typedef long long ll;

using namespace std;

const ll maxn = 1e5 + 100;
struct info
{
    ll number, weight;
    info(ll _number = 0, ll _weight = 0)
    {
        number = _number;
        weight = _weight;
    }
    bool operator < (const info b) const
    {
        return weight < b.weight;
    }
};
info a[maxn];
ll b[maxn], n;
ll num[maxn][10];
ll p10[20];

void solve()
{
    ll ans = 0;
    for(ll s = 1;s<=9;s++)
    {
        for(ll i = 1;i<=n;i++)
        {
            a[i].number = (b[i]/p10[s-1])%10;
            a[i].weight = b[i]%p10[s-1];
        }
        sort(a+1, a+1+n);
//                    printf("----\n");
//            for(ll j = 1;j<=n;j++)
//                printf("%d ", a[j].weight);
//            printf("\n");
        for(ll i = 0;i<=9;i++)
            num[n+1][i] = 0;
        for(ll i = n;i>=1;i--)
        {
            for(ll j = 0;j<=9;j++)
                num[i][j] = num[i+1][j];
            num[i][a[i].number]++;
        }
        for(ll i = 1;i<=n;i++)
        {
            for(ll j = 10 - a[i].number;j<=9;j++)
                ans += num[i+1][j];
//            printf("----\n");
//            for(ll j = 1;j<=n;j++)
//                printf("%d ", a[j].weight);
//            printf("\n");
            info tmp;
            tmp.weight = p10[s-1]-a[i].weight;
            ll pos = lower_bound(a+1, a+1+n, tmp) - a;
            pos = max(pos, i+1);
            ans += num[pos][9-a[i].number];
        }
    }
    printf("%lld\n", ans);
}

int main()
{
    freopen("2Bin.txt", "r", stdin);
    //freopen("3Bout.txt", "w", stdout);
    p10[0] = 1;
    for(ll i = 1;i<=9;i++)
        p10[i] = p10[i-1]*10;
    while(scanf("%lld", &n) != EOF)
    {
        for(ll i = 1;i<=n;i++)
            scanf("%lld", &b[i]);
        solve();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值