Cod sursa(job #1961257)

Utilizator workwork work work Data 10 aprilie 2017 23:32:42
Problema Cautare binara Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.13 kb
#include <fstream>

using namespace std;

ifstream F("br.in");
ofstream G("br.out");

int n, t, v[15001], a[15001][15001], st, dr, mij, x, c, ok, k;

int main()
{
    F >> n >> t;
    for(int i = 0; i < n; ++ i) F >> v[i];
    for(int i = 0; i < t; ++ i)
    {
        F >> x >> c;
        if(!a[x - 1][0])
        {
            a[x - 1][0] = v[x-1];
            k = x;
            for(int j = 1; j < n; ++ j)
            {
                a[x - 1][j] = a[x - 1][j - 1] + v[k ++];
                k == n ? k = 0 : k;
            }
        }
        if(a[x - 1][0] > c)
            G << "0\n";
        else if(a[x - 1][n - 1] < c)
            G << n << "\n";
        else
        {
            st = 0; dr = n - 1;
            while(st <= dr)
            {
                mij = (st + dr) / 2;
                if(a[x - 1][mij] <= c)
                    st = mij + 1;
                else if(a[x - 1][mij] > c)
                    dr = mij - 1;
            }
            mij = (st + dr) / 2;
            if(a[x-1][mij] > c)
                -- mij;
            G << mij  + 1<< '\n';
        }
    }
    return 0;
}