Problem 1 bfs+set
$des$
小G有一个长度为 $n$ 的 01 串 T ,其中只有 $T_S = 1$,其余位置都是 $0$。现在小G可以进行若干
次以下操作:
选择一个长度为 $K$ 的连续子串(K是给定的常数),翻转这个子串。
对于每个 $i,i ∈ [1,n]$,小G想知道最少要进行多少次操作使得 $T_i = 1$. 特别的,有 $m$ 个 “禁
止位置”,你需要保证在操作过程中 $1$ 始终不在任何一个禁止位置上。
$sol$
考虑当前在每个点时可以通过一次翻转转移到哪些点,直接遂遆道即可算出每个点的所需步
数。然而边数会达到 $O(n ^ 2)$ 级别。
可以发现转移到的点一定是一段区间内的奇数或者偶数点,于是一种简单的优化
方法是在 BFS 时开两个 SET 维护当前有哪些奇数点和偶数点还未被 BFS 到,转移时直接
在 SET 上 lower_bound,就不会访问已经 BFS 到过的点了。$O(nlogn)$
$code$
#include <bits/stdc++.h> #define Rep(i, j, k) for (int i = j; i <= k; i++) using namespace std; const int N = 1e5 + ; int n, K, m, S;
int dis[N];
bool ban[N];
set<int> add1, even2; void BFS() {
memset(dis, -, sizeof dis);
queue<int> q;
dis[S] = , q.push(S);
while (!q.empty()) {
int o = q.front(); q.pop();
int L = max(, o - K + ), R = min(n, o + K - );
L = L + (L + K - ) - o, R = R + (R - K + ) - o;
// cout << o << " " << L << " " << R << "\n";
set<int> &p = L & ? add1 : even2;
for (set<int> :: iterator i = p.lower_bound(L); i != p.end() && *i <= R; p.erase(i++))
dis[*i] = dis[o] + , q.push(*i);
}
} int main() {
scanf("%d%d%d%d", &n, &K, &m, &S);
Rep(i, , m) {
int x;
scanf("%d", &x);
ban[x] = true;
}
Rep(i, , n) if (!ban[i] && i != S) i & ? add1.insert(i) : even2.insert(i);
BFS();
Rep(i, , n) printf("%d ", dis[i]);
return ;
}
Problem 1 bfs+set的更多相关文章
- HDU-4471 Yet Another Multiple Problem (BFS+路径还原)
Problem Description There are tons of problems about integer multiples. Despite the fact that the to ...
- UVA-810 A Dicey Problem (BFS)
题目大意:滚骰子游戏,骰子的上面的点数跟方格中的数相同时或格子中的数是-1时能把格子滚过去,找一条从起点滚到起点的路径. 题目大意:简单BFS,状态转移时细心一些即可. 代码如下; # include ...
- Codeforces Round #360 (Div. 2)——C. NP-Hard Problem(BFS染色判二分图)
C. NP-Hard Problem time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Word Ladder Problem (DFS + BFS)
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- 孤岛营救问题 (BFS+状压)
https://loj.ac/problem/6121 BFS + 状压 写过就好想,注意细节debug #include <bits/stdc++.h> #define read rea ...
- leetcode 784. Letter Case Permutation——所有BFS和DFS的题目本质上都可以抽象为tree,这样方便你写代码
Given a string S, we can transform every letter individually to be lowercase or uppercase to create ...
- 【POJ - 3414】Pots(bfs)
Pots 直接上中文 Descriptions: 给你两个容器,分别能装下A升水和B升水,并且可以进行以下操作 FILL(i) 将第i个容器从水龙头里装满(1 ≤ i ≤ 2); DRO ...
- HDU 1495 非常可乐
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=103711#problem/M /*BFS简单题 链接地址: http://acm.hdu ...
- 【LeetCode OJ】Word Ladder I
Problem Link: http://oj.leetcode.com/problems/word-ladder/ Two typical techniques are inspected in t ...
随机推荐
- 深度自适应增量学习(Incremental Learning Through Deep Adaptation)
深度自适应增量学习(Incremental Learning Through Deep Adaptation) 2018-05-25 18:56:00 木呆呆瓶子 阅读数 10564 收藏 更多 分 ...
- 组件系列之RabbitMQ
官网: https://www.rabbitmq.com/getstarted.html 1.四种交换机 TODO... 2.消费者获取消息方式:推 和 拉 拉取:每次拉取一条,循环拉取需要的条数在批 ...
- java之hibernate之多对多双向关联映射
1.比如在权限管理中,角色和权限之间的关系就是多对多的关系,表结构为: 2.类结构 Role.java public class Role implements Serializable{ priva ...
- 哪个参数用来区分请求来自客户(手机)端还是服务器(PC)端?
cookie 和 session 会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session. Cookie通过在客户端记录信息确定用 ...
- python day6 装饰器补充,正则表达式
目录 python day 6 1. 装饰器decorator 2. 正则表达式re 2.1 正则表达式概述 2.2 re模块常用方法 python day 6 2019/10/09 学习资料来自老男 ...
- Java xml出现错误 javax.xml.transform.TransformerException: java.lang.NullPointerException
转自:https://www.jb51.net/article/98644.htm Java xml出现错误 javax.xml.transform.TransformerException: jav ...
- 造成thrift 编译构建项目失败的原因之一:thrift环境变量没设置
由于重装了系统和IDEA工具,打开原项目时使用install 构建使用thrift 协议的项目时失败.下面是说说我遇到的问题 看下图,就是构建时控制台输出的日志,因为是乱码,当时没重视,就先去检查po ...
- JDK的安装(mac)
1.第一步安装brew 教学网址 2.用brew安装jdk. brew update brew cask install java(未翻墙时长很长,大概猴年马月两个小时) 安装完成后就可以执行JAVA ...
- Linux ping:unknown host问题排查
一.检查网卡配置:输入ifconfig可以查看当前网卡配置的IP地址并且查看配置文件中网络的设置: [root@bqh- ~]# ifconfig eth0 Link encap:Ethernet H ...
- 搭建exsi主机6.5版本
1.服务器读取到镜像,进入此图: 2.回车或者F11安装进行下一步 至此exsi主机安装和配置IP完成(80和443端口的开关会影响远程登录) 在浏览器输入IP登录exsi主机 正常安装centos就 ...