結果

問題 No.2012 Largest Triangle
ユーザー 👑 ygussanyygussany
提出日時 2022-07-16 00:26:01
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 5,084 bytes
コンパイル時間 1,457 ms
コンパイル使用メモリ 34,024 KB
実行使用メモリ 11,332 KB
最終ジャッジ日時 2023-09-10 06:55:11
合計ジャッジ時間 110,082 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,491 ms
4,380 KB
testcase_01 AC 2,490 ms
4,376 KB
testcase_02 AC 2,490 ms
4,380 KB
testcase_03 AC 2,491 ms
4,376 KB
testcase_04 AC 2,491 ms
4,376 KB
testcase_05 AC 2,490 ms
4,376 KB
testcase_06 AC 2,490 ms
4,376 KB
testcase_07 AC 2,491 ms
4,380 KB
testcase_08 AC 2,491 ms
4,376 KB
testcase_09 AC 2,490 ms
4,376 KB
testcase_10 AC 2,490 ms
4,376 KB
testcase_11 AC 2,490 ms
4,376 KB
testcase_12 AC 2,490 ms
4,376 KB
testcase_13 AC 2,491 ms
4,376 KB
testcase_14 AC 2,490 ms
4,376 KB
testcase_15 AC 2,490 ms
4,380 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 2,491 ms
4,376 KB
testcase_37 AC 2,490 ms
4,380 KB
testcase_38 AC 2,491 ms
4,380 KB
testcase_39 AC 2,490 ms
4,376 KB
testcase_40 AC 2,491 ms
4,380 KB
testcase_41 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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;
}

#define MT_N 624
#define MT_M 397
#define MT_MATRIX_A 0x9908b0dfUL
#define MT_UPPER_MASK 0x80000000UL
#define MT_LOWER_MASK 0x7fffffffUL

static unsigned int mt[MT_N];
static int mti = MT_N + 1;

void init_genrand(unsigned int s)
{
    mt[0] = s & 0xffffffffUL;
    for (mti = 1; mti < MT_N; mti++) {
        mt[mti] = (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti); 
        mt[mti] &= 0xffffffffUL;
    }
}

unsigned int genrand()
{
    unsigned int y;
    static unsigned int mag01[2] = {0x0UL, MT_MATRIX_A};

    if (mti >= MT_N) {
        int kk;
        if (mti == MT_N + 1) init_genrand(5489UL);
		
        for (kk = 0; kk < MT_N - MT_M; kk++) {
            y = (mt[kk] & MT_UPPER_MASK) | (mt[kk+1] & MT_LOWER_MASK);
            mt[kk] = mt[kk+MT_M] ^ (y >> 1) ^ mag01[y&0x1UL];
        }
        for (; kk < MT_N - 1; kk++) {
            y = (mt[kk] & MT_UPPER_MASK) | (mt[kk+1] & MT_LOWER_MASK);
            mt[kk] = mt[kk+(MT_M-MT_N)] ^ (y >> 1) ^ mag01[y&0x1UL];
        }
        y = (mt[MT_N-1] & MT_UPPER_MASK) | (mt[0] & MT_LOWER_MASK);
        mt[MT_N-1] = mt[MT_M-1] ^ (y >> 1) ^ mag01[y&0x1UL];

        mti = 0;
    }
  
    y = mt[mti++];

    y ^= (y >> 11);
    y ^= (y << 7) & 0x9d2c5680UL;
    y ^= (y << 15) & 0xefc60000UL;
    y ^= (y >> 18);

    return y;
}

int main()
{
	struct timeval tt;
	double beg, cur;
	gettimeofday(&tt, NULL);
	beg = tt.tv_sec + (double)tt.tv_usec / 1000000;
	
	int i, N;
	point p[200001];
	data d[200001];
	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 = distance(p[0], p[i]);
		d[i-1].id = i;
	}
	merge_sort(N, d);
	
	int j;
	long long ans = 0;
	while (1) {
		gettimeofday(&tt, NULL);
		cur = tt.tv_sec + (double)tt.tv_usec / 1000000;
		if (cur - beg > 2.49) break;
		
		do {
			i = genrand() % N + 1;
			j = genrand() % N + 1;
		} while (i == j);
		chmax(&ans, area_of_triangle(p[0], p[i], p[j]));
	}
	/* 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