CodeforcesRound#749(Div.1+Div.2,basedonTechnocup2022EliminationRound1)-C. Omkar and Determination-题解

本文主要是介绍CodeforcesRound#749(Div.1+Div.2,basedonTechnocup2022EliminationRound1)-C. Omkar and Determination-题解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在这里插入代码片

目录

    • Codeforces Round #749 (Div. 1 + Div. 2, based on Technocup 2022 Elimination Round 1) - C. Omkar and Determination
      • Problem Description
      • Input
      • Output
      • Sample Input
      • Sample Onput
      • Note
  • 题目大意
  • 解题思路
  • AC代码

Codeforces Round #749 (Div. 1 + Div. 2, based on Technocup 2022 Elimination Round 1) - C. Omkar and Determination

传送门
Time Limit: 2 seconds
Memory Limit: 256 megabytes

Problem Description

The problem statement looms below, filling you with determination.

Consider a grid in which some cells are empty and some cells are filled. Call a cell in this grid exitable if, starting at that cell, you can exit the grid by moving up and left through only empty cells. This includes the cell itself, so all filled in cells are not exitable. Note that you can exit the grid from any leftmost empty cell (cell in the first column) by going left, and from any topmost empty cell (cell in the first row) by going up.

Let’s call a grid determinable if, given only which cells are exitable, we can exactly determine which cells are filled in and which aren’t.

You are given a grid a of dimensions n × m n×m n×m , i. e. a grid with n n n rows and m m m columns. You need to answer q q q queries ( 1 ≤ q ≤ 2 ⋅ 1 0 5 ) (1≤q≤2⋅10^5) (1q2105). Each query gives two integers x 1 , x 2 ( 1 ≤ x 1 ≤ x 2 ≤ m ) x_1,x_2 (1≤x_1≤x_2≤m) x1,x2(1x1x2m) and asks whether the subgrid of a consisting of the columns x 1 , x 1 + 1 , … , x 2 − 1 , x 2 x_1,x_1+1,…,x_2−1,x_2 x1,x1+1,,x21,x2 is determinable.

Input

The first line contains two integers n , m ( 1 ≤ n , m ≤ 1 0 6 , n m ≤ 1 0 6 ) n,m (1≤n,m≤10^6, nm≤10^6) n,m(1n,m106,nm106) — the dimensions of the grid a.

n n n lines follow. The y y y-th line contains m characters, the x x x-th of which is ‘X’ if the cell on the intersection of the the y y y-th row and x x x-th column is filled and “.” if it is empty.

The next line contains a single integer q ( 1 ≤ q ≤ 2 ⋅ 1 0 5 ) q (1≤q≤2⋅10^5) q(1q2105) — the number of queries.

q q q lines follow. Each line contains two integers x 1 x_1 x1 and x 2 ( 1 ≤ x 1 ≤ x 2 ≤ m ) x_2 (1≤x_1≤x_2≤m) x2(1x1x2m), representing a query asking whether the subgrid of a containing the columns x 1 , x 1 + 1 , … , x 2 − 1 , x 2 x_1,x_1+1,…,x_2−1,x_2 x1,x1+1,,x21,x2 is determinable.

Output

For each query, output one line containing “YES” if the subgrid specified by the query is determinable and “NO” otherwise. The output is case insensitive (so “yEs” and “No” will also be accepted).

Sample Input

4 5
..XXX
...X.
...X.
...X.
5
1 3
3 3
4 5
5 5
1 5

Sample Onput

YES
YES
NO
YES
NO

Note

For each query of the example, the corresponding subgrid is displayed twice below: first in its input format, then with each cell marked as “E” if it is exitable and “N” otherwise.

For the first query:

..X EEN
... EEE
... EEE
... EEE

For the second query:

X N
. E
. E
. E

Note that you can exit the grid by going left from any leftmost cell (or up from any topmost cell); you do not need to reach the top left corner cell to exit the grid.

For the third query:

XX NN
X. NN
X. NN
X. NN

This subgrid cannot be determined only from whether each cell is exitable, because the below grid produces the above “exitability grid” as well:

XX
XX
XX
XX

For the fourth query:

X N
. E
. E
. E

For the fifth query:

..XXX EENNN
...X. EEENN
...X. EEENN
...X. EEENN

This query is simply the entire grid. It cannot be determined only from whether each cell is exitable because the below grid produces the above “exitability grid” as well:

..XXX
...XX
...XX
...XX

题目大意

  • 给你一个地图,X是障碍.是空地

  • 每次可以向左或向上移动,但不能经过任何一处障碍

  • 你可以从地图最左边或最上边离开地图

  • 现在有Q次询问,每次询问都将地图的第 l l l 列到第 r r r 列作为新的地图

  • 在新的地图中,你可以确定出所有的 可逃离点不可逃离点,并把这些点绘制成一张新的地图给别人看

  • 问别人看到你这张可否逃离点地图后,能否唯一还原出之前的障碍空地地图

