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

Friday, October 17, 2014

PRATA - Roti Prata

Roti Prata

Given below code is for prata spoj or roti prata spoj.

Hint :-> Use Binary Search 

This problem can also be solved by BFS also but i don't know how to solve it using bfs/dfs if any one know it then please comment of mail me your approach @ raj.nishant360@gmail.com.



#include <bits/stdc++.h>
using namespace std;
#define ULL unsigned long long 
int n;
ULL get_num(int a,ULL sum)
{
    ULL d = (ULL)sqrt(a*a + 8*a*sum);
    d-=a;
    return d / (2*a);
}
int check(int arr[],ULL chef,ULL tim)
{
    ULL count = 0;
    for(ULL i =0 ;i<chef;i++)
    {
        count += get_num(arr[i],tim);
        if(count >= n)
            return 1;
    }
    return 0;
}
ULL binary_search(int arr[],ULL chef,ULL max)
{
    ULL low = 1,mid,high = max;
    while(low<high)
    {
        mid = (low+high)>>1;
        if(check(arr,chef,mid))
            high = mid;
        else
            low = mid + 1;
    }
    return low;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int chef;
        scanf("%d%d",&n,&chef);
        int arr[chef+9],ma = -1;
        for(int i = 0;i<chef ; i++){
            scanf("%d",&arr[i]);
        }
        sort(arr,arr+chef);
        ULL res = binary_search(arr,chef,(ULL)10*n*(n+1)/2);
        printf("%lld\n",res);
    }
    return 0;
}

4 comments:

  1. how did you implement get_num ? can you please put any resource for it?

    ReplyDelete
  2. shridharaycharya formula for solving the eqauation of form ax^2+bx+c=0;

    ReplyDelete
  3. package QuestionsTillLec19;

    import java.util.Scanner;

    public class RotiPrata {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int paratha = sc.nextInt();
    int cook = sc.nextInt();
    int[] cooksRank = new int[cook];
    for (int i = 0; i < cooksRank.length; i++) {
    cooksRank[i] = sc.nextInt();
    }
    System.out.println(parathaSpoj(cooksRank,paratha)) ;
    }

    static boolean isPossible(int[] arr, int paratha, int mid) {
    int time = 0;
    int parCount = 0;
    for (int i = 0; i < arr.length; i++) {
    time = arr[i];
    int j = 2;
    while (time <= mid) {
    parCount++;
    time = time + (arr[i] * j);
    j++;
    }
    if (parCount >= paratha) return true;
    }
    return false;
    }

    static int parathaSpoj(int[] arr, int paratha) {
    int ans = -1;
    int low = 0, high = Integer.MAX_VALUE;

    while (low <= high) {
    int mid = low + (high - low) / 2;
    if (isPossible(arr, paratha, mid)) {
    ans = mid;
    high = mid - 1;
    } else {
    low = mid + 1;
    }
    }
    return ans;
    }
    }

    ReplyDelete

Your comment is valuable to us