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 来优化存储。

总结

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

猜你喜欢

转载自blog.csdn.net/justidle/article/details/112593027
今日推荐