Unofficial Editorials January Cook Off

Lol, I was hunting for cakewalk problem in first few min my order was-

>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.
1 Like
#DP_Intensifies

Happened with me too.

DP is in the air…

Nice solution @vivek_1998299

I think it was an overkill here btw thanks.

your solution fails too @vijju123 :smiley:

1 Like

May I know where the announcement thread is located @vijju123 ?

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)

Its not hard to miss :confused: https://discuss.codechef.com/questions/121750/invitation-for-january-cook-off-2018

Invitation threads, by default, are feedback threads as well. :slight_smile:

#include <bits/stdc++.h>
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??

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.

I think the 3rd problem can be solved using dp.

We’ll take the two cases(a1a3… and a1>a2<a3…) separately.

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.

We start from left and move to center.

We have following recurrence:
Val1=dp[i-1][0],val2=dp[i-1][1]
dp[i][0]=min(val1,val2) here first u should check if these satisfy the relations(i mean lets say if i-1 is not swapped then check a[i-1],a[i] and a[N-(i-1)+1],a[N-i+1]) if they dont accordingly set val1 Or val2 to inf.

Similarly,
Dp[i][1]=1+min(dp[i-1][0],dp[i-1][1])

My ac solution(though implemented now and not in contest):

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)

2 Likes

My both Solutions got AC in one go.

Problem: SURVIVE

My short solution in PYTHON-3.5:Mine is O(n) could be done in O(1) also.

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

My Short solution in PYTHON 3.5:-Time Complexity O(1)

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:-

For a number to be divisible by Three sum should be divisisble by 3.Keep on adding sum and 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.

Essentially almost same as solution of @taran_1407

Like,Comment, Follow and award reputation points if u like. For any other queries or doubts contact me.

For Codes as files to download visit:-
THIS PAGE

3 Likes

Didn’t get that.

1 Like

The first problem can be solved in O(1) time complexity per test case,
here is my code in cpp


#include <bits/stdc++.h>
using namespace std;
int main(){
	ios::sync_with_stdio(false);
	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.

1 Like

It fails on the corner case of “9 8 10” where it should print -1. Lucky that cases were weak :stuck_out_tongue:

@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 ,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](however swap a[2],a[5] and then check)

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)

1 Like