COKE - Editorial

why i am getting WA.
https://www.codechef.com/viewsolution/26054811

Can any one help me out with the following code.It is giving a wrong answer when submitted.

public static void main(String[] args) throws IOException {

    Scanner reader = new Scanner(System.in);

	long t = Long.parseLong(reader.nextLine());
	while (t-- > 0) {
		String[] input = reader.nextLine().split(" ");

		long cheapPrice = 0L;

		long n = Long.parseLong(input[0]);

		long m = Long.parseLong(input[1]);

		long k = Long.parseLong(input[2]);

		long l = Long.parseLong(input[3]);

		long r = Long.parseLong(input[4]);
		
		long j = 0;
		do {
		    
		    String[] tempPrice = reader.nextLine().split(" ");
			long c = Long.parseLong(tempPrice[0]);
			long p = Long.parseLong(tempPrice[1]);
			
			
			if (c < k - 1) {
				c += m;
			} else if (c > k + 1) {
				c -= m;
			}
			if (c >= l && c <= r) {
				if (cheapPrice == 0 || cheapPrice > p) {
					cheapPrice = p;
				}
			}
			j++;
		} while (j < n && reader.hasNextLine());

		if (cheapPrice == 0) {
			cheapPrice = -1;
		}
		System.out.println(cheapPrice);
	}
	reader.close();
}

hey i am getting SIGTSTP error with this can someone tell me the reason.TIA.

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

int main() {
int t;
cin>>t;
while(tā€“)
{
int n,m,k,l,r;
cin>>n>>m>>k>>l>>r;
int C,P,count =0,price;
vector fp(n);

    for(int i=0;i<n;i++)
    {
        cin>>C>>P;
        int t= C;
        while(m--)
        {
            if(t>k+1)t = t-1;
            else if (t<k-1) t=t+1;
            else t = k;
        }
        
        if (t>=l && t<=r)
        {
            fp.push_back(P);
            count++;
            
            
        }
    }
    if(count ==0){
        cout<<-1<<endl;
    }
    else{
        int price =  *min_element(fp.begin(),fp.end());

        cout<<price<<endl;
    }
}
return 0;

}

Your ā€˜lower_priceā€™ variable is 1 lac. Initialize it to 1000001. This is suppose there is a coke with price of 1 lac and the final temperature is fitting into the lower and upper range, your program will print -1.

Eg test case:

1
1 100 30 20 50
30 1000000

The answer should be 1 lac, but you are printing -1.

Yes, itā€™s simple but I couldnā€™t understand the below problem in the problemā€¦>
Since in the problem statement, if t>k+1 then we have to decrement t by 1 and if t<k-1 then we have to increment t by 1, then why in the solution, we are just comparing t and k instead of k+1 or k-1. Can anyone tell me something about this (the reason about which I am not able to thinkā€¦) ?

I would be thankfulā€¦if you help me

Please read the problem statement. It is clearly mentioned that if k - 1 <= t <= k + 1; Then 1 minute later t = k;

donā€™t know why my solution fails. Anyone ?

My code is also having a time complexity of O(n), but still itā€™s showing TLE, can anyone please tell whatā€™s going wrong in this code: CodeChef: Practical coding for everyone

// can any one help me , what is wrong in this code , i tried using map


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

#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
#define vll vector<ll>
#define vpll vector<pair<ll,ll>>
#define vch vector<char>
#define vvll vector<vector<ll>>
#define mod 1000000007
#define fori(x,n) for(int x = 0 ; x< n ; x++)

#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif

void _print(ll t) {cerr << t;}
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(lld t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ull t) {cerr << t;}

class solution {
public:
	void solve() {
		ll n , m , k , l , r ;
		cin >> n >> m >> k >> l >> r  ;
		multimap<ll , ll>  v ;
		for (int i = 1 ; i <= n ; i++) {
			ll c , p ;
			cin >> c >> p ;
			v.insert({p, c}) ;
		}

		for (auto it : v) {
			if (it.second < k - 1 ) {
				ll temp = it.second + m ;
				if (temp >= k + 1 || temp == k - 1) temp = k ;
				if (temp <= r && temp >= l) {
					cout << it.first << endl ;
					return ;
				}
			}
			else if (it.second > k + 1) {
				ll temp = it.second - m ;
				if (temp <= k - 1 || temp == k + 1) temp = k ;
				if (temp <= r && temp >= l) {
					cout << it.first << endl ;
					return ;
				}
			}
			else if (it.second >= k - 1 && it.second <= k + 1) {
				if (k <= r && k >= l) {
					cout << it.first << endl ;
					return ;
				}
			}
		}
		cout << -1 << endl ;

	}
};

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

	ll t ;
	cin >> t ;

	while (t--) {
		solution obj ;
		obj.solve() ;

	}
	return 0;
}

Hey, there are multiple errors in your logic, especially in the if conditions that you have written.
Here, I have corrected those mistakes in your code and got an AC, please compare both the codes to understand.

Submission Link

1 Like

thank u very much sir