Codeforces Game of Robots

// Accepted

#include<bits/stdc++.h>
using namespace std;

#define fast ios_base::sync_with_stdio(false)
#define high 1000010

typedef long long LL;

struct mystruct
{
    LL seqval, realval;
};

mystruct ar[high];

LL khoj(LL lo, LL hi, LL itm)
{
    LL mid;

    while(lo <= hi)
    {
        mid = (lo + hi) / 2;

        if(ar[mid].seqval == itm) return mid;
        else if(ar[mid].seqval > itm) hi=mid-1;
        else if(ar[mid].seqval < itm) lo=mid+1;
    }

    return lo;
}

int main()
{
    fast;
    LL n,k,i;
    while(cin >> n >> k)
    {
        LL x , tmp=0;

        for(i=1; i<=n; i++)
        {
            cin >> x;

            tmp+=i;
            ar[i].realval = x;
            ar[i].seqval = tmp;
        }

        //for(i=1; i<=n; i++)cout << ar[i].realval << " " << ar[i].seqval << "\n";

        LL indx = khoj(1, n, k); //cout << indx << " " << ar[indx].seqval << " \n";

        if(ar[indx].seqval == k) cout << ar[indx].realval << "\n";

        else
        {
            LL sval = ar[indx].seqval - k;
            indx-=sval;
            cout << ar[indx].realval << "\n";
        }
    }

    return 0;
}

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number