CCF-买菜

陌路散爱 提交于 2019-11-29 06:26:32

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

#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
*/

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!