Showing posts with label Spoj. Show all posts
Showing posts with label Spoj. Show all posts

Sunday, 5 June 2016

Wooden Sticks (UVALive 2322, ZOJ 1025, HDU 1051, POJ 1065, SPOJ MSTICK, Regionals 2001 >> Asia - Taejon)

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / 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 100000


int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc, kk=1, n;
    pair<int, int> pii[5002];
    bool vis[5002];
    string s;
    char ch;
    cin>>tc;
    while(tc--)
    {
        CLR(vis);
        cin>>n;
        for(int i=0;i<n;i++)
            cin>>pii[i].first>>pii[i].second;
        sort(pii, pii+n);
        int cnt=0;
        for(int i=0;i<n;i++)
        {
            if(!vis[i])
            {
                cnt++;
                vis[i]=true;
                int cur=i;
                for(int j=i+1;j<n;j++)
                    if(!vis[j])
                        if(pii[j].second>=pii[cur].second)
                        {
                            vis[j]=true;
                            cur=j;
                        }
            }

        }
        cout<< cnt <<"\n";
    }
return 0;
}

Friday, 27 May 2016

Prime Path (UVA 12101, SPOJ PPATH, POJ 3126, HDU 1973, UVALive 3639, Regionals 2006 >> Europe - Northwestern)

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / 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 10000

vector<int>prime;
bool status[MX+2];
int pl;

void PrimeGenerate(int n)
{
    int sq=sqrt(n);
    for(int i=3; i<=sq; i+=2)
    {
        if(!status[i])
            for(int j=i*i; j<=n; j+=(i<<1))
                status[j]=true;
    }

    status[0]=status[1]=true;
    for(int i=4;i<=n;i+=2) status[i]=true;

    prime.push_back(2);
    for(int i=3; i<=n; i+=2)
        if(!status[i])
            prime.push_back(i);
return;
}

int dp[MX], st, ed, taken[MX], dist[10010];
vector<int>v[10010];

int bfs(int num)
{
    if(num==ed) return 1;
    dist[num]=1;
    queue<int>Q;
    Q.push(num);
    while(!Q.empty())
    {
        int qq=Q.front();
        Q.pop();
        for(int i=0;i<v[qq].size();i++)
            if(!dist[v[qq][i]])
            {
                dist[v[qq][i]]=dist[qq]+1;
                if(v[qq][i]==ed) return dist[v[qq][i]];
                Q.push(v[qq][i]);
            }
    }
return 100000;
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc, kk=1, n;
    PrimeGenerate(MX);
    int loopst;

    pl=prime.size();
    for(int i=0;i<pl;i++)
        if(prime[i]>999)
        {
            loopst=i;
            break;
        }

    for(int i=loopst;i<pl;i++)
    {

        for(int j=loopst;j<pl;j++)
        {
            int tmp=prime[i];
            int tmp2=prime[j], cnt=0;

            while(tmp && tmp2)
            {
                if(tmp%10 != tmp2%10) cnt++;
                tmp/=10;
                tmp2/=10;
            }

            if(cnt==1)
            {
                v[prime[i]].push_back(prime[j]);
            }
        }
    }

    cin>>tc;
    while(tc--)
    {
        cin>>st>>ed;
        if(ed>st) swap(st, ed);
        CLR(dist);
        int ans=bfs(st);
        if(ans<100000) cout<<ans-1<<"\n";
        else cout<<"Impossible\n";
    }

return 0;
}

Wednesday, 20 January 2016

Seinfeld (POJ 3991, UVALive 4733, HDU 3351, SPOJ ANARC09A, Regionals 2009 >> Africa/Middle East - Africa and Arab)

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / 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 100000


int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc, kk=1, n;
    string s;
    while(cin>>s)
    {
        if(s[0]=='-') break;
        int sl=s.size(), balance=0, ans=0;
        for(int i=0;i<sl;i++)
        {
            if(s[i]=='}') balance--;
            else balance++;

            if(balance==-1)
            {
                ans++;
                balance=1;
            }
        }
        ans+=balance/2;
        cout<<kk++<<". "<<ans<<"\n";
    }

return 0;
}

Tuesday, 15 December 2015

Conversions (UVALive 3911, HDU 1985, ZOJ 2988, SPOJ GNY07B, Regionals 2007 >> North America - Greater NY)

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / uva / uvalive), 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 100000


int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc, kk=1, n, m, x, y, a, b, c;
    double val;
    string s;
    cin>>tc;
    while(tc--)
    {
        cin>>val>>s;
        if(s=="kg") cout<< kk++ <<" "<<setprecision(4)<<fixed<<val*2.2046<<" lb\n";
        else if(s=="lb")    cout<< kk++ <<" "<<setprecision(4)<<fixed<<val*0.4536<<" kg\n";
        else if(s=="l")     cout<< kk++ <<" "<<setprecision(4)<<fixed<<val*0.2642<<" g\n";
        else if(s=="g")     cout<< kk++ <<" "<<setprecision(4)<<fixed<<val*3.7854<<" l\n";
    }
return 0;
}