Monday, 13 August 2012

Spoj 11. Factorial Solution

#include<iostream>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<utility>
#include<iomanip>
#include<queue>

using namespace std;

#define INF (1<<29)
#define SET(a) memset(a,-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define CLR(a) memset(a,0,sizeof(a))
#define FILL(a,v) memset(a,v,sizeof(a))
#define PB push_back
#define FOR(i,n) for(int i = 0;i<n;i++)
#define PI acos(-1.0)
#define EPS 1e-9
#define MP(a,b) make_pair(a,b)
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define LL long long


int main()
{
    int point1,point2,n,x1,y1,x2,y2,ans;
    while(cin>>n>>x1>>y1>>x2>>y2)
    {
        if(y1==0)
            point1=1;
        else if(x1==n)
            point1=2;
        else if(y1==n)
            point1=3;
        else if(x1==0)
            point1=4;

        if(y2==0)
            point2=1;
        else if(x2==n)
            point2=2;
        else if(y2==n)
            point2=3;
        else if(x2==0)
            point2=4;

        if((point1==point2) && (point1==1 || point1==3))
            {
              if(x1>x2)
                ans=x1-x2;
              else
                ans=x2-x1;
            }
        else if((point1==point2) && (point1==2 || point1==4))
            {
              if(y1>y2)
                ans=y1-y2;
              else
                ans=y2-y1;
            }
        else if(point1==1 && point2==2)
            ans=y2+n-x1;
        else if(point1==2 && point2==1)
            ans=y1+n-x2;
        else if(point1==2 && point2==3)
            ans=n-x2+n-y1;
        else if(point1==3 && point2==2)
            ans=n-x1+n-y2;
        else if(point1==3 && point2==4)
            ans=x1+n-y2;
        else if(point1==4 && point2==3)
            ans=x2+n-y1;
        else if(point1==4 && point2==1)
            ans=x2+y1;
        else if(point1==1 && point2==4)
            ans=x1+y2;
        else if((point1==1 && point2==3) || (point1==3 && point2==1))
            ans=min(3*n-x1-x2, n+x1+x2);
        else if((point1==2 && point2==4) || (point1==4 && point2==2))
            ans=min(3*n-y1-y2, n+y1+y2);

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

No comments:

Post a Comment