Cod sursa(job #2496057)

Utilizator vlad2004SuntUnNanoTub vlad2004 Data 20 noiembrie 2019 10:18:12
Problema Shop Scor 0
Compilator cpp-64 Status done
Runda ad_hoc Marime 1.03 kb
#include <fstream>
#include <cmath>
#include <algorithm>
using namespace std;
ifstream cin("butoaie.in");
ofstream cout("butoaie.in");

int n,k,p,q,dif;

const int nmax=100001;
const int mxval=10000001;

int ans, mij;

int v[nmax+1];

//1 - e ok, poate pot gasi mai mic
//0 - nu e ok, prea mic nr de zile

bool vrf()
{
    int newx, q, rs;
   for(int i=1; i<=n; i++)
   {
        newx=v[i]-q*mij;
        q=newx/dif;
        rs=newx%dif;
        if(q+rs<=n)
            return 1;
        else
            return 0;
   }
}

int main()
{
    cin>>n>>k>>p>>q;
    int sum=0;
    for(int i=1; i<=n; i++)
        {cin>>v[i]; sum+=v[i];}

    int st=1, dr=mxval, mij;

    //p max
    //q min
    if(p<q)
    {
        swap(p,q);
        k=n-k;
    }
    dif=p-q;



    //caut bin

    while(st<=dr)
    {
        mij=(st+dr)/2;
        if(vrf()==1)
        {
            ans=mij;
            dr=mij-1;
        }
        else
            st=mij+1;
    }

    cout<<st;
    return 0;
}