#include<bits/stdc++.h>
using namespace std;
long long int max(long long int a,long long int b)
{
if(a>=b)
return a;
else
return b;
}
int main()
{
int i,t;
cin>>t;
for(i=0;i<t;i++)
{
long long q=0;
long n,j,r,k;
cin>>n>>r;
vector<long long> a(n), b(n),ans(n);
for(j=0;j<n;j++)
{
cin>>a[j];
}
for(j=0;j<n;j++)
{
cin>>b[j];
}
for(j=0;j<n;j++)
{
k=(a[j+1]-a[j])*r;
q+=b[j]-max(k,0);
ans[j]=q;
}
sort(ans.begin(),ans.end());
cout<<ans[n-1];
q=0;
cout<<'\n';
}
return 0;
}
#include
using namespace std;
int main()
{ int t;
cin>>t;
while(t>0)
{
long long int n,r,i,x=0,y=0,ans;
cin>>n>>r;
long long int a[n],b[n];
for(i=1;i<=n;i++)
{
cin>>a[i];
}
for(i=1;i<=n;i++)
{
cin>>b[i];
}
while (t–)
{
ll int N, relief;
cin >> N >> relief;
vector<ll int> times(N);
for (ll int i = 0; i < N; i++)
cin >> times[i];
vector<ll int> tension(N);
for (ll int i = 0; i < N; i++)
cin >> tension[i];
if (N == 1)
{
cout << tension[0] << endl;
}
else
{
ll int T = tension[0];
for (ll int i = 1; i < N; i++)
{
// You are calculating tension -> by including subtraction of relief and
// addition of tension in next exercise which is wrong.
T = T - relief + tension[i];
// Instead of this as mentioned in the Q that before any workouts, relief =0.
// so in above statement if (T - relief ) < 0 then T should be considered as
// zero before adding another exercise tension to it.
// and relief is not alone to be considered as relief it must be ->
// relief (times[i] - times[i-1])
/* T = T - relief (times[i] - times[i-1]) ;
if(T < 0)
T = 0;
T = T + tension[i]; */
}
cout << T << endl;
}
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
int n,r;
cin>>n>>r;
vector a;
vector b;
for(int i=0;i<n;i++){
long long int k;
cin>>k;
a.push_back(k);
}
for(int i=0;i<n;i++){
long long int k;
cin>>k;
b.push_back(k);
}
long long int sum=0;
for(int i=0;i<n;i++){
sum+=b[i];
if(i<n-1)
{
sum=sum-(r*(abs(a[i]-a[i+1])));
}
}
if(sum<0){
sum=0;
}
cout<<sum<<endl;
}
can you please help me also in solving my doubt @thetufguy
WHY CANT WE FIRST ADD ALL THE TENSION UNITS
ll tension = 0;
for (ll i = 0; i < n; i++)
{
tension = tension + b[i];
}
AND THEN CALCULATE THE REST UNITS
ll rest = 0;
for (ll i = 1; i < n; i++)
{
rest = rest + r * (a[i] - a[i - 1]);
}
AND THEN SUBTRACT THE REST UNITS FROM TENSION UNITS
ll final_tension = 0;
final_tension = tension - rest;
if (final_tension < 0)
cout << 0 << endl;
else
cout << final_tension << endl;
MY SAMPLE TEST CASES ARE PASSING BUT WHILE SUBMITTING ITS GIVING WRONG ANS AND I HAVE TRIED ALL TYPE OF TESTCASES EXCEPT FOR LARGE TEST CASES
CAN YOU PLEASE TELL ME WHERE IS THE MISTAKE IN MY CODE AND WHAT KIND OF TEST CASE MIGHT BE THAT WHICH IS FAILING
MY FULL CODE LINK
/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc=new Scanner(System.in);
List output=new ArrayList<>();
if(sc.hasNext())
{
int t=sc.nextInt();
for(int i=0;i<t;i++)
{
long n=sc.nextLong();
long r=sc.nextLong();
int size=(int)n;
long[] time=new long[size];
long[] get_tension=new long[size];
long[] final_tension=new long[size];
for(int j=0;j<size;j++)
{
time[j]=sc.nextLong(); //System.out.println(time[j]);
}
for(int j=0;j<size;j++)
{
get_tension[j]=sc.nextLong(); //System.out.println(get_tension[j]);
}
for(int j=0;j<size;j++)
{
if(j==0)
{
final_tension[j]=get_tension[j];
}
else if(j>0)
{
final_tension[j]=get_tension[j]+final_tension[j-1]-(time[j]-time[j-1])*r;
}
}
/long max=final_tension[0];
for(int j=0;j<size;j++)
{
if(max<final_tension[j])
{
max=final_tension[j];
}
}/
long count=0;
if(final_tension[size-1]<count)
output.add(count);
else
output.add(final_tension[size-1]);
}
for(int i=0;i<t;i++)
{
System.out.println(output.get(i));
}
}
}
}
Can Anyone tell mw what is wrong in my code ?
It corrects on test cases which are given .
ummm ohkay. See you are just calculating the tension at the end but we are to calculate the tension stepwise and see at which step it becomes maximum.
Consider the input:
2 1
1 500
500 1
Here when he does first workout tension becomes 500 but since there is 500 minute gap in the end tension is 1 which is less than 500 so final tension is not always the answer. Hope this helps
int main() {
// your code goe
int t;
cin>>t;
while(t–)
{
long long int n,r ;
cin>>n>>r;
long long int *ex=new long long int[n];
for( long long int i=0;i<n;i++)
{
cin>>ex[i];
}
long long int *te=new long long int[n];
for( long long int i=0;i<n;i++)
{
cin>>te[i];
}
long long int ans=0;
int max=0;
max=te[0];
for( long long int i=1;i<n;i++)
{
ans=max-(r*(ex[i]-ex[i-1]))+te[i];
if(ans>=max)
{
max=ans;
}
if(ans<0)
{
max=0;
}
}
if(n==1)
{
ans=te[0];
}
cout<<ans<<endl;
}
return 0;
this above mentioned calculation is same as what codechef provided while explaining the test cases.
so to prove which ans is correct i just searched in the submission list for the correct ans user’s code who also used c++17 and the same approach which you mentioned and i found the code of @akashkoduru just to check what is the ans? 2 or as you mentioned 1 and the ans which i got is 500.
i mean how is this possible ?
is there still something which needs to be taken care of?
note:- since this is my first discussion in codechef i dont know whether it is ok to point
out about other users code, if it is not then please say i would not repeat that
again. but i would be thankfull if both of you help me in clearing my doubt.
That’s what I said. The answer should be 500.
We have to give the maximum tension that he achieves at any point during his whole workout and not only at the end.
IN my test cases, let’s go step-wise.
He starts workout at t=1
at t=1 tension=500
Now he rests for 499 mins at the rate of one, so,
at t=500 tension=500-499=1
so there you go, you have seen the tension at all times when he does workout. Now all you have to do is to see which tension of all these is maximum. In our case, it is 500, which is at t=1;
Consider another case
3 2
1 2 3
5 6 1
here at t=1 tension=5
at t=2 tension =5-2*(2-1)+6=9
at t=3 tension =9-2*(3-2)+1=8
so here also maximum tension is found at t=2 and not at the end.
I hope you understand now : )
@thetufguy
yes i understood now
once again thank you soo much for replying and clearing my doubt
i have coded for same and ans got accepted
my code #include <bits/stdc++.h>
using namespace std; #define ll long long
int main()
{
int t;
cin >> t;
while (t–)
{
ll n, r;
cin >> n >> r;
ll a[n];
ll b[n];
for (ll i = 0; i < n; i++)
{
cin >> a[i];
}
for (ll i = 0; i < n; i++)
{
cin >> b[i];
}
ll tension = b[0];
ll max_tension = tension;
ll x = 0;
for (ll i = 1; i < n; i++)
{
tension = max(tension - (a[i] - a[i - 1]) * r, x) + b[i];
max_tension = max(max_tension, tension);
}
if (max_tension >= 0)
cout << max_tension << endl;
}
return 0;
}
can someone pls tell error guys my code had passed sample case but on submitting it shows wrong answer.
#include
using namespace std; #include <bits/stdc++.h>
int main() {
long long int n;
cin>>n;
for(long long int i = 0;i<n;i++){
long long int a,r;
cin>>a >>r;
long long int c[a];
long long int d[a];
for(long long int j = 0;j<a;j++){
cin>>c[j];
}
for(long long int k = 0;k<a;k++){
cin>>d[k];
}
long long int e[a];
e[0] = d[0];
for(long long int l = 1;l<a;l++){
long long int time = (c[l] - c[l-1]);
long long int relax = time*r;
long long int total = 0;
if(relax >= d[l-1]) total = 0 + d[l];
else total = d[l-1] - relax + d[l];
e[l] = total;
}
sort(e,e + a);
cout<<e[a-1]<<endl;
}
return 0;
}