Atcoder Beginner Contest 311 C - E题讲解

news2024/10/10 22:26:41

C - Find it!

1. Description

Problem Statement

There is a directed graph with N N N vertices and N N N edges.

The i i i-th edge goes from vertex i i i to vertex A i A_i Ai. (The constraints guarantee that i ≠ A i i \neq A_i i=Ai.)

Find a directed cycle without the same vertex appearing multiple times.

It can be shown that a solution exists under the constraints of this problem.

Notes

The sequence of vertices B = ( B 1 , B 2 , … , B M B_1, B_2, \dots, B_M B1,B2,,BM) is called a directed cycle when all of the following conditions are satisfied:
∙ \bullet M ≥ 2 M \geq 2 M2
∙ \bullet The edge from vertex B i B_i Bi to vertex B i + 1 B_{i+1} Bi+1 exists. ( 1 ≤ i ≤ M − 1 1 \leq i \leq M-1 1iM1)
∙ \bullet The edge from vertex B M B_M BM to vertex B 1 B_1 B1 exists.
∙ \bullet If i ≠ j i \neq j i=j , then B i ≠ B j B_i \neq B_j Bi=Bj .

Constraints

All input values are integers.
2 ≤ N ≤ 2 × 1 0 5 2 \le N \le 2 \times 10^5 2N2×105
1 ≤ A i ≤ N 1 \le A_i \le N 1AiN
A i ≠ i A_i \neq i Ai=i

Input

The input is given from Standard Input in the following format:
N N N
A 1 A_1 A1 A 2 A_2 A2 … \dots A N A_N AN

Output

Print a solution in the following format:
M M M
B 1 B_1 B1 B 2 B_2 B2 … \dots B M B_M BM

M M M is the number of vertices, and B i B_i Bi is the i i i-th vertex in the directed cycle.
The following conditions must be satisfied:
2 ≤ M 2 \le M 2M
B i + 1 = A B i B_{i+1} = A_{B_i} Bi+1=ABi ( 1 ≤ i ≤ M − 1 1 \le i \le M-1 1iM1 )
B 1 = A B M B_{1} = A_{B_M} B1=ABM
B i ≠ B j B_i \neq B_j Bi=Bj ( i ≠ j i \neq j i=j )

If multiple solutions exist, any of them will be accepted.

Sample Input 1

7
6 7 2 1 3 4 5

Sample Output 1

4
7 5 3 2

7 → 5 → 3 → 2 → 7 7 \rightarrow 5 \rightarrow 3 \rightarrow 2 \rightarrow 7 75327 is indeed a directed cycle.
Here is the graph corresponding to this input:

Here are other acceptable outputs:
4
2 7 5 3

3
4 1 6

Note that the graph may not be connected.

Sample Input 2

2
2 1

Sample Output 2

2
1 2
This case contains both of the edges 1 → 2 1 \rightarrow 2 12 and 2 → 1 2 \rightarrow 1 21.
In this case, 1 → 2 → 1 1 \rightarrow 2 \rightarrow 1 121 is indeed a directed cycle.
Here is the graph corresponding to this input, where 1 ↔ 2 1 \leftrightarrow 2 12 represents the existence of both 1 → 2 1 \rightarrow 2 12 and 2 → 1 2 \rightarrow 1 21:

Sample Input 3

8
3 7 4 7 3 3 8 2

Sample Output 3

3
2 7 8
Here is the graph corresponding to this input:

2. Solution

Atcoder Begginer Contest 311(C题)

3. Code

#include <iostream>
#include <vector>
#include <cstring>
#include <unordered_map>

using namespace std;

const int N = 2e5 + 10;

int n;
int a[N];
int h[N], e[N], idx, ne[N], st[N];
vector<int> pass;
int res;
unordered_map<int, int> pos;
int b[N];
bool flg;

void add(int a, int b)
{
	e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}
