Tuesday, 5 May 2015

UVA 10131 Is Bigger Smarter?

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / hackerrank / uva / uvalive / spoj), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

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

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100010

int path[1005], w[1005], s[1005], dp[1005], n;

int func(int pos)
{
    if(pos>=n) return 0;

    int &ret=dp[pos];
    if(ret!=-1) return ret;
    int mx=0;
    for(int i=0;i<n;i++)
        if(w[i]>w[pos] && s[i]<s[pos])
        {
            int val = func(i);
            if(val>mx)
            {
                mx=val;
                path[pos]=i;
            }
        }
    ret=mx+1;
return ret;
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc, kk=1;
    //string s;
    n=0;
    while(cin>>w[n]>>s[n])
    {
        n++;
    }
    SET(dp);
    SET(path);

    int mx=0, st;
    for(int i=0;i<n;i++)
    {
        int val=func(i);
        //cout<<val<<endl;
        if(val>mx)
        {
            st=i;
            mx=val;
        }
    }
    cout<<mx<<"\n";
    while(path[st]!=-1)
    {
        cout<<st+1<<"\n";
        st=path[st];
    }
    cout<<st+1<<"\n";
return 0;
}

No comments:

Post a Comment