My solution is getting WA for Sub-task 2, Task#4, can anyone suggest any edits?
t = int(input())
for i in range(t):
x, y, k, n = map(int, input().split())
l = [0]*n
cost = [1000]*n
j=0
for j in range(n):
l = list(map(int, input().split()))
if l[0]>=(x-y):
cost[j] = l[1]
if min(cost)<=k:
print("LuckyChef")
else:
print("UnluckyChef")
When I am running my code on ideone compiler ,the output is coming correct.But on running through it shows wrong answer.Can someone explain how to do subtasks?I made variable N and T long int still my answer is not getting accepted
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
while(T > 0){
String result = "UnluckyChef";
int X = sc.nextInt();
int Y = sc.nextInt();
int K = sc.nextInt();
int N = sc.nextInt();
while(N > 0){
int P = sc.nextInt();
int C = sc.nextInt();
if(C <= K && P >= X-Y){
result = "LuckyChef";
break;
}
N--;
}
System.out.println(result);
T--;
}
}
After the implementaion on C++ code, in the statement "Note that we used ‘\n’ instead of endl, because endl forces a flush, " what is flush and when can we know flush occured in the program (or) not?
There is a problem because you broke out of the loop too early while reading N pairs of integers (Pi, Ci). This means that the next Pi will be interpreted as the X of the next case, the Ci will be interpreted as the Y, and so on. Or if you are processing the input line by line, then you might get runtime error, because you were expecting four integers (X, Y, K, N) but only got two (Pi, Ci).
@saiavinashiitr a link explaining “flush” has been added. The idea is that when you do cout << something, the something isn’t necessarily immediately printed to the console/stdout. cout usually buffers them first, and then prints by batch, because printing something to the console is slow.
You call “flush” when you want it to be printed immediately (just like flushing a toilet). However, endl always forces a flush, causing unnecessary overhead in printing. By using '\n', there is no flush, so it’s faster.
for _ in xrange(input()):
X, Y, K, N = map(int, raw_input().split())
print ["UnluckyChef", "LuckyChef"][any(P >= X - Y and C <= K for P, C in [map(int, raw_input().strip().split()) for i in xrange(N)])]
because you are breaking the loop after getting result. you are not collecting all the input data. hence getting the wrong answer. remove break statement and you would get the correct answer.
i get a wrong answer when i use this snippet to take an integer input
int fastscan()
{
int c = gc();
int x = 0;
for (; (c>47 && c<58); c=gc())
x = x *10 + c - 48;
return x;
}
but when i used cin instead of this snippet my solution gets accepted.
#include <iostream>
using namespace std;
int main(int argc, char *arg[]) {
long int T, X, Y, K, N;
cin >> T;
long int t = 0, n, pi, ci, req_pages;
bool found;
while (t < T) {
cin >> X >> Y >> K >> N;
req_pages = X-Y;
n = 0;
found = false;
while (n < N) {
cin >> pi >> ci;
if ((req_pages <= pi) && (ci <= K)) {
found = true;
break;
}
++n;
}
cout << (found ? "LuckyChef" : "UnluckyChef") << '\n';
++t;
}
return 0;
}
I don’t see anything wrong with the program and yet I’m unable to get the right …