P1514 引水入城
概述
首先,这是一道好题,这道题既考查了图论的dfs知识,又考察了区间贪心问题中很典型的区间覆盖问题,着实是一道好题。
大概思路说明
我们观察到,只有第一行可以放水库,而第一行在哪里放水库的结果就是直接导致最后一行某些点被覆盖。所以我们只需要找到第一行水库与最后一行被覆盖的关系即可完成决策,中间的行没有意义。我们对于第一行的每一个数进行dfs,可以预处理出所有的区间。然后问题就转化成了用一些区间覆盖一条线段的问题。直接求解即可。
参考代码
#include <bits/stdc++.h>
using namespace std;
const int maxn = 505;
int n, m, high[maxn][maxn], vis[maxn][maxn];
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
void dfs(int x, int y) {
vis[x][y] = 1;
for(int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if(nx >= 1 && nx <= m && ny >= 1 && ny <= n &&!vis[nx][ny] && high[nx][ny] < high[x][y]) {
dfs(nx, ny);
}
}
}
struct line
{
int left, right, num;
bool operator < (const line b) const {
if(this->left != b.left) return this->left < b.left;
int la = this->right - this->left;
int lb = b.right - b.left;
return la > lb;
}
}ls[maxn];
int main() {
// freopen("input.in", "r", stdin);
cin >> n >> m;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++)
cin >> high[j][i];
}
int dfn = 0;
for(int i = 1; i <= m; i++) {
memset(vis, 0, sizeof(vis));
dfs(i, 1);
int l = -1, r = -1;
for(int j = 1; j <= m; j++) {
if(vis[j][n]) {l = j; break;}
}
int j;
if(l!=-1)for(j = l; j <= m; j++) {
if(!vis[j][n]) {r = j - 1; break;}
}
if(j == m + 1 && l != -1) r = m;
if(l != -1 && r != -1) ls[dfn++] = {l, r, i};
}
int s = 1;
set<int> ans;
set<int> no;
while(s <= m) {
for(int i = 0; i < dfn; i++) {
if(ls[i].left <s ) ls[i].left = s;
if(ls[i].left > ls[i].right) ls[i] = {maxn, maxn, maxn};
}
sort(ls, ls+dfn);
if(ls[0].left == maxn) { //没有可用的区间
for(int i = s; i <= m; i++) no.insert(i);
break;
}
if(ls[0].left!=s) {
for(int i = s; i < ls[0].left; i++) no.insert(i);
}
ans.insert(ls[0].num);
s = ls[0].right+1;
}
if(no.empty()) {
cout << 1 << endl << ans.size();
}
else {
cout << 0 << endl << no.size();
}
return 0;
}
P1514 引水入城的更多相关文章
- 洛谷P1514 引水入城
洛谷P1514 引水入城 原题链接 一道好题...细节真多 第一次提交90分,然后就GG了,不知从何改起 其实比较简单吧... 首先,一个点的水流向最后一排,一定可以形成一个区间. 不行的话肯定GG ...
- 洛谷 P1514 引水入城 解题报告
P1514 引水入城 题目描述 在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个 NN 行 \times M×M 列的矩形,如上图所示,其中每个格 ...
- [luogu]P1514 引水入城[搜索][记忆化][DP]
[luogu]P1514 引水入城 引水入城 题目描述在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个N 行M 列的矩形 ,如下图所示,其中每个格 ...
- CODEVS 1066/洛谷 P1514引水入城
1066 引水入城 2010年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 在一个遥远的国 ...
- 洛谷P1514 引水入城 [搜索,区间DP]
题目传送门 引水入城 题目描述 在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个 N 行×M 列的矩形,如上图所示,其中每个格子都代表一座城市,每 ...
- 【luogu P1514 引水入城】 题解
题目链接:https://www.luogu.org/problemnew/show/P1514 // luogu-judger-enable-o2 #include <iostream> ...
- 洛谷P1514 引水入城——dfs
题目:https://www.luogu.org/problemnew/show/P1514 搜索+DP: 自己想出来的方法第一次80分好高兴! 再改了改就A了,狂喜乱舞: 也就是 dfs,仔细一想第 ...
- [NOIP2010] 提高组 洛谷P1514 引水入城
题目描述 在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个N 行M 列的矩形,如上图所示,其中每个格子都代表一座城市,每座城市都有一个海拔高度. ...
- P1514 引水入城 DFS
题目描述 在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个NN 行\times M×M 列的矩形,如上图所示,其中每个格子都代表一座城市,每座城市 ...
随机推荐
- Shell 编程基础之括号的作用
一.小括号() 单小括号 命令组.括号中的命令将会新开一个子shell顺序执行,所以括号中的变量不能够被脚本余下的部分使用.括号中多个命令之间用分号隔开,最后一个命令可以没有分号,各命令和括号之间不必 ...
- 2014-2015 ACM-ICPC, NEERC, Moscow Subregional Contest F. Friends
F. Friends time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...
- python 代码片段19
#coding=utf-8 # 函数 def foo(x): print x foo(123) # import httplib def check_web_server(host,port,path ...
- vim g s 对比
vim g s http://blog.chinaunix.net/uid-10597892-id-3311441.html
- HTTP请求头详解
http://blog.csdn.net/kfanning/article/details/6062118 HTTP由两部分组成:请求和响应.当你在Web浏览器中输入一个URL时,浏览 器将根据你的要 ...
- Android AIDL 进行进程间通讯(IPC)
编写AIDL文件时,需要注意: 1.接口名和aidl文件名相同. 2.接口和方法前不用加访问权限修饰符 (public.private.protected等,也不能用final.static). 3. ...
- jQuery自定义滚动条样式插件mCustomScrollbar
如果你构建一个很有特色和创意的网页,那么肯定希望定义网页中的滚动条样式,这方面的 jQuery 插件比较不错的,有两个:jScrollPane 和 mCustomScrollbar. 关于 jScro ...
- HDU 4778 Gems Fight!(DP)
题目链接 当我放弃的时候过了.sb啊,卡常数!!! 换了好几个姿势,本来没写预处理,预处理+俩剪枝,尼玛就过了.. #include <stdio.h> #include <stri ...
- POJ 2955 Brackets(区间DP)
题目链接 #include <iostream> #include <cstdio> #include <cstring> #include <vector& ...
- BZOJ4184: shallot
Description 小苗去市场上买了一捆小葱苗,她突然一时兴起,于是她在每颗小葱苗上写上一个数字,然后把小葱叫过来玩游戏. 每个时刻她会给小葱一颗小葱苗或者是从小葱手里拿走一颗小葱苗,并且 让小葱 ...