WEIGHTBL - EDITORIAL

PROBLEM LINK:

Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3

Author: Daanish Mahajan
Tester: Rahul Dugar
Editorialist: Nandini Kapoor

DIFFICULTY:

Cakewalk

PREREQUISITES:

Math

PROBLEM:

All day eating and no play makes belly fat. The same thing happened with Chef during the lockdown. His weight before the lockdown was w_1 kg(measured on the most accurate machine in a hospital) and after M months of lockdown when he measured the weight on his home machine(can have errors) it came out to be w_2 ( >w_1) kg. As per a scientific research in all the growing kids, weights increase between x_1 kg to x_2 kg per month. Assuming himself to be a growing kid, Chef wants to know whether the machine is predicting correct results.

QUICK EXPLANATION:

If the weight that the machine displays lies between the minimum and maximum weight Chef could have after M months, then the machine is predicting correct results.

EXPLANATION:

The least weight that Chef could gain each month is x_1 as it is given x_1 \le x_2, whereas the maximum weight he could gain is x_2. Thus the minimum total weight he has put on in M months is x_1\times M and the maximum is x_2 \times M.

If the final weight of Chef i.e. w_2 is given to be greater than his initial weight w_1 by more than x_2\times M or less than x_1\times M, then the machine must not be showing the correct result and the output must be 0. Whereas if his weight gain has been between the two extremities, then the machine is predicting correct results in accordance with the scientific research on growing kids like Chef. As he could have gained any amount of weight between x_1 and x_2 inclusive each month, any result complying with the research would mean the machine is producing correct results.

TIME COMPLEXITY:

O(1) per test case

SOLUTIONS:

Setter
#include<bits/stdc++.h>
# define pb push_back 
#define pii pair<int, int>
#define mp make_pair
# define ll long long int
 
using namespace std;
 
const int maxt = 1e5;
const string newln = "\n", space = " ";
 
int main()
{   
    int t; cin >> t;
    while(t--){
        int w1, w2, x1, x2, m; cin >> w1 >> w2 >> x1 >> x2 >> m;
        int ans = (w2 - w1 >= x1 * m && w2 - w1 <= x2 * m ? 1 : 0); 
        cout << ans << endl;
    }
} 
Tester
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#ifndef rd
#define trace(...)
#define endl '\n'
#endif
#define pb push_back
#define fi first
#define se second
#define int long long
typedef long long ll;
typedef long double f80;
typedef pair<int,int> pii;
typedef pair<double,double> pdd;
//#define double long double
#define pll pair<ll,ll>
#define sz(x) ((long long)x.size())
#define fr(a,b,c) for(int a=b; a<=c; a++)
#define rep(a,b,c) for(int a=b; a<c; a++)
#define trav(a,x) for(auto &a:x)
#define all(con) con.begin(),con.end()
const int infi=0x3f3f3f3f;
const ll infl=0x3f3f3f3f3f3f3f3fLL;
//const int mod=998244353;
const int mod=1000000007;
typedef vector<int> vi;
typedef vector<ll> vl;
 
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> oset;
auto clk=clock();
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) {
	uniform_int_distribution<int> uid(0,lim-1);
	return uid(rang);
}
int powm(int a, int b) {
	int res=1;
	while(b) {
		if(b&1)
			res=(res*a)%mod;
		a=(a*a)%mod;
		b>>=1;
	}
	return res;
}
 
 
long long readInt(long long l, long long r, char endd) {
	long long x=0;
	int cnt=0;
	int fi=-1;
	bool is_neg=false;
	while(true) {
		char g=getchar();
		if(g=='-') {
			assert(fi==-1);
			is_neg=true;
			continue;
		}
		if('0'<=g&&g<='9') {
			x*=10;
			x+=g-'0';
			if(cnt==0) {
				fi=g-'0';
			}
			cnt++;
			assert(fi!=0 || cnt==1);
			assert(fi!=0 || is_neg==false);
 
			assert(!(cnt>19 || ( cnt==19 && fi>1) ));
		} else if(g==endd) {
			if(is_neg) {
				x=-x;
			}
			assert(l<=x&&x<=r);
			return x;
		} else {
			assert(false);
		}
	}
}
string readString(int l, int r, char endd) {
	string ret="";
	int cnt=0;
	while(true) {
		char g=getchar();
		assert(g!=-1);
		if(g==endd) {
			break;
		}
		cnt++;
		ret+=g;
	}
	assert(l<=cnt&&cnt<=r);
	return ret;
}
long long readIntSp(long long l, long long r) {
	return readInt(l,r,' ');
}
long long readIntLn(long long l, long long r) {
	return readInt(l,r,'\n');
}
string readStringLn(int l, int r) {
	return readString(l,r,'\n');
}
string readStringSp(int l, int r) {
	return readString(l,r,' ');
}
 
void solve() {
	int w1=readIntSp(1,100),w2=readIntSp(w1+1,100),x1=readIntSp(0,10),x2=readIntSp(x1,10),m=readIntLn(1,10);
	if(m*x2>=w2-w1&&m*x1<=w2-w1) {
		cout<<1<<endl;
	} else
		cout<<0<<endl;
}
signed main() {
	ios_base::sync_with_stdio(0),cin.tie(0);
	srand(chrono::high_resolution_clock::now().time_since_epoch().count());
	cout<<fixed<<setprecision(10);
	int t=readIntLn(1,100000);
//	int t;
//	cin>>t;
	fr(i,1,t)
		solve();
	assert(getchar()==EOF);
#ifdef rd
	cerr<<endl<<endl<<endl<<"Time Elapsed: "<<((double)(clock()-clk))/CLOCKS_PER_SEC<<endl;
#endif
}
 
Editorialist
    #include<bits/stdc++.h>
using namespace std;

#define _z ios_base::sync_with_stdio(false); cin.tie(NULL);
#define int long long int
#define endl "\n"
#define mod 1000000007
#define pb_ push_back
#define mp_ make_pair
//______________________________z_____________________________

void solve()
{
    int w1, w2, x1, x2, m;
    cin>>w1>>w2>>x1>>x2>>m;
    if(w2-w1<=m*x2 && w2-w1>=m*x1) cout<<1<<endl;
    else cout<<0<<endl;
}

int32_t main()
{
    _z;
    int t=1;
    cin>>t;
    while(t--)
    {
        solve();
    }
}
2 Likes

whats wrong with my code It’s giving TLE
https://www.codechef.com/viewsolution/43989149

Can anyone explain testers solution?

Chwck out my code.
You have a mistake in if condition.
My code :Solution: 44379053 | CodeChef
Thanks.

Here is my solution hope you will understand well as it is easy to understand;
https://www.codechef.com/viewsolution/45136654

what s wrong with my code. It’s giving WRONG ANSWER.
https://www.codechef.com/viewsolution/60096993

In line 11 your doing an integer divsion (truncating the fraction), due to which condition at line 12 is not accurate.