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

Monday, June 2, 2014

CRAN01-An Experiment by Penny

An Experiment by Penny

Below given c++ code is for CRAN01 spoj or An experiment by penny.

Here to find the logic you can test it for some small test by pen & paper. For myself I have checked for approx. 20 test cases then I got the logic .

Logic is maximum corner distance from given point is the minimum time to fill the entire box.
For example I am taking a grid of 7 x 7 and starting position as (4,5).


Here in the above table there are four corners coloured as yellow green black & blue and the starting position is red coloured box. Now the corners coloured with yellow and black are at maximum distances = 7 So here answer will be 7 .I get this logic from observation so if you want to clear your logic please use pen & paper and draw some small test cases or use brute-force.

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m,x,y;
        scanf("%d%d%d%d",&n,&m,&x,&y);
        int r1,r2,r3,r4,result;
        r1 = abs(x-1) + abs(y-1);
        r2 = abs(x-1) + abs(y-m);
        r3 = abs(x-n) + abs(y-1);
        r4 = abs(x-n) + abs(y-m);
        result = max(r1,max(r2,max(r3,r4)));
        printf("%d\n",result);
    }
    return 0;
}
here also python 2.7 code is for CRAN01 spoj or An experiment by penny.

import sys,math
t=int(sys.stdin.readline())
while t:
    n,m = map(int,sys.stdin.readline().split())
    x,y = map(int,sys.stdin.readline().split())
    r1 = abs(x-1) + abs(y-1)
    r2 = abs(x-1) + abs(y-m)
    r3 = abs(x-n) + abs(y-1)
    r4 = abs(x-n) + abs(y-m)
    result = max(r1,max(r2,max(r3,r4)))
    sys.stdout.write(str(result))
    sys.stdout.write("\n")
    t=t-1

No comments:

Post a Comment

Your comment is valuable to us