CCF 20180902 买菜(求区间重叠长度模板)

3 篇文章 0 订阅

思路:本题可简化成已知区间,求区间的重叠长度。已知两个区间[a,b],[c,d],x = min(b,d);y = max(a,c)。if(y-x>0)有重叠区间,区间长度为y-x;else 无重叠区间。类比的可以求矩阵的重叠面积。

#include<iostream>
#include<vector>
#include<map>

using namespace std;
/*
[a,b],[c,d]
x = min(b,d);y = max(a,c)
if(y-x>0)有重叠区间,区间长度为y-x
else 无重叠区间
*/
const int N = 2000;
vector<pair<int,int> > v1(N),v2(N);

int main(){
    int n,sum = 0;
    cin>>n;

    for(int i = 0; i < n; i++){
        cin>>v1[i].first>>v1[i].second;
    }
    for(int i = 0; i < n; i++){
        cin>>v2[i].first>>v2[i].second;
    }
    vector<pair<int,int> > :: iterator it1,it2;
    for(it1 = v1.begin(); it1 != v1.end(); it1++){
        for(it2 = v2.begin(); it2 != v2.end(); it2++){
            int x = max(it1->first, it2->first);
            int y = min(it1->second, it2->second);
            if(y - x > 0){
                sum += (y - x);
            }
        }
    }
    cout<<sum;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值