首页 > 其他分享 >AtCoder Beginner Contest 264 ABCDE

AtCoder Beginner Contest 264 ABCDE

时间:2023-07-07 17:35:14浏览次数:39  
标签:AtCoder sz int fy ABCDE cin fx fa 264

AtCoder Beginner Contest 264

A - "atcoder".substr()

Problem Statement

题意:截取字符串 atcoder的[L,R]一段并输出。

Solution

题解:

用string.substr直接写

#include<bits/stdc++.h>
using namespace std;


int main()
{
	string s = "?atcoder";
	int l,r;
	cin>>l>>r;
	cout<<s.substr(l,r-l+1)<<endl;
	return 0;
}

B - Nice Grid

Problem Statement

题意:给你一个图,由黑白块组成。

给你一个坐标问你是黑的还是白的

Solution

思路:直接暴力模拟

#include<bits/stdc++.h>
using namespace std;


int main()
{
	int n,m;
	cin>>n>>m;
	if(n==15||n==1||m==15||m==1)cout<<"black\n";
	else if(n==2||n==14)cout<<"white\n";
	else if(n==3||n==13)
	{
		if(m==2||m==14)cout<<"white\n";
		else cout<<"black\n";
	}
	else if(n==4||n==12)
	{
		if(m==3||m==13)cout<<"black\n";
		else cout<<"white\n";
	}
	else if(n==5||n==11)
	{
		if(m==2||m==4||m==14||m==12)cout<<"white\n";
		else cout<<"black\n";
	}
	else if(n==6||n==10)
	{
		if(m==3||m==5||m==13||m==11)cout<<"black\n";
		else cout<<"white\n";
	}
	else if(n==7||n==9)
	{
		if(m==2||m==4||m==6||m==10||m==12||m==14)cout<<"white\n";
		else cout<<"black\n";
	}
	else
	{
		if(n%2)cout<<"black\n";
		else cout<<"white\n";
	}

	return 0;
}

C - Matrix Reducing

Problem Statement

题意:给你两个矩阵,问你能否通过对第一个矩阵删除任意行和任意列得到第二个矩阵。

Solution

思路:数据范围只有10,直接暴力,二进制枚举选第一个矩阵的哪几个即可,再check是不是和第二个矩阵长得一样。

#include<bits/stdc++.h>
using namespace std;
const int N = 15;
int a[N][N],b[N][N];

int main()
{
	int h1,w1,h2,w2;
	cin>>h1>>w1;
	for(int i = 1;i<=h1;i++)
		for(int j = 1;j<=w1;j++)
			cin>>a[i][j];
	cin>>h2>>w2;
	for(int i = 1;i<=h2;i++)
		for(int j = 1;j<=w2;j++)
			cin>>b[i][j];
	for(int i = 0;i<(1<<h1);i++)
	{
		for(int j = 0;j<(1<<w1);j++)
		{
			vector<int>x,y;
			for(int k = 0;k<h1;k++)
				if((i>>k)&1)x.push_back(k+1);

			for(int k = 0;k<w1;k++)
				if((j>>k)&1)y.push_back(k+1);

			if((int)x.size()!=h2||(int)y.size()!=w2)continue;
			bool ok = true;
			for(int k = 1;k<=h2;k++)
			{
				for(int l = 1;l<=w2;l++)
				{
					if(a[x[k-1]][y[l-1]]!=b[k][l])
					{
						ok = false;
						break;
					}
				}
				if(!ok)break;
			}
			if(ok)
			{
				cout<<"Yes\n";
				return 0;
			}
		}
	}
	cout<<"No\n";
	return 0;
}

D - "redocta".swap(i,i+1)

Problem Statement

思路:给你一个字符串,你可以交换相邻两个位置的字符,问你至少交换多少次可以得到字符串atcoder.

Solution

思路:bfs。

当前串可以通过交换任意两个字符得到新串,代价为1,那可以考虑bfs写。

用map记录这个串有没有得到过,因为如果得到过,由现在的转移到得到过的串一定不会更优。

最后输出答案。

