P1514 引水入城

题目描述

在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠。该国的行政区划十分特殊,刚好构成一个 \(N\) 行 \(\times M\) 列的矩形,如上图所示,其中每个格子都代表一座城市,每座城市都有一个海拔高度。

为了使居民们都尽可能饮用到清澈的湖水,现在要在某些城市建造水利设施。水利设施有两种,分别为蓄水厂和输水站。蓄水厂的功能是利用水泵将湖泊中的水抽取到所在城市的蓄水池中。

因此,只有与湖泊毗邻的第 1 行的城市可以建造蓄水厂。而输水站的功能则是通过输水管线利用高度落差,将湖水从高处向低处输送。故一座城市能建造输水站的前提,是存在比它海拔更高且拥有公共边的相邻城市,已经建有水利设施。由于第 \(N\) 行的城市靠近沙漠,是该国的干旱区,所以要求其中的每座城市都建有水利设施。那么,这个要求能否满足呢?如果能,请计算最少建造几个蓄水厂;如果不能,求干旱区中不可能建有水利设施的城市数目。

BFS 记忆化搜索(优先队列优化)+ 区间完全覆盖问题(贪心)。

定理:蓄水厂所在城市海拔必然不低于左右城市。即对于可能建造蓄水厂的城市 \(G(1,i)\),满足 \(G(1,i-1) \leq G(1,i) \geq G(1,i+1)\)。

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std; int n, m, G[503][503], v[503][503], cc[503], rat, tot; struct node {
int a, b;
bool operator < (const node& aa) const {return a<aa.a; }
} ra[503];
priority_queue<node> q; int main() {
scanf("%d%d", &n, &m);
for (int i=1; i<=n; ++i) for (int j=1; j<=m; ++j)
scanf("%d", &G[i][j]);
for (int i=1; i<=m; ++i) ra[i].a=1000003;
for (int i=1; i<=m; ++i) if (G[1][i-1]<=G[1][i] && G[1][i]>=G[1][i+1]) {
q.push((node) {1, i}); ++rat;
memset(v, 0, sizeof v);
while (!q.empty()) {
node p=q.top(); q.pop(); int &x=p.a, &y=p.b;
if (v[x][y]) continue; v[x][y]=i;
if (x==n) {
if (!cc[y]) ++tot; cc[y]=1;
ra[rat].a = min(ra[rat].a, y),
ra[rat].b = max(ra[rat].b, y);
}
else if (!v[x+1][y]) if (G[x+1][y]<G[x][y]) q.push((node) {x+1, y});
if (x>1) if (!v[x-1][y]) if (G[x-1][y]<G[x][y]) q.push((node) {x-1, y});
if (y<m) if (!v[x][y+1]) if (G[x][y+1]<G[x][y]) q.push((node) {x, y+1});
if (y>1) if (!v[x][y-1]) if (G[x][y-1]<G[x][y]) q.push((node) {x, y-1});
}
}
if (tot<m) printf("0\n%d\n", m-tot);
else {
printf("1\n");
sort(ra+1, ra+rat+1);
int ans=0, bat=0, bat2=0, i=1;
while (bat<m) {
for (; i<=rat && ra[i].a-1<=bat; ++i)
bat2=max(bat2, ra[i].b);
bat=bat2, ++ans;
}
printf("%d\n", ans);
}
return 0;
}

抽象出来的 区间完全覆盖问题

求给定区间集合的一个子集,使得覆盖全部区间,且该子集的元素个数最小。

贪心:左端点排序,按照能否覆盖前一区间贪心统计。

int maxLen, n;

struct node {
int l, r;
bool operator < (const node& aa) const {return l<aa.l; }
} D[503]; int main() {
scanf("%d", &n);
for (int i=1; i<=n; ++i)
scanf("%d%d", &D[i].l, &D[i].r), maxLen=max(maxLen, D[i].r);
sort(D+1, D+n+1);
int ans=0, end=0, end2=0, i=1;
while (end<maxLen) {
for (; i<=n&& D[i].l-1<=end; ++i)
end2=max(end2, D[i].r);
end=end2, ++ans;
}
printf("%d\n", ans);
return 0;
}

[SCOI2009] Windy数

参考 数位 DP - OI Wiki.

\(f(x, pre, op) = \displaystyle \sum_{|pre-i|\geq 2} f(x-1, i, op \operatorname{and} i=M)\).

数位 DP 的 记忆化搜索实现:

#include <cstdio>
#include <cmath>
#include <cstring> int G[13], gt; long long A, B, f[13][13]; long long search(int x, int pre, int op) {
if (!x) return 1;
if (!op && ~f[x][pre]) return f[x][pre];
int M = op ? G[x] : 9; long long res=0;
for (int i=0; i<=M; ++i) if (abs(pre-i)>=2) {
if (pre==11 && !i) res+=search(x-1, 11, op && i==M);
else res+=search(x-1, i, op && i==M);
}
if (!op) f[x][pre]=res;
return res;
} long long sum(long long x) {
gt=0;
while (x) G[++gt]=x%10, x/=10;
G[gt+1]=-1;
return search(gt, 11, 1);
} int main() {
scanf("%lld%lld", &A, &B);
memset(f, -1, sizeof f);
printf("%lld\n", sum(B)-sum(A-1));
return 0;
}