void dfs(int u)
{
	if (st[u] || flg) return;
	st[u] = 1;
	
	for (int i = h[u]; ~i; i = ne[i])
	{
		if (st[e[i]] == 1)
		{
			res = e[i];
			flg = 1;
			return;
		}
		else if (st[e[i]] != 2)
		{
			pass.push_back(e[i]);
			dfs(e[i]);
			if (flg) return;
			pass.pop_back();
		}
	}
	
	st[u] = 2;
}

int main()
{
	cin >> n;
	
	for (int i = 1; i <= n; i ++)
		cin >> a[i], add(i, a[i]), pos[a[i]] = i;
		
	for (int i = 1; i <= n; i ++)
		if (!flg)
			dfs(i);
		
	b[1] = res;
	int k = n;
	for (int i = 1; i <= n; i ++)
	{
		b[i + 1] = a[b[i]];
		if (b[i + 1] == b[1])
		{
			k = i;
			break;
		}
	}	
	
	cout << k << endl;
	for (int i = 1; i <= k; i ++)
		cout << b[i] << " ";

	return 0;
}


D - Grid Ice Floor

1. Description

Problem Statement

There is an N × M N \times M N×M grid and a player standing on it.

Let ( i , j ) (i,j) (i,j) denote the square at the i i i-th row from the top and j j j-th column from the left of this grid.

Each square of this grid is ice or rock, which is represented by N N N strings S 1 , S 2 , … , S N S_1,S_2,\dots,S_N S1,S2,,SN of length M M M as follows:
if the j j j-th character of S i S_i Si is ., square ( i , j ) (i,j) (i,j) is ice;
if the j j j-th character of S i S_i Si is #, square ( i , j ) (i,j) (i,j) is rock.
The outer periphery of this grid (all squares in the 1 1 1-st row, N N N-th row, 1 1 1-st column, M M M-th column) is rock.
Initially, the player rests on the square ( 2 , 2 ) (2,2) (2,2), which is ice.

The player can make the following move zero or more times.
First, specify the direction of movement: up, down, left, or right.
Then, keep moving in that direction until the player bumps against a rock. Formally, keep doing the following:
if the next square in the direction of movement is ice, go to that square and keep moving;
if the next square in the direction of movement is rock, stay in the current square and stop moving.
Find the number of ice squares the player can touch (pass or rest on).

Constraints

3 ≤ N , M ≤ 200 3 \le N,M \le 200 3N,M200
S i S_i Si is a string of length M M M consisting of # and ..
Square ( i , j ) (i, j) (i,j) is rock if i = 1 i=1 i=1, i = N i=N i=N, j = 1 j=1 j=1, or j = M j=M j=M.
Square ( 2 , 2 ) (2,2) (2,2) is ice.

Input

The input is given from Standard Input in the following format:

N N N M M M
S 1 S_1 S1
S 2 S_2 S2
⋮ \vdots
S N S_N SN

Output

Print the answer as an integer.

Sample Input 1

6 6
######
#....#
#.#..#
#..#.#
#....#
######

Sample Output 1

12

For instance, the player can rest on ( 5 , 5 ) (5,5) (5,5) by moving as follows:
( 2 , 2 ) → ( 5 , 2 ) → ( 5 , 5 ) (2,2) \rightarrow (5,2) \rightarrow (5,5) (2,2)(5,2)(5,5).
The player can pass ( 2 , 4 ) (2,4) (2,4) by moving as follows:
( 2 , 2 ) → ( 2 , 5 ) (2,2) \rightarrow (2,5) (2,2)(2,5), passing ( 2 , 4 ) (2,4) (2,4) in the process.
The player cannot pass or rest on ( 3 , 4 ) (3,4) (3,4).

Sample Input 2