#include<bits/stdc++.h>
using namespace std;

int main()
{
	string s;
	cin>>s;
	unordered_map<string,int>mp;
	mp[s] = 0;
	queue<string>q;
	q.push(s);
	while(!q.empty())
	{
		string t = q.front();
		q.pop();
		if(t=="atcoder")
		{
			cout<<mp[t]<<endl;
			return 0;
		}
		for(int i = 0;i<6;i++)
		{
			string cur = t;
			swap(cur[i],cur[i+1]);
			if(!mp[cur])
			{
				mp[cur] = mp[t]+1;
				q.push(cur);
			}
		}
	}
	return 0;
}

E - Blackout 2

Problem Statement

题意:

一个国家由N个城市和M个发电厂。

一共由N+M个建筑,1,2...N个是城市,N+1,N+2...M个是发电厂。

这个国家由E条电线,告诉你这E条线的连接。

一个城市称为有电当且仅当可以到达至少一个发电厂。

现在有Q个事件,每次删去一条电线,询问删去后还有多少个城市有电。

Solution

思路:正向不好考虑,因为还要删边。我们考虑离线+反向加边。这样原本有电的不会变成没有电,就好处理很多。

那么我们要如何判断一个城市是否有电?

并查集每次令父亲是大的那个,这样保证如果连到发电厂(>n),能被识别。还要注意记录每个并查集的大小,这样连接的时候才知道要加上几个。为什么这样是可以的呢?因为每个并查集只有一个父亲,那我们又令父亲是最大的,如果最大的都<=n,那说明没有电,如果此时连上了一个>n的,说明整个并查集的元素都可以有电。

即:

如果fx<=n&&fy>n,cnt += sz[fx],fa[fx] = fy

#include<bits/stdc++.h>
using namespace std;
const int N = 5e5+10;
bool ok[N];
map<int,pair<int,int>>mp;
int evt[N];
vector<int>edge[N*2];
int fa[N],ans[N],sz[N];
int find(int x)
{
	if(x==fa[x])
		return x;
	return fa[x] = find(fa[x]);
}
int main()
{
	int n,m,e;
	cin>>n>>m>>e;
	for(int i = 1;i<=n+m;i++)
		fa[i] = i;
	for(int i = 1;i<=n;i++)
		sz[i] = 1;
	for(int i = 1;i<=e;i++)
	{
		int x,y;
		cin>>x>>y;
		mp[i] = {x,y};
	}
	int q;
	cin>>q;
	set<int>s;
	for(int i = 1;i<=q;i++)
	{
		cin>>evt[i];
		s.insert(evt[i]);
	}
	int cnt = 0;
	for(int i = 1;i<=e;i++)
	{
		if(s.find(i)==s.end())
		{
			auto t = mp[i];
			int x = t.first,y = t.second;
			int fx = find(x),fy = find(y);
			if(fx>fy)swap(fx,fy);
			if(fx!=fy)
			{
				if(fx<=n&&fy>n)
					cnt += sz[fx];
				fa[fx] = fy;
				sz[fy] += sz[fx]; 
			}
		}
	}


	for(int i = q;i>=1;i--)
	{
		ans[i] = cnt;
		auto t = mp[evt[i]];
		int x = t.first,y = t.second;
		int fx = find(x),fy = find(y);
		if(fx>fy)swap(fx,fy);
		if(fx!=fy)
		{
			if(fx<=n&&fy>n)
					cnt += sz[fx];
			fa[fx] = fy;
			sz[fy] += sz[fx]; 
		}
	}


	for(int i = 1;i<=q;i++)
		cout<<ans[i]<<endl;

	return 0;
}

标签:AtCoder,sz,int,fy,ABCDE,cin,fx,fa,264
From: https://www.cnblogs.com/nannandbk/p/17535614.html

