-
Notifications
You must be signed in to change notification settings - Fork 3
/
fence.c
151 lines (135 loc) · 2.64 KB
/
fence.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include<stdio.h>
#define MAX 100
typedef struct{
int x;
int y;
int given_position;
}Point;
Point points[MAX];
Point start;
long no_of_sheeps, start_index;
void swap(long a,long b)
{
Point temp;
temp.x=points[a].x;
temp.y=points[a].y;
temp.given_position=points[a].given_position;
points[a].x=points[b].x;
points[a].y=points[b].y;
points[a].given_position=points[b].given_position;
points[b].x=temp.x;
points[b].y=temp.y;
points[b].given_position=temp.given_position;
}
void read_input()
{
int i;
scanf("%ld",&no_of_sheeps);
for(i=0;i<no_of_sheeps;i++)
{
scanf("%d",&points[i].x);
scanf("%d",&points[i].y);
points[i].given_position=i+1;
}
}
void findStartingPoint()
{
int i;
start.x=10001;
start.y=10001;
for(i=0;i<no_of_sheeps;i++)
{
if((points[i].y<start.y)||(points[i].y==start.y)&&(points[i].x<start.x))
{
start.x=points[i].x;
start.y=points[i].y;
start_index=i;
}
}
}
int compare(long first, long second)
{
int value =(points[first].y-points[start_index].y)*(points[second].x-points[first].x) -
(points[first].x-points[start_index].x)*(points[second].y-points[first].y);
if(value==0)
{
return (points[first].given_position<points[second].given_position)? first:second;
}
else if(value < 0)
return 1;
else
return -1;
}
void mergeSorted(long low, long mid, long high)
{
Point temp[MAX];
int comp;
long l,h,i=0;
l=low;
h=mid+1;
while((l<=mid) || (h < high))
{
// returns +ve if first point makes
// smaller angle to start point
comp=compare(l, h);
if(comp>0)
{
temp[i].x=points[l].x;
temp[i].y=points[l].y;
temp[i].given_position=points[l].given_position;
l++;
}
else
{
temp[i].x=points[h].x;
temp[i].y=points[h].y;
temp[i].given_position=points[h].given_position;
h++;
}
i++;
}
while(l<=mid)
{
temp[i].x=points[l].x;
temp[i].y=points[l].y;
temp[i].given_position=points[l].given_position;
l++;
i++;
}
while(h<=high)
{
temp[i].x=points[h].x;
temp[i].y=points[h].y;
temp[i].given_position=points[h].given_position;
h++;
i++;
}
}
void sortpointsByAngle(long low, long high)
{
long midPoint;
if(low < high)
{
midPoint=(low+high)/2;
sortpointsByAngle(low, midPoint);
sortpointsByAngle(midPoint+1, high);
mergeSorted(low, midPoint, high);
}
}
void solveSingleTest()
{
read_input();
findStartingPoint();
swap(start_index,0);
sortpointsByAngle(1,no_of_sheeps);
}
int main()
{
int test, i;
scanf("%d", &test);
for(i=0;i<test;i++)
{
solveSingleTest();
}
return 0;
}