Professional Documents
Culture Documents
You are not logged in. Please login at www.codechef.com to post your questions! ×
CodeChef Discussion
questions tags
Search Here...
users
Hello Guys, The editorials for January Cook of are cooked (first two only). Once you sign in you will be
able to subscribe for any
11 Problem: SURVIVE updates here
By RSS:
Problem Statement:
Answers
Given a shop opening from Monday to Saturday, you are allowed to buy N sweets everyday the shop is
open and you eat K sweets everyday, determine if you can survive for next S days. Answers and Comments
Difficulty:Cakewalk
Solution: Question tags:
The constraints of problem are small enough to check for everyday.
editorial ×13,001
For every ith day (1<=i<=S) not being divisible by 7, buy N sweets. Eat K sweets everyday. If at any
cook90 ×81
day you have less than K sweets to eat, answer is impossible.
multhree ×29
If you survive S days, and supposing C is the number of remaining sweets and A being number of days
you bought sweets, just subtract C/N from A. (The number of sweet boxes untouched). survive ×28
Finding X: Check for start point of pattern (will be from p = 3 to 6 in 0-based indexing), X = floor((K- SAD - Editorial
p)/4).
Edge case: If fourth digit is 0, we take sum of first three digits and check its divisibility by 3.
PS: I have intentionally kept the length of editorials short. If you don't get it, feel free to post a
comment. :)
"After first three digits, only the pattern "2486" repeats till end of the number".
For 18 out of 90 valid d0 , d1 combinations d4 = 0. That's 20%. You call it "edge case"? :)
3★ eugalt (24 Jan, 13:30)
My choice ;)
The actual reason to mention it as edge case was, that i forgot to mention it earlier.
@eugalt - The fact here is, the pattern followed by that case is different which cannot be handled by current
algorithm. Plus, how many people were able to catch the case at first try? Not 20% I am sure :)
1 of 7 27/01/18, 11:07 PM
Unofficial Editorials January Cook Off - CodeCh... https://discuss.codechef.com/questions/121790...
You are not logged in. Please login at www.codechef.com to post your questions! ×
13 Answers:
Let dp[i][j] denotes the minimum swap needed to make array great for indices 1 to i and N-i+1 to N
with j denoting if this element is swapped or not.
Similarly, Dp[i][1]=1+min(dp[i-1][0],dp[i-1][1])
https://www.codechef.com/viewsolution/17125237
Edit:This solution should fail since i didnt consider the case for -1 when n>=4 and n is even and
middle 2 elements are same
(Lucky)
@vivek_1998299 could you explain that in a bit simpler way ....for noobs like me .
Ohk so lets say that elements from 1 to i-1 and its opposite side n-i+1 to N are arranged in an order .So lets say
N=6 ,i=2 so we consider that a[1]<a[2] and="" a[5]="">a[6] and lets say that we know the minimum swaps
required to do this arrangement ,then we can calculate it for i=3.
So dp[i][2] indicates the minimum number of swaps required to make array great for indices 1 to i and its
opposite side ,dp[i][0] denotes minimum value if i and N-i+1 indices are not swapped ,similarly dp[i][1] denotes
minimum value if i and N-i+1 are swapped.
So i want to calculate dp[3][0] ,i can use dp[2][0],dp[2][1] as since i know that now values upto 2 are arranged
1
,i just have to check for 1 pair of element(3,4).Now i need to check if i can use dp[2][0],so just see if a[2]>a[3]
and a[4]>a[5],if it is then i can use dp[2][0],similaarly do for dp[2]1
Similarly we calculate dp[3][1] but now we swap a[3],a[4] and then check the same things,(we do 1+min(...) as
this swap contributes thaat 1)
For the problem code SURVIVE 8 out of 10 randomly selected accepted AC's gave an output of 9 while
the correct output should be -1:
11
1
9 8 10
Please post it at the announcement thread- which is the official thread for feedback. It will be seen by problem
setting panel there.
Lmao yeah XD. If anything, my solutions always find something in the cook off :3 . The most hilarious was, when
my brute force got accepted in p3 XD (I think @kingofnumber was the setter of that one)
more than 75% solutions of SURVIVIE would have failed if only the corner cases were designed properly.
Yup, I did expected a corner case somewhere on lines of not buying on sunday giving -1. But I think those cases
also have N<K or some other condition which made them weaker- and hence lack of an individual case with
this feature alone.
BTW, do discuss that in feedback thread, the setter will value your insight surely.
3 Dp[i][j][k] denotes the minimum number of boxes taken upto ith day such that u have a balance of
j,and k denotes if on that day a box is taken.
Dp[i][j][0]=min(dp[i-1][j+k][0],dp[i-1][j+k][1])
Dp[i][j][1]=min(dp[i-1][j-(n-k)][0],dp[i-1][j-(n-k)][1]) + 1
2 of 7 27/01/18, 11:07 PM
Unofficial Editorials January Cook Off - CodeCh... https://discuss.codechef.com/questions/121790...
You are not logged in. Please login at www.codechef.com to post your questions! ×
Really good solution @taran_1407.
#DP_Intensifies
DP is in the air...
You just made 1718 people feel like genius with that xD
2
6★ abdullah768 (22 Jan, 23:23)
2 Problem: SURVIVE
te=int(input())
for _ in range(te):
n,k,s=input().split()
n,k,s=int(n),int(k),int(s)
capacity=k*s
availablity=n*s
availablity-=n*(s//7)
maans=0
if(capacity<=availablity):
answer=0
counter=0
for i in range(s):
counter+=1
counter%=7
if(counter!=0):
maans+=1
answer+=n
if(answer>=capacity):
break
print(maans)
else:
print("-1")
LOGIC:-
Maxmimum choclates he needs is ks which is capacity. And the availablity of chocolates is
ns-(n*(s//7)) as on Sunday shop is closed and he can't buy any chocolates. So if capacity >
availablity print("-1") as it is not possible to purchase. Else Run a loop to find the number of
days and dont forget not to calculate sundays and add n chocolates only on non sundays.
Problem: MULTHREE
test=int(input())
for y in range(test):
k,dig0,dig1=input().split()
k,dig0,dig1=int(k),int(dig0),int(dig1)
sum1=dig0+dig1
for i in range(3,k+2):
y=sum1%10
sum1+=y
if(y==2):
ter1=((k-i)//4)
ter1*=20
sum1+=ter1
ter=k-i
if(ter%4==3):
sum1+=18
elif(ter%4==2):
sum1+=12
elif(ter%4==1):
sum1+=4
break
if(y==0):
break
if(sum1%3==0):
print("YES")
else:
print("NO")
LOGIC:-
3 of 7 27/01/18, 11:07 PM
Unofficial Editorials January Cook Off - CodeCh... https://discuss.codechef.com/questions/121790...
You are not logged in. Please login at www.codechef.com to post your questions! ×
finding digits(sum%10); if digit is 0 then break as sum will remain same even after u do any
operation so break. Else break when u find a two as 2,4,8,6 block will keep on repeating.
therefore add ((k-i)//4)(2+4+6+8) to sum ((k-i)//4) is floor division plus remaining 4 12 or 18
depending on whether 4 or 4,8 or 4,8,6 is remaining. This block's size is got by (k-i)%4.Then
at last check whether sum is divisible by 3.
Like,Comment, Follow and award reputation points if u like. For any other queries or doubts
contact me.
Lovely editorial for 2nd problem. Even I followed the same idea- the solution was easy (had I just been
more careful and not done silly errors).
1
While implementing p2 tho, I did quite LOTS of silly errors-
1) Not updating K while calculating third digit. (i.e. After we check for first 2 digits, we should do K-2
as 2 digits are attached to the K digit num- I missed that).
2) In data type. At some point I used int (I think for sum)- should had avoided that since K is out of its
range, so generated sum will also be.
3) Be careful of any case when sum of digits becomes 5 ,which can lead to situation where 0 is
appended. Got TLE cause of this T_T.
When will the editorial for 3rd problem be there @taran_1407? That was some hell of casing (if-else,
case checks etc.)
I wasted two hours on third problem messing with if else blocks. and yes, if i manage to solve it soon (by
tomorrow i mean) i will write editorial.
Same here buddy. My entire time whooshed away in those if else. BTW, check out this solution, you might like
it-
http://codeforces.com/blog/entry/57259?#comment-409134
For the first problem, I used binary search on answer and for k no of days, we can use initial k days for
buying one box of chocolates.
1
Link to my solution.
Though your solution is nice one, it have a worse Time complexity of O(Slog(7*S)) while above mentioned
2
solution has time complexity of O(S).
You can improve your check function to work in O(1) by a simple change, and even shorter code.
I think binary search is an overkill here. But well, Binary search is <3 .
1
5★ vijju123 ♦ (22 Jan, 01:40)
Initially I waited for few mins before coding and then I saw multiple WAs,so I thought O(S) might be missing
some corner cases. I was sure about BS solution,so implemented it :p
Lol, I was hunting for cakewalk problem in first few min my order was-
1
>Opened p4. ".....He asked to find the matrix? DEFINITELY NOT CAKEWALK"
>p5 has "graph" in it. Lol graph no cakewalk.
>Great array...hmmm. Might be. (2 min after reading the statement and thinking "Noo....please this be
p4.....XD
>Multhree "YEAH This can be cakewalk!!" (Submitted a soln- got WA- "Hmm...Not cakewalk I guess lol")
>And finally chocoland lol.
The first problem can be solved in O(1) time complexity per test case, here is my code in cpp
1
#include <bits/stdc++.h>
using namespace std;
int main(){
4 of 7 27/01/18, 11:07 PM
Unofficial Editorials January Cook Off - CodeCh... https://discuss.codechef.com/questions/121790...
You are not logged in. Please login at www.codechef.com to post your questions! ×
int t;
cin >> t;
while(t--){
int n,k,s;
cin>>n>>k>>s;
int sundays = s/7;
int TOTAL = k*s;
int buy = n*(s-sundays);
if(buy < TOTAL){
cout << "-1" << endl;
}else{
cout << ceil(float(TOTAL)/n) << endl;
}
}
return 0;
}
The logic: If total number of chocolates needed for S days is MORE that the number of chocolates
possible to buy in that (S-number of Sundays) days then it isn't possible to survive, else it is just
ceil(float(TOTAL Needed chocolates)/(maximum number of chocolates possible to buy per day i.e. N ))
.. ceil() function is used as integer division will exclude some days at the end. i.e. a box of chocolates
should be bought at the day number of chocolates left is less than K.
It fails on the corner case of "9 8 10" where it should print -1. Lucky that cases were weak :p
Same kind of observation for 2nd question. Instead of 2486, I took 8624. Link to solution : Solution .
Buy everyday, because if you don't buy everyday, it may cause problem on coming Sunday. Take care to exclude
X boxed later only if you can survive.
for e.g if i take the test case 13 10 8 then on the first coming sunday the chocolates we would be having are
less than k. So the answer should be -1. But the correct answer is 7. What am i missing? coz for this test case
my code is giving output as -1
Here's the simulation of first 7 days for your TC. Set dif = 0, count = 0,
2
Day 1: diff = diff+10-8 = 2, count = 1
Day 5: diff = 8-8 = 0, count = 4 Your mistake, as you didn't buy this day and later on concluded
insufficiency of sweets.
And to all who read this comment, if stuck in a simulation, use print statements to do all this. ;)
5 of 7 27/01/18, 11:07 PM
Unofficial Editorials January Cook Off - CodeCh... https://discuss.codechef.com/questions/121790...
You are not logged in. Please login at www.codechef.com to post your questions! ×
Invitation threads, by default, are feedback threads as well. :)
#include <bits/stdc++.h>
0 using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--){
long long k;
int x,y,p=0;
int b[]={2,4,8,6};
int c[4];
cin>>k>>x>>y;
int z=2*(x+y);
int s=z%10;
if(k>2){
if(s==0){cout<<"NO\n";}
else{
int a=(((k-3)/4)%3)*2;
//cout<<"a="<<a;
for(int i=0;i<4;i++){c[i]=b[i];}
int r=1;
while(s!=c[0]){
for(int j=0;j<4;j++){
c[(j+r)%4]=b[j];
}
r++;
//for(int i=0;i<4;i++){cout<<c[i]<<" ";} cout<<"\n";
}
for(int i=1;i<=(k-3)%4;i++){
p=p+c[i-1];
}
//cout<<"p="<<p;
z=z+a+p;
//cout<<"z="<<z;
if(z%3==0){cout<<"YES\n";}
else{cout<<"NO\n";}
}
}
else{
if((x+y)%3==0){cout<<"YES\n";}
else{cout<<"NO\n";}
}
}
return 0;
}
what's wrong in my code to show a WA??
I had been busy the whole day, so i didn't even solve the third problem, let alone writing editorial.
Do they plan to release the official editorial for the MAGA problem? -_-
We will soon provide the official editorial. So sorry for the delays :(
1
admin ♦♦ (24 Jan, 11:38)
6 of 7 27/01/18, 11:07 PM
Unofficial Editorials January Cook Off - CodeCh... https://discuss.codechef.com/questions/121790...
You are not logged in. Please login at www.codechef.com to post your questions! ×
Preview
7 of 7 27/01/18, 11:07 PM