AtCoder题解 —— AtCoder Beginner Contest 188 —— D - Snuke Prime —— 前缀和

题目相关

题目链接

AtCoder Beginner Contest 188 D 题,https://atcoder.jp/contests/abc188/tasks/abc188_d

Problem Statement

Snuke Inc. offers various kinds of services.
A payment plan called Snuke Prime is available.
In this plan, by paying C C C yen (the currency of Japan) per day, you can use all services offered by the company without additional fees.
You can start your subscription to this plan at the beginning of any day and cancel your subscription at the end of any day.
Takahashi is going to use N N N of the services offered by the company.
He will use the i i i-th of those services from the beginning of the a i a_i ai-th day until the end of the b i b_i bi-th day, where today is the first day.
Without a subscription to Snuke Prime, he has to pay c i c_i ci yen per day to use the i i i-th service.
Find the minimum total amount of money Takahashi has to pay to use the services.

Input

Input is given from Standard Input in the following format:

N C
a1 b1 c1
⋮
aN bN cN

Output

Print the minimum total amount of money Takahashi has to pay.

Sample 1

Sample Input 1

2 6
1 2 4
2 2 4

Sample Output 1

10

Explaination

He will use the 1 1 1-st service on the 1 1 1-st and 2 2 2-nd days, and the 2 2 2-nd service on the 2 2 2-nd day.
If he subscribes to Snuke Prime only on the 2 2 2-nd day, he will pay 4 4 4 yen on the 1 1 1-st day and 6 6 6 yen on the 2 2 2-nd day, for a total of 10 10 10 yen.
It is impossible to make the total payment less than 10 10 10 yen, so we should print 10 10 10.

Sample 2

Sample Input 2

5 1000000000
583563238 820642330 44577
136809000 653199778 90962
54601291 785892285 50554
5797762 453599267 65697
468677897 916692569 87409

Sample Output 2

163089627821228

Explaination

It is optimal to do without Snuke Prime.

Sample 3

Sample Input 3

5 100000
583563238 820642330 44577
136809000 653199778 90962
54601291 785892285 50554
5797762 453599267 65697
468677897 916692569 87409

Sample Output 3

88206004785464

Constraints

  • 1 ≤ N ≤ 2 × 1 0 5 1≤N≤2×10^5 1N2×105
  • 1 ≤ C ≤ 1 0 9 1≤C≤10^9 1C109
  • 1 ≤ a i ≤ b i ≤ 1 0 9 1≤a_i≤b_i≤10^9 1aibi109
  • 1 ≤ c i ≤ 1 0 9 1≤c_i≤10^9 1ci109
  • All values in input are integers.

题解报告

题目翻译

鹬公司提供多种服务,其中一种支付方式称为鹬素数计划,在这个计划中,每天支付 C C C 日圆,就可以使用鹬公司的所有服务。
高桥将使用鹬公司的 N N N 项服务。第 i i i 项服务,高桥将从第 a i a_i ai 天到第 b i b_i bi 天使用,如果不使用鹬素数计划,第 i i i 项服务将支付 c i c_i ci 的费用。
请找出最小的支付金额。

样例数据分析

样例数据 1

根据样例数据。
第一行的数据: 2 6 2 \quad6 26 N = 2 N=2 N=2 表示一共有 2 2 2 个服务。 C = 6 C=6 C=6 表示鹬素数计划每天需要 6 6 6 日圆。
第二行的数据: 1 2 4 1 \quad2 \quad4 124。表示第 1 1 1 项服务从第 1 1 1 天到 2 2 2 天使用,每天费用为 4 4 4 日圆。
第三行的数据: 2 2 4 2 \quad2 \quad4 224。表示第 2 2 2 项服务从第 2 2 2 天到 2 2 2 天使用,每天费用为 4 4 4 日圆。
这样,我们以天为单位,可以写出高桥不使用鹬素数计划时候每天的费用。如下表:

1 1 1 2 2 2
4 4 4 8 8 8

1 1 1 天,费用为 4 4 4 日圆。由于 4 < 6 4<6 4<6,因此不需要使用鹬素数计划,当前总费用为 a n s w e r = 4 answer=4 answer=4
2 2 2 天,费用为 8 8 8 日圆。由于 8 > 6 8>6 8>6,因此需要使用鹬素数计划,当前总费用为 a n s w e r = 4 + 6 = 10 answer=4+6=10 answer=4+6=10

题目分析

根据样例数据分析,我们知道对于第 i i i 天的总费用,只需要不超过鹬素数计划价格是,我们就不使用鹬素数计划。只有当费用超过了鹬素数计划,我们才使用鹬素数计划。
这样,我们将本题转换为统计第 i i i 天的费用。如果 i i i 的范围比较小,我们用最朴素的方法,代码如下:

for (int i=1; i<=n; i++) {
	//读取数据
	cin>>a[i]>>b[i]>>c[i];
	//统计第 j 天的费用
	for (int j=a[i]; j<=b[i]; j++) {
		cost[j] += c[i];
	}
}

但是本题的 i i i 的范围非常大,这样的方法会有以下几个问题:
1、由于 a i , b i a_i, b_i ai,bi 的数据范围非常大,最大是 1e9,这样 cost 数组需要定义为 1e9,太大了。
2、这样设计算法,再最极限的情况下,需要进行 N × [ m a x ( b i ) − m i n ( a i ) ] = 2 × 1 0 5 × ( 1 0 9 − 1 ) ≈ 2 × 1 0 14 N\times[max(b_i)-min(a_i)]=2 \times 10^{5}\times(10^9-1) \approx 2 \times 10^{14} N×[max(bi)min(ai)]=2×105×(1091)2×1014,这个计算量是无法在 1 1 1 秒内完成的。
所以,我们需要对这个统计算法进行优化。

