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

Sunday, March 16, 2014

IITWPC4C-Maggu and Vectors

Maggu and Vectors

below given code is for iitwpc4c spoj or maggu and vectors spoj.

for first time i get approximately 20 WA in this problem . here in this problem we have to check only that vector can form triangle or not.
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <cstdlib>
using namespace std;
class vect
{
public:
 int x,y;
 vect operator +(vect v)
 {
  vect temp;
  temp.x=v.x + x;
  temp.y=v.y + y;
  return temp;
 }
 vect operator -()
 {
  vect temp;
  temp.x = -1*x;
  temp.y = -1*y;
  return temp;
 }
 bool operator ==(vect v)
 {
  if(v.x==x && v.y == y)
   return true;
  else 
   return false;
 }
 bool operator !=(vect v)
 {
  if(v.x != x || v.y != y)
   return true;
  else 
   return false;
 }
 bool is_colinear(vect v)
 {
  if(x==v.x||y==v.y)
         return 1;
     else if(x==0&&y==0)
         return 1;
     else if(v.x==0&&v.y==0)
         return 1;
     else if(y==0)
         return 0;
     else if(v.y==0)
          return 0;
      else if(x/(double)v.x==y/(double)v.y)
          return 1;
     else
         return 0;
 }
 vect make_pair()
 {
  vect temp;
  if(x<0)
   temp.x=1001 + (-1*x);
  else 
   temp.x=x;
  if(y<0)
   temp.y=1001 + (-1 * y);
  else
   temp.y=y;
  return temp;
 }
};
int check[2009][2009]={0};
int main()
{
 int t;
 scanf("%d",&t);
 while(t--)
 {
  int n,i,j,k,flag=0,pos_y,pos_x,x2,y2;
  vect v[1009],temp;
  for(i=0;i<2003;i++)
   for(j=0;j<=2003;j++)
    check[i][j]=0;
  scanf("%d",&n);
  for(i=0;i<n;i++){
   scanf("%d%d",&v[i].x,&v[i].y);
   if(v[i].x<0){
    pos_x=1001 + (-1*v[i].x);
   }
   else {
    pos_x=v[i].x;}
   if(v[i].y<0){
    pos_y=1001 + (-1 * v[i].y);}
   else{
    pos_y=v[i].y;   }
   check[pos_y][pos_x]=1;
  }
  vect zero,pos;
  zero.x=0;
  zero.y=0;
  if(n<3)
  {
   printf("NO\n");
   continue;
  }
  flag=0;
  for(i=0;i<n;i++)
  {
   for(j=i+1;j<n;j++)
   {
    if(v[i].is_colinear(v[j]))
     continue;
    temp= v[i] + v[j];
    if(temp.x<=1000 && temp.x>=-1000 && temp.y<=1000 && temp.y>=-1000)
    { 
     pos=temp.make_pair();
     if(check[pos.y][pos.x])
     {
      flag=1;
      break;
     }
    }
    temp= v[i] + (-v[j]);
    if(temp.x<=1000 && temp.x>=-1000 && temp.y<=1000 && temp.y>=-1000)
    { 
     pos=temp.make_pair();
     if(check[pos.y][pos.x])
     {
      flag=1;
      break;
     }
    }
    temp= (-v[i]) + (-v[j]);
    if(temp.x<=1000 && temp.x>=-1000 && temp.y<=1000 && temp.y>=-1000)
    { 
     pos=temp.make_pair();
     if(check[pos.y][pos.x])
     {
      flag=1;
      break;
     }
    }
    temp= (-v[i]) + v[j];
    if(temp.x<=1000 && temp.x>=-1000 && temp.y<=1000 && temp.y>=-1000)
    { 
     pos=temp.make_pair();
     if(check[pos.y][pos.x])
     {
      flag=1;
      break;
     }
    }
   }
   if(flag)
    break;
  }
  if(!flag)
   printf("NO\n");
  else 
   printf("YES\n");
 }
 return 0;
}

No comments:

Post a Comment

Your comment is valuable to us