codeforces 424D Biathlon Track

codeforces 424D Biathlon Track

题意

题解

代码

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for(int i=(a); i<(b); i++)
#define sz(a) (int)a.size()
#define de(a) cout << #a << " = " << a << endl
#define dd(a) cout << #a << " = " << a << " "
#define all(a) a.begin(), a.end()
#define endl "
"
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
//---

const int N = 333;

int n, m, t, tp, tu, td;
int x11, y11, x2, y2;
int a[N][N];
ll ans;
ll pre[N][N][4];

inline void upd(ll x, int i, int j, int a, int b) {
	if(!ans || abs(x - t) < abs(ans - t)) {
		ans = x;
		x11 = i;
		x2 = j;
		y11 = a;
		y2 = b;
	}
}

inline int calc(int x, int y) {
	if(x == y) return tp;
	if(x > y) return tu;
	return td;
}

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	cin >> n >> m >> t;
	cin >> tp >> tu >> td;
	rep(i, 1, n+1) rep(j, 1, m+1) cin >> a[i][j];
	rep(i, 1, n+1) rep(j, 2, m+1) pre[i][j][0] = pre[i][j-1][0] + calc(a[i][j], a[i][j-1]);
	rep(i, 1, n+1) rep(j, 1, m) pre[i][j][2] = pre[i][j-1][2] + calc(a[i][j], a[i][j+1]);
	rep(j, 1, m+1) rep(i, 2, n+1) pre[i][j][1] = pre[i-1][j][1] + calc(a[i][j], a[i-1][j]);
	rep(j, 1, m+1) rep(i, 1, n) pre[i][j][3] = pre[i-1][j][3] + calc(a[i][j], a[i+1][j]);
	rep(i, 1, n+1) rep(j, i+2, n+1) {
		set<pair<ll, int> > s;
		rep(k, 1, m+1) {
			if(k-2>=1) {
				int c = k - 2;
				ll t = - pre[i][c][0] - pre[j][c-1][2] + pre[j-1][c][3] - pre[i-1][c][3];
				s.insert(mp(t, c));
			}
			if(!s.empty()) {
				ll res = pre[i][k][0] + pre[j][k-1][2] + pre[j][k][1] - pre[i][k][1];
				ll c = t - res;
				auto tmp = s.lower_bound(mp(c, 0));
				if(tmp != s.end()) {
					upd(res + tmp->fi, i, j, tmp->se, k);
				}
				if(tmp != s.begin()) {
					tmp--;
					upd(res + tmp->fi, i, j, tmp->se, k);
				}
			}
		}
	}
	cout << x11 << " " << y11 << " " << x2 << " " << y2 << endl;
	return 0;
}
原文地址:https://www.cnblogs.com/wuyuanyuan/p/9334346.html