相关文章

  • AtCoder Regular Contest 163
    A只需暴力判断能否分成两部分即可。时间复杂度\(\mathcal{O}(n^2)\)。B肯定是选值域连续的一段数操作,排序后枚举区间即可。时间复杂度\(\mathcal{O}(n\logn)\)。C场上降智了15min,我是什么shaber啊。注意到\(1=\frac1n+\sum_{i<n}\frac{1}{i(i+1)}\),但......
  • python opencv无法编码h264、opencv编码的mp4视频无法在网页中播放
    pythonopencv无法编码h264、opencv编码的mp4视频无法在网页中播放,这好像是因为开源许可的协议不同,导致pythonopencv中没有内置h264的编码,无法以h264的格式保存视频。所以我就直接使用webm格式的视频:output_path='output_video.webm'output_codec=cv2.VideoWriter_fourcc......
  • AtCoder Beginner Contest 304
    A:1#include<cstdio>2#include<cstring>3#include<algorithm>4#include<iostream>5#include<string>6#include<vector>7#include<stack>8#include<bitset>9#include<cstdlib>10#include......
  • 基于瑞芯微平台cif接口dvp相机的视频接入(ov2640、rv1126为例)
    名词定义CIF,指RK芯片中的VIP模块,用以接收Sensor数据并保存到Memory中,仅转存数据,无ISP功能DVP,一种并行数据传输接口,即DigitalVideoPortHSYNC,指DVP接口的行同步信号PCLK,指Sensor输出PixelClockVSYNC,指DVP接口的场同步信号V4L2,即Video4Linux2,Linuxkernel的视频处理模块 ......
  • EasyCVR平台如何在不修改分辨率的情况下进行H.265自动转码H.264?
    EasyCVR视频融合平台基于云边端一体化架构,可支持多协议、多类型设备接入,在视频能力上,平台可实现视频直播、录像、回放、检索、云存储、告警上报、语音对讲、电子地图、集群、H.265转码、智能分析以及平台级联等。我们在此前的文章中介绍过关于EasyCVR平台H.265自动转码的功能,今......
  • AtCoder Beginner Contest 308 - E
    题目链接:abc308前四题简单就不放了E-MEX阿巴阿巴,比赛的时候想复杂了,一直在想怎么快速的统计27种mex的情况,啊,前面对后面的影响等等等,反正就是想复杂了现在再想想,看了官方题解,从'E'出发,统计其前后各3种数字的个数,再用mex函数判答案,\(O(n)\)即可!剩下的见代码吧,做完之后发现,没......
  • Atcoder Beginer Contest 306 D ~ E
    vp中途突然拉肚子>_<D-PoisonousFull-CourseD-PoisonousFull-Course(atcoder.jp)题意一个人初始是健康的,现在有n道菜摆在他面前,每道菜都有自已的美味值,但是有些菜是有毒的,有些菜是无毒的。如果他在健康状态吃了有毒的菜就会变成中毒状态,如果他在中毒状态吃了无毒的菜就......
  • AtCoder Regular Contest 163
    Preface补题,这场比赛的时候被拉去开科研组会了,所以就没现场打了这两天军训在伤病连划水,白天可以好好想题目舒服的一批这场D题确实很妙,需要一些竞赛图相关的知识才能想到转化,不过也算是学到一个重要trick了吧A-DivideString显然只要考虑能否分成两个串即可,首先如果存在\(i......
  • AtCoder Regular Contest 163 D Sum of SCC
    洛谷传送门AtCoder传送门怎么连这种相对传统的计数也不会……考虑换种方式描述强连通分量个数。考虑竞赛图缩点后存在一条极长的链,因此转化为把缩完点后的链劈成左右两个集合,使得左边集合不为空的方案数。于是我们现在只要统计点集\(A,B\)数量,满足\(A\ne\varnothing,A......
  • AtCoder Regular Contest 153 E Deque Minimization
    洛谷传送门AtCoder传送门我们考虑给定\(X\),如何贪心地求\(f(X)\)。队列为空时加入队首或队尾都是一样的。队列不为空,设队首为\(c\)。因为我们的目标是最小化字典序,于是如果\(X_i\lec\),我们把\(X_i\)加入队首,否则加入队尾。由此也容易发现,加入队首的数一定单调不升。......