简便写法 Get:

  • ~a \(\Leftrightarrow\) a!=-1
  • !a \(\Leftrightarrow\) a==0.

6 October的更多相关文章

  1. October 14th 2016 Week 42nd Friday

    Who am I? Coming October 18, 2016! 我是谁?2016.10.18 拭目以待! Don't worry. You will be a wow. Don't worry. ...

  2. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 I. Illegal or Not?

    I. Illegal or Not? time limit per test 1 second memory limit per test 512 megabytes input standard i ...

  3. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 G. Garden Gathering

    Problem G. Garden Gathering Input file: standard input Output file: standard output Time limit: 3 se ...

  4. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 D. Delay Time

    Problem D. Delay Time Input file: standard input Output file: standard output Time limit: 1 second M ...

  5. [Under the hood]---Matt Pietrek October 1996 MSJ

    Matt Pietrek October 1996 MSJ Matt Pietrek is the author of Windows 95 System Programming Secrets (I ...

  6. October 23, 2013 - Fires and smoke in eastern China

    October 23, 2013 - Fires and smoke in eastern China Satellite: Aqua Date Acquired: 10/12/2013 Resolu ...

  7. [luogu4860][Roy&October之取石子II]

    题目链接 思路 这个题和上个题类似,仔细推一下就知道这个题是判断是否是4的倍数 代码 #include<cstdio> #include<iostream> #define f ...

  8. [luogu4018][Roy&October之取石子]

    题目链接 思路 这个题思路挺巧妙的. 情况一: 首先如果这堆石子的数量是1~5,那么肯定是先手赢.因为先手可以直接拿走这些石子.如果石子数量恰好是6,那么肯定是后手赢.因为先手无论怎样拿也无法直接拿走 ...

  9. Codechef October Challenge 2018 游记

    Codechef October Challenge 2018 游记 CHSERVE - Chef and Serves 题目大意: 乒乓球比赛中,双方每累计得两分就会交换一次发球权. 不过,大厨和小 ...

  10. Laravel项目October安装

    October是一个免费,开源,自托管的基于laravel PHP框架CMS平台.在github平台上laravel应用排名第二,可以拿来研究一下.官方介绍:October是一个内容管理系统(CMS) ...

随机推荐

  1. oracle--本地网络配置tnsnames.ora和监听器listener.ora

    文件tnsnames.ora 是给orcl客户端使用 配置本地网络服务:(客户端) 第一种使用暴力方式直接操作: 修改:C:\app\Administrator\product\11.2.0\dbho ...

  2. python基础-7模块,第三方模块安装方法,使用方法。sys.path os sys time datetime hashlib pickle json requests xml

    模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才 ...

  3. go工具链

    1 编辑器 goland 2 GOPATH GOPATH是go的一个环境变量,它以绝对路径提供go的工作目录. go工程的源码存放在${GOPATH}/src目录下,go编译过程中生成的中间文件存放在 ...

  4. [常用类]Number & Math 类(转载)

    下面的表中列出的是 Number & Math 类常用的一些方法: 序号 方法与描述 1 xxxValue() 将 Number 对象转换为xxx数据类型的值并返回. 2 compareTo( ...

  5. 详解 HiveUDF 函数

    更多精彩原创内容请关注:JavaInterview,欢迎 star,支持鼓励以下作者,万分感谢. Hive 函数 相信大家对 Hive 都不陌生,那么大家肯定用过 Hive 里面各种各样的函数.可能大 ...

  6. maven整合S2SH

    1.pom.xml <?xml version="1.0"?> <project xsi:schemaLocation="http://maven.ap ...

  7. Solr的学习使用之(六)获取数据列表-SolrDocumentList

    以下是我项目中获取新闻数据列表的写法,包括数据总量.数据列表,接下来会贴出分片查询(facet)等高级查询 基本的注释都有了: private ListPage<News> queryFr ...

  8. Express 2015 RC for Windows 10 安装

    支持的操作系统 Windows 10 Technical Preview 硬件要求 1.6 GHz 或更快的处理器 1 GB RAM(如果在虚拟机上运行,则为 1.5 GB) 4 GB 可用硬盘空间 ...

  9. 如何官网下载chrome谷歌浏览器离线安装包

    目录 1. 下载步骤 2. 将语言更改为中文 3. 插件 3.1. chrome 网上应用店 3.1.1. google-access-helper 4. 更新失败 1. 下载步骤 注意 需要梯子才能 ...

  10. BZOJ-1907 树的路径覆盖 贪心

    题意:给一个n个点的树,求树的最小路径覆盖.(这个最小路径覆盖不能有重点) 解法:往图论方向想很久,想得太复杂了,其实直接贪心.这个大佬题解写得很好: https://blog.csdn.net/bl ...