CSP 2018-9-2 买菜

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/TheWildPointer/article/details/84489859
#include <iostream>
using namespace std;

const int N = 2005; 
struct  Time{
    int x, y;
}h[N],w[N];

int main()
{
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> h[i].x>>h[i].y;
    }
    for (int i = 1; i <= n; i++) {
        cin >> w[i].x >> w[i].y;
    }
    long long aus = 0;
    for (int i = 1, j = 1; i <= n && j <= n;) {
        if (h[i].x<w[j].x){
            if (h[i].y<=w[j].x){
                i++;
            }
            else if (h[i].y <= w[j].y)
            {
                aus +=h[i].y - w[j].x;
                i++;
            }
            else {
                aus += w[j].y - w[j].x;
                j++;//
            }
        }
        else
        {
            if (h[i].x >= w[j].y) {
                j++;
            }
            else if (h[i].y >= w[j].y)
            {
                aus += w[j].y - h[i].x;
                j++;
            }
            else {
                aus += h[i].y - h[i].x;
                i++;
            }
        }
    }
    cout << aus;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/TheWildPointer/article/details/84489859