[UOJ#132][BZOJ4200][luogu_P2304][NOI2015]小园丁与老司机

[UOJ#132][BZOJ4200][luogu_P2304][NOI2015]小园丁与老司机

试题描述

小园丁 Mr. S 负责看管一片田野,田野可以看作一个二维平面。田野上有 (n) 棵许愿树,编号 (1,2,3, cdots , n),每棵树可以看作平面上的一个点,其中第 (i) 棵树 ((1 le i le n)) 位于坐标 ((x_i, y_i))。任意两棵树的坐标均不相同。

老司机 Mr. P 从原点 ((0,0)) 驾车出发,进行若干轮行动。每一轮,Mr. P 首先选择任意一个满足以下条件的方向:

1.为左、右、上、左上 (45°)、右上 (45°) 五个方向之一。

2.沿此方向前进可以到达一棵他尚未许愿过的树。

完成选择后,Mr. P 沿该方向直线前进,必须到达该方向上距离最近的尚未 许愿的树,在树下许愿并继续下一轮行动。如果没有满足条件的方向可供选择, 则停止行动。他会采取最优策略,在尽可能多的树下许愿。若最优策略不唯一, 可以选择任意一种。

不幸的是,小园丁 Mr. S 发现由于田野土质松软,老司机 Mr. P 的小汽车在 每轮行进过程中,都会在田野上留下一条车辙印,一条车辙印可看作以两棵树(或 原点和一棵树)为端点的一条线段。

在 Mr.P 之后,还有很多许愿者计划驾车来田野许愿,这些许愿者都会像 Mr. P 一样任选一种最优策略行动。Mr.S 认为非左右方向(即上、左上 (45°)、右 上 (45°) 三个方向)的车辙印很不美观,为了维护田野的形象,他打算租用一些轧路机,在这群许愿者到来之前夯实所有“可能留下非左右方向车辙印”的地面。“可能留下非左右方向车辙印”的地面应当是田野上的若干条线段,其中每条线 段都包含在某一种最优策略的行进路线中。每台轧路机都采取满足以下三个条件的工作模式:

1.从原点或任意一棵树出发。

2.只能向上、左上 (45°)、右上 (45°) 三个方向之一移动,并且只能在树下改变方向或停止。

3.只能经过“可能留下非左右方向车辙印”的地面,但是同一块地面可以 被多台轧路机经过。

现在 Mr. P 和 Mr. S 分别向你提出了一个问题:

1.请给 Mr.P 指出任意一条最优路线。

2.请告诉 Mr.S 最少需要租用多少台轧路机。

输入

(1) 行包含 (1) 个正整数 (n),表示许愿树的数量。

接下来 (n) 行,第 (i + 1) 行包含 (2) 个整数 (x_i , yi),中间用单个空格隔开,表示第 (i) 棵许愿树的坐标。

输出

包括 (3) 行。

(1) 行输出 (1) 个整数 (m),表示 Mr. P 最多能在多少棵树下许愿。

输出文件的第 (2) 行输出 (m) 个整数,相邻整数之间用单个空格隔开,表示 Mr.P 应该依次在哪些树下许愿。

输出文件的第 (3) 行输出 (1) 个整数,表示 Mr. S 最少需要租用多少台轧路机。

输入示例1

6
-1 1
1 1
-2 2
0 8
0 9
0 10

输出示例1

3
2 1 3
3

输入示例2

4
0 1
-2 1
2 1
3 2

输出示例2

4
1 2 3 4
2

数据规模及约定

去 UOJ 看吧。。。

题解

按照 (x + y)(x - y)(x) 分别排一遍序连边,然后由于每一行不会超过 (1000) 个,每个点出去的转移不会超过 (O(1000)) 种,直接 dp 就好了。

输出方案就是 dp 的时候记一个“上一步最优位置”然后逆着找回去。

第三问被坑了。。。他问的是每条边至少被覆盖一次,我理解成恰好覆盖一次了。。。于是你再搜一遍把所有可能出现在最优解中的边加进去,然后每条这样的边限制流量下界为 (1),上界无穷,跑最小流。当然最小流需要规定源、汇,不难想到对于每个出度比入度多的点,从源点向它连边;对于入度比出度多的点,从它向汇点连边;然后一个可行流就是把它流满,(流满后)这个时候我们从 T 向 S 跑一个最大流,用之前的结果减掉这次的最大流就是答案了。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <vector>
using namespace std;
#define rep(i, s, t) for(int i = (s); i <= (t); i++)
#define dwn(i, s, t) for(int i = (s); i >= (t); i--)

int read() {
	int x = 0, f = 1; char c = getchar();
	while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
	while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
	return x * f;
}

#define maxn 50010
#define maxm 150010
#define oo 2147483647

struct Vec {
	int x, y, id;
	Vec() {}
	Vec(int _1, int _2, int _3): x(_1), y(_2), id(_3) {}
} ps[maxn];
bool cmpxpy(const Vec& a, const Vec& b) {
	return a.x + a.y != b.x + b.y ? a.x + a.y < b.x + b.y : a.y < b.y;
}
bool cmpx_y(const Vec& a, const Vec& b) {
	return a.x - a.y != b.x - b.y ? a.x - a.y < b.x - b.y : a.y < b.y;
}
bool cmpx(const Vec& a, const Vec& b) {
	return a.x != b.x ? a.x < b.x : a.y < b.y;
}
bool cmpy(const Vec& a, const Vec& b) {
	return a.y != b.y ? a.y < b.y : a.x < b.x;
}

struct Edge {
	int a, b;
	Edge() {}
	Edge(int _, int __): a(_), b(__) {}
} es[maxm];
int m, head[maxn], nxt[maxm];
void AddEdge(Vec A, Vec B) {
	if(A.y > B.y) swap(A, B);
	int a = A.id, b = B.id;
//	printf("%d -> %d
", a, b);
	es[++m] = Edge(a, b); nxt[m] = head[a]; head[a] = m;
	return ;
}

vector <Vec> line[maxn];
int cnty, bel[maxn], lid[maxn], f[maxn], fa[maxn], faq[maxn], path[maxn], cntp;

int CanReach[maxn];
bool CanEdge[maxm];
int get(int i, int ans) {
	if(CanReach[i] >= 0) return CanReach[i];
	int at = bel[i], pos = lid[i];
	if(f[i] + (int)line[at].size() - 1 == ans) return CanReach[i] = 1;
	CanReach[i] = 0;
	if(!f[i]) return CanReach[i];
	rep(j, 0, pos - 1) {
		int u = line[at][j].id;
		for(int e = head[u]; e; e = nxt[e])
			if(f[es[e].b] == f[i] + (int)line[at].size() - j && get(es[e].b, ans))
				CanReach[i] = CanEdge[e] = 1;
	}
	int u = i;
	for(int e = head[u]; e; e = nxt[e]) if(f[es[e].b] == f[i] + 1 && get(es[e].b, ans))
		CanReach[i] = CanEdge[e] = 1;
	rep(j, pos + 1, (int)line[at].size() - 1) {
		int u = line[at][j].id;
		for(int e = head[u]; e; e = nxt[e])
			if(f[es[e].b] == f[i] + j + 1 && get(es[e].b, ans))
				CanReach[i] = CanEdge[e] = 1;
	}
	return CanReach[i];
}

int deg[maxn], mm, hd[maxn], Nxt[maxm], To[maxm];

struct Dinic {
	int n, m, s, t, head[maxn], nxt[maxm<<1];
	struct Edge {
		int from, to, flow;
		Edge() {}
		Edge(int _1, int _2, int _3): from(_1), to(_2), flow(_3) {}
	} es[maxm<<1];
	int Q[maxn], hd, tl, vis[maxn];
	int cur[maxn];
	
	void init() {
		m = 0; memset(head, -1, sizeof(head));
		return ;
	}
	void setn(int _) {
		n = _;
		return ;
	}
	
	void AddEdge(int a, int b, int c) {
		es[m] = Edge(a, b, c); nxt[m] = head[a]; head[a] = m++;
		es[m] = Edge(b, a, 0); nxt[m] = head[b]; head[b] = m++;
		return ;
	}
	
	bool BFS() {
		memset(vis, 0, sizeof(vis));
		vis[t] = 1;
		hd = tl = 0; Q[++tl] = t;
		while(hd < tl) {
			int u = Q[++hd];
			for(int i = head[u]; i != -1; i = nxt[i]) {
				Edge& e = es[i^1];
				if(!vis[e.from] && e.flow) {
					vis[e.from] = vis[u] + 1;
					Q[++tl] = e.from;
				}
			}
		}
		return vis[s] > 0;
	}
	
	int DFS(int u, int a) {
		if(u == t || !a) return a;
		int flow = 0, f;
		for(int& i = cur[u]; i != -1; i = nxt[i]) {
			Edge& e = es[i];
			if(vis[e.to] == vis[u] - 1 && (f = DFS(e.to, min(a, e.flow)))) {
				flow += f; a -= f;
				e.flow -= f; es[i^1].flow += f;
				if(!a) return flow;
			}
		}
		return flow;
	}
	
	int MaxFlow(int _s, int _t) {
		s = _s; t = _t;
		int flow = 0;
		while(BFS()) {
			rep(i, 1, n) cur[i] = head[i];
			flow += DFS(s, oo);
		}
		return flow;
	}
} sol;

int num[20], cntn;
void writeint(int x) {
	if(!x) putchar('0');
	cntn = 0;
	while(x) num[++cntn] = x % 10, x /= 10;
	dwn(i, cntn, 1) putchar(num[i] + '0');
	return ;
}

int main() {
	int n = read();
	bool has_00 = 0;
	rep(i, 1, n) {
		int x = read(), y = read();
		ps[i] = Vec(x, y, i);
		if(!x && !y) has_00 = 1;
	}
	if(!has_00) ps[n+1] = Vec(0, 0, n + 1), n++;
	sort(ps + 1, ps + n + 1, cmpxpy);
	rep(i, 2, n) if(ps[i-1].x + ps[i-1].y == ps[i].x + ps[i].y) AddEdge(ps[i-1], ps[i]);
	sort(ps + 1, ps + n + 1, cmpx_y);
	rep(i, 2, n) if(ps[i-1].x - ps[i-1].y == ps[i].x - ps[i].y) AddEdge(ps[i-1], ps[i]);
	sort(ps + 1, ps + n + 1, cmpx);
	rep(i, 2, n) if(ps[i-1].x == ps[i].x) AddEdge(ps[i-1], ps[i]);
	sort(ps + 1, ps + n + 1, cmpy);
	line[cnty = 1].push_back(ps[1]); bel[ps[1].id] = 1; lid[ps[1].id] = 0;
	rep(i, 2, n) {
		if(ps[i].y != ps[i-1].y) cnty++;
		line[cnty].push_back(ps[i]);
		bel[ps[i].id] = cnty; lid[ps[i].id] = (int)line[cnty].size() - 1;
	}
	
	int i = 1, ans = 0, ansp = 0;
	while(ps[i].x || ps[i].y) i++;
	f[ps[i].id] = 1;
	for(; i <= n; i++) if(f[ps[i].id]) {
		int at = bel[ps[i].id], pos = lid[ps[i].id];
		rep(j, 0, pos - 1) {
			int u = line[at][j].id;
			for(int e = head[u]; e; e = nxt[e]) if(f[es[e].b] < f[ps[i].id] + (int)line[at].size() - j)
				f[es[e].b] = f[ps[i].id] + (int)line[at].size() - j, fa[es[e].b] = ps[i].id, faq[es[e].b] = u;
		}
		int u = ps[i].id;
		for(int e = head[u]; e; e = nxt[e]) if(f[es[e].b] < f[ps[i].id] + 1)
			f[es[e].b] = f[ps[i].id] + 1, fa[es[e].b] = ps[i].id, faq[es[e].b] = u;
		rep(j, pos + 1, (int)line[at].size() - 1) {
			int u = line[at][j].id;
			for(int e = head[u]; e; e = nxt[e]) if(f[es[e].b] < f[ps[i].id] + j + 1)
				f[es[e].b] = f[ps[i].id] + j + 1, fa[es[e].b] = ps[i].id, faq[es[e].b] = u;
		}
		if(ans < f[ps[i].id] + (int)line[at].size() - 1) ans = f[ps[i].id] + (int)line[at].size() - 1, ansp = ps[i].id;
	}
	
	printf("%d
", ans - !has_00);
	int u = ansp, ter;
	while(u) {
		int at = bel[u], pos = lid[u];
		if(u == ansp) {
			if(pos == (int)line[at].size() - 1)
				rep(i, 0, (int)line[at].size() - 1) path[++cntp] = line[at][i].id;
			else {
				dwn(i, (int)line[at].size() - 1, pos + 1) path[++cntp] = line[at][i].id;
				rep(i, 0, pos) path[++cntp] = line[at][i].id;
			}
		}
		else {
			if(ter == pos) path[++cntp] = u;
			else if(ter > pos) {
				dwn(i, ter, pos + 1) path[++cntp] = line[at][i].id;
				rep(i, 0, pos) path[++cntp] = line[at][i].id;
			}
			else {
				rep(i, ter, pos - 1) path[++cntp] = line[at][i].id;
				dwn(i, (int)line[at].size() - 1, pos) path[++cntp] = line[at][i].id;
			}
		}
		ter = lid[faq[u]];
		u = fa[u];
	}
	dwn(i, has_00 ? cntp : cntp - 1, 1) writeint(path[i]), putchar(i > 1 ? ' ' : '
');
	
	memset(CanReach, -1, sizeof(CanReach));
	i = 1; while(ps[i].x || ps[i].y) i++;
	get(ps[i].id, ans);
	sol.init();
	rep(i, 1, m) if(CanEdge[i]) deg[es[i].a]++, deg[es[i].b]--, sol.AddEdge(es[i].a, es[i].b, oo);
	int S = n + 1, T = S + 1, sum = 0;
	sol.setn(T);
	rep(i, 1, n) {
		if(deg[i] > 0) sol.AddEdge(i, S, deg[i]), sum += deg[i];
		if(deg[i] < 0) sol.AddEdge(T, i, -deg[i]);
	}
	printf("%d
", sum - sol.MaxFlow(T, S));
	
	return 0;
}

好难写。。。辣鸡洛谷还卡常数,测评机太慢了!太慢了!太慢了!(强行用 #pragma 开 O2 才过)

原文地址:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/7955056.html