Ideas: The X-axis and the y-axis of the case is independent, can be considered separately, so long as the enumeration position, can maintain the minimum value can be, as long as the formula is taken apart, preprocessing two prefixes and can, one is the prefix of W and a w * x prefix and
Code:
#include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int N = 50005; typedef long Long Ll;int t, N, M, Q;int X[n], y[n];ll sum[n][2], w[n];ll cal (int v, int n) {ll ans1 = sum[v][1] * V- SUM[V][0]; ll ans2 = (sum[n][0]-sum[v][0])-(sum[n][1]-sum[v][1]) * v; return ans1 + ans2;} int Gao (int *x, int n) {memset (sum, 0, sizeof (sum)); for (int i = 0; i < Q; i++) {sum[x[i]][0] + = w[i] * X[i]; SUM[X[I]][1] + = W[i]; } for (int i = 1, i <= N; i++) {for (int j = 0; J < 2; j + +) Sum[i][j] + = sum[i-1][j]; } int ans = 1; for (int i = 1; I <= n; i++) {if (Cal (I, N) < cal (ans, n)) ans = i; } return ans; int main () {int cas = 0; scanf ("%d", &t); while (t--) {scanf ("%d%d%d", &n, &m, &q); for (int i = 0; i < q; i++) scanf ("%d%d%lld", &x[i], &y[i], &w[i]); printf ("Case%d:%d%d\N ", ++cas, Gao (x, N), Gao (Y, M)); } return 0;}
Bnuoj 13268 Aladdin and the Optimal invitation