Files
python/LuoGu/RuMen/P1568.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

53 lines
1.5 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0;
char c = getchar();
bool flag = 0;
while (c < '0' || c > '9') {
if (c == '-')flag = 1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = (x + (x << 2) << 1) + c - '0';
c = getchar();
}
return flag ? -x : x;
}
int n, m, ttime = 0, tot = 0;
//ttime记录总时间,tot记录答案
int ans1, ans2, cnt1 = 1, cnt2 = 1, t1, t2;
//ans表示两人的路程cnt表示当前的阶段,t表示状态转移的时间点
struct ingredient {
int v, t;
} n1[1005], m1[1005];
bool judge(int x, int y) {
if (x > y) return 1;
else return 0;
}//比较函数,可以知道两人的先后顺序
int main() {
n = read(), m = read();
for (int i = 1; i <= n; ++i)
n1[i].v = read(), n1[i].t = read(), ttime += n1[i].t;
for (int i = 1; i <= m; ++i)
m1[i].v = read(), m1[i].t = read();
bool flag;//记录两人的先后顺序
if (n1[1].v > m1[1].v) flag = 1;
else flag = 0;
t1 = n1[1].t, t2 = m1[1].t;//初始化
for (int i = 1; i <= ttime; ++i)//枚举每一秒的状态
{
ans1 += n1[cnt1].v;
ans2 += m1[cnt2].v;//通过的总路程
if (judge(ans1, ans2) != flag) tot++, flag = !flag;
//如果两人先后发生变化,记录下当前状态,更新答案
if (i == t1) cnt1++, t1 += n1[cnt1].t;
if (i == t2) cnt2++, t2 += m1[cnt2].t;
//各自进入下一状态
}
cout << tot << endl;
}