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 ...
随机推荐
- java 简易日历表
在页面上输出1900年以后任意一年的简易日历表 package text3; import java.util.Scanner; public class MyCalendar { public st ...
- Golang --多个变量同时赋值
编程最简单的算法之一,莫过于变量交换.交换变量的常见算法需要一个中间变量进行变量的临时保存.用传统方法编写变量交换代码如下: var a int = 100 var b int = 200 var t ...
- go ---fmt
//main package main import ( "fmt" "os" ) type Data struct { } func (self Data) ...
- IdentityServer4实现OAuth2.0四种模式之授权码模式
接上一篇:IdentityServer4实现OAuth2.0四种模式之隐藏模式 授权码模式隐藏码模式最大不同是授权码模式不直接返回token,而是先返回一个授权码,然后再根据这个授权码去请求token ...
- Django:ORM介绍
1.ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术. 简单的说,ORM是通过使用描述 ...
- vue动态加载图片
如果是直接动态获取完整的图片地址可以使用以下方法 <template> <img :src="url"> </template> <scr ...
- DML 操作表中数据
DML 是对于表中的记录进行增删改操作 一.添加数据 语法格式: insert into 表名[字段名] values[字段值] 表名:表示往那张表中添加数据 (字段名1,字段名2, ...
- 解决Vivado XSDK在Ubuntu系统上自带UART Terminal Crash问题
在Ubuntu 18.04 LTS系统上使用某些版本的Vivado XSDK的Eclipse IDE中自带的串口Terminal会有Crash的问题.Xilinx的XSDK的Terminal插件是用的 ...
- a标签中target属性为“_blank”时存在安全问题
今天看到一个比较有意思的洞,虽然不够严重,但是却普遍存在各大src中 熟悉js的朋友都应该知道当我们在调用window下的open方法创建一个新窗口的同时,我们可以获得一个创建窗口的opener句柄, ...
- Android笔记(五十二) 侧滑菜单SlidingMenu
SlidingMenu是一个优秀的开源项目,可以实现侧滑菜单,简单介绍一下这SlidingMenu的使用: 常用属性和方法: setTouchModeAbove(int i )是否可以通过滑动手势打开 ...