解题思路

这题描述有点绕

假如给你了一个可否逃离点地图,你看到后是否可以唯一还原出障碍空地地图呢?

如果可以唯一还原,那么每个点的信息都必须是有效的。也就是说不能通过其他点推断出这个点是否可以逃离。如果通过其他点已经确定这点必定不可逃离了,那么无论这一点的原障碍空地地图中是空地还是障碍,这点都是无法逃离点,此时不能还原。

那么什么时候根据其他的点就能判断这一点必定不可逃离呢?

如果这一点的上面的点和这一点的左边的点都不可逃离,那么这一点必定不可逃离,无论它是空地还是障碍。

因为这点只能向上或向左走。

此时,可否逃离点地图的这点必是不可逃离点,因而无法还原出这点的原地图是空地还是障碍。

因此我们可以寻找这样的图形:

在这里插入图片描述

2 × 2 2\times2 2×2的格子里,如果右上角和左下角都是障碍,那么同时包含这两列的可否逃离点地图就不可还原。

我们可以用 O ( n m ) O(nm) O(nm)的时间复杂度的时间求出所有的这样的列,存到Swords数组中。之后给出的所有询问,只要同时包含这两列,都是No。否则就是Yes。

还有一个问题就是询问次数数量级是 2 ∗ 1 0 5 2*10^5 2105,而不可同时包含数组Swords的数量级也可能是 1 0 6 ( → m ) 10^6 (\to m) 106m级别,暴力求解每个询问还是要超时,因此我们还要处理一下所有的询问,把所有的询问进行离线操作。

读入了所有的询问 l l l r r r后,我们把询问排个序,依据是 l l l从小到大优先, r r r从小到大其次。

蓝色的横线代表排序后的询问:
在这里插入图片描述

假如我们在Swords中记录下了 3 3 3个值: 1 , 3 , 5 1,3,5 1,3,5(也就是说同时包含 1 ∼ 2 1\sim2 12 3 ∼ 4 3\sim4 34 5 ∼ 6 5\sim6 56的地图都输出No)

我们可以用一个变量to来记录处理到了哪个询问,假如这次处理的是 1 ∼ 2 1\sim2 12,那么我们这次就处理所有未处理过的且l<2的询问。包含 1 ∼ 2 1\sim2 12就No,不包含就Yes。

