CCF-CSP 201809-2 买菜
题目描述
问题链接
代码
#include <bits/stdc++.h>
using namespace std;
class Node{
public:
int x,y;
Node(int a,int b) {
x = a;
y = b;
}
};
int main() {
ios::sync_with_stdio(false);
int n; cin >> n;
vector<Node> a;
vector<Node> b;
int x,y;
for (int i = 0; i < n; i++) {
cin >> x >> y;
a.push_back(Node(x,y));
}
for (int i = 0; i < n; i++) {
cin >> x >> y;
b.push_back(Node(x,y));
}
int i = 0,j = 0;
int count = 0;
while (i < a.size() && j < b.size()) {
if (a[i].y > b[j].y) {
if (a[i].x < b[j].x) {
count += b[j].y - b[j].x;
}
else if (a[i].x < b[j].y) {
count += b[j].y - a[i].x;
}
else {
}
j++;
}
else {
if (b[j].x < a[i].x) {
count += a[i].y - a[i].x;
}
else if (b[j].x < a[i].y) {
count += a[i].y - b[j].x;
}
else {
}
i++;
}
}
cout << count << endl;
return 0;
}