USACO: Ordered Fractions

0

Problem Link : http://train.usaco.org/usacoprob2?a=qBACVBxoOsW&S=frac1


/*
PROG: frac1
LANG: C++
*/

#include <bits/stdc++.h>

#define pii             pair <int,int>
#define sc              scanf
#define pf              printf
#define Pi              2*acos(0.0)
#define ms(a,b)         memset(a, b, sizeof(a))
#define pb(a)           push_back(a)
#define MP              make_pair
#define db              double
#define ll              long long
#define EPS             10E-10
#define ff              first
#define ss              second
#define sqr(x)          (x)*(x)
#define D(x)            cout<<#x " = "<<(x)<<endl
#define VI              vector <int>
#define DBG             pf("Hi\n")
#define MOD             100007
#define MAX             10000
#define CIN             ios_base::sync_with_stdio(0); cin.tie(0)
#define SZ(a)           (int)a.size()
#define sf(a)           scanf("%d",&a)
#define sfl(a)          scanf("%lld",&a)
#define sff(a,b)        scanf("%d %d",&a,&b)
#define sffl(a,b)       scanf("%lld %lld",&a,&b)
#define sfff(a,b,c)     scanf("%d %d %d",&a,&b,&c)
#define sfffl(a,b,c)    scanf("%lld %lld %lld",&a,&b,&c)
#define loop(i,n)       for(int i=0;i<n;i++)
#define REP(i,a,b)      for(int i=a;i<=b;i++)
#define TEST_CASE(t)    for(int z=1;z<=t;z++)
#define PRINT_CASE      printf("Case %d: ",z)
#define all(a)          a.begin(),a.end()
#define intlim          2147483648
#define inf             1000000
#define ull             unsigned long long

using namespace std;


/*----------------------Graph Moves----------------*/
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*------------------------------------------------*/

/*-----------------------Bitmask------------------*/
//int Set(int N,int pos){return N=N | (1<<pos);}
//int reset(int N,int pos){return N= N & ~(1<<pos);}
//bool check(int N,int pos){return (bool)(N & (1<<pos));}
/*------------------------------------------------*/

int n;

map<double,bool>mp;
vector< pair<double, pii > > v;

int main()
{
    freopen("frac1.in","r",stdin);
    freopen("frac1.out","w",stdout);
    cin>>n;
    mp[0/1.0]=1;
    v.pb(MP(0/1.0,MP(0,1)));

    REP(i,1,n) REP(j,1,n)

    {
        double a=i*1.0;
        double b=j*1.0;
        if(a/b<=1.0)
        {
            if(!mp[a/b])
            {
                mp[a/b]=1;
                v.pb(MP(a/b,MP(i,j)));
            }
        }
    }

    sort(all(v));
    loop(i,SZ(v))
    cout<<v[i].ss.ff<<"/"<<v[i].ss.ss<<endl;
    return 0;
}

UVa 11413 – Fill the Containers

0

/*
User ID: turing_13
Link: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2408
*/

/*
         +-+ +-+ +-+
         |R| |.| |S|
         +-+ +-+ +-+
 */

#include <bits/stdc++.h>

#define pii             pair <int,int>
#define sc              scanf
#define pf              printf
#define Pi              2*acos(0.0)
#define ms(a,b)         memset(a, b, sizeof(a))
#define pb(a)           push_back(a)
#define MP              make_pair
#define db              double
#define ll              long long
#define EPS             10E-10
#define ff              first
#define ss              second
#define sqr(x)          (x)*(x)
#define D(x)            cout<<#x " = "<<(x)<<endl
#define VI              vector <int>
#define DBG             pf("Hi\n")
#define MOD             100007
#define MAX             10000
#define CIN             ios_base::sync_with_stdio(0); cin.tie(0)
#define SZ(a)           (int)a.size()
#define sf(a)           scanf("%d",&a)
#define sfl(a)          scanf("%lld",&a)
#define sff(a,b)        scanf("%d %d",&a,&b)
#define sffl(a,b)       scanf("%lld %lld",&a,&b)
#define sfff(a,b,c)     scanf("%d %d %d",&a,&b,&c)
#define sfffl(a,b,c)    scanf("%lld %lld %lld",&a,&b,&c)
#define loop(i,n)       for(int i=0;i<n;i++)
#define REP(i,a,b)      for(int i=a;i<b;i++)
#define TEST_CASE(t)    for(int z=1;z<=t;z++)
#define PRINT_CASE      printf("Case %d: ",z)
#define all(a)          a.begin(),a.end()
#define intlim          2147483648
#define inf             1000000
#define ull             unsigned long long

