CCF-买菜

分析:将各自时间段分多钟情况匹配即可

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

struct Time
{
    int st;
    int ed;
};

bool sort1(Time x, Time y)
{
    return x.st<y.st;
}
int main()
{
    int n;
    Time TH[5000];
    Time TW[5000];

    cin>>n;
    for(int i = 0; i < n; i++)cin>>TH[i].st>>TH[i].ed;
    for(int i = 0; i < n; i++)cin>>TW[i].st>>TW[i].ed;

    sort(TH,TH+n,sort1);
    sort(TW,TW+n,sort1);

    ///以H为基础审核W时间段
    int indexh = 0;
    int indexw = 0;
    int ans = 0;
    while(indexh<n&&indexw<n)
    {
        /// [W] [H]
        if(TH[indexh].st>=TW[indexw].ed)
        {
            indexw++;
            continue;
        }
        ///  [H] [W]
        if(TH[indexh].ed<=TW[indexw].st)
        {
            indexh++;
            continue;
        }
        ///   [H [W] H]
        if(TH[indexh].st<=TW[indexw].st&&TH[indexh].ed>=TW[indexw].ed)
        {
            ans+=(TW[indexw].ed-TW[indexw].st);
            indexw++;
            continue;
        }
        ///[W [H] W]
        if(TH[indexh].st>=TW[indexw].st&&TH[indexh].ed<=TW[indexw].ed)
        {
            ans+=(TH[indexh].ed-TH[indexh].st);
            indexh++;
            continue;
        }
        ///[W [交集] H]
        if(TH[indexh].st<=TW[indexw].ed&&TH[indexh].st>=TW[indexw].st)
        {
            ans+=(TW[indexw].ed-TH[indexh].st);
            indexw++;
            continue;
        }
        ///[H [交集] W]
        if(TH[indexh].ed>=TW[indexw].st&&TH[indexh].st<=TW[indexw].st)
        {
            ans+=(TH[indexh].ed-TW[indexw].st);
            indexh++;
            continue;

        }
    }
    cout<<ans<<endl;


    return 0;
}
/*
4
1 3
5 6
9 13
14 15
2 4
5 7
10 11
13 14

3
*/

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值