Uva 11991 - Easy Problem from Rujia Liu?

/*

The Problem is vary impressive. Find the number's index among such queries. But that does not matter. The matter is Time limit 1sec.
If you do linear search than your complexity may be O(N^2) or more, so, that will be inefficient for that time.
Another Process, You may do Binary Search which has complexity O(lgN), but we know that the precondition of binary search is
to Sort data first. That means it will take O(NlgN) complexity. So, it will also inefficient for that time.
I have a method to solve that problem by using Precalculation method or DP.

Uva 11991 - Easy Problem from Rujia Liu?
Verdict : Accepted
Time: 0.900
Solution Method: Structure, DP

*/

#include<bits/stdc++.h>
#define N 1000010
#define sf scanf
#define pf printf
using namespace std;

typedef long long LL;
typedef map<long long, long long> mpll;
typedef map<long long, bool>mpbll;

struct my
{
    LL dx, num, cnt;
};

my starr[N];
LL numarr[N], parr[N], plen=0 , starrlen=1;

void input_n(LL k)
{
    mpll mp1;
    mp1.clear();
    LL mpocnt=0;

    for(LL i=1;i<=k;i++)
    {
        LL x;
        sf("%lld",&x);
        mp1[x]++; // Counting map. I have just count how many times a number exist. complexity O(lgN)
        mpocnt = mp1[x];
        numarr[i] = x;
        starr[x].num=x;
        starr[x].dx = i;
        starr[x].cnt=mpocnt;
    }

//    for(LL i=1;i<=starrlen; i++)
//    {
//        cout << starr[i].num << " " << starr[i].cnt << " " << starr[i].dx << "\n";
//    }
}

int main()
{
    LL n,m;

    while(~sf("%lld %lld",&n,&m))
    {
        plen=0;

        input_n(n);

        while(m--)
        {
            LL kotobar, nm;
            sf("%lld %lld",&kotobar,&nm);

            if(starr[nm].cnt == kotobar)
            {
                parr[plen++]=starr[nm].dx;
            }

            else if(starr[nm].cnt > kotobar)
            {
                LL i, chkcnt=0;
                bool f=false;

                for(i=1; i<=starr[nm].dx; i++)
                {
                    if(nm == numarr[i])
                    {
                        chkcnt++;
                    }

                    if(chkcnt == kotobar)
                    {
                        parr[plen++]=i;
                        break;
                    }
                }
            }

            else
            {
                parr[plen++]=0;
            }
        }

        for(LL i=0;i<plen;i++)
        {
            pf("%lld\n",parr[i]);
        }
    }

    return 0;
}

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number