一道差分的题

某一天,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
    评论
假设你的csv文件包含5道的成绩,其中第1列是学生姓名,第2-6列是5道的成绩,第7列是每道的权重,你可以使用以下代码来计算每一道目的平均值、标准,以及计算自己每一道目的标准分和综合加权标准分: ```python import csv import math # 打开csv文件 with open('example.csv', newline='') as csvfile: # 读取csv文件内容 reader = csv.reader(csvfile) # 读取表头 header = next(reader) # 初始化每道的成绩列表和权重列表 scores = [[] for i in range(5)] weights = [] # 循环遍历每一行 for row in reader: # 遍历每一列 for i in range(1, 6): # 判断成绩是否有效 if row[i] != '-1': # 将有效成绩添加到成绩列表中 scores[i-1].append(float(row[i])) # 将本目的权重添加到权重列表中 weights.append(float(row[6])) # 计算每道的平均值和标准 means = [] stds = [] for score in scores: # 剔除无效成绩(-1分) valid_scores = [s for s in score if s != -1] # 计算平均值和标准 mean = sum(valid_scores) / len(valid_scores) std = math.sqrt(sum([(s - mean)**2 for s in valid_scores]) / len(valid_scores)) means.append(mean) stds.append(std) # 计算自己每一道目的标准分和综合加权标准分 name = 'your name' # 自己的姓名 scores = [] for row in reader: if row[0] == name: scores = [float(s) for s in row[1:6]] break z_scores = [(s - means[i]) / stds[i] for i, s in enumerate(scores)] weighted_z_scores = [z * w for z, w in zip(z_scores, weights)] weighted_z_scores_sum = sum(weighted_z_scores) total_weight = sum(weights) final_score = weighted_z_scores_sum / total_weight # 打印结果 print("每道目的平均值:", means) print("每道目的标准:", stds) print("自己每一道目的标准分:", z_scores) print("自己的综合加权标准分:", final_score) ``` 在上面的代码中,我们首先读取csv文件,并提取出每道的成绩和权重,然后使用`sum`和`len`函数计算每道的平均值和标准。接着,我们读取自己的成绩,并计算出自己每一道目的标准分和综合加权标准分。最后,我们打印出每道目的平均值、标准,自己每一道目的标准分,以及自己的综合加权标准分。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值