for _ in xrange(input()):
X, Y, K, N = map(int, raw_input().split())
print ["UnluckyChef", "LuckyChef"][min(1, len(filter(lambda b: b[0] <= 0 and b[1] >= 0, [[x - y for x, y in zip([X - Y, K], map(int, raw_input().split()))] for __ in xrange(N)])))]
I know it wasn’t code golf or anything like that but boy it was fun to squeeze it into 3 lines. xD
I don’t understand “it immediately breaks out of the loop, so it fails to read the next case properly”. How breaking out would cause problem for the next case.
In this example, even if there is a better solution, it wouldn’t be looked into. only the first solution that matches is taken. Is this the objective? obviously if there is a better combination of price and pages that satisfies the solution, but more optimised (in terms of price, say) then it wouldn’t be printed. Maybe I assumed that the optimised should be printed. Thanks!
ps. Great Editorial with multi-language implementation.
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.