Maximal Intersection- Codeforces1029 C

题目传送门
思路:
本来还以为要很复杂的算法,没想到muktiset也可以。其实计算一下复杂度,确实可以过。
复杂度是O(n*logn*logn),n = 3e5时,是1.2*e8.所以要好好利用c++的STL.

AC code:
#include<bits/stdc++.h> 
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 3e5 + 10;
pair<int,int> s[maxn];
//复杂度计算一下,还是可以过的 
int main(){
    int n = 0;
    while(scanf("%d",&n) != EOF){
        multiset<int> l,r;
        for(int i = 0;i < n;++i){
            scanf("%d %d",&s[i].first,&s[i].second);
            l.insert(s[i].first),r.insert(s[i].second);
        }
        int maxv = 0;
        for(int i = 0;i < n;++i){
            l.erase(l.find(s[i].first)),r.erase((r.find(s[i].second)));
            maxv = max ( maxv , *r.begin() - *l.rbegin());//rbegin()指向的是最后一个元素 
        //  maxv = max ( maxv , *r.begin() - *l.end());//而end()指向的是最后一个元素的下一个 
            l.insert(s[i].first),r.insert(s[i].second);
        }
        printf("%d\n",maxv);
    }   
    return 0;
} 
大佬的简洁版:
#include <bits/stdc++.h>
using namespace std;

int main(){
    ios::sync_with_stdio(0) , cout.tie(0) , cin.tie(0);

    int n ;
    cin >> n;
    multiset<int> L;
    multiset<int> R;
    pair<int , int> arr[n];
    for(int i = 0 ; i <n ; i++){
        cin >> arr[i].first >> arr[i].second;
        L.insert(arr[i].first);
        R.insert(arr[i].second);
    }

    int ans = 0;
    for(int i = 0 ; i < n ; i++) {
        L.erase(L.find(arr[i].first));
        R.erase(R.find(arr[i].second));

        ans = max(ans , *R.begin() - *L.rbegin());

        L.insert(arr[i].first);
        R.insert(arr[i].second);
    }
    cout << ans;
    return 0 ;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值