Problem

共有n个格子,有两个硬币在a,b格子上,还有q个操作。
每个操作给你一个编号,要求将一个硬币移到这个编号上。
问你硬币移动的总距离最小值。

Solution

O(n^3):DP[i][a][b]表示i个操作后,1个硬币在a,一个硬币在b的总距离最小值。
O(n^2):DP[i][b]表示i个操作后,一个硬币在x[i],一个硬币在b的总距离最小值。
那么,DP[i][b] = DP[i - 1][b] + abs(a[i] - a[i - 1])
特别地,DP[i][x[i - 1]] = DP[i - 1][j] + abs(a[i] - j)
O(nlogn):用两棵线段树来维护RMQ,一棵维护其DP值减去x[i - 1],一颗维护其DP值加上x[i - 1]。
对于第一个方程,相当于整棵线段树加上abs(a[i] - a[i - 1])
对于第二个方程,j<a[i]时,即为DP[i - 1][j] - j + a[i], j > a[i]时,即为DP[i - 1][j] + j - a[i];

Notice

别忘了要开longlong

Code

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define sqz main
#define ll long long
#define reg register int
#define rep(i, a, b) for (reg i = a; i <= b; i++)
#define per(i, a, b) for (reg i = a; i >= b; i--)
#define travel(i, u) for (reg i = head[u]; i; i = edge[i].next)
const ll INF = 1e15;
const int N = 800000;
const double eps = 1e-6, phi = acos(-1);
ll mod(ll a, ll b) {if (a >= b || a < 0) a %= b; if (a < 0) a += b; return a;}
ll read(){ ll x = 0; int zf = 1; char ch; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;}
void write(ll y) { if (y < 0) putchar('-'), y = -y; if (y > 9) write(y / 10); putchar(y % 10 + '0');}
int x[N / 4 + 5];
struct node
{
	ll val[2][N + 5];
	int left[N + 5], right[N + 5];
	ll tag[N + 5];
	inline void down(int u)
	{
		val[0][u + u] += tag[u], val[0][u + u + 1] += tag[u];
		val[1][u + u] += tag[u], val[1][u + u + 1] += tag[u];
		tag[u + u] += tag[u], tag[u + u + 1] += tag[u];
		tag[u] = 0;
	}
	inline void up(int u)
	{
		val[0][u] = min(val[0][u + u] , val[0][u + u + 1]);
		val[1][u] = min(val[1][u + u] , val[1][u + u + 1]);
	}
	
	void build(int u, int l, int r) 
	{
		left[u] = l, right[u] = r, val[0][u] = val[1][u] = INF, tag[u] = 0;
		if (l == r) return;
		int mid = (l + r) >> 1;
		build(u + u, l, mid);
		build(u + u + 1, mid + 1, r);
	}
	
	void modify(int u, int pos, ll v)
	{
		int l = left[u], r = right[u], mid = (l + r) >> 1;
		if (l == r)
		{
			val[0][u] = min(val[0][u], v - pos), val[1][u] = min(val[1][u], v + pos);
			return;
		}
		down(u);
		if (pos <= mid) modify(u + u, pos, v);
		else modify(u + u + 1, pos, v);
		up(u);
	}
	
	ll query(int t, int u, int x, int y)
	{
		int l = left[u], r = right[u], mid = (l + r) >> 1; ll T = INF;
		if (x <= l && y >= r) return val[t][u];
		down(u);
		if (x <= mid) T = min(T, query(t, u + u, x, y));
		if (y > mid) T = min(T, query(t, u + u + 1, x, y));
		return T;
	}
	
	ll find(int u)
	{
		int l = left[u], r = right[u], mid = (l + r) >> 1;
		if (l == r) return min(val[0][u] + l, val[1][u] - l);
		down(u);
		return min(find(u + u), find(u + u + 1));
	}
}Segment_tree;
int sqz()
{
	int n = read(), q = read(), pos = read(); x[0] = read();
	Segment_tree.build(1, 1, n);
	Segment_tree.modify(1, pos, 0);
	rep(i, 1, q)
	{
		x[i] = read();
		ll now = min(Segment_tree.query(0, 1, 1, x[i]) + x[i], Segment_tree.query(1, 1, x[i], n) - x[i]);
		Segment_tree.tag[1] += abs(x[i] - x[i - 1]), Segment_tree.val[0][1] += abs(x[i] - x[i - 1]), Segment_tree.val[1][1] += abs(x[i] - x[i - 1]);
		Segment_tree.modify(1, x[i - 1], now);
	}
	printf("%lld\n", Segment_tree.find(1));
}
posted on 2017-09-29 20:19  WizardCowboy  阅读(266)  评论(0编辑  收藏  举报