21 25
#########################
#..............###...####
#..............#..#...###
#........###...#...#...##
#........#..#..#........#
#...##...#..#..#...#....#
#..#..#..###...#..#.....#
#..#..#..#..#..###......#
#..####..#..#...........#
#..#..#..###............#
#..#..#.................#
#........##.............#
#.......#..#............#
#..........#....#.......#
#........###...##....#..#
#..........#..#.#...##..#
#.......#..#....#..#.#..#
##.......##.....#....#..#
###.............#....#..#
####.................#..#
#########################

Sample Output 2

215

2. Solution

Atcoder Begginer Contest 311(D题)

3. Code

#include <bits/stdc++.h>
#define x first
#define y second
#define int long long

using namespace std;

const int N = 2e2 + 10;
typedef pair<int, int> PII;

int h, w;
char s[N][N];
bool st[N][N], vis[N][N];
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

void bfs(int sx, int sy)
{
	queue<PII> q;
	
	q.push({sx, sy});
	
	while (q.size())
	{
		auto t = q.front();
		q.pop();
		
		for (int i = 0; i < 4 ;i ++)
		{
			int xx = t.x, yy = t.y;
			while (s[xx][yy] == '.')
				st[xx][yy] = 1, xx += dx[i], yy += dy[i];
			xx -= dx[i], yy -= dy[i];
			
			if (!vis[xx][yy])
				vis[xx][yy] = 1, q.push({xx, yy});
		}
	}
}

signed main()
{
	cin >> h >> w;
	
	for (int i = 1; i <= h; i ++)
		for (int j = 1; j <= w; j ++)
			cin >> s[i][j];
			
	bfs(2, 2);
	
	int res = 0;
	for (int i = 1; i <= h; i ++)
		for (int j = 1; j <= w; j ++)
			res += st[i][j];
			
	cout << res << endl;
			
	return 0;
}

E - Defect-free Squares

1. Description

Problem Statement

There is a grid with H H H rows and W W W columns. Let ( i , j ) (i, j) (i,j) denote the square at the i i i-th row from the top and j j j-th column from the left of the grid.

Each square of the grid is holed or not. There are exactly N N N holed squares: ( a 1 , b 1 ) , ( a 2 , b 2 ) , … , ( a N , b N ) (a_1, b_1), (a_2, b_2), \dots, (a_N, b_N) (a1,b1),(a2,b2),,(aN,bN).
When the triple of positive integers ( i , j , n ) (i, j, n) (i,j,n) satisfies the following condition, the square region whose top-left corner is ( i , j ) (i, j) (i,j) and whose bottom-right corner is ( i + n − 1 , j + n − 1 ) (i + n - 1, j + n - 1) (i+n1,j+n1) is called a holeless square.
i + n − 1 ≤ H i + n - 1 \leq H i+n1H.
j + n − 1 ≤ W j + n - 1 \leq W j+n1W.
For every pair of non-negative integers ( k , l ) (k, l) (k,l) such that 0 ≤ k ≤ n − 1 , 0 ≤ l ≤ n − 1 0 \leq k \leq n - 1, 0 \leq l \leq n - 1 0kn1,0ln1, square ( i + k , j + l ) (i + k, j + l) (i+k,j+l) is not holed.
How many holeless squares are in the grid?

Constraints

1 ≤ H , W ≤ 3000 1 \leq H, W \leq 3000 1H,W3000
0 ≤ N ≤ min ⁡ ( H × W , 1 0 5 ) 0 \leq N \leq \min(H \times W, 10^5) 0Nmin(H×W,105)
1 ≤ a i ≤ H 1 \leq a_i \leq H 1aiH
1 ≤ b i ≤ W 1 \leq b_i \leq W 1biW
All ( a i , b i ) (a_i, b_i) (ai,bi) are pairwise different.
All input values are integers.

Input

The input is given from Standard Input in the following format:

H H H W W W N N N
a 1 a_1 a1 b 1 b_1 b1
a 2 a_2 a2 b 2 b_2 b2
⋮ \vdots
a N a_N aN b N b_N bN

Output

Print the number of holeless squares.

Sample Input 1