优化:区间和

现在我们有数据 a i b i c i a_i \quad b_i \quad c_i aibici,该数据的意义是从 a i a_i ai b i b_i bi 之内的所有数据都加上 c i c_i ci,这不就是一个区间内的数据 [ a i , b i ] [a_i, b_i] [ai,bi] 都加上 c i c_i ci 的问题,即区间和问题,具体问题可以参考 http://47.110.135.197/problem.php?id=5143。我们可以使用前缀和思路,将该数据理解为:
1、 a i − 1 a_i-1 ai1 天加上 c i c_i ci 日圆。
2、 b i b_i bi 天减去 c i c_i ci 日圆。
然后排序,遍历就可以解决问题了。这样,我们将计算的算法优化为 O ( N ) O(N) O(N)

为什么要排序

因为我们最终是按照时间来统计数据,因此,需要按照时间排序。

如何统计

思路请参考前缀和。代码可以看后面。

数据范围

N N N 的最大值为 2e5, a i a_i ai 的最小值为 1 1 1,b_i$ 的最大值为 1 0 9 10^9 109 c i c_i ci 的最大值为 1 0 9 10^9 109,因此本题出现的最大数据将为 2 ∗ 1 0 5 ∗ ( 1 0 9 − 1 ) ∗ 1 0 9 ≈ 2 × 1 0 24 2*10^{5}*(10^{9}-1)*10^{9} \approx 2 \times 10^{24} 2105(1091)1092×1024,我去,超过了 long long 范围。我哪里算错了?

AC 代码

//https://atcoder.jp/contests/abc188/tasks/abc188_d
//D - Snuke Prime
#include <bits/stdc++.h>

using namespace std;

//如果提交到OJ,不要定义 __LOCAL
//#define __LOCAL

typedef long long ll;

const int MAXN = 2e5+4;
int a[MAXN];
int b[MAXN];
int c[MAXN];

typedef struct _COST {
    int day;
    ll fee;

    bool operator < (const struct _COST &x) {
        if (day==x.day) {
            return fee > x.fee;
        } else {
            return day < x.day;
        }
    }
} COST;
COST cost[2*MAXN];//第i天花费

int main() {
#ifndef __LOCAL
    //这部分代码需要提交到OJ,本地调试不使用
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#endif
    ll n, C;
    cin>>n>>C;
    for (int i=1; i<=n; i++) {
        cin>>a[i]>>b[i]>>c[i];
    }

    //处理数据
    int cnt=0;
    for (int i=1; i<=n; i++) {
        cost[++cnt].day = a[i]-1;
        cost[cnt].fee = c[i];
        cost[++cnt].day = b[i];
        cost[cnt].fee = -c[i];
    }
    sort(cost+1, cost+cnt+1);

    //遍历输出
    ll ans=0;
    ll fee=0;
    int t=0;
    for (int i=1; i<=cnt; i++) {
        if (cost[i].day != t) {
            ans += min(C, fee)*(cost[i].day-t);
            t = cost[i].day;
        }
        fee += cost[i].fee;
    }

    cout<<ans<<"\n";

#ifdef __LOCAL
    //这部分代码不需要提交到OJ,本地调试使用
    system("pause");
#endif
    return 0;
}

在这里插入图片描述

时间复杂度

O ( N ∗ l o g N ) O(N*logN) O(NlogN)

空间复杂度

O ( N ) O(N) O(N)。当然本题还可以使用 STL 来优化存储。

总结

本题出题水平很高。自己也是推敲了好久。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
AtCoder Beginner Contest 134 是一场 AtCoder 的入门级比赛,以下是每道的简要题解: A - Dodecagon 目描述:已知一个正十二边形的边长,求它的面积。 解思路:正十二边形的内角为 $150^\circ$,因此可以将正十二边形拆分为 12 个等腰三角形,通过三角形面积公式计算面积即可。 B - Golden Apple 目描述:有 $N$ 个苹果和 $D$ 个盘子,每个盘子最多可以装下 $2D+1$ 个苹果,求最少需要多少个盘子才能装下所有的苹果。 解思路:每个盘子最多可以装下 $2D+1$ 个苹果,因此可以将苹果平均分配到每个盘子中,可以得到最少需要 $\lceil \frac{N}{2D+1} \rceil$ 个盘子。 C - Exception Handling 目描述:给定一个长度为 $N$ 的整数序列 $a$,求除了第 $i$ 个数以外的最大值。 解思路:可以使用两个变量 $m_1$ 和 $m_2$ 分别记录最大值和次大值。遍历整个序列,当当前数不是第 $i$ 个数时,更新最大值和次大值。因此,最后的结果应该是 $m_1$ 或 $m_2$ 中较小的一个。 D - Preparing Boxes 目描述:有 $N$ 个盒子和 $M$ 个物品,第 $i$ 个盒子可以放入 $a_i$ 个物品,每个物品只能放在一个盒子中。现在需要将所有的物品放入盒子中,每次操作可以将一个盒子内的物品全部取出并分配到其他盒子中,求最少需要多少次操作才能完成任务。 解思路:首先可以计算出所有盒子中物品的总数 $S$,然后判断是否存在一个盒子的物品数量大于 $\lceil \frac{S}{2} \rceil$,如果存在,则无法完成任务。否则,可以用贪心的思想,每次从物品数量最多的盒子中取出一个物品,放入物品数量最少的盒子中。因为每次操作都会使得物品数量最多的盒子的物品数量减少,而物品数量最少的盒子的物品数量不变或增加,因此这种贪心策略可以保证最少需要的操作次数最小。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力的老周

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值