Weird Walk||codechef||beginner solution ||programming info

Weird Walk

Alice and Bob are walking on an infinite straight street. Initially, both are at the position X=0 and they start walking in the direction of increasing X. After N seconds, they stop. Let's denote Alice's speed and Bob's speed during the i-th of these seconds by Ai and Bi respectively.
Sometimes, Alice and Bob walk together, i.e. with the same speed side by side. Let's define the weird distance as the total distance they walk this way. Find this weird distance.

Input

  • The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
  • The first line of each test case contains a single integer N.
  • The second line contains N space-separated integers A1,A2,,AN.
  • The third line contains N space-separated integers B1,B2,,BN.

Output

For each test case, print a single line containing one integer ― the total weird distance. It can be proved that this distance is an integer.

Constraints

  • 1T20
  • 1N105
  • 1Ai105 for each valid i
  • 1Bi105 for each valid i
  • the sum of N over all test cases does not exceed 106

Subtasks

Subtask #1 (30 points): 1N1,000
Subtask #2 (70 points): original constraints

Example Input

3
4
1 3 3 4
1 2 4 4
2
2 3
3 2
2
3 3
3 3

Example Output

5
0
6

Explanation

Example case 1:
  • Alice and Bob walk side by side during the first second, from X=0 to X=1.
  • Then, Alice starts walking faster than Bob, so they do not walk side by side during second 2. At the end of second 2, Alice is at X=4, while Bob is at X=3.
  • During the next second, they again do not walk side by side, but Bob walks faster, so they both end up at X=7.
  • During the last second, they both walk side by side and the distance they walk is 4.
  • Alice and Bob walk side by side during the 1-st and 4-th second and the total weird distance they travel is 1+4=5.
Example case 2:
  • First, Alice walks with speed 2 and Bob walks with speed 3, so they do not walk side by side. Alice ends up at X=2, while Bob ends up at X=3 at the end of the 1-st second.
  • Then, Alice walks with speed 3 and Bob walks with speed 2, so they do not walk side by side either.
  • Although Alice and Bob both end up at X=5 at the end of the 2-nd second, the weird distance is 0.
Example case 3: We can see that Alice and Bob always walk together, so the weird distance is 3+3=6.
Solution using c++ language:-
#include <iostream>
#include<bits/stdc++.h>

#define ll long long int

using namespace std;

int main() 
    {
        int t;
        cin>>t;
        while(t--)
            {
                ll n;
                cin>>n;
                ll a[n],b[n],i;
                for(i=0;i<n;i++)
                        {
                            cin>>a[i];
                        }
                for(i=0;i<n;i++)
                        {
                            cin>>b[i];
                        }
                ll apart=0,bpart=0,sum=0;
                  if(a[0]==b[0])
                  {
                      apart=+a[0];
                      bpart+=b[0];
                  }
                for(i=0;i<n;i++)
                        {
                            if(a[i]==b[i] && (apart==bpart))
                                {
                                    sum+=a[i];
                                }
                            else if(a[i]!=b[i])
                                {
                                    apart+=a[i];
                                    bpart+=b[i];
                                }
                        }
                    cout<<sum<<"\n";
            }
    // your code goes here
    return 0;
    }

Share:

No comments:

Post a Comment

Translate

Recommended platforms

  1. codechef
  2. hackerrank
  3. codeforces
  4. leetcode
  5. hackerearth

Popular Posts

programming_info. Powered by Blogger.

Blog Archive

Recent Posts

other platforms

  • geeks for geeks
  • w3schools
  • codepen
  • skillshare
  • udemy

Pages

reader support Support