Ultraviolet A 270 Lining Up

Source: Internet
Author: User

UVA_270

I only wanted to optimize the constant for this question. I did not expect the AC, but the time consumption was not optimistic. This question seems to have a n ^ 2logn algorithm, which should be sorted and processed first, however, no detailed ideas have been found yet.

The constant optimization method is to use an adjacent matrix to record which two points have been computed on a line before, in this case, you do not need to enumerate the two points as a straight line during enumeration.

After pondering the sentence "sort by Polar Angle", I suddenly realized that the enumeration process was changed from two enumeration points to one enumeration point, then, all the other points are sorted by the slope of the point, and the number of points on each Ray is calculated (this will not be lost, because we enumerate all vertices as the origin, we must enumerate the endpoints of the possible straight line, and then the straight line is equivalent to the ray), and the calculation process can be in O (n) the sorting time is O (nlogn) and the Basic time of enumeration is O (n). Therefore, the total complexity is O (n ^ 2 logn ).

#include<stdio.h>
#include<string.h>
#define MAXD 710
int g[MAXD][MAXD], s[MAXD], N, x[MAXD], y[MAXD], max;
char b[110];
void init()
{
N = 0;
while(gets(b) != NULL)
{
if(b[0] == '\0')
break;
sscanf(b, "%d%d", &x[N], &y[N]);
++ N;
}
}
void solve()
{
int i, j, k, p, q, top;
memset(g, -1, sizeof(g));
max = 0;
for(i = 0; i < N; i ++)
for(j = i + 1; j < N; j ++)
if(g[i][j])
{
top = 0;
for(k = 0; k < N; k ++)
if((y[j] - y[i]) * (x[k] - x[i]) == (x[j] - x[i]) * (y[k] - y[i]))
s[top ++] = k;
if(top > max)
max = top;
for(p = 0; p < top; p ++)
for(q = p + 1; q < top; q ++)
g[s[p]][s[q]] = g[s[q]][s[p]] = 0;
}
printf("%d\n", max);
}
int main()
{
int t, tt;
gets(b);
sscanf(b, "%d", &t);
gets(b);
for(tt = 0; tt < t; tt ++)
{
init();
if(tt)
printf("\n");
solve();
}
return 0;
}

 

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAXD 710
int r[MAXD], N, M, x[MAXD], y[MAXD], max, C;
char b[110];
int cmp(const void *_p, const void *_q)
{
int *p = (int *)_p;
int *q = (int *)_q;
int ans = (y[*p] - y[C]) * (x[*q] - x[C]) - (y[*q] - y[C]) * (x[*p] - x[C]);
if(x[*q] - x[C] < 0)
ans = -ans;
if(x[*p] - x[C] < 0)
ans = -ans;
return ans;
}
void init()
{
N = 0;
while(gets(b) != NULL)
{
if(b[0] == '\0')
break;
sscanf(b, "%d%d", &x[N], &y[N]);
++ N;
}
}
void solve()
{
int i, j, k, p, q;
max = 2;
for(i = 0; i < N; i ++)
{
M = 0;
for(j = 0; j < N; j ++)
if(x[j] != x[i])
r[M ++] = j;
if(N - M > max)
max = N - M;
C = i;
qsort(r, M, sizeof(r[0]), cmp);
for(j = 1, k = 2; j < M; j ++)
{
p = r[j - 1], q = r[j];
if((y[p] - y[i]) * (x[q] - x[i]) == (y[q] - y[i]) * (x[p] - x[i]))
++ k;
else
{
if(k > max)
max = k;
k = 2;
}
}
if(k > max)
max = k;
}
printf("%d\n", max);
}
int main()
{
int t, tt;
gets(b);
sscanf(b, "%d", &t);
gets(b);
for(tt = 0; tt < t; tt ++)
{
init();
if(tt)
printf("\n");
solve();
}
return 0;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.