Submission #636566

#TimeUsernameProblemLanguageResultExecution timeMemory
636566rainboySightseeing in Kyoto (JOI22_kyoto)C11
40 / 100
123 ms146636 KiB
#include <stdio.h>
#include <string.h>

#define N	100000
#define M	100000
#define N_	3000
#define M_	3000
#define INF	0x3f3f3f3f3f3f3f3f

long long min(long long a, long long b) { return a < b ? a : b; }

long long cross2(int x1, int y1, int x2, int y2) {
	return (long long) x1 * y2 - (long long) x2 * y1;
}

long long cross(int x0, int y0, int x1, int y1, int x2, int y2) {
	return cross2(x1 - x0, y1 - y0, x2 - x0, y2 - y0);
}

int main() {
	static int aa[N], bb[M], ii[N_], jj[M_];
	static long long dp[N_][M_];
	int n, n_, m, m_, i, j;

	scanf("%d%d", &n, &m);
	for (i = 0; i < n; i++)
		scanf("%d", &aa[i]);
	for (j = 0; j < m; j++)
		scanf("%d", &bb[j]);
	n_ = 0;
	for (i = 0; i < n; i++) {
		while (n_ >= 2 && cross(ii[n_ - 2], aa[ii[n_ - 2]], ii[n_ - 1], aa[ii[n_ - 1]], i, aa[i]) <= 0)
			n_--;
		ii[n_++] = i;
	}
	m_ = 0;
	for (j = 0; j < m; j++) {
		while (m_ >= 2 && cross(jj[m_ - 2], bb[jj[m_ - 2]], jj[m_ - 1], bb[jj[m_ - 1]], j, bb[j]) <= 0)
			m_--;
		jj[m_++] = j;
	}
	for (i = 0; i < n_; i++)
		memset(dp[i], 0x3f, m_ * sizeof *dp[i]);
	dp[0][0] = 0;
	for (i = 0; i < n_; i++)
		for (j = 0; j < m_; j++) {
			long long x = dp[i][j];

			if (x == INF)
				continue;
			if (j + 1 < m_)
				dp[i][j + 1] = min(dp[i][j + 1], x + (long long) aa[ii[i]] * (jj[j + 1] - jj[j]));
			if (i + 1 < n_)
				dp[i + 1][j] = min(dp[i + 1][j], x + (long long) bb[jj[j]] * (ii[i + 1] - ii[i]));
		}
	printf("%lld\n", dp[n_ - 1][m_ - 1]);
	return 0;
}

Compilation message (stderr)

kyoto.c: In function 'main':
kyoto.c:25:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   25 |  scanf("%d%d", &n, &m);
      |  ^~~~~~~~~~~~~~~~~~~~~
kyoto.c:27:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   27 |   scanf("%d", &aa[i]);
      |   ^~~~~~~~~~~~~~~~~~~
kyoto.c:29:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   29 |   scanf("%d", &bb[j]);
      |   ^~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...