using namespace std;


/*----------------------Graph Moves----------------*/
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*------------------------------------------------*/

/*-----------------------Bitmask------------------*/
//int Set(int N,int pos){return N=N | (1<<pos);}
//int reset(int N,int pos){return N= N & ~(1<<pos);}
//bool check(int N,int pos){return (bool)(N & (1<<pos));}
/*------------------------------------------------*/

int n,m;
int ara[100000];

int func(ll mid)
{
    int cnt=0;
    ll sum=0;
    for(int i=0;i<n;)
    {
        sum=0;
        while(i<n && sum+ara[i]<=mid)
        {
            sum+=ara[i];
            i++;
        }
        if(cnt>m)
            return cnt;
        cnt++;
    }
    return cnt;
}

int main()
{
    ///freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);

    while(sff(n,m)==2)
    {
         ll lo=1, hi=0,mid;
        loop(i,n) sf(ara[i]),hi+=ara[i];

        while(lo<hi)
        {
            mid=lo+(hi-lo)/2;
            if(func(mid)<=m)
                hi=mid;
            else
                lo=mid+1;
        }
        pf("%lld\n",hi);
    }
    return 0;
}

UVa 10370 – Above Average

0

/*
User ID: turing_13
Link: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1311
*/

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,c,ara[1000],i,j=0;
    float ans,avarage=0;
    scanf("%d", &n);
    while(n--)
    {
        scanf("%d", &c);
        for(i=0;i<c;i++)
        {
            scanf("%d", &ara[i]);
            avarage+=(float)(ara[i]);
        }
        avarage/=c;
        for(i=0;i<c;i++)
        {
            if(ara[i]>avarage)
                j++;
        }
        ans=(float)j/c*100;
        printf("%.3lf%%\n",ans);
        avarage=j=0;
    }
    return 0;
}


UVa 10055 – Hashmat the Brave Warrior

0

/*
User ID: turing_13
Link: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=12&page=show_problem&problem=996
*/

#include <stdio.h>

int main()
{
	long long a,b,c;
	while(scanf("%lld %lld",&a,&b)!=EOF)
	{
	    c=a-b;
	    if(c<0)
            c*=-1;
		printf("%lld\n",c);
	}
	return 0;
}

UVa 10812 – Beat the Spread!

0

/*
User ID: turing_13
Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1753
*/

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int i,n;
    scanf("%d",&n);
    for(i=0; i<n; i++)
    {
        int s,d,a,b;
        scanf("%d %d",&s,&d);
        a=(s+d)/2;
        b=(s-a);
        if(a>-1 && b>-1 && (a+b)==s && (a-b)==d)
        {
            printf("%d %d\n",a,b);
        }
        else
        {
            printf("impossible\n");
        }
    }
    return 0;
}

UVa 11479 – Is this the easiest problem?

0

/*
User ID: turing_13
Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2474
*/

#include <stdio.h>