2 3 1
2 3

Sample Output 1

6

There are six holeless squares, listed below. For the first five, n = 1 n = 1 n=1, and the top-left and bottom-right corners are the same square.
The square region whose top-left and bottom-right corners are ( 1 , 1 ) (1, 1) (1,1).
The square region whose top-left and bottom-right corners are ( 1 , 2 ) (1, 2) (1,2).
The square region whose top-left and bottom-right corners are ( 1 , 3 ) (1, 3) (1,3).
The square region whose top-left and bottom-right corners are ( 2 , 1 ) (2, 1) (2,1).
The square region whose top-left and bottom-right corners are ( 2 , 2 ) (2, 2) (2,2).
The square region whose top-left corner is ( 1 , 1 ) (1, 1) (1,1) and whose bottom-right corner is ( 2 , 2 ) (2, 2) (2,2).

Sample Input 2

3 2 6
1 1
1 2
2 1
2 2
3 1
3 2

Sample Output 2

0

There may be no holeless square.

Sample Input 3

1 1 0

Sample Output 3

1

The whole grid may be a holeless square.

Sample Input 4

3000 3000 0

Sample Output 4

9004500500

2. Solution

此题可以用两个思路来做,一是二分+前缀和,二是dp,具体见视频。

Atcoder Begginer Contest 311(E题)

3. Code

思路1:(二分+前缀和)

#include <bits/stdc++.h>
#define int long long

using namespace std;

const int N = 3e3 + 10;

int h, w, k;
int a, b;
bool mp[N][N];
int sum[N][N];

bool check(int a, int b, int c)
{
	int tmp = sum[a + c - 1][b + c - 1] - sum[a + c - 1][b - 1] - sum[a - 1][b + c - 1] + sum[a - 1][b - 1];
	return (tmp == 0);
}

signed main()
{
	cin >> h >> w >> k;
	
	for (int i = 1; i <= k; i ++)
		cin >> a >> b, mp[a][b] = 1;
		
	for (int i = 1; i <= h; i ++)
		for (int j = 1; j <= w; j ++)
			sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + mp[i][j];
			
	int res = 0;
	for (int i = 1; i <= h; i ++)
		for (int j = 1; j <= w; j ++)
		{
			int l = 0, r = min(h - i + 1, w - j + 1);
			while (l < r)
			{
				int mid = l + r + 1 >> 1;
				if (check(i, j, mid)) l = mid;
				else r = mid - 1;
			}
			
			res += l;
		}
		
	cout << res << endl;
	
	return 0;
}

思路2:(Dp)

#include <iostream>
#define int long long

using namespace std;

const int N = 3e3 + 10;

int n, m, k;
int a, b;
int mp[N][N], dp[N][N];

signed main()
{
	cin >> n >> m >> k;
	
	for (int i = 1; i <= k; i ++)
			cin >> a >> b, mp[a][b] = 1;
			
	int res =0 ;
	for (int i = 1; i <= n; i ++)
		for (int j = 1; j <= m; j ++)
		{
			dp[i][j] = min(dp[i - 1][j], min(dp[i][j - 1], dp[i - 1][j - 1])) + 1;
			if (mp[i][j])
				dp[i][j] = 0;
			res += dp[i][j];
		}
		
	cout << res << endl;
	
	return 0;
}

最后祝大家早日在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/790101.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

记负均正 C语言实现

记负均正 描述 首先输入要输入的整数个数n&#xff0c;然后输入n个整数。输出为n个整数中负数的个数&#xff0c;和所有正整数的平均值&#xff0c;结果保留一位小数。 0即不是正整数&#xff0c;也不是负数&#xff0c;不计入计算。如果没有正数&#xff0c;则平均值为0。 数…

【C++】-多态的语法细节详解

