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 ...
随机推荐
- PKUSC2019题解
$D1T1$:$n$个村庄,第$i$个村庄的人要去第$p_i$个村庄(保证$p_i$为排列),每次可以将相邻两个村庄的人位置交换直到所有人都到达目的地.再给定一个长为$n-1$的排列$a$,表示第$i ...
- The three day 给你一个有效的 IPv4 地址 address,返回这个 IP 地址的无效化版本
""" 给你一个有效的 IPv4 地址 address,返回这个 IP 地址的无效化版本. 所谓无效化 IP 地址,其实就是用 "[.]" 代替了每个 ...
- JAVA MyBybatis分页
java: import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; impo ...
- springboot打成jar包后无法解压
springboot打成jar包后无法解压 Springboot打出来的jar,用压缩工具解压报错.Why? 先说解决办法. 1.解决办法 executable属性导致的,属性改成false后重新打包 ...
- c#中冒泡排序算法描述
int temp = 0; int b = 0; int[] arr = { 23, 44, 66, 76, 98, 11, 3, 99, 7 };# region该段与排序无关Console.Wri ...
- ..\USER\stm32f10x.h(428): error: #67: expected a "}" ADC1_2_IRQn = 18, /*!
MDK软件编译,出现如下错误: ..\USER\stm32f10x.h(428): error: #67: expected a "}" ADC1_2_IRQn = 18, /*! ...
- VSCode - Beautify插件配置
注: 本文摘自 黑火巨雷 - 简书 1. 在工作目录下建立.jsbeautifyrc文件 官方文档 { "brace_style": "none,preserve-inl ...
- Android笔记(六十六) android中的动画——XML文件定义属性动画
除了直接在java代码中定义动画之外,还可以使用xml文件定义动画,以便重用. 如果想要使用XML来编写动画,首先要在res目录下面新建一个animator文件夹,所有属性动画的XML文件都应该存放在 ...
- spice在桌面虚拟化中的应用系列之二(Linux平台spice客户端的编译安装,支持USB映射)
1.系统环境 1.1 测试环境 centos6.4最小化安装(centos6.x桌面版也适用) 使用yum源为163源加EPEL源 1.2 spice客户端介绍 spice作为远程连接工具,可以支持远 ...
- HDFS重启集群导致数据损坏,使用fsck命令修复过程
HDFS重启集群导致数据损坏,使用fsck命令修复过程 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 我们先看一组输出 [root@flume112 ~]# hdfs fsck / ...