买菜
#include #include #include using namespace std; int main() { int N; cin>>N; pair ab[N], cd[N]; for (int i = 0; i < N; ++i) { cin>>ab[i].first>>ab[i].second; } for (int i = 0; i < N; ++i) { cin>>cd[i].first>>cd[i].second; } int ans = 0; int index1 = 0, index2 = 0; while (index1 < N && index2 < N) { int a = ab[index1].first, b = ab[index1].second; int c = cd[index2].first, d = cd[index2].second; if (d <= a) { ++index2; } else if (b <= c) { ++index1; } else if (a <= c && d <= b) { ans += d - c; ++index2; } else if (c <= a && d <= b) { ans += d - a; ++index2; } else if (a <= c && b <= d) { ans += b - c; ++index1; } else if (c <= a && b <= d){ ans += b - a; ++index1; } } cout<
上一篇 Python调用C/C++库函数
下一篇 算法学习——1.连通性问题(一)
版权所有 (c)2021-2022 MSHXW.COM
ICP备案号:晋ICP备2021003244-6号