Here you will find solutions of many problems on spoj. If you want solution of some problem which is not listed in blog or have doubt regarding any spoj problem (which i have solved) or any programming concept (data structure) you can mail me @ raj.nishant360@gmail.com

And my humble request to you all that don't copy the code only try to understand the logic and algorithm behind the code. I have started this because if you tried as hard as you can and still can't find any solution to the problem then you can refer to this.
You can read my answer how to start competitive programming CLICK HERE

Thursday, June 5, 2014

VENOM-Touch of Venom

Touch of Venom

Given below c++ code is for venom spoj or touch the venom spoj.
 Explanation:- 
 Let H= Health, P = Poison, A = Heal Value
 Let Hero survived for “X” step (here we can see that answer will be odd position because health decreases on only odd position we will see this below)

 Now writing step from 1 






























  At fifth step you can calculate the relation between “X” and “n” by observing its values.
  X=  n + n – 1  ->  2n -1

  Now at X position we got an equation whose value should be less than or equal to zero.



































From above formula we will get value of "n" but actual value will be ceil(n) because we are checking for zero or less then zero.
Our answer will be X = 2n-1




#include <bits/stdc++.h>
using namespace std;
#define LL long long 
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int h,p,a;
        scanf("%d%d%d",&h,&p,&a);
        if(h<=p)
        {
            printf("1\n");
            continue;
        }
        double y,z,d;
        LL result=0;
        y= p - 2*a;
        z= 2*(a-h);
        d= sqrt(y*y - 4*p*z);
        result = ceil((-1*y + d)/(2*p));
        result = result + (result-1);
        printf("%lld\n",result);
    }
    return 0;
}

No comments:

Post a Comment

Your comment is valuable to us