Same as this guy: CNOTE - Editorial - #33 by ssjgz
Alright. Checking into it right away.
I’ve just edited that post as the testcase contained some extra whitespace that caused your solution to choke, so make sure you’re using the updated testcase to debug
Got it working. I really need to start focusing on the little things. Thanks for the help!
Exact replica for your Python 2 code in Python 3 does not work. I mean it gives runtime error. Please help!
T = int(input())
for i in range(T):
X,Y,K,N = map(int, input().split(’ ‘))
books = []
for k in range(N):
p,c = map(int, input().split(’ '))
books.append((p,c))
for p,c in books:
if p>=(X-Y) and c<=K:
print(‘LuckyChef’)
break
else:
print(‘UnluckyChef’)
Figured out the solution myself.
I think there was extra whitespace in test cases…so added input().strip().split(’ ')
and it is working now!!
#include
using namespace std;
int main() {
int i,t,x,y,k,n,p,c,j=0;
cin>>t;
for(int i=0;i<t;i++)
{cin>>x>>y>>k>>n;
bool found=false;
for(int i=0;i<n;i++)
{cin>>p>>c;
if(p>=x-y&&c<=k)
{found=true;
break;
}}
cout<<(found?“LuckyChef”:“UnluckyChef”)<<endl;}
return 0;
}
Please help me.
When i run the program it runs successfully but on submission shows wrong answer.
Can somebody please point out the mistakes.
Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile!
Edit:
While we’re waiting - make sure your code gets the correct answer for this testcase.
Very well organized and over simplified tutorial ! Exactly what a newbie like me needs !
GREAT WORK!
https://www.codechef.com/viewsolution/37036315
This solution is giving runtime error because of break statement in the if block. Please tell me the reason behind it.
Try with this testcase:
and all should become clear
#include
using namespace std;
int main() {
long long t,poet,chefpage,chefK,n,pageN,costN;
cin>>t;
while(t–){
bool f=false;
cin>>poet>>chefpage>>chefK>>n;
while(n–){
cin>>pageN>>costN;
if(pageN>=poet-chefpage && costN<=chefK){
f=!f;
break;
}
}
if(f){
cout<<“LuckyChef”<<endl;
}else{
cout<<“UnluckyChef”<<endl;
}
}
return 0;
}
The only difference I see in the array for cost and pages. int P[111111];
int C[111111]; how does This give the correct answer please explain if you get to know.
The only difference I see in the array for cost and pages. int P[111111];
int C[111111]; how does This give the correct answer please explain if you get to know.
very good editorial.
Thanks author
my python code
t=int(input())
for _ in range(t):
x,y,k,n=[int(a) for a in input().split()]
want=x-y
re="UnluckyChef"
for l in range(n):
p,c=[int(a) for a in input().split()]
if p>=want and c<=k:
re="LuckyChef"
print(re)
a=int(input())
for i in range(a):
x,y,k,n=map(int,input().split())
for j in range(n):
p,c=map(int,input().split())
if y+p>x & k>=c:
print("LuckyChef")
else:
print("UnluckyChef")
How is this even wrong it works in Run section but not in submit did I forgot something?
Your code can print “LuckyChef” and “UnluckyChef” multiple times for a particular i (i.e test case). But we are required to output either “LuckyChef” or “UnluckyChef” (exactly one of them) once for each test case.
So, correct logic would be to maintain a flag fl := 0 which is handled as follows:
if (y + p >= x and k >= c) :
fl = 1
After processing all the (p_i, c_i)'s we can print based on fl.
I don’t know why my implement doesnt work, any suggestion, thank yoiu
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Codechef.FastIO io = new Codechef().new FastIO();
int tc = io.nextInt();
for(int tce=0;tce<tc; tce++){
int x = io.nextInt();
int y = io.nextInt();
int k = io.nextInt();
int n = io.nextInt();
boolean found = false;
for(int i = 0; i<n; i++){
int pi = io.nextInt();
int ci = io.nextInt();
if (pi >= x - y && ci <= k) {
found = true;
break;
}
}
System.out.println(found ? "LuckyChef" : "UnluckyChef");
}
io.close();
}
class FastIO extends PrintWriter {
private InputStream stream;
private byte[] buf = new byte[1 << 16];
private int curChar;
private int numChars;
// standard input
public FastIO() { this(System.in, System.out); }
public FastIO(InputStream i, OutputStream o) {
super(o);
stream = i;
}
// file input
public FastIO(String i, String o) throws IOException {
super(new FileWriter(o));
stream = new FileInputStream(i);
}
// throws InputMismatchException() if previously detected end of file
private int nextByte() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars == -1) {
return -1; // end of file
}
}
return buf[curChar++];
}
// to read in entire lines, replace c <= ' '
// with a function that checks whether c is a line break
public String next() {
int c;
do {
c = nextByte();
} while (c <= ' ');
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = nextByte();
} while (c > ' ');
return res.toString();
}
public int nextInt() { // nextLong() would be implemented similarly
int c;
do {
c = nextByte();
} while (c <= ' ');
int sgn = 1;
if (c == '-') {
sgn = -1;
c = nextByte();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res = 10 * res + c - '0';
c = nextByte();
} while (c > ' ');
return res * sgn;
}
public double nextDouble() { return Double.parseDouble(next()); }
}
}