SCU-4437 弱校联萌十一大决战之强力热身 B. Carries 【lower_bound 二分】

本文解析了SCU-4437竞赛中的B题“Carries”,该题要求计算在给定整数集合中两两相加产生的进位次数。通过使用二分查找与lower_bound优化算法,实现了高效的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SCU-4437 弱校联萌十一大决战之强力热身 B. Carries 【lower_bound 二分】

题目链接:http://acm.scu.edu.cn/soj/problem/4437/

题意:h(x,y)【x+y 进位的个数】 ,求在n个ai中枚举有多少个对数产生进位并计数。

题解:纯暴力TLE,要二分优化。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long LL;
const int maxn = 100100;
int n;
LL a[maxn],b[maxn];
int main(){
    while(~scanf("%d",&n)){
        int maxx = -1;
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
            maxx = maxx>a[i]?maxx:a[i];
        }
        int cnt1 = 0;
        while(maxx){
            maxx/=10;
            cnt1++;
        }
        sort(a+1,a+1+n);
        LL ans = 0;
        LL mul = 1;
        for(int j=1;j<=cnt1;j++){//数位
            mul*=10;
            for(int i=1;i<=n;i++) b[i] = a[i]%mul;
            sort(b+1,b+1+n);
            for(int i=1;i<n;i++){
                int x = mul - b[i];
                int y = lower_bound(b+i+1,b+1+n,x)-b;
                if(y>n||(b[y])>mul) continue;

                int z = lower_bound(b+i+1,b+1+n,mul)-b;// 记录>mul 的值
                if(b[z]==mul) z++;

                if(n<=z) ans += (z-y);
                else ans += (n-y)+1;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}
/*
5
0 1 2 3 99
5
1 2 3 99 999
2
5 5
10
0 1 2 3 4 5 6 7 8 9

*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值