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

Tuesday, October 21, 2014

SVADA - Svada

Svada

Given below code is for svada spoj.

Hint : -> Binary search :

Here actually what I am doing is partitioning time in two part and checking
1->how many coconut can be plucked by first group of monkey in first part (let say X the number of coconut)
2-> how many coconut can be braked by second group of monkey in second part(say Y - the number of coconut)

let say for any partition of time if  X <= Y then that partition is possible .
And among such possible partition you have to check for partition which have least Y (according to question)

For checking we have applied binary search on time .



#include <bits/stdc++.h>
using namespace std;
#define LL long long 
LL solve(LL a,LL b,LL t)
{
    LL res = (t - a) < 0 ? 0 : (t-a)/b + 1;
    return res;
}
LL calculate(LL a[] , LL b[] , LL t , int sz)
{
    int total = 0;
    for(int i = 0 ; i < sz ; i++)
        total += solve(a[i] , b[i] , t);
    return  total;
}
LL b_search(LL a[] , LL b[] , LL c[] , LL d[] , LL t ,int sa, int sc)
{
    LL low = 1 , high = t , mid , lhalf , rhalf , ma = -1;
    while(low < high)
    {
        mid = (low + high)>>1;
        lhalf = calculate(a,b,mid,sa);
        rhalf = calculate(c,d,t - mid , sc);
        if(lhalf <= rhalf){
            low = mid + 1;
            ma = max(mid,ma);
        }
        else if(lhalf > rhalf)
            high = mid;
    }
    return ma;
}
int main()
{
    LL t;
    scanf("%lld",&t);
    int sa , sb;
    scanf("%d",&sa);
    LL a[100], b[100] , c[100] , d[100];
    for(int i = 0 ; i < sa ; i++)
        scanf("%lld%lld",&a[i] , &b[i]);
    scanf("%d",&sb);
    for(int i = 0 ;i<sb ; i++)
        scanf("%lld%lld",&c[i] ,&d[i]);
    printf("%lld\n", b_search(a,b,c,d,t,sa,sb));
    return  0;
}

1 comment:

Your comment is valuable to us