Amir and Bond are walking on a 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 Amir's speed and Bond's speed during the i-th of these seconds by Ai and Bi respectively.
Sometimes, Aman and Bond walk together, i.e. with the same speed side by side. Let's define the xtreme distance as the total distance they walk this way. Find this xtreme 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
1≤T≤20
1≤N≤10e5
1≤Ai≤10e5 for each valid i
1≤Bi≤10e5 for each valid i
the sum of N over all test cases does not exceed 10e6
Code
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
int T;
cin >> T;
while(T--)
{
long long int N;
long long int i, w = 0;
cin >> N;
int * A = new int [N+1];
int * X = new int [N+1];
int * B = new int [N+1];
int * Y = new int [N+1];
Y[0] = 0;
X[0] = 0;
for(i=1;i<=N;i++)
{
cin >> A[i];
X[i] = X[i-1] + A[i];
}
for(i=1;i<=N;i++)
{
cin >> B[i];
Y[i] = Y[i-1] + B[i];
}
for(i=1;i<=N;i++)
{
if((X[i]-X[i-1]) == (Y[i]-Y[i-1]))
w += (Y[i] - Y[i-1]);
}
cout << w << endl;
delete [] A;
delete [] B;
delete [] X;
delete [] Y;
}
return 0;
}
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
Error
I am not able to figure out the error (may be there is error in constraints)
Related
Question:
Starting with a 1-indexed array of size n filled with all zeroes, you are required to perform the following operation m times:
Each operation contains 3 integers a, b and k. You are required to add k to the value at all the indices from a to b (both inclusive).
Once all operations have been performed, return the maximum value in the array.
Input Format:
The first line contains two space-separated integers n and m, the size of the array and the number of operations respectively.
Each of the next m lines contains three space-separated integers a, b and k, the left index, right index and integer to add respectively.
Constraints:
1 <= n, m <= 10^5
1 <= a <= b <= n
-10^9 <= k <= 10^9
Examples:
Sample Input 1:
10 3
1 5 3
4 8 7
6 9 1
Sample Output 1:
10
Explanation:
Given n = 10 and m = 3
Queries are interpreted as follows:
a b k
1 5 3
4 8 7
6 9 1
Add the values of k between the indices a and b inclusive:
1 2 3 4 5 6 7 8 9 10 (index)
[0,0,0,0,0,0,0,0,0,0] (Initially)
[3,3,3,3,3,0,0,0,0,0] (After Query 1)
[3,3,3,10,10,7,7,7,0,0] (After Query 2)
[3,3,3,10,10,8,8,8,1,0] (After Query 3)
The largest value is 10 after all operations are performed.
Sample Input 2:
5 3
1 2 100
2 5 100
3 4 100
Sample Output:
200
Explanation:
After the first update the list is 100 100 0 0 0.
After the second update list is 100 200 100 100 100.
After the third update list is 100 200 200 200 100.
The maximum value is 200.
This code ran and gave the correct output:
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<long long int> arr(n, 0);
for (int i = 0; i < m; i++) {
int start, finish, value;
cin >> start >> finish >> value;
arr[start - 1] += value;
arr[finish] -= value;
}
long long int ans = 0;
long long int current = 0;
for (int value : arr) {
current += value;
if (current > ans) {
ans = current;
}
}
cout << ans;
return 0;
}
but this code is giving me segment fault:
#include <bits/stdc++.h>
using namespace std;
int main(){
long long int n,m;
cin>>n>>m;
long long int x[n] ={0};
for(int i=0;i<m;i++)
{
long long int a,b,k;
cin>>a>>b>>k;
x[a-1] += k;
if(b<n)
x[b] -= k;
}
long long int max=0,current=0;
for(int i=0;i<n;i++)
{
current=x[i]+current;
if(max<current)
max=current;
}
cout<<max<< endl;
return 0;
}
Well the problem in your code is in the line x[b] -= k;
Since b is 1-indexed, this means that b can be upto n but your array of size n has indices from 0 to n-1 and if there is a query where b = n, then you get segmentation fault.
You can correct this by placing an if condition just before this line like this:
if (b < n)
Also, in the first code, you should have encountered the same problem but somehow your compiler overlooked the indexing problem in vector.
background: I was writing a c++ program to solve this problem:
For a positive integer N, the digit-sum of N is defined as the sum of N itself and its digits. When M
is the digitsum of N, we call N a generator of M.For example, the digit-sum of 245 is 256 (= 245 + 2 + 4 + 5). Therefore, 245 is a generator of
256. Not surprisingly, some numbers do not have any generators and some numbers have more than one generator. For example, the generators of 216 are 198 and 207.
You are to write a program to find the smallest generator of the given integer.
Input
Your program is to read from standard input.
The input consists of T test cases.
The number of test cases T is given in the first line of the input.
Each test case takes one line containing an integer N, 1 ≤ N ≤ 100, 000.
Output
Your program is to write to standard output.
Print exactly one line for each test case.
The line is to contain a generator of N for each test case.
If N has multiple generators, print the smallest.
If N does not have any generators, print ‘0’.
my problem: the program below always terminated with status -1073740940, I wonder why and need some help
int main()
{
int* ans = new int[100005]();
int y;
int i_op;
for(int i = 1; i < 100001; ++i){
y = i;
i_op = i;
while(i_op){
y += i_op%10;
i_op /= 10;
}
if(ans[y] == 0 || i < ans[y]){
ans[y] = i;
}
}
int t;
int n;
cin >> t;
for(int i = 0; i < t; ++i){
cin >> n;
cout << ans[n] << endl;
}
//========================
//problem occurs here //after doing all output, the process terminated with status -1073740940
//========================
delete[] ans;
return 0;
}
input data: (both terminated with status -1073740940)
10
70587
38943
37061
95352
84205
96532
21150
26337
97804
65891
and
100000
1
2
……
100000
You may be writing past the end of your array during the computation and corrupting something. What happens for i = 99999? I don't think 100005 is quite enough to contain that. Let's check:
#include <stdio.h>
int main() {
int i = 99999;
int y = i;
int i_op = i;
while(i_op){
y += i_op%10;
i_op /= 10;
}
printf("%d\n", y);
}
Outputs 100044. Indeed.
Our programming activity is to construct a C program that creates an array of size 12 and then display separately the sum of even-indexed cells and odd-indexed cells as well as their average. The sample run must be like this.
Sample Run:
Enter 12 elements: 1 1 1 1 1 1 1 1 1 1 1 1
Sum of even-indexed cells: 6
Sum of odd-indexed cells: 6
Average: 6.0
However, i'm having a trouble getting the sum of odd and even numbers. Like how i can make the program identify the odd and even placing in the array? Do i need to add "if-else" Here's my code.
#include <iostream>
using namespace std;
int A[11],B[11],C[11];
int sum,ave;
int x;
main()
{
cout<<"Enter 12 elements A: "<<endl;
for(x=0; x<11; x++)
cin>>A[x];
cout<<endl<<"Sum of even-indexed number: "<<endl;
for(x=0; x<=11; x++){
cout<<B[x];
}
cout<<endl<<"Sum of odd-indexed number: "<<endl;
for(x=0; x<=11; x++){
cout<<C[x];
}
cout<<endl<<"Average: "<<endl;
for(x=3; x<3; x++)
ave += B[x]+C[x]/A[x];
cout<<A[x]<<" ";
return 0;
}
You don't need 3 arrays. Just an array for the elements and 2 variables for the sums of evens and odds.
You could check the position in the array by checking if the remainder of the index divided by 2 equals 0.
You could also start an even position and increment the index by 2 so it would only check the even positions. For the odd positions you do the same, increment by 2 but start at an odd position.
#include <iostream>
using namespace std;
int main() {
int array[12];
int evenTotal = 0;
int oddTotal = 0;
for(int i=0;i<12;i++) {
cin>>array[i];
cout<<array[i]<<" ";
if(i%2 == 0){
//If we consider indexing with 0 to 11
// If we consider indexing with 1 to 12
// than this condition will be odd
evenTotal += array[i];
}
else {
oddTotal += array[i];
}
}
cout<<"\n";
// Because we have 6 evens and odds
int average = (evenTotal + oddTotal)/12;
cout<<"Even Total = "<<evenTotal<<"\n";
cout<<"Ödd Total = "<<oddTotal<<"\n";
cout<<"Average = "<<average;
return 0;
}
Check for output here : https://ideone.com/msYFh5
using namespace std;
#include<iostream>
class biker
{
public:
int initspeed, acc, speeding;
};
void input(int n, biker a[])
{
for (int i = 0; i < n; i++)
{
//cout<<"Enter the initspeed:"<<"\n";
std::cin >> a[i].initspeed;
//cout<<"Enter the acceleration:"<<"\n";
std::cin >> a[i].acc;
}
}
int main()
{
int n = 0, i = 0, t = 0, max_speed = 0, flag = 0, minimum = 0;
biker a[100];
std::cin >> t;
for (int k = 0; k < t; k++)
{
//cout<<"Enter no of bikers"<<"\n";
std::cin >> n;
//cout<<"enter the max track speed"<<"\n";
std::cin >> max_speed;
//cout<<"Enter the min niker speed"<<"\n";
std::cin >> minimum;
input(n, a);
int j = 1, x = 0;
while (flag < 500)
{
int sum = 0;
for (i = 0; i < n; i++)
{
int prod = 0;
prod = a[i].acc * j;
x = a[i].initspeed + prod;
// cout<<"VAL"<<x<<"\n";
if (x >= minimum)
{
//cout<<"It is greater than minimum";
sum = sum + a[i].initspeed + prod;
//cout<<sum<<"\n";
}
}
if (sum >= max_speed)
{
//cout<<"MAXIMUM ACHIEVED\n";
x = sum;
break;
}
j++;
flag++;
}
//cout<<x<<"\n";
std::cout << j;
}
return 0;
}
The above code is my solution to the bike racing problem in geeksforgeeks.
Summary:
A Bike race is to be organized. There will be N bikers. You are given initial Speed of the ith Biker by Hi and the Acceleration of ith biker as Ai KiloMeters per Hour.
The organizers want the safety of the bikers and the viewers.They monitor the Total Speed on the racing track after every Hour.
A biker whose Speed is 'L' or more, is considered a Fast Biker.
To Calculate the Total speed on the track- They Add the speed of each Fast biker ,at that Hour.
As soon as The total speed on the track is 'M' KiloMeters per Hour or more, The safety Alarm buzzes.
You need to tell what is the minimum number of Hours after which the safety alarm will buzz.
Input:
The first Line contains T- denoting the number of test cases.
The first line of each test case contains three space-separated integers N, M and L denoting the number of bikers and speed limit of the track respectively, and A fast Biker's Minimum Speed.
Each of next N lines contains two space-separated integers denoting Hi and Ai respectively.
Output:
For each test case-Output a single integer denoting the minimum number of Hours after which alarm buzzes.
Constraints:
1<=T<=100
1<=N<=1e5
1 ≤ M,L ≤ 1e10
1 ≤ Hi, Ai ≤ 1e9
My code runs properly on my computer and also on the ide .But when i click submit, it crashes returning Segmentation fault(SIGSEGV).
The array a can contain at most 100 bikers. The problem statement specifies that the number of bikers can be up to 10,000.
Overrunning the end of the array invokes Undefined Behaviour (UB) which is manifesting as a segfault on the judging system. Either the UB is manifesting differently on your development system or you have not tested it with a sufficiently large number of bikers.
I want to find total factors of any number.
In number theory, factorization is the breaking down of a composite number into smaller non-trivial divisors, which when multiplied together equal the original integer. Your job is to calculate number of unique factorization(containing at least two positive integers greater than one) of a number.
For example: 12 has 3 unique factorizations: 2*2*3, 2*6, 3*4 . Note:
3*4 and 4*3 are not considered different.
I have attempted to find that but not getting exact for all.
Here is my code :
#include<iostream>
using namespace std;
int count=0;
void factor(int n,int c,int n1)
{
for(int i=n1; i<n ; i++)
{
if(c*i==n)
{count++;
return;}
else
if(c*i>n)
return;
else
factor(n,c*i,i+1);
}
return;
}
int main()
{
int num,n;
cin>>num;
for(int i=0 ; i<num ; i++)
{
cin>>n;
count=0;
factor(n,1,1);
cout<<count<<endl;
}
return 0;
}
Input is number of test cases followed by test-cases(Numbers).
Example : Input: 3 12 36 3150
Output: 3 8 91
I think you are looking for number of factorizations of a number which are unique.
For this I think you need to find the count of number of prime factor of that number. Say for
12 = 2, 2, 3
Total count = 3;
For 2, 2, 3 we need
(2*2)*3 ~ 4*3
2*(2*3) ~ 2*6
2*2*3 ~ 2*2*3
To solve this we have idea found in Grimaldi, discrete and combinatorial mathematics.
To find number of ways of adding to a number(n) is 2^(n-1) -1. For 3 we have...
3 =
1+1+1
2+1
1+2
Total count = 2^(3-1) -1 = 4-1 = 3
We can use analogy to see that
1+1+1 is equivalent to 2*2*3
1+2 is equivalent to 2*(2*3)
2+1 is equivalent to (2*2)*3
Say number of prime factors = n
So we have number of factorizations = 2^(n-1)-1
The code:
#include <stdio.h>
int power(int x, int y)
{
int prod =1, i ;
for(i=1; i<=y;i++) prod *= x;
return prod;
}
int main()
{
int number,div;
int count = 0, ti, t;
printf("Input: ");
scanf("%d",&t);
for(ti=1; ti<=t;ti++)
{
scanf("%d", &number);
div = 2;count = 0;
while(number != 0)
{
if(number%div!=0) div = div + 1;
else
{
number = number / div;
//printf("%d ",div);
count++;
if(number==1) break;
}
}
printf("%d ", power(2,count-1)-1);
}
return 0;
}
Using mod is really useful in attempting to factor:
for(int i = 1; i <= fnum; ++i){ //where fnum is the number you wish to factor
if(!(fnum % i)) ++count;
}
return count;
Of cross this is the number of factors, not unique factors, if you want the number of unique factors, you have to do some additional work.
The solution is to realize that of all permutations, precisely one is sorted. 2 * 4 * 7 * 3 gives the same result as 2 * 3 * 4 * 7. That means that when you've found one factor, you should not check the remainder for lower factors. However, you should check if the same factor appears again: 12 = 2 * 2 * 3. The sequence 2 2 3 is also sorted.
BTW, you should give your variables clearer names, or at least add some comments describing them.