$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. grafana部署安装

    部署grafana 在prometheus& grafana server节点部署grafana服务. 1. 下载&安装 # 下载 [root@prometheus ~]# cd /u ...

  2. C# vb .net实现色调调整特效滤镜

    在.net中,如何简单快捷地实现Photoshop滤镜组中的色调调整呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 第一步 ...

  3. ②将SVN迁移到GitLab-多分支多标签迁移

    之前我们介绍了<①将SVN迁移到GitLab-单分支迁移>,文中研究了svn迁移到git单分支的操作过程,本文针对实际开发过程中svn使用到的trunk.branches.tags情况进行 ...

  4. GIt三剑客

    一. Github 什么是Github? github是一个基于git的代码托管平台,付费用户可以建私人仓库,我们一般的免费用户只能使用公共仓库,也就是代码要公开. Github 由Chris Wan ...

  5. C++遍历磁盘驱动器

    #include <stdio.h> #include <windows.h> typedef struct tagDRIVER { // (1)磁盘盘符 wchar_t di ...

  6. 实战FFmpeg--iOS平台使用FFmpeg将视频文件转换为YUV文件

    做播放器的开发这里面涉及的东西太多,我只能一步步往前走,慢慢深入.播放器播放视频采用的是渲染yuv文件.首先,要知道yuv文件是怎么转换得来的,其次,要知道怎么把视频文件保存为yuv文件.雷神的文章1 ...

  7. vue routes路由

    mode: 'history',去掉浏览器上url前的#号

  8. SQL SERVER-CROSS APPLY

    CROSS APPLY和 OUTER APPLY 区别详解 SQL Server 2005 新增 cross apply 和 outer apply 联接语句,增加这两个东东有啥作用呢? 我们知道有个 ...

  9. centos7 编译安装redis

    1.下载redis源码包 cd /opt wget http://download.redis.io/releases/redis-4.0.12.tar.gz 2.解压源码包 tar zxf redi ...

  10. request-html 简单爬虫

    import asyncio from requests_html import HTMLSession url = 'http://www.xiaohuar.com/hua/' session = ...