Here you will find solutions of many problems on spoj. If you want solution of some problem which is not listed in blog or have doubt regarding any spoj problem (which i have solved) or any programming concept (data structure) you can mail me @ raj.nishant360@gmail.com

And my humble request to you all that don't copy the code only try to understand the logic and algorithm behind the code. I have started this because if you tried as hard as you can and still can't find any solution to the problem then you can refer to this.
You can read my answer how to start competitive programming CLICK HERE

Thursday, June 5, 2014

INS14J-Checkers

Checkers

Given below c++ code is for INS14J spoj or Checkers spoj.
Here is the hint to question which was given to me by Anurag Garg.

Topic : Game Theory, Grundy Numbers
This is a two player non-partisan game with perfect information. It can thus be reduced to the following nim game.
You have N steps in a staircase, each of which contains some given number of stones a[i] (i <= N). In one move, a player can choose a step, remove any number of stones from that step and add them to the step just below the chosen step. All stones that reach the ground level are removed from the game. The last player to make a move wins.
In the given question, N is the number of checkers on the board and a[i] is the number of cells that the checker can be moved before it comes to that axis. (either x=0 or y=0)
The grundy number of this nim game can be calculated by taking XOR of the number of stones placed at every odd numbered step (starting with the step just above the ground level) eg: if G s the ground level and the number of stones in each subsequent step are given as:
G 2 4 5 1 2
Grundy number = XOR(2,5,2) = 5
If the XOR is greater than 0, the first player wins. If XOR is zero, the second player wins the game.

If you want to study grundy number CLICK HERE

For proof of above theory CLICK HERE

For more details CLICK HERE


#include <bits/stdc++.h>
using namespace std;
class pairM
{
public:
    int x,y,d;
    void operator =(pairM p)
    {
        x=p.x;
        y=p.y;
        d=p.d;
    }
};
inline void read(int &x) {
    register int c = getchar_unlocked();
    x = 0;
    int neg = 0;
    for(; ((c<48 || c>57) && c != '-'); c = getchar_unlocked());
    if(c=='-') {
        neg = 1;
        c = getchar_unlocked();}
    for(; c>47 && c<58 ; c = getchar_unlocked()) {
        x = (x<<1) + (x<<3) + c - 48;}
    if(neg)
        x = -x;
}
void merge(pairM p[],int low,int mid,int high)
{
    pairM temp[10009];
    int i=low,j=mid+1,k=low;
    while(i<=mid && j<=high)
    {
        if(p[i].d >= p[j].d)
            temp[k++] = p[i++];
        else
            temp[k++] = p[j++];
    }
    while(i<=mid)
        temp[k++] = p[i++];
    while(j<=high)
        temp[k++] = p[j++];
    for(i=low;i<=high;i++)
        p[i] = temp[i];
}
void partition(pairM p[],int low,int high)
{
    int mid;
    if(low<high)
    {
        mid = (low + high)>>1;
        partition(p,low,mid);
        partition(p,mid+1,high);
        merge(p,low,mid,high);
    }
}
int main()
{
    int t;
    read(t);
    while(t--)
    {
        int n;
        read(n);
        pairM p[10009];
        for(int i=0;i<n;i++)
        {
            read(p[i].x);
            read(p[i].y);
            p[i].d= p[i].y-p[i].x;
        }
        partition(p,0,n-1);
        long long sum=0;
        for(int i=0;i<n;i++)
            if(!(i&1)){
                sum = sum^( min(p[i].y,p[i].x));
            }
        if(sum)
            printf("Yes\n");
        else
            printf("No\n"); 
    }
    return 0;
}

No comments:

Post a Comment

Your comment is valuable to us