这样下来,每个询问都只处理了一次,处理的总时间复杂度是 O ( Q ) O(Q) O(Q)(小于排序复杂度 O ( Q log ⁡ Q ) O(Q\log Q) O(QlogQ)


AC代码

/** @Author: LetMeFly* @Date: 2021-10-17 19:53:37* @LastEditors: LetMeFly* @LastEditTime: 2021-10-18 23:29:26*/ // LastEditTime: 2021-10-17 20:06:52
#include <bits/stdc++.h>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define dbg(x) cout << #x << " = " << x << endl
#define fi(i, l, r) for (int i = l; i < r; i++)
#define cd(a) scanf("%d", &a)
typedef long long ll;
char c[1000010]; // 用来输入一行
struct querys { // 询问int l, r, th; // l到r,第th次询问bool no; // 是否应输出No
}q[200010];
bool cmp(const querys &a, const querys &b) { // 排序规则1:l从小到大优先,r从小到大其次if (a.l != b.l) return a.l < b.l;if (a.r != b.r) return a.r < b.r;return a.th < b.th;
}
bool cmp2(const querys &a, const querys &b) { // 排序规则2:按询问顺序从小到大排序(为了输出)return a.th < b.th;
}
int main() {int n,m;cin>>n>>m;vector<bool> a[n]; // 存储原始地图for (int i = 0; i < n; i++) {a[i].resize(m); // 每一行都有m列}for (int i = 0; i < n; i++) {scanf("%s", c); // 读入这一行for (int j = 0; j < m; j++) {a[i][j] = c[j] == 'X'; // 处理这一行的每一列}}vector<int>Swords; // 假如a在Swords中,那么同时包含第a列和第a+1列的子地图都应输出No。因每次Swords[i]都把询问一分两半,故美其名曰“宝剑”for (int j = 0; j + 1 < m; j++) {for (int i = 1; i < n; i++) {if (a[i][j] && a[i - 1][j + 1]) { // 2x2的方格,左下角和右上角都是XSwords.push_back(j); // 就存下这一列break; // 退出循环}}}int Q;cin>>Q; // Q次询问for (int i = 0; i < Q; i++) {scanf("%d%d", &q[i].l, &q[i].r);q[i].l--, q[i].r--;q[i].th = i;q[i].no = false;}sort(q, q + Q, cmp); // 依据规则1进行排序int to = 0; // 该处理哪个询问for (int &left: Swords) { // 遍历Swords中的每个元素int right = left + 1;int newTo = to; // 这次处理之后该处理哪个询问while (newTo < Q && q[newTo].l < right) {newTo++;}// [to, newTo)是未处理的queryfor (int i = to; i < newTo; i++) {if (q[i].l <= left && q[i].r >= right) { // 如果询问把这两列包含在其中q[i].no = true; // 就应该输出No}}}sort(q, q + Q, cmp2); // 恢复成发出询问的顺序(其实这里可以换成桶排)for (int i = 0; i < Q; i++) { // 输出if (q[i].no) puts("NO");else puts("YES");}return 0;
}

原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/120836197

这篇关于CodeforcesRound#749(Div.1+Div.2,basedonTechnocup2022EliminationRound1)-C. Omkar and Determination-题解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/893885

相关文章

C++ | Leetcode C++题解之第393题UTF-8编码验证

题目: 题解: class Solution {public:static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num &

C语言 | Leetcode C语言题解之第393题UTF-8编码验证

题目: 题解: static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return

C - Word Ladder题解

C - Word Ladder 题解 解题思路: 先输入两个字符串S 和t 然后在S和T中寻找有多少个字符不同的个数(也就是需要变换多少次) 开始替换时: tips: 字符串下标以0开始 我们定义两个变量a和b,用于记录当前遍历到的字符 首先是判断:如果这时a已经==b了,那么就跳过,不用管; 如果a大于b的话:那么我们就让s中的第i项替换成b,接着就直接输出S就行了。 这样

【秋招笔试】9.07米哈游秋招改编题-三语言题解

🍭 大家好这里是 春秋招笔试突围,一起备战大厂笔试 💻 ACM金牌团队🏅️ | 多次AK大厂笔试 | 大厂实习经历 ✨ 本系列打算持续跟新 春秋招笔试题 👏 感谢大家的订阅➕ 和 喜欢💗 和 手里的小花花🌸 ✨ 笔试合集传送们 -> 🧷春秋招笔试合集 🍒 本专栏已收集 100+ 套笔试题,笔试真题 会在第一时间跟新 🍄 题面描述等均已改编,如果和你笔试题看到的题面描述

LeetCode 第414场周赛个人题解

目录 Q1. 将日期转换为二进制表示 原题链接 思路分析 AC代码 Q2. 范围内整数的最大得分 原题链接 思路分析 AC代码 Q3. 到达数组末尾的最大得分 原题链接 思路分析 AC代码 Q4. 吃掉所有兵需要的最多移动次数 原题链接 思路分析 AC代码 Q1. 将日期转换为二进制表示 原题链接 Q1. 将日期转换为二进制表示 思路分析

牛客小白月赛100部分题解

比赛地址:牛客小白月赛100_ACM/NOI/CSP/CCPC/ICPC算法编程高难度练习赛_牛客竞赛OJ A.ACM中的A题 #include<bits/stdc++.h>using namespace std;#define ll long long#define ull = unsigned long longvoid solve() {ll a,b,c;cin>>a>>b>

P2858 [USACO06FEB] Treats for the Cows G/S 题解

P2858 题意 给一个数组。每天把最左或者最右的东西卖掉,第 i i i个东西,第 d a y day day天卖出的价格是 a [ i ] ∗ d a y a[i]*day a[i]∗day。 记忆化搜索 void dfs(int l,int r,int day,ll sum){if(v[l][r]>=sum)return;v[l][r]=sum;if(l>r)//这就是dp答案{

【C++题解】1272. 郭远摘苹果

欢迎关注本专栏《C++从零基础到信奥赛入门级(CSP-J)》 问题:1272. 郭远摘苹果 类型:二维数组 题目描述: 郭远有一天走到了一片苹果林,里面每颗树上都结有不同数目的苹果,郭远身上只能拿同一棵树上的苹果,他每到一棵果树前都会把自己身上的苹果扔掉并摘下他所在树上的苹果并带走(假设郭远会走过每一棵苹果树),问在郭远摘苹果的整个过程中,他身上携带的最多苹果数与最小苹果数的差是多少?

【最新华为OD机试E卷-支持在线评测】机器人活动区域(100分)多语言题解-(Python/C/JavaScript/Java/Cpp)

🍭 大家好这里是春秋招笔试突围 ,一枚热爱算法的程序员 ✨ 本系列打算持续跟新华为OD-E/D卷的三语言AC题解 💻 ACM金牌🏅️团队| 多次AK大厂笔试 | 编程一对一辅导 👏 感谢大家的订阅➕ 和 喜欢💗 🍿 最新华为OD机试D卷目录,全、新、准,题目覆盖率达 95% 以上,支持题目在线评测,专栏文章质量平均 94 分 最新华为OD机试目录: https://blog.

Codeforces#295(Div.2)A、B(模拟+BFS)

解题报告链接:点击打开链接 C. 题目链接:点击打开链接 解题思路: 对于给定的字符串,取出现次数最多的字母(可以同时有多个)。由这些字母组成长度为n的字符串,求有多少种组合。最后用数学知识即可。 完整代码: #include <algorithm>#include <iostream>#include <cstring>#include <climits>