Problem:
Problem B
Overtaking
Two athletes run a long-distance race, but the race is
different from usual athletics events.
They run for a predetermined duration, and the one who runs longer will be the winner.
At every minute after the start, the distances both runners ran
in the previous one minute are recorded.
You are expected to write a program that, given the record of a race,
counts the number of overtakings by either runner during the race.
You should assume that both runners keep constant paces
for every one minute before their distances are recorded.
Here, the term
overtaking
stands for the event
where the runner previously behind takes the lead.
Note that, before overtaking, the two runners may run side by side for a while,
during which neither takes the lead.
Figure B-1 shows the times and the positions of two runners
for the third dataset of Sample Input.
The number of overtakings in this case is two.
Figure B-1: The third dataset of Sample Input
Input
The input consists of multiple datasets, each in the following format.
n
a
1
⋯
a
n
b
1
⋯
b
n
n
is an integer between 2 and 1000, inclusive.
It represents the duration of the race in minutes.
Each of
a
k
(
k
= 1,  ,
n
) is an integer
between 1 and 1000, inclusive.
It represents the distance the first runner ran between
k
− 1 minutes and
k
minutes after the start of the race,
in meters.
Similarly,
b
k
(
k
= 1,  ,
n
)
represent the distances the second runner ran.
The end of the input is indicated by a line consisting of a zero.
The number of datasets does not exceed 100.
Output
For each dataset, output the number of overtakings on a line.
Sample Input
3
5 15 5
10 5 15
9
10 10 10 10 10 10 10 10 10
5 15 10 5 15 10 5 15 10
9
10 10 10 5 15 10 10 10 10
5 15 10 10 10 10 5 15 10
0
Output for the Sample Input
2
0
2

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
#undef long
#define long long long
#define vec vector
void solve()
{
    int n;
    cin >> n;
    if (n == 0)
        exit(0);
    vec<int> a(n), b(n);
    rep(i, n) cin >> a[i];
    rep(i, n) cin >> b[i];
    rep(i, n - 1)
    {
        a[i + 1] += a[i];
        b[i + 1] += b[i];
    }
    int fu = 0;
    int ans = 0;
    rep(i, n)
    {
        if (a[i] < b[i])
        {
            ans += fu == -1;
            fu = 1;
        }
        if (a[i] > b[i])
        {
            ans += fu == 1;
            fu = -1;
        }
    }
    cout << ans << endl;
}
int main()
{
    while (1)
        solve();
}
accuracy: 1
time_ms: 10
memory_kb: 3120
final_rank: 3

code_snippet: #include <iostream>
#include <vector>
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
using namespace std;

int main() {
    while(true) {
        int n; 
        cin >> n;

        if (n == 0) break;
        
        vector<int> a(n), b(n); 

        rep(i,n) cin >> a[i];
        rep(i,n) cin >> b[i];

        int lead = 0, new_lead = 0, ans = 0, disA = 0, disB = 0; 

        rep(i, n) {
            disA += a[i];
            disB += b[i];

            if (disA > disB) {
                new_lead = 1;
            } else if (disA < disB) {
                new_lead = -1;
            }
            if (lead != 0 && new_lead != 0 && lead != new_lead) {
                ans++;
            }
            lead = new_lead;
        }
        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3076
final_rank: 1

code_snippet: #include <iostream>
#include <vector>
#include <queue>
using namespace std;

int main(void){
    bool is_a_lead = false;
    bool is_draw = true;
    queue<int> ans;
    while(1){
        int n;
        cin >> n;
        if(n == 0){
            break;
        }
        vector<int> a(n);
        for(int i=0;i<n;i++){
            cin >> a[i];
        }
         is_a_lead = false;
          is_draw = true;
        int sum1 = 0;
        int sum2 = 0;
        int count = 0;
        for(int i=0;i<n;i++){
            int v;
            cin >> v;
            sum1 += a[i];
            sum2 += v;
            if(sum1==sum2){
                continue;
            }
            if(sum1 > sum2){
                if(!is_draw && !is_a_lead){
                    count++;
                }
                is_a_lead = true;
                is_draw =false;
            }
            else{
                if(!is_draw && is_a_lead){
                    count++;
                }
                is_a_lead = false;
                is_draw =false;
            }
        }
        ans.push(count);
    }
    while(!ans.empty()){
        cout << ans.front()  << endl;
        ans.pop();
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3092
final_rank: 2