int main()
{
    long int n,i,j,k,side[3],swap;
    while(scanf("%ld", &n)!= EOF)
    {
    for(i=1; i<=n; i++)
    {
        for(j=0; j<3; j++)
        {
            scanf("%ld",&side[j]);
        }
        for(j=0; j<3-1; j++)
        {
            for(k=0; k<3-j-1; k++)
            {
                if(side[k]>side[k+1])
                {
                    swap=side[k];
                    side[k]=side[k+1];
                    side[k+1]=swap;
                }
            }
        }
        if(side[0]<=0)
            printf("Case %ld: Invalid\n", i);
        else if(side[0]+side[1]<=side[2])
            printf("Case %d: Invalid\n", i);
        else if(side[0]==side[1] && side[1]==side[2])
            printf("Case %d: Equilateral\n", i);
        else if(side[0]==side[1] || side[1]==side[2])
            printf("Case %d: Isosceles\n", i);
        else
            printf("Case %d: Scalene\n", i);
    }
    }
    return 0;
}

Light OJ 1433 – Minimum Arc Distance

0

Problem Link : http://www.lightoj.com/volume_showproblem.php?problem=1433


#include <bits/stdc++.h>

#define pii pair <int,int>
#define sc scanf
#define pf printf
#define Pi 2*acos(0.0)
#define ms(a,b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define MP make_pair
#define oo 1<<29
#define dd double
#define ll long long
#define EPS 10E-10
#define ff first
#define ss second
#define MAX 10000
#define CIN ios_base::sync_with_stdio(0)
#define SZ(a) (int)a.size()
#define getint(a) scanf("%d",&a)
#define getint2(a,b) scanf("%d%d",&a,&b)
#define getint3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define loop(i,n) for(int i=0;i<n;i++)
#define TEST_CASE(t) for(int z=1;z<=t;z++)
#define PRINT_CASE printf("Case %d: ",z)
#define all(a) a.begin(),a.end()
#define intlim 2147483648
#define inf 1000000
#define rtintlim 46340
#define llim 9223372036854775808
#define rtllim 3037000499
#define ull unsigned long long
#define I int

using namespace std;

/* Bits operation */
int Set(int n,int pos)  { return n = n | 1<<pos;}
bool check(int n,int pos) { return n & 1<<pos;}
int Reset(int n, int pos) { return n=n & ~(1<<pos);}

int main()
{
    ///freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    int t,x,y;
    getint(t);

    TEST_CASE(t)
    {
        getint2(x,y);
        pii o(x,y);
        getint2(x,y);
        pii a(x,y);
        getint2(x,y);
        pii b(x,y);
        double ab=sqrt((a.ff-b.ff)*(a.ff-b.ff)+(a.ss-b.ss)*(a.ss-b.ss));
        double oa=sqrt((a.ff-o.ff)*(a.ff-o.ff)+(a.ss-o.ss)*(a.ss-o.ss));
        double ob=sqrt((b.ff-o.ff)*(b.ff-o.ff)+(b.ss-o.ss)*(b.ss-o.ss));
        double angle=acos(((oa*oa)+(ob*ob)-(ab*ab))/(2*oa*ob));
        PRINT_CASE;
        pf("%lf\n",angle*ob);
    }
    return 0;
}


Light OJ 1016 – Brush (II)

0

/*
Link: http://www.lightoj.com/volume_showproblem.php?problem=1016
*/

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <sstream>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <string>
#include <map>

#define pii pair <int,int>
#define sc scanf
#define pf printf
#define Pi 2*acos(0.0)
#define ms(a,b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define MP make_pair
#define oo 1<<29
#define dd double
#define ll long long
#define EPS 10E-10
#define ff first
#define ss second
#define MAX 10000
#define CIN ios_base::sync_with_stdio(0)
#define SZ(a) (int)a.size()
#define getint(a) scanf("%d",&a)
#define loop(i,n) for(int i=0;i<n;i++)
#define TEST_CASE(t) for(int z=1;z<=t;z++)
#define PRINT_CASE printf("Case %d: ",z)
#define all(a) a.begin(),a.end()
#define intlim 2147483648
#define inf 1000000
#define rtintlim 46340
#define llim 9223372036854775808
#define rtllim 3037000499
#define ull unsigned long long
#define I int

using namespace std;

