ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1207. Median on the Plane

problems with qsort
Posted by gt11 23 Jun 2009 17:05
#include <iostream>
using namespace std;
#include <math.h>

struct Point
{
    long double x;
    long double y;
};
struct Point2
{
    int number;
    long double angle;
};
void insertSort(Point2 ** a, long size) {
  Point2 * x;
  long i, j;

  for ( i=0; i < size; i++) {  // цикл проходов, i - номер прохода
    x = a[i];

    // поиск места элемента в готовой последовательности
    for ( j=i-1; j>=0 && (a[j]->angle < x->angle); j--)
      a[j+1] = a[j];      // сдвигаем элемент направо, пока не дошли

    // место найдено, вставить элемент
    a[j+1] = x;
  }
}
/*
int compare( const void *arg1, const void *arg2 )
{
   if(((Point2*)arg1)->angle>((Point2*)arg2)->angle)
       return -1;
   else
        return 1;

}*/
int main()
{

    int n;
    cin>>n;
    Point * arr = new Point[n];
    long double min_a=0;
    int min_ai;
    for(int i = 0;i<n;++i)
    {
        cin>>arr[i].x;
        cin>>arr[i].y;
        if((!i)||arr[i].y<min_a)
        {
            min_a = arr[i].y;
            min_ai = i;
        }
    }
    cout<<min_ai+1<<" ";
    Point2 ** arr2 = new Point2*[n-1];
    int j = 0;
    long double xx = arr[min_ai].x;
    long double yy = arr[min_ai].y;
    for(int i = 0;i<n;++i)
    {
        if(i!=min_ai)
        {
            arr2[j] = new Point2;
            arr2[j]->number = i;
            arr2[j]->angle =  (arr[i].x - xx)/sqrt(pow(arr[i].x-xx,2)+pow(arr[i].y-yy,2));
            ++j;

        }
    }
    //qsort(arr2,n-1,4,compare);
    insertSort(arr2,n-1);
    cout<<arr2[(n-1)/2]->number+1<<endl;
    return 0;
}

I don't know why, but qsort sorts them wrongly (I commented part that is qsort and changed it with simple O(n^2) sorting and got AC... But could tell me, what's so wrong with this qsort? I think I could get a better time with it.
Re: problems with qsort
Posted by [LG]_#\#@P$T[101]R 23 Jun 2009 18:33
In this task qSort works very quick :) But you time is not very bad ;)
Re: problems with qsort
Posted by Partisan 23 Jun 2009 19:09
If you will use sort from <algortihm>, you will get AC.
http://www.cplusplus.com/reference/clibrary/cstdlib/qsort/
I changed

int compare( const void *arg1, const void *arg2 )
{
if(((Point2*)arg1)->angle>((Point2*)arg2)->angle)
return -1;
else
return 1;

to

int compare( const void *arg1, const void *arg2 )
{
if(((Point2*)arg1)->angle>((Point2*)arg2)->angle)
return 1;
else
return -1;

according to conditions presented in the href above. On my computer I get answer "1 4" on first test, but on server in gets WA #1.

/* qsort example */
#include <stdio.h>
#include <stdlib.h>

int values[] = { 40, 10, 100, 90, 20, 25 };

int compare (const void * a, const void * b)
{
  return ( *(int*)a - *(int*)b );
}

int main ()
{
  int n;
  qsort (values, 6, sizeof(int), compare);
  for (n=0; n<6; n++)
     printf ("%d ",values[n]);
  return 0;
}
Re: problems with qsort
Posted by Vit Demidenko 16 Feb 2010 15:25
It is very very strange. QSort does not work here. Use Merge or Heap sort.