Sunday, December 29, 2013

codechef MARCHA2 - "johnny and the beanstalk" solution

codechef MARCHA2 - "johnny and the beanstalk": http://www.codechef.com/problems/MARCHA2

codechef MARCHA2 - "johnny and the beanstalk" solution: http://ideone.com/MCNevO

#include <iostream>
#include <cstdio>
using namespace std;

int main() {
// your code goes here
int t, k, a, b, flag;
scanf("%d", &t);
while(t--) {
scanf("%d", &k);
b=1; flag=0;
while(k--) {
scanf("%d", &a); if(a>b) {
flag++; break;
} b=(b-a)*2;
}
if(flag || b) printf("No\n");
else printf("Yes\n");
}
return 0;
}

codechef MARCHA2 - "johnny and the beanstalk" guidance: 

Approach-1 (Lowest to highest level):
1.  The number of leaves on every level is at most the number of stems brought over from the previous level.
2.  The tree will stop growing once there are no more stems.  At the last level the number of stems is zero (they should all be leaves).

1st case: if(a>b) {
flag++; break;
}
2nd case: b=(b-a)*2; b=0;

No comments:

Post a Comment