CHECHOC - Editorial

@iit_yt_28 @dhruv788 can you plz explain why answer will be x not y when matrix is of size 1 by 1 square matrix (so called edge case) and x>y. Because in this case sum of itself is greater then y.

1 Like

@omhari Bro but can you think of a neighbour in case of 1x1? If thereā€™s no neighbour then we donā€™t really care about y. If we want to fill the 1x1 matrix upto the fullest we should think of max value of x that can be achieved.Y only comes into the picture when there are neighbours.

1 Like

Damn!! My mind is blowing up? I tested my solution against each testcase which I can think of and editorial gave (which I already thought during the contest) and even after rigorous testing against editorial solution I still canā€™t figure out why which testcase fails :frowning:
Can somebody take a look where it is wrong

int f(int x){return x<0?0:x;}

void solve(){
		int n,m,x,y;
		cin >> n >> m >> x >> y;
		int A = (n*m);
		int X = (A + 2 - 1)/2 * min(x,y); // rounding up the bigger quantity 
		int Y = A/2 * min(x,f(y-x));      // rounding down the lower quantity
		cout << X + Y << endl;
}

Can SOMEONE please help me figure out my error. Iā€™m getting WA:

#include<bits/stdc++.h>
#define ll long long int
using namespace std;

int main(){
ll n,m,x,y,res=0,t,p,r;
cin>>t;
while(tā€“){
cin>>n>>m>>x>>y;
p=(nm)/2;
r=(n
m)%2;
if(y>=(2x)) res=nm*x;

    else if(y>x and y<(2*x)){
        res+=(p*y);
        if(r==1) res+=x;
    }

    else{    //y<=x
        res+=(p*y);
        if(r==1) res+=y;
    }
    
    cout<<res<<"\n";
    res=0;
}

}

1*1 ruined my contest

void solve(){
	int n,m,x,y;
	cin>>n>>m>>x>>y;
	int sum = 0;
	if (y<=x) {
	    y = x;
	}
	if (y >= 2*x) {
		y = 2*x;
	}
	int odd = m/2 + m%2;
	int even = m - odd;
	for (int i=0; i<n; i++) {
		if (i % 2 == 0) 
			sum += (y-x)*(even) + (x)*(odd);
		else
			sum += (x)*(even) + (y-x)*(odd);
	}
	cout << sum << "\n";
}

Can someone tell what is wrong with this ?

I just realized it. Didnā€™t handle 1*1 edge case .

It was just one extra line from a perfect AC. Ahhhhhhh!!!

please help in making counter test case for my solution CodeChef: Practical coding for everyone thanks in advanceā€¦

// Created by Manmeet Singh Parmar
// name of snippet-> temp.sublime-snippet
// path -> sublime text 3/packages/user/temp.sublime-snippet

#include <bits/stdc++.h>
using namespace std;
#define int long long

typedef long double ld;

typedef pair<int, int> pi;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;

#define FOR(i, n)  for(int i=0; i<(n); i++)
#define FORA(i, a, n) for(int i = a; i <= (n); ++i)
#define FORD(i, a, n) for(int i = a; i >= (n); --i);
#define mod 1000000007
#define pi 2acos(0.0)
#define MP make_pair
#define PB push_back
#define EB emplace_back // its's faster than push_back
#define F first
#define S second
#define sz(x) (int)(x).size()
#define what_is(x) cerr << #x << "is" << x << endl;
int gcd(int a,int b) { while(a!=b) {if(a>b) a -=b; else b -=a;}
	return a;
}

int32_t main()
{
   	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t;
    cin>>t;
    while(t--){
    	int n,m; cin >> n >> m;
    	int x,y; cin >> x >> y;
    	int ans;
    	if(n==1 && m==1) cout << (((n*m)+1)/2)*x;
    	else {
    		if (x >= y)
    		{
    			ans = (((n*m)+1)/2)*(min(x,y));
    			/* code */
    		}
    		else {
    			ans = (((n*m)+1)/2)*x + ((n*m)/2)*(min(x,y-x));
    		}
    		cout << ans;
    	}
    	cout << '\n';
    }
    return 0;
}

What is wrong in my code? I didnā€™t miss a single case(I guess :sweat_smile:) and covered all possibilitiesā€¦ but still twas giving wrong answer :confused: .Code : CodeChef: Practical coding for everyone

Can anyone tell me where my code went wrong??
https://www.codechef.com/viewsolution/35959097

Thatā€™s literally a straight A explanation :sunglasses: :sunglasses:

2 Likes

Can anyone tell me where my code is going wrong?
https://www.codechef.com/viewsolution/36028200

someone please help me debug my codeā€¦ I cant find what am I missing

https://www.codechef.com/viewsolution/35947208

i guess it should be (n*m)/2 on line 35

Please tell why it is going to wrong
https://www.codechef.com/viewsolution/36028491

very simple solution easy to understand

my ans is 1ā€¦isnā€™t it right?

Can anyone please tell me where I am wrong. My code is giving correct ans for all the test cases that I made and I followed the exact same approach as the editorial. Corner case gives correct output too.
https://www.codechef.com/viewsolution/35986128

IN PYTHON