[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\n", 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\n", 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 ? ' ' : '\n'); 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\n", sum - sol.MaxFlow(T, S)); return 0;
}

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

[UOJ#132][BZOJ4200][luogu_P2304][NOI2015]小园丁与老司机的更多相关文章

  1. 【BZOJ4200】[Noi2015]小园丁与老司机 DP+最小流

    [BZOJ2839][Noi2015]小园丁与老司机 Description 小园丁 Mr. S 负责看管一片田野,田野可以看作一个二维平面.田野上有 nn 棵许愿树,编号 1,2,3,…,n1,2, ...

  2. 【bzoj4200】[Noi2015]小园丁与老司机 STL-map+dp+有上下界最小流

    题目描述 小园丁 Mr. S 负责看管一片田野,田野可以看作一个二维平面.田野上有 nn 棵许愿树,编号 1,2,3,…,n1,2,3,…,n,每棵树可以看作平面上的一个点,其中第 ii 棵树 (1≤ ...

  3. [BZOJ4200][Noi2015]小园丁与老司机

    4200: [Noi2015]小园丁与老司机 Time Limit: 20 Sec  Memory Limit: 512 MBSec  Special JudgeSubmit: 106  Solved ...

  4. luogu P2304 [NOI2015]小园丁与老司机 dp 上下界网络流

    LINK:小园丁与老司机 苦心人 天不负 卧薪尝胆 三千越甲可吞吴 AC的刹那 真的是泪目啊 很久以前就写了 当时记得特别清楚 写到肚子疼.. 调到胳膊疼.. ex到根不不想看的程度. 当时wa了 一 ...

  5. BZOJ4200 & 洛谷2304 & UOJ132:[NOI2015]小园丁与老司机——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=4200 https://www.luogu.org/problemnew/show/P2304 ht ...

  6. uoj132/BZOJ4200/洛谷P2304 [Noi2015]小园丁与老司机 【dp + 带上下界网络流】

    题目链接 uoj132 题解 真是一道大码题,,,肝了一个上午 老司机的部分是一个\(dp\),观察点是按\(y\)分层的,而且按每层点的上限来看可以使用\(O(nd)\)的\(dp\),其中\(d\ ...

  7. [BZOJ]4200: [Noi2015]小园丁与老司机

    Time Limit: 20 Sec  Memory Limit: 512 MBSec  Special Judge Description 小园丁 Mr. S 负责看管一片田野,田野可以看作一个二维 ...

  8. [Noi2015]小园丁和老司机

    来自FallDream的博客,未经允许,请勿转载,谢谢. 小园丁 Mr. S 负责看管一片田野,田野可以看作一个二维平面.田野上有n棵许愿树,编号1,2,3,…,n,每棵树可以看作平面上的一个点,其中 ...

  9. 【洛谷2304_LOJ2134】[NOI2015]小园丁与老司机(动态规划_网络流)

    题目: 洛谷 2304 LOJ 2134 (LOJ 上每个测试点有部分分) 写了快一天 -- 好菜啊 分析: 毒瘤二合一题 -- 注意本题(及本文)使用 \(x\) 向右,\(y\) 向上的「数学坐标 ...

随机推荐

  1. 2017.12.4 JavaWeb中EL表达式的运用

    <%@ page contentType="text/html; charset=gb2312"%> <html> <head> <tit ...

  2. 【luogu P1783 海滩防御】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1783 先把题目改造一下:题目所求是要一条能从0列到n列的路径,使其路径上的最大边长一半最小. 为什么是一半呢 ...

  3. CUDA常见问题与解答

    源 1.在SDK自带的例子程序中,发现SRC文件珜下有.cpp文件和.cu文件.这两种文件的关系和各自的作用是什么呀? 答:SDK自带例子中的.cpp文件主要是一些CPU端处理,或者是使用CPU计算对 ...

  4. Jquery-EasyUI combobox下拉框使用

    制作一个json文件: <input data-options="url:'${pageContext.request.contextPath }/json/combobox_data ...

  5. react 信用卡格式检验

    前言: 技术栈主要基于react + ant-design 描述: 填写信用卡卡号时,会自动四位空格,并格式校验判断卡种  ,这里我们业务只涉及到四种卡. 代码解析 // ant 组件自引,这里我只讲 ...

  6. wepy框架构建小程序(1)

    wepy框架构建小程序(1) 基本操作: # 安装脚手架工具 npm install wepy-cli -g # 创建一个新的项目 npm init standard myproject # 进入新项 ...

  7. [BZOJ] 3875: [Ahoi2014&Jsoi2014]骑士游戏

    设\(f[x]\)为彻底杀死\(x\)号怪兽的代价 有转移方程 \[ f[x]=min\{k[x],s[x]+\sum f[v]\} \] 其中\(v\)是\(x\)通过普通攻击分裂出的小怪兽 这个东 ...

  8. http 工作模式与模块

    目录 http 工作模式与模块 http 服务器应用 MPM工作模式 prefork worker event 进程角色 httpd功能特性 http 安装 centos6配置目录 http 2.2 ...

  9. Mysql主从数据同步cheksum问题

    做主从同步时出现问题,show slave status显示错误: Last_IO_Error: Got fatal error from master when reading data from ...

  10. js字符串去掉所有空格

    字符串去掉所有空格 "abc 123 def".replace(/\s/g, "") 字符串去掉左右两端空格 " abc 123 def " ...