Light OJ 1010 – Knights in Chessboard

0

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

#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 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 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;
        sc("%d %d",&n,&m);
        pf("Case %d: ",z);
        if(n==1 || m==1)
            {
                pf("%d\n",n*m);
            }
        else if(n==2 || m==2)
        {
            int ans=((n*m)/8)*4;
            if((n*m)%8==2)
                ans+=2;
            else if((n*m)%8==6 || (n*m)%8==4)
                ans+=4;
            pf("%d\n",ans);
        }
        else
        {
            pf("%d\n",((n*m)+1)/2);
        }
    }
    return 0;
}

Advertisements

Light OJ 1008 – Fibsieve`s Fantabulous Birthday

0

This is one of my favorite problem.. 🙂


/*
User ID: 
Link: http://www.lightoj.com/volume_showproblem.php?problem=1008
*/

#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 SZ(a) (int)a.size()
#define getint(a) scanf("%d",&a)
#define loop(i,n) for(int i=0;i<n;i++)
#define all(a) a.begin(),a.end()
#define TEST_CASES(t) for(int z=1;z<=t;z++)
#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_CASES(t)
    {
        long long s;
        cin>>s;
        long long a=ceil(sqrt(s));
        if(a%2==0)
        {
            long long temp=a*a-a+1;
            long long i=a;
            long long j=a;
            while(temp<s)
            {
                temp++;
                j--;
            }
            while(temp>s)
            {
                temp--;
                i--;
            }
            pf("Case %d: %lld %lld\n",z,i,j);
        }
        else
        {
            long long temp=a*a-a+1;
            long long i=a;
            long long j=a;
            while(temp<s)
            {
                temp++;
                i--;
            }
            while(temp>s)
            {
                temp--;
                j--;
            }
            pf("Case %d: %lld %lld\n",z,i,j);
        }

    }
    return 0;
}

UVa 119 – Greedy Gift Givers

0

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

#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 SZ(a) (int)a.size()
#define getint(a) scanf("%d",&a)
#define loop(i,n) for(int i=0;i<n;i++)
#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;

struct data
{
    string name;
    int initial_value,final_value;
};

bool test=0;
int main()
{
    ///freopen("in.txt","r",stdin);
    //freopen("gift1.out","w",stdout);

    vector<data>v;
    map<string,int>mp;
    int n;
    data sample;

    while(cin>>n)
    {
        if(test)
            cout<<endl;
        test=1;
        for(int i=0; i<n; i++)
        {
            cin>>sample.name;
            sample.initial_value=0;
            sample.final_value=0;
            v.pb(sample);
            mp[sample.name]=i;
        }

        string str,str1;
        int a,b;

        for(int i=0; i<n; i++)
        {
            cin>>str>>a>>b;
            v[mp[str]].initial_value=a;
            int c=0;

            for(int j=0; j<b; j++)
            {
                cin>>str1;
                v[mp[str1]].final_value+=(a/b);
                c+=(a/b);
            }

            v[mp[str]].final_value+=(a-c);
        }

        for(int i=0; i<n; i++)
            cout<<v[i].name<<" "<<v[i].final_value-v[i].initial_value<<endl;
        v.clear();
        mp.clear();
        str.clear();
        str1.clear();
    }
    return 0;
}

USACO : Your Ride Is Here

0

/*
Link : http://train.usaco.org/usacoprob2?a=3rv4k6xDvNx&S=ride
*/

/*
PROG: ride
LANG: C++
*/

#include <bits/stdc++.h>

#define ll long long
#define SZ(a) a.size()

using namespace std;

int main()
{
    freopen("ride.in","r",stdin);
    freopen("ride.out","w",stdout);
    string str1,str2;
    ll a=1,b=1;
    cin>>str1>>str2;
    for(int i=0;i<SZ(str1);i++)
    {
        a=((a%47)*(str1[i]-'A'+1)%47)%47;
    }

    for(int i=0;i<SZ(str2);i++)
    {
        b=((b%47)*(str2[i]-'A'+1)%47)%47;
    }

    if(a==b)
        cout<<"GO"<<endl;
    else
        cout<<"STAY"<<endl;
    return 0;
}

Codeforces : 495C Treasure

0

This one is one of my favorite. 🙂


/*
User ID: Tanmoy_Datta
Link : http://codeforces.com/contest/495/problem/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 oo 1<<29
#define dd double
#define ll long long
#define EPS 10E-10
#define ff first
#define ss second
#define MAX 10000
#define SZ(a) (int)a.size()
#define getint(a) scanf("%d",&a)
#define loop(i,n) for(int i=0;i<n;i++)
#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 c,d,a,b;
    c=d=a=b=0;
    string str;
    cin>>str;
    int len=SZ(str);
    for(int i=0; i<len; i++)
    {
        if(str[i]=='(')
        {
            a++;
            c++;
        }
        if(str[i]==')')
        {
            b++;
            if(c!=0)
                c--;
        }
        if(str[i]=='#')
        {
            d++;
            c=0;
        }
        if(b+d>a)
        {
            cout<<-1<<endl;
            return 0;
        }
    }

    if(c>0)
    {
        cout<<-1<<endl;
        return 0;
    }

    for(int i=1; i<d; i++)
    {
        cout<<1<<endl;
        b++;
    }
    cout<<a-b<<endl;
    return 0;
}

Codeforces: 508C Anya and Ghosts

0

/*
User ID: Tanmoy_Datta
Link : http://codeforces.com/problemset/problem/508/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 oo 1<<29
#define dd double
#define ll long long
#define EPS 10E-10
#define ff first
#define ss second
#define MAX 120
#define SZ(a) (int)a.size()
#define getint(a) scanf("%d",&a)
#define loop(i,n) for(int i=0;i<n;i++)
#define all(a) a.begin(),a.end()
#define intlim 2147483648
#define inf 100000
#define rtintlim 46340
#define llim 9223372036854775808
#define rtllim 3037000499
#define ull unsigned long long
#define I int

using namespace std;
ll ara[350];
int main()
{
    ///freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    ms(ara,-1000000);
    int m,t,r;
    cin>>m>>t>>r;
    vector<int>v;
    int num;
    for(int i=0; i<m; i++)
    {
        cin>>num;
        v.pb(num);
    }
    if(t<r)
        cout<<-1<<endl;
    else
    {
        int ans=0;
        //reverse(all(v));
        for(int i=0; i<m; i++)
        {
            int c=0;
            for(int j=1; j<=r; j++)
            {
                if(ara[j]+t>=v[i])
                    c++;
            }
            if(c==r)
                continue;
            else
            {
                //ans+=c;
                c=0;
                for(int j=1; j<=r; j++)
                {
                    if(ara[j]+t<v[i])
                    {
                        ara[j]=v[i]-(++c);
                        ans++;
                    }
                }

            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

Codeforces 508B Anton and currency you all know

0

/*
User ID: Tanmoy_Datta
Link : http://codeforces.com/problemset/problem/508/B
*/

#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 120
#define SZ(a) (int)a.size()
#define getint(a) scanf("%d",&a)
#define loop(i,n) for(int i=0;i<n;i++)
#define all(a) a.begin(),a.end()
#define intlim 2147483648
#define inf 100000
#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);
    string str;
    cin>>str;
    int lenth=SZ(str);
    int last=str[lenth-1]-'0';
    int a=0;
    int pos=-1,val=-1,dif=1000;
    for(int i=0; i<lenth-1; i++)
    {
        int d=(str[i]-'0');
        if(d%2==0)
        {
            if(last>d)
            {
                char c=str[i];
                str[i]=str[lenth-1];
                str[lenth-1]=c;
                a=1;
                break;
            }
            else
                pos=i;
        }
    }
    if(a)
        cout<<str<<endl;
    else
    {
        if(pos!=-1)
        {
            char c=str[pos];
            str[pos]=str[lenth-1];
            str[lenth-1]=c;
            cout<<str<<endl;
        }
        else
            cout<<-1<<endl;
    }
    return 0;
}