&#x1f496;作者&#xff1a;小树苗渴望变成参天大树&#x1f388; &#x1f389;作者宣言&#xff1a;认真写好每一篇博客&#x1f4a4; &#x1f38a;作者gitee:gitee✨ &#x1f49e;作者专栏&#xff1a;C语言,数据结构初阶,Linux,C 动态规划算法&#x1f384; 如 果 你 …

软路由系统 --- iKuai虚拟机修改虚拟机网卡类型

软路由系统&#xff1a;iKuai 注意&#xff1a;2.0系列及以后版本&#xff0c;默认安装成功后只是绑定了LAN口&#xff0c;其他的接口需要自己进爱快的WEB界面手工绑定。 虚拟机网卡类型说明&#xff1a; 个人版vmware 建议选择Linux ubuntu64位&#xff0c;否则默认网卡类型…

MySQL 数据抽稀 每分钟取一条

假如原始数据为每5秒一个数据&#xff0c;现在想展示为每4分钟一条数据&#xff0c;先按照分钟数把除以4余数为0的行选出来&#xff0c;在按照 年月日 时分&#xff0c;做组内排序&#xff08;窗函数ROW_NUMBER&#xff09;&#xff0c;最后再拿出序号为1的行。 WITH data_01 …

基于SpringBoot+vue的私人健身与教练预约管理系统设计与实现(源码+LW+部署文档等)

博主介绍&#xff1a; 大家好&#xff0c;我是一名在Java圈混迹十余年的程序员&#xff0c;精通Java编程语言&#xff0c;同时也熟练掌握微信小程序、Python和Android等技术&#xff0c;能够为大家提供全方位的技术支持和交流。 我擅长在JavaWeb、SSH、SSM、SpringBoot等框架…

谈谈区块链技术

目录 1.什么是区块链 2.区块链的实现原理 3.区块链技术有哪些应用 4.区块链技术潜在的危害 5.区块链技术未来会怎么发展 1.什么是区块链 区块链是一种技术&#xff0c;它通过在一个分布式网络中记录和验证交易和数据&#xff0c;实现去中心化的数字账本。区块链的核心概念是…

读数据压缩入门笔记11_读后总结与感想兼导读

1. 基本信息 数据压缩入门 Understanding Compression [美]柯尔特麦克安利斯&#xff08;Colt McAnlis&#xff09;、[美]亚历克斯海奇 著&#xff1b; 王凌云 译 人民邮电出版社,2020年4月出版&#xff0c;1版 1.1. 读薄率 书籍总字数300千字&#xff0c;笔记总字数18197…

Linux用户权限

1.用户、权限、组的概念 1.1 用户 用户 是Linux系统工作中重要的一环&#xff0c;在Linux系统中&#xff0c;不论是由本机或是远程登录系统&#xff0c;每个系统都必须拥有一个账号&#xff0c;一个账号就是一个用户。 1.2 权限 在Linux系统中&#xff0c;每一个用户对不同…

汇编语言(第4版)实验5 编写、调试具有多个段的程序

&#xff08;1&#xff09;参考答案&#xff1a; ①不变&#xff08;0123h,0456h,0789h,0abch,0defh,0cabh,0987h&#xff09; ②076c 076b 076a ③X-2 X-1 &#xff08;2&#xff09;参考答案&#xff1a; ①不变&#xff08;0123h,0456h&#xff09; ②076c 076b 076a ③X-2…

AcWing 1275. 最大数—线段树单点修改

题目链接 AcWing 1275. 最大数 题目描述 分析 这道题是线段树的模板题&#xff0c;属于一眼ding真&#xff0c;鉴定为线段树的裸题&#xff0c;正好用来学习线段树。线段树、树状数组等数据结构题&#xff0c;难的地方是在如何分析出来要用哪个数据结构解决。 线段树要比树状…

上海VR全景展示,快速了解VR全景拍摄

