Sunday, 11 October 2015

Speed Limit (UVALive 3059, HDU 3030, POJ 2017, ZOJ 2176, Regionals 2004 >> North America - Mid-Central USA)

///     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, x, t;
    string s;
    char ch;
    while(cin>>n && n!=-1)
    {
        int ans=0, pt=0;
        for(int i=0;i<n;i++)
        {
            cin>>x>>t;
            ans+=x*(t-pt);
            pt=t;
        }
        cout<< ans <<" miles\n";
    }
return 0;
}

Doubles (UVALive 2787, HDU 1303, ZOJ 1760, POJ 1552, Regionals 2003 >> North America - Mid-Central USA)

///     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, arr[20], x;
    bool exist[202];
    string s;
    char ch;
    while(cin>>x && x!=-1)
    {
        int n=0;
        arr[n++]=x;
        CLR(exist);
        exist[x]=true;
        while(cin>>x && x)
        {
            arr[n++]=x;
            exist[x]=true;
        }
        int cnt=0;
        for(int i=0;i<n;i++)
            if(exist[arr[i]*2])
                cnt++;
        cout<< cnt <<"\n";
    }
return 0;
}

Class Statistics (UVALive 5682, HDU 4176, Regionals 2011 >> South Pacific)

///     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, arr[55];
    string s;
    cin>>tc;
    while(tc--)
    {
        cin>>n;
        for(int i=0;i<n;i++) cin>>arr[i];
        sort(arr, arr+n);
        int gap=0;
        for(int i=1;i<n;i++) gap=max(gap, arr[i]-arr[i-1]);
        cout<<"Class "<<kk++<<"\n";
        cout<< "Max "<<arr[n-1]<<", Min "<<arr[0]<<", Largest gap "<<gap<<"\n";
    }
return 0;
}

UVA 1428 Ping pong (UVALive 4329, POJ 3928, HDU 2492, Regionals 2008 >> Asia - Beijing)

///     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 tree[MX+2], mxl[20002], mnl[20002], mxr[20002], mnr[20002], arr[20002];

void insert(int x)
{
    while(x<=MX)
    {
        tree[x]++;
        x += x & -x;
    }
return;
}

int read(int x)
{
    int ret=0;
    while(x)
    {
        ret+=tree[x];
        x-= x & -x;
    }
return ret;
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc, kk=1, n, m, x, y, a, b, c, pos;
    string s;
    cin>>tc;
    while(tc--)
    {
        CLR(tree);
        cin>>n;
        for(int i=0;i<n;i++)
        {
            cin>>x;
            arr[i]=x;
            mnl[i]=read(x);
            mxl[i]=i-mnl[i];
            insert(x);
        }
        CLR(tree);

        for(int i=n-1;i>=0;i--)
        {
            x=arr[i];
            mnr[i]=read(x);
            mxr[i]=n-1-i-mnr[i];
            insert(x);
        }
        long long ans = 0;
        for(int i=0;i<n;i++) //cout<<mxr[i]<<endl;
            ans += mnl[i]*mxr[i] + mnr[i] * mxl[i];
        cout<< ans <<"\n";
    }
return 0;
}

Saturday, 10 October 2015

UVA 1203 Argus (UVALive 3135, POJ 2051, ZOJ 2212, FZU 1182, Regionals 2004 >> Asia - Beijing)

///     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 100010

struct Z{
    int id, val, inc;
    bool operator < (Z const p) const{
        if(val==p.val) return id>p.id;
        return val>p.val;
    }
};
Z z;
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc, kk=1, n, k;
    string s;
    char ch;
    priority_queue<Z>pq;
    while(cin>>s && s!="#")
    {
        cin>>z.id>>z.inc;
        z.val=z.inc;
        pq.push(z);
    }
    cin>>k;
    while(k--)
    {
        z = pq.top();
        pq.pop();
        cout<<z.id<<"\n";
        z.val+=z.inc;
        pq.push(z);
    }
return 0;
}

UVA 1210 Sum of Consecutive Prime Numbers (UVALive 3399, POJ 2739, Regionals 2005 >> Asia - Tokyo)

///     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];

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 main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc, kk=1, n;
    string s;
    char ch;
    PrimeGenerate(10000);
    int pl=prime.size();
    while(cin>>n && n)
    {
        int cnt=0;
        for(int i=0;i<pl;i++)
        {
            int tot=0;
            for(int j=i;j<pl;j++)
            {
                tot+=prime[j];
                if(tot==n) cnt++;
                if(tot>=n) break;
            }
        }
        cout<< cnt <<"\n";
    }
return 0;
}