great editorial
Can anybody tell me whats wrong with my code?
Python code, you say? Here’s mine.
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
Awesome Editorial! keep it up. looking forward to more editorials
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.
great explanation
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
How to solve it, if we are allowed to buy multiple notebooks instead of just one? Do we need to need to use Knapsack DP?
package easyLevel;
import java.util.Scanner;
public class ChefNNotebooks_CNOTE {
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--;
}
}
}
gettinh WA, not sure why
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?
Love your approach, multiple examples in multiple language !!!
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).
The explanation has been updated to explain this.
@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.
@kevinsogo : Thank you, and your way of pointing out the common mistakes is nice and overall u made this editorial awesome
Now I get it. Thanks !
Here’s a more pythonic code golf:
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)])]
When you post code, you should try indenting all your code four spaces, so that it renders properly as code.