一道差分的题

某一天,Rain Sure同学来到一个豪华酒店,这个酒店可以提供各种正经服务。
这家酒店的计费方式如下:
你可以选择办理会员,会员每天需要交纳C元的会员费,成为会员后你可以随意享受各种服务。
并且,你可以任意选择在某天成为会员,或者在某天停止成为会员。
Rain Sure同学决定来体验n种服务,每种服务价钱如下所示:
对于第i项服务,Rain Sure同学会在第ai​天到第bi​天进行享受,如果没有办理会员的话,那么每天需要支付ci​元的费用。
请你帮助他计算一下,如果想要体验完这n项服务,最少需要花多少钱?

输入格式

第一行两个正整数,分别代表n和C。
后面n行,每行三个正整数,分别为ai​,bi​,ci​
1≤n≤2×105
1≤C≤109
1≤ai​≤bi​≤109
1≤ci​≤109

输出格式

输出一个整数,代表最少需要花费的钱。

测试样例一

2 6
1 2 4
2 2 4
10

说明:第一天花费4元体验服务1,第二天开会员花费6元体验服务1和服务2.

测试样例二

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

测试样例三

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

 


题目说的是在任意时候可以开启或取消会员,我一直认为只能开一次呢。。

这是请教了佬的AC代码

#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main()
{
	int n, C;cin >> n >> C;
	map<int, int> mp;
	while (n--)
	{
		int a, b, c;cin >> a >> b >> c;
		mp[a] += c, mp[b + 1] -= c;
	}
	int sum = 0;
	auto it = mp.begin();
	while (it != mp.end())
	{
		sum += (next(it)->first - it->first) * min(C, it->second);
		it++;
		it->second += prev(it)->second;
	}
	cout << sum;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define int long long
const int N = 4e5+10;
int n,m;
int a,b,c;
struct node{
    int x;
    int money;
}s[N];
int cnt,sum,res,pre;
bool cmp(node n1,node n2)
{
    return n1.x<n2.x;
}
signed main()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
    {
        scanf("%d%d%d",&a,&b,&c);
        s[cnt].x=a,s[cnt++].money=c;
        s[cnt].x=b+1,s[cnt++].money=-c;
    }
    sort(s,s+cnt,cmp);
    
    pre = s[0].money;
    res = min(pre,m);
    for(int i=1;i<cnt;i++)
    {
        int d = s[i].x-s[i-1].x;
        sum+=d*res;
        pre += s[i].money;
        res = min(pre,m);
    }
    cout<<sum;
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值