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, June 3, 2016

EIUASSEMBLY-Assembly line

Assembly line

Given below c++ code is for EIUASSEMBLY spoj or Assembly line spoj.

Hint:- Simple Binary Search Implementation.





/*
===================================================
Name :- Nishant Raj
Email :- raj.nishant360@gmail.com
College :- Indian School of Mines
Branch :- Computer Science and Engineering
Time :- 03 June 2016 (Friday) 10:26
===================================================*/
#include <bits/stdc++.h>
using namespace std;
#define pii pair<int , int>
#define ll long long
#define mp make_pair
#define pb push_back
bool check(vector<int> &p, vector<int> &m, ll &total_cost, ll level){
    ll total = 0;
    for(int i = 0 ; i < p.size() ; i++){
        ll diff = level - p[i];
        if(diff > 0)
            total += diff*(ll)m[i];
        if(total > total_cost)
            return false;
    }
    return true;
}
ll b_search(vector<int> &p, vector<int> &m , ll total_cost){
    ll low = 0 , high = (LLONG_MAX/10), mid , ans = -1;
    while(low < high){
        mid = (low + high) >> 1;
        if(check(p, m, total_cost , mid)){
            low = mid+1;
            ans = max(mid , ans);
        } else high = mid;
    }
    return ans;
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        ll n , total_cost;
        scanf("%lld%lld", &n , &total_cost);
        vector<int> p(n) , m(n);
        for(int i = 0 ; i < n ; i++){
            scanf("%d%d" , &p[i] , &m[i]);
        }
        printf("%lld\n" , b_search(p , m , total_cost));
    }
    return 0;
}

No comments:

Post a Comment

Your comment is valuable to us