导语&#xff1a; 随着科技的不断进步&#xff0c;虚拟现实技术的应用日益广泛。在这其中&#xff0c;VR全景图片作为一种数字化助力的全景拍摄方式&#xff0c;正逐渐成为人们关注的焦点。通过数字化技术&#xff0c;VR全景图片能够以360度全方位的视角呈现真实的场景&#x…

docker学习和进阶2023

文末有下载地址。如有侵权请联系作者谢谢&#xff01; docker学习和进阶2023 参考引用 docker学习笔记&#xff1a;https://blog.csdn.net/m0_46188681/article/details/128993319 Docker容器配置和资源限制&#xff1a;https://www.cnblogs.com/xiugeng/p/16254087.html 容…

idea使用命令将jar包导入到maven仓库中

因为今天突然忘了命令&#xff0c;记下来方便以后查看 pom文件的依赖 jar包路径 进入idea中命令窗 输入命令 mvn install:install-file -DfileD:\Project\spring-cloud\dubbo-api\target\dubbo-api-1.0-SNAPSHOT.jar -DgroupIdcom.wmx -DartifactIddubbo-api -Dversion1.0…

flutter数字动画库:animated_flip_counter

前言 在数字动画的制作中&#xff0c;有时候我们会面临时间紧张或效果不尽如人意的情况。这时&#xff0c;使用现成的动画库或工具可以大大提高效率&#xff0c;同时也能够获得更好的效果。animated_flip_counter就是一个非常不错的数字动画库&#xff0c;可以帮助我们快速地创…

Leetcode-每日一题【剑指 Offer II 010. 和为 k 的子数组】

题目 给定一个整数数组和一个整数 k &#xff0c;请找到该数组中和为 k 的连续子数组的个数。 示例 1&#xff1a; 输入:nums [1,1,1], k 2输出: 2解释: 此题 [1,1] 与 [1,1] 为两种不同的情况 示例 2&#xff1a; 输入:nums [1,2,3], k 3输出: 2 提示: 1 < nums.leng…

SIP视频对讲sip广播网关

SV-PA2是专门对行业用户需求研发的一款SIP音视频对讲&#xff0c;媒体流传输采用标准IP/RTP/RTSP协议。它很好的继承了锐科达话机稳定性好、电信级音质的优点&#xff0c;且完美兼容当下所有基于SIP的主流IPPBX/软交换/IMS平台,如Asterisk, Broadsoft, 3CX, Elastix 等。它集多…

linux 使用nethogs命令查看各个进程使用网络的情况

1&#xff0c;使用nethogs命令查看各个进程使用网络的情况 #yum -y install nethogs 2&#xff0c;使用nethogs命令查看进程使用网络的情况 #nethogs

【Docker】Docker部署私有仓库的配置及应用

文章目录 一、Docker-registry 搭建本地私有仓库1. Registry 的概念2. Registry 的部署过程 二、Docker-harbor 搭建私有仓库1. 什么是Harbor2. Harbor 的特性3. Harbor的构成4. Harbor 的部署过程4.1 安装 harbor4.2 创建项目并进行上传下载4.3 上传镜像到私有仓库4.4 从私有仓…

LLM Data Pipelines: 解析大语言模型训练数据集处理的复杂流程

编者按&#xff1a;在训练大语言模型的过程中,构建高质量的训练数据集是非常关键的一步&#xff0c;但关于构建大模型训练所需数据集的通用数据处理流程&#xff08;Data pipelines)的相关资料极为稀少。 本文主要介绍了基于Common Crawl数据集的数据处理流程。首先,文章概述了…

Java版知识付费源码 Spring Cloud+Spring Boot+Mybatis+uniapp+前后端分离实现知识付费平台 +支持二次开发定制

提供职业教育、企业培训、知识付费系统搭建服务。系统功能包含&#xff1a;录播课、直播课、题库、营销、公司组织架构、员工入职培训等。 提供私有化部署&#xff0c;免费售后&#xff0c;专业技术指导&#xff0c;支持PC、APP、H5、小程序多终端同步&#xff0c;支持二次开发…