https://www.codechef.com/viewsolution/36014583
I am thinking over this solution and I can’t get why this
if(x>=y && n * m%2==1) System.out.println(y*(m*n+1)/2);
is right. If your test case is 3 3 6 5 than your max pattern would be 3 2 3 (1st row), 2 3 2 (2nd row), 3 2 3 (3rd row). So 5 * 3 + 4 * 2 = 23 candies total, while the solution gives 5 * (3 * 3 + 1) /2 = 25, which is totally wrong?
ohhh! I missed 1x1 case for y<x only.
Add me, edge case was clear to me but i didn’t solve it. lol
even I followed the same approach but got WA every time.
Please if anyone can help
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main
{
public static void main(String[] args) throws Exception
{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
String stest=br.readLine();
int itest=Integer.parseInt(stest);
for(int i=0;i<itest;i++)
{
int r,c,x,y;
String s=br.readLine();
String[] sa=s.split(" ",s.length());
r=Integer.parseInt(sa[0]);
c=Integer.parseInt(sa[1]);
x=Integer.parseInt(sa[2]);
y=Integer.parseInt(sa[3]);
int total=r*c, min=0,max=0;
if(r==1 && c==1)
{
System.out.println(x);
}
else if(total%2==0)
{
max=(total/2)*x;
if(x<y)
{
min=(total/2)*(y-x);
}
else if(x==y)
{
min=(total/2)*(y);
}
else if(x>y)
{
max=0;
min=(total/2)*(y);
}
System.out.println(min+max);
}
else
{
max=((total/2)+1)*x;
if(x<y)
{
min=(total/2)*(y-x);
}
else if(x==y)
{
min=0;
}
else if(x>y)
{
min=0;
max=((total/2)+1)*(y);
}
System.out.println(min+max);
}
}
br.close();
}
}
Please help every test case including edge case giving correct answer on dry run.
And WA on submission
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main
{
public static void main(String[] args) throws Exception
{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
String stest=br.readLine();
int itest=Integer.parseInt(stest);
for(int i=0;i<itest;i++)
{
int r,c,x,y;
String s=br.readLine();
String[] sa=s.split(" ",s.length());
r=Integer.parseInt(sa[0]);
c=Integer.parseInt(sa[1]);
x=Integer.parseInt(sa[2]);
y=Integer.parseInt(sa[3]);
int total=r*c, min=0,max=0;
if(r==1 && c==1)
{
System.out.println(x);
}
else if(total%2==0)
{
max=(total/2)*x;
if(x<y)
{
min=(total/2)*(y-x);
}
else if(x==y)
{
min=(total/2)*(y);
}
else if(x>y)
{
max=0;
min=(total/2)*(y);
}
System.out.println(min+max);
}
else
{
max=((total/2)+1)*x;
if(x<y)
{
min=(total/2)*(y-x);
}
else if(x==y)
{
min=0;
}
else if(x>y)
{
min=0;
max=((total/2)+1)*(y);
}
System.out.println(min+max);
}
}
br.close();
}
}
Can please anyone tell whats wrong with this approach
https://www.codechef.com/viewsolution/36010895
Thanks in advance
What is wrong wih this code of mine?
Online GDB is online ide with compiler and debugger for C/C++. Code, Compiler, Run, Debug Share code nippets.
Even I followed the same.
Please if anyone can help
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
ll t;
cin>>t;
while(t–){
ll n,m,x,y;
cin>>n>>m>>x>>y;
ll total=nm;
ll ans;
if(y>=x){
ll minn=min(x,(y-x));
ll maxx=x;
// cout<<maxx<<endl;
// cout<<minn<<endl;
if(total%2==0)
{
ll temp=maxx+minn;
ans=(total/2) (temp);
}
else
{
ll minnum=total/2;
//cout<<“fd”<<minnum<<endl;
ll maxnum=total-minnum;
ans=(maxnummaxx)+(minnum minn);
}
}
else
{
if(total%2==0)
ans=(total/2)y;
else
{
ll temp=(total/2)+1;
ans=temp y;
}
}
cout<<ans<<endl;
}
}
Yeah got it
1 row and 1 col edge case was not handled very anoying lost 100 points
can anyone plzz help to fix the bug in my code!!!
https://www.codechef.com/viewsolution/36023337
Because the question was intended for brute force to pass here.
void solve()
{
//Code here;
int n, m, x, y;
cin >> n >> m >> x >> y;
int a[n + 1][m + 1];
for (int i = 1; i <= n; i++)
{
if (y >= x)
{
if (i & 1)
{
for (int j = 1; j <= m; j++)
{
a[i][j] = x;
int d = (y - x);
if (d > x)
{
a[i][j + 1] = x;
} else
{
a[i][j + 1] = d;
}
j++;
}
} else
{
for (int j = 1; j <= m; j++)
{
int f = (y - x);
if (f < x)
{
a[i][j] = f;
} else
{
a[i][j] = x;
}
a[i][j + 1] = x;
j++;
}
}
} else
{
if (i & 1)
{
for (int j = 1; j <= m; j++)
{
a[i][j] = y;
a[i][j + 1] = 0;
j++;
}
} else
{
for (int j = 1; j <= m; j++)
{
a[i][j] = 0;
a[i][j + 1] = y;
j++;
}
}
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
int tot = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
tot += a[i][j];
}
}
cout << tot << endl;
}
can anyone say where is my wrong? Here the 1x 1 ans also coming write but know why wa is coming?
your concept is not right if take the pattern as
5 0 5
0 5 0
5 0 5
then obviously it will give 25
so always take ma(y,x) and if (y>x) then only take x and (y-x)
@nandansharma1 The error is that in not accepted code for n*m==1 you have written cout<<min(x,y) which will return y when x is greater
but since it has no adjacent element so the second requirement is worthless in this test case i.e when n=m=1
@yashsri421 In line 35 you are doing 2 * n * m * x whereas it should be n * m * x
kaushik_199832:
a[i][j + 1]
This will go out of the bounds of the matrix .
eg - suppose at some iteration, j=4 and m=4 so it will enter the while loop but the line a[i][j+1] will be a[i][5+1] which exceeds the size of array.
can anyone please tell me what is wrong with this please…?
https://www.codechef.com/viewsolution/35999059