vector<ll>v;
int main()
{
    ///freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    int t;
    getint(t);
    TEST_CASE(t)
    {
        int n,w;
        ll a,b;
        sc("%d %d",&n,&w);
        loop(i,n)
        {
            sc("%lld %lld",&a,&b);
            v.pb(b);
        }
        sort(all(v));
        int ans=1;
        a=v[0];
        for(int i=1; i<n; i++)
        {
            if(v[i]-a>w)
            {
                ans++;
                a=v[i];
            }
        }
        PRINT_CASE;
        pf("%d\n",ans);
        v.clear();
    }
    return 0;
}


Light OJ 1387 – Setu

0

/*
Link : http://www.lightoj.com/volume_showproblem.php?problem=1387
*/

#include <bits/stdc++.h>

#define pii pair <int,int>
#define sc scanf
#define pf printf
#define Pi 2*acos(0.0)
#define ms(a,b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define MP make_pair
#define oo 1<<29
#define dd double
#define ll long long
#define EPS 10E-10
#define ff first
#define ss second
#define MAX 10000
#define CIN ios_base::sync_with_stdio(0)
#define SZ(a) (int)a.size()
#define getint(a) scanf("%d",&a)
#define loop(i,n) for(int i=0;i<n;i++)
#define TEST_CASE(t) for(int z=1;z<=t;z++)
#define PRINT_CASE printf("Case %d:\n",z)
#define all(a) a.begin(),a.end()
#define intlim 2147483648
#define inf 1000000
#define rtintlim 46340
#define llim 9223372036854775808
#define rtllim 3037000499
#define ull unsigned long long
#define I int

using namespace std;

int main()
{
    ///freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);

    int t;
    cin>>t;
    TEST_CASE(t)
    {
        int n,m;
        ll sum=0;
        cin>>n;
        string str;
        PRINT_CASE;
        while(n--)
        {
            cin>>str;
            if(str=="donate")
            {
                cin>>m;
                sum+=m;
            }
            else if(str=="report")
            {
                cout<<sum<<endl;
            }
        }
    }
    return 0;
}

Light OJ 1354 – IP Checking

0

/*
Link : http://www.lightoj.com/volume_showproblem.php?problem=1354
*/

#include <bits/stdc++.h>

#define pii pair <int,int>
#define sc scanf
#define pf printf
#define Pi 2*acos(0.0)
#define ms(a,b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define MP make_pair
#define oo 1<<29
#define dd double
#define ll long long
#define EPS 10E-10
#define ff first
#define ss second
#define MAX 10000
#define CIN ios_base::sync_with_stdio(0)
#define SZ(a) (int)a.size()
#define getint(a) scanf("%d",&a)
#define loop(i,n) for(int i=0;i<n;i++)
#define TEST_CASE(t) for(int z=1;z<=t;z++)
#define PRINT_CASE printf("Case %d: ",z)
#define all(a) a.begin(),a.end()
#define intlim 2147483648
#define inf 1000000
#define rtintlim 46340
#define llim 9223372036854775808
#define rtllim 3037000499
#define ull unsigned long long
#define I int

using namespace std;

 string func(int a)
 {
     string str;
     while(a)
     {
         str+=(a%2)+'0';
         a/=2;
     }
     while(SZ(str)<8)
        str+='0';
     reverse(all(str));
     return str;
 }

int main()
{
    ///freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    int t;
    cin>>t;
    TEST_CASE(t)
    {
        int a,b,c,d;
        sc("%d.%d.%d.%d",&a,&b,&c,&d);
        string str,temp[4];
        str.clear();
        loop(i,4)
            temp[i].clear();
        cin>>str;
        for(int i=0,cnt=0;i<SZ(str);i++)
        {
            if(str[i]=='.')
            {
                cnt++;
                continue;
            }
            temp[cnt]+=str[i];
        }

        PRINT_CASE;
        
        if(temp[0]!=func(a) || temp[1]!=func(b) || temp[2]!=func(c)|| temp[3]!=func(d))
        {
            cout<<"No"<<endl;
            continue;
        }
        else
            cout<<"Yes"<<endl;
    }
    return 0;
}