답안 #1096895

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1096895 2024-10-05T11:44:16 Z vjudge1 Sailing Race (CEOI12_race) C++17
100 / 100
582 ms 5140 KB
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
int n, k, dp1[2][505][505], dp2[2][505][505], ans1, ans2 = 1;
vector<int> vec[505], rvec[505];
int next(int x, int y) { return ((x + y - 1) % n + n) % n + 1; }
int min(int x, int y) { return x < y ? x : y; }
int max(int x, int y) { return x > y ? x : y; }
bool bemiddle(int l, int m, int r) { return l < r && l < m && m < r || l > r && (l < m || m < r); }
int main()
{
	scanf("%d%d", &n, &k);
	for (int i = 1, x; i <= n; ++i)
		while (true)
		{
			scanf("%d", &x);
			if (x == 0)
				break;
			vec[i].push_back(x);
			rvec[x].push_back(i);
		}
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j < i; ++j)
			for (int k = 0; k < 2; ++k)
				dp2[k][i][j] = dp2[k][j][i] = -1e9;
		sort(rvec[i].begin(), rvec[i].end());
	}
	for (int len = 2; len <= n; ++len)
		for (int i = 1; i <= n; ++i)
		{
			int j = next(i, len - 1);
			for (int k : vec[i])
				if (i < j && i <= k && k <= j || i > j && (k >= i || k <= j))
				{
					dp1[0][i][j] = max(dp1[0][i][j], max(dp1[1][next(i, 1)][k], dp1[0][k][j]) + 1);
					dp2[0][i][j] = max(dp2[0][i][j], dp2[0][k][j] + 1);
				}
			for (int k : vec[j])
				if (i < j && i <= k && k <= j || i > j && (k >= i || k <= j))
				{
					dp1[1][i][j] = max(dp1[1][i][j], max(dp1[1][i][k], dp1[0][k][next(j, -1)]) + 1);
					dp2[1][i][j] = max(dp2[1][i][j], dp2[1][i][k] + 1);
				}
		}
	for (int i = 1; i <= n; ++i)
	{
		int x = max(dp1[0][i][next(i, -1)], dp1[1][next(i, 1)][i]);
		if (x > ans1)
		{
			ans1 = x;
			ans2 = i;
		}
	}
	if (k == 1)
		for (int j = 1; j <= n; ++j)
			for (int k = 1; k <= n; ++k)
				for (int l : vec[k])
				{
					vector<int>::iterator it = lower_bound(rvec[j].begin(), rvec[j].end(), k);
					if (bemiddle(j, l, k) && it != rvec[j].begin()) // j - l - i - k
						if (bemiddle(l, *(--it), k))
						{
							int x = 1 + dp2[1][k][j] + 1 + max(dp1[1][next(j, 1)][l], dp1[0][l][next(*it, -1)]);
							if (x > ans1)
							{
								ans1 = x;
								ans2 = *it;
							}
						}
					it = upper_bound(rvec[j].begin(), rvec[j].end(), k);
					if (bemiddle(k, l, j) && it != rvec[j].end() && bemiddle(k, *it, l)) // k - i - l - j
					{
						int x = 1 + dp2[0][j][k] + 1 + max(dp1[1][next(*it, 1)][l], dp1[0][l][next(j, -1)]);
						if (x > ans1)
						{
							ans1 = x;
							ans2 = *it;
						}
					}
					// for (int i : rvec[j])
					// 	if (i < j && i < k && k < j || i > j && (k > i || k < j))
					// 	{
					// 		if (j < i && j < l && l < i || j > i && (l > j || l < i)) // j - l - i - k
					// 		{
					// 			int x = 1 + dp2[1][k][j] + 1 + max(dp1[1][next(j, 1)][l], dp1[0][l][next(i, -1)]);
					// 			if (x > ans1)
					// 			{
					// 				ans1 = x;
					// 				ans2 = i;
					// 			}
					// 		}
					// 	}
					// 	else if (k != i && k != j)
					// 	{
					// 		if (i < j && i < l && l < j || i > j && (l > i || l < j)) // k - i - l - j
					// 		{
					// 			int x = 1 + dp2[0][j][k] + 1 + max(dp1[1][next(i, 1)][l], dp1[0][l][next(j, -1)]);
					// 			if (x > ans1)
					// 			{
					// 				ans1 = x;
					// 				ans2 = i;
					// 			}
					// 		}
					// 	}
				}
	printf("%d\n%d\n", ans1, ans2);
	return 0;
}

Compilation message

race.cpp: In function 'bool bemiddle(int, int, int)':
race.cpp:10:60: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   10 | bool bemiddle(int l, int m, int r) { return l < r && l < m && m < r || l > r && (l < m || m < r); }
      |                                             ~~~~~~~~~~~~~~~^~~~~~~~
race.cpp: In function 'int main()':
race.cpp:35:25: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   35 |     if (i < j && i <= k && k <= j || i > j && (k >= i || k <= j))
      |         ~~~~~~~~~~~~~~~~^~~~~~~~~
race.cpp:41:25: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   41 |     if (i < j && i <= k && k <= j || i > j && (k >= i || k <= j))
      |         ~~~~~~~~~~~~~~~~^~~~~~~~~
race.cpp:13:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 |  scanf("%d%d", &n, &k);
      |  ~~~~~^~~~~~~~~~~~~~~~
race.cpp:17:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   17 |    scanf("%d", &x);
      |    ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 0 ms 604 KB Output is correct
4 Correct 1 ms 708 KB Output is correct
5 Correct 1 ms 860 KB Output is correct
6 Correct 3 ms 860 KB Output is correct
7 Correct 2 ms 860 KB Output is correct
8 Correct 2 ms 1116 KB Output is correct
9 Correct 3 ms 1116 KB Output is correct
10 Correct 7 ms 1400 KB Output is correct
11 Correct 4 ms 1116 KB Output is correct
12 Correct 29 ms 2160 KB Output is correct
13 Correct 55 ms 2956 KB Output is correct
14 Correct 36 ms 3672 KB Output is correct
15 Correct 408 ms 4840 KB Output is correct
16 Correct 426 ms 4952 KB Output is correct
17 Correct 329 ms 4696 KB Output is correct
18 Correct 47 ms 4444 KB Output is correct
19 Correct 523 ms 5140 KB Output is correct
20 Correct 582 ms 4956 KB Output is correct