結果

問題 No.2012 Largest Triangle
ユーザー 👑 ygussanyygussany
提出日時 2022-07-16 00:35:25
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 3,934 bytes
コンパイル時間 1,031 ms
コンパイル使用メモリ 33,476 KB
実行使用メモリ 14,736 KB
最終ジャッジ日時 2023-09-10 07:03:13
合計ジャッジ時間 18,333 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,384 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 153 ms
14,632 KB
testcase_17 AC 152 ms
14,584 KB
testcase_18 AC 151 ms
14,612 KB
testcase_19 AC 152 ms
14,648 KB
testcase_20 AC 151 ms
14,656 KB
testcase_21 AC 152 ms
14,660 KB
testcase_22 AC 150 ms
14,736 KB
testcase_23 AC 150 ms
14,700 KB
testcase_24 AC 151 ms
14,736 KB
testcase_25 AC 151 ms
14,608 KB
testcase_26 AC 151 ms
14,628 KB
testcase_27 AC 151 ms
14,592 KB
testcase_28 AC 151 ms
14,636 KB
testcase_29 AC 153 ms
14,692 KB
testcase_30 AC 152 ms
14,696 KB
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 2 ms
4,376 KB
testcase_39 AC 2 ms
4,380 KB
testcase_40 AC 2 ms
4,380 KB
testcase_41 AC 26 ms
7,776 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/time.h>

const double pi = 3.14159265358979;

typedef struct {
	long long x, y;
} point;

double distance(point p, point q)
{
	return sqrt((double)(p.x - q.x) * (p.x - q.x) + (double)(p.y - q.y) * (p.y - q.y));
}

int is_above(point p, point q, point r)
{
	if (p.x == q.x) {
		if (r.x == p.x) return 0;
		else if (r.x < p.x) return 1;
		else return -1;
	}
	if (p.x == r.x) {
		if (p.y < r.y) return 1;
		else if (p.y > r.y) return -1;
		else return 0;
	}
	
	point t;
	if (q.x < p.x) {
		t = q;
		q = p;
		p = t;
	}
	if ((q.y - p.y) * (r.x - p.x) < (r.y - p.y) * (q.x - p.x)) return 1;
	else if ((q.y - p.y) * (r.x - p.x) > (r.y - p.y) * (q.x - p.x)) return -1;
	else return 0;
}

// Twice of the area of the triangle pqr
long long area_of_triangle(point p, point q, point r)
{
	long long L, R, B, T;
	L = p.x;
	if (L > q.x) L = q.x;
	if (L > r.x) L = r.x;
	R = p.x;
	if (R < q.x) R = q.x;
	if (R < r.x) R = r.x;
	B = p.y;
	if (B > q.y) B = q.y;
	if (B > r.y) B = r.y;
	T = p.y;
	if (T < q.y) T = q.y;
	if (T < r.y) T = r.y;
	
	long long ans = (R - L) * (T - B) * 2 - (llabs(p.x - q.x) * llabs(p.y - q.y) + llabs(q.x - r.x) * llabs(q.y - r.y) + llabs(r.x - p.x) * llabs(r.y - p.y));
	if (p.x != L && p.x != R && p.y != B && p.y != T) {
		if (is_above(q, r, p) == 1) {
			if ((q.x == L && q.y == B) || (q.x == R && q.y == T)) ans -= (p.x - L) * (T - p.y);
			else ans -= (R - p.x) * (T - p.y);
		} else {
			if ((q.x == L && q.y == B) || (q.x == R && q.y == T)) ans -= (R - p.x) * (p.y - B);
			else ans -= (p.x - L) * (p.y - B);
		}
	} else if (q.x != L && q.x != R && q.y != B && q.y != T) {
		if (is_above(r, p, q) == 1) {
			if ((r.x == L && r.y == B) || (r.x == R && r.y == T)) ans -= (q.x - L) * (T - q.y);
			else ans -= (R - q.x) * (T - q.y);
		} else {
			if ((r.x == L && r.y == B) || (r.x == R && r.y == T)) ans -= (R - q.x) * (q.y - B);
			else ans -= (q.x - L) * (q.y - B);
		}
	} else if (r.x != L && r.x != R && r.y != B && r.y != T) {
		if (is_above(p, q, r) == 1) {
			if ((p.x == L && p.y == B) || (p.x == R && p.y == T)) ans -= (r.x - L) * (T - r.y);
			else ans -= (R - r.x) * (T - r.y);
		} else {
			if ((p.x == L && p.y == B) || (p.x == R && p.y == T)) ans -= (r.x - L) * (r.y - B);
			else ans -= (R - r.x) * (r.y - B);
		}
	}
	return ans;
}

typedef struct {
	double key;
	int id;
} data;

void merge_sort(int n, data x[])
{
	static data y[200001] = {};
	if (n <= 1) return;
	merge_sort(n / 2, &(x[0]));
	merge_sort((n + 1) / 2, &(x[n/2]));
	
	int i, p, q;
	for (i = 0, p = 0, q = n / 2; i < n; i++) {
		if (p >= n / 2) y[i] = x[q++];
		else if (q >= n) y[i] = x[p++];
		else y[i] = (x[p].key < x[q].key)? x[p++]: x[q++];
	}
	for (i = 0; i < n; i++) x[i] = y[i];
}

void chmax(long long* a, long long b)
{
	if (*a < b) *a = b;
}

int main()
{
	struct timeval tt;
	double beg, cur;
	gettimeofday(&tt, NULL);
	beg = tt.tv_sec + (double)tt.tv_usec / 1000000;
	
	int i, j, N;
	long long ans = 0;
	point p[200001];
	data d[400001];
	scanf("%d", &N);
	for (i = 1, p[0].x = 0, p[0].y = 0; i <= N; i++) {
		scanf("%lld %lld", &(p[i].x), &(p[i].y));
		d[i-1].key = atan2(p[i].y, p[i].x);
		d[i-1].id = i;
	}
	merge_sort(N, d);
	for (i = 0; i < N; i++) {
		d[i+N].key = d[i].key + pi * 2.0;
		d[i+N].id = d[i].id;
	}
	for (i = 0, j = 1; i < N; i++) {
		while (fabs(d[j+1].key - d[i].key - pi) < fabs(d[j].key - d[i].key - pi)) j++;
		chmax(&ans, area_of_triangle(p[0], p[d[i].id], p[d[j].id]));
	}

	for (i = 1; i <= N; i++) {
		d[i-1].key = distance(p[0], p[i]);
		d[i-1].id = i;
	}
	merge_sort(N, d);
	
	for (i = N - 1; i >= 0; i--) {
		for (j = i - 1; j >= 0; j--) {
			if (ans - d[i].key * d[j].key >= 0.0) break;
			chmax(&ans, area_of_triangle(p[0], p[d[i].id], p[d[j].id]));
		}
		gettimeofday(&tt, NULL);
		cur = tt.tv_sec + (double)tt.tv_usec / 1000000;
		if (cur - beg > 2.49) break;
	}
	printf("%lld\n", ans);
	fflush(stdout);
	return 0;
}
0