$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的更多相关文章

  1. HDU-4471 Yet Another Multiple Problem (BFS+路径还原)

    Problem Description There are tons of problems about integer multiples. Despite the fact that the to ...

  2. UVA-810 A Dicey Problem (BFS)

    题目大意:滚骰子游戏,骰子的上面的点数跟方格中的数相同时或格子中的数是-1时能把格子滚过去,找一条从起点滚到起点的路径. 题目大意:简单BFS,状态转移时细心一些即可. 代码如下; # include ...

  3. 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 ...

  4. Word Ladder Problem (DFS + BFS)

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  5. 孤岛营救问题 (BFS+状压)

    https://loj.ac/problem/6121 BFS + 状压 写过就好想,注意细节debug #include <bits/stdc++.h> #define read rea ...

  6. 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 ...

  7. 【POJ - 3414】Pots(bfs)

    Pots 直接上中文 Descriptions: 给你两个容器,分别能装下A升水和B升水,并且可以进行以下操作 FILL(i)        将第i个容器从水龙头里装满(1 ≤ i ≤ 2); DRO ...

  8. HDU 1495 非常可乐

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=103711#problem/M /*BFS简单题 链接地址: http://acm.hdu ...

  9. 【LeetCode OJ】Word Ladder I

    Problem Link: http://oj.leetcode.com/problems/word-ladder/ Two typical techniques are inspected in t ...

随机推荐

  1. Jekyll自动检测代码更新

    Jekyll自动检测代码更新 jekyll是一个静态博客生成软件, 我们把代码放在一个仓库里, 只要远程代码更新, 我们就从把它拉到自己的服务器, 然后重新启动jekyll. cd /root/blo ...

  2. 深度学习 Bottleneck layer / Bottleneck feature

    最近在学习deeplearning的时候接触到了bottle-neck layer,好奇它的作用于是便扒了一些论文(论文链接放在文末吧),系统的了解一下bottle-neck feature究竟有什么 ...

  3. Docker安装带中文全文搜索插件zhparser的Postgresql数据库

    上一篇讲了在已经安装了PG数据库的情况下,安装全文搜索插件zhparser遇到的问题.在一个全新的环境中安装带有全文搜索插件zhparser的PG数据库,可以使用已经做好的Docker镜像,在安装的过 ...

  4. CachedThreadPool

    /** * <html> * <body> * <P> Copyright 1994 JsonInternational</p> * <p> ...

  5. Python接口自动化基础---token鉴权

    有些登录使用cookie,有些登录需要token验证,token传参一般有两种形式,一种是在请求头中,一种是使用URL传参 这里举例说明一下请求头中的token方式: #登录 param1={'use ...

  6. .net 调用存储过程

    语言:C# 一.调用带输入参数的存储过程 首先自然是在查询分析器里创建一个存储过程喽~~   如下所示: create proc proc_1 @uid int, @pwd varchar(255) ...

  7. 【方法】list<?> 两个list集合 查找不同元素,求差值

    //方法1 //自己声明list//思路,从list1中删除list2中相同的元素//使用循环遍历对比的方式删除//list1包含list2,list1多与list2//结束得出list1为不相同元素 ...

  8. linux 非root用户安装nginx

    第一步:首先下载依赖包 下载地址 pcre(www.pcre.org),zlib(www.zlib.org),openssl(www.openssl.org) 第二步:上传那个nginx的安装包 下载 ...

  9. Spring WebSocket中403错误解决

    最近测试了一下spring的websocket,遇到了一个比较恶心的问题,在这记录一下. 问题源自之前开发的一个h5项目,这个项目在80端口下一直放着,就顺便在里面随便加了几行代码测试websocke ...

  10. Spring-05 -AOP [面向切面编程] -Schema-based 实现aop的步骤

    一.AOP [知识点详解] AOP:中文名称面向切面编程 英文名称:(Aspect Oriented Programming) 正常程序执行流程都是纵向执行流程 3.1 又叫面向切面编程,在原有纵向执 ...