CCPC-Wannafly & Comet OJ 夏季欢乐赛(2019)三元组

本文深入探讨了双指针算法在特定问题中的高效应用,通过实例分析展示了如何利用该技巧解决排序与区间计算问题,特别关注了算法优化与性能提升。

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

 min, max 什么的都是骗人的, 展开排序后发现随着i的增加, 它的右端点在减少, 所以可以采用双指针来做。

题目链接

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 200005;
const int mod = 1e9+7;

struct tri
{
    LL a, b, c;
    bool operator < (const tri &p) const{
        return 2 * a - b < 2 * p.a - p.b;
    }
}P[N];
LL sum[N];
int n;

int cal()
{
    sort(P + 1, P + n + 1);
    sum[0] = 0;
    for(int i = 1;i <= n; i++){
        sum[i] = (sum[i-1] + P[i].c) % mod;
    }
    int l = 1, r = n;
    while(l < r){
        int mid = (l + r + 1) >>1;
        if(2 * P[1].a - P[1].b + 2 * P[mid].a - P[mid].b > 0)r = mid - 1;
        else l = mid;
    }
    int ans = 0;
    for(l = 1; l <= r; l++){
        while(l <= r && 2 * P[l].a - P[l].b + 2 * P[r].a - P[r].b > 0)r--;
        if(r < l)break;
        ans = (ans + P[l].c * (sum[r] - sum[l-1]) % mod) % mod;
    }
    return ans;
}

int main()
{
    scanf("%d",&n);
    for(int i = 1; i <= n; i++){
        scanf("%lld%lld%lld",&P[i].a, &P[i].b, &P[i].c);
    }
    int ans = cal();
    for(int i = 1; i <= n; i++){
        swap(P[i].a, P[i].b);
    }
    ans += cal();
    ans = (ans % mod + mod) %mod;
    printf("%d\n",ans);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值