CF1909_C. Heavy Intervals题解

CF1909_C. Heavy Intervals题解

题目传送门(Problem - C - CodeforcesCodeforces. Programming competitions and contests, programming communityicon-default.png?t=N7T8https://codeforces.com/contest/1909/problem/C)。

题目翻译如下:(图片来源:洛谷)

这题已经出的很直了……可能也有暴力做法,大家可以尝逝,我直接将数论方法。

先给亿点前置芝士(排序不等式):

通俗来讲,就是对于两个有序序列,顺序之积之和大于等于乱序之积之和大于逆序之积之和。

我都知道你们在想什么,下面来到了大家喜闻乐见的Ctrl+C/V环节,AC Code走起!

#include<bits/stdc++.h>
#define N 110000
using namespace std;
multiset<int>tl={};
int a[N]={},c[N]={},l[N]={},r[N]={},n=0,t=0;
int main(){
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        tl.clear();
        for(int i=1;i<=n;i++){
            scanf("%d",&l[i]);
            tl.insert(l[i]);
        }
        for(int i=1;i<=n;i++){
            scanf("%d",&r[i]);
        }
        for(int i=1;i<=n;i++){
            scanf("%d",&c[i]);
        }
        sort(r+1,r+1+n);
        for(int i=1;i<=n;i++){
            auto id=tl.lower_bound(r[i]);
            id--;
            a[i]=r[i]-(*id);
            tl.erase(id);
        }
        sort(a+1,a+1+n);
        sort(c+1,c+1+n);
        long long ans=0;
        for(int i=1;i<=n;i++){
            ans+=a[i]*1LL*c[n-i+1];
        }
        printf("%lld\n",ans);
    }
    return 0;
}

感谢我这个大好人!没登录的人也可以复制哦!

#include<bits/stdc++.h>
#define N 110000
using namespace std;
multiset<int>tl={};
int a[N]={},c[N]={},l[N]={},r[N]={},n=0,t=0;
int main(){
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        tl.clear();
        for(int i=1;i<=n;i++){
            scanf("%d",&l[i]);
            tl.insert(l[i]);
        }
        for(int i=1;i<=n;i++){
            scanf("%d",&r[i]);
        }
        for(int i=1;i<=n;i++){
            scanf("%d",&c[i]);
        }
        sort(r+1,r+1+n);
        for(int i=1;i<=n;i++){
            auto id=tl.lower_bound(r[i]);
            id--;
            a[i]=r[i]-(*id);
            tl.erase(id);
        }
        sort(a+1,a+1+n);
        sort(c+1,c+1+n);
        long long ans=0;
        for(int i=1;i<=n;i++){
            ans+=a[i]*1LL*c[n-i+1];
        }
        printf("%lld\n",ans);
    }
    return 0;
}

当然登录了是更快捷的。

时间复杂度O(n*log(n)),完全能过。

代码有亿点长,说点注意事项:

①long long问题。

②lower_bound要用multiset里封装的,不要用单独的函数(这样会退化成O(n))。

  • 25
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
fig, ax = plt.subplots() ax.plot(RV_5.index, RV_5['RV_5min'], color='black', label='Actual') ax.fill_between(RV_5.index, RV_5['RV_5min'] - forecast_error, RV_5['RV_5min'] + forecast_error, alpha=0.2, color='blue', label='无ps置信区间') ax.fill_between(RV_5.index, conf_int_lower, conf_int_upper, alpha=0.2, color='green', label='有ps置信区间') ax.set_title('Prediction Intervals') ax.set_xlabel('Time') ax.set_ylabel('RV_5min') plt.legend() plt.show() fig, ax = plt.subplots() ax.plot(RV_5.index, RV_5['RV_5min'], color='black', label='Actual') ax.fill_between(RV_5.index, RV_5['RV_5min'] - forecast_error, RV_5['RV_5min'] + forecast_error, alpha=0.2, color='blue', label='无ps置信区间') ax.fill_between(RV_5.index, conf_int_lower, conf_int_upper, alpha=0.2, color='green', label='有ps置信区间') fig, ax = plt.subplots() ax.plot(RV_5.index, RV_5['RV_5min'], color='black', label='Actual') ax.fill_between(RV_5.index, RV_5['RV_5min'] - forecast_error, RV_5['RV_5min'] + forecast_error, alpha=0.2, color='blue', label='无ps置信区间') ax.fill_between(RV_5.index, conf_int_lower, conf_int_upper, alpha=0.2, color='green', label='有ps置信区间') fig, ax = plt.subplots() ax.plot(RV_5.index, RV_5['RV_5min'], color='black', label='Actual') ax.fill_between(RV_5.index, RV_5['RV_5min'] - forecast_error, RV_5['RV_5min'] + forecast_error, alpha=0.2, color='blue', label='无ps置信区间') ax.fill_between(RV_5.index, conf_int_lower, conf_int_upper, alpha=0.2, color='green', label='有ps置信区间') fig, ax = plt.subplots() ax.plot(RV_5.index, RV_5['RV_5min'], color='black', label='Actual') ax.fill_between(RV_5.index, RV_5['RV_5min'] - forecast_error, RV_5['RV_5min'] + forecast_error, alpha=0.2, color='blue', label='无ps置信区间') ax.fill_between(RV_5.index, conf_int_lower, conf_int_upper, alpha=0.2, color='green', label='有ps置信区间') 怎么将两个ax.fill_between画在一幅图上,不相互遮挡,
07-08

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值