Weird Walk
Alice and Bob are walking on an infinite straight street. Initially, both are at the position and they start walking in the direction of increasing . After seconds, they stop. Let's denote Alice's speed and Bob's speed during the -th of these seconds by and 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 denoting the number of test cases. The description of test cases follows.
- The first line of each test case contains a single integer .
- The second line contains space-separated integers .
- The third line contains space-separated integers .
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
- for each valid
- for each valid
- the sum of over all test cases does not exceed
Subtasks
Subtask #1 (30 points):
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 to .
- Then, Alice starts walking faster than Bob, so they do not walk side by side during second . At the end of second , Alice is at , while Bob is at .
- During the next second, they again do not walk side by side, but Bob walks faster, so they both end up at .
- During the last second, they both walk side by side and the distance they walk is .
- Alice and Bob walk side by side during the -st and -th second and the total weird distance they travel is .
Example case 2:
- First, Alice walks with speed and Bob walks with speed , so they do not walk side by side. Alice ends up at , while Bob ends up at at the end of the -st second.
- Then, Alice walks with speed and Bob walks with speed , so they do not walk side by side either.
- Although Alice and Bob both end up at at the end of the -nd second, the weird distance is .
Example case 3: We can see that Alice and Bob always walk together, so the weird distance is .
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;
}
No comments:
Post a Comment