草庐IT

【CodeForces】Educational Codeforces Round 132 (Rated for Div. 2)

HEY 2023-03-28 原文

比赛链接

链接

A. Three Doors

题目链接

链接

题目描述

输入

输出

样例

输入
4
3
0 1 2
1
0 3 2
2
3 1 0
2
1 3 0
输出
YES
NO
YES
NO

题目大意

面前有三个门,编号分别为1,2,3。
再给你一把编号为x的钥匙,打开每扇门后,可以有一把编号为a[i]的钥匙,判断所给的x是否能把三扇门都打开。

思路

按照题意进行模拟,并且用a[]存放钥匙编号,st[]用来判断门是否打开

代码

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>

using namespace std;

void solve()
{
	int x ;
	cin >> x;
	
	int a[4];
	cin >> a[1] >> a[2] >> a[3];
	
	bool st[4];
	memset(st,false,sizeof st);
	
	while(x != 0)
	{
		st[x] = true;
		
		x = a[x];
	}
	
	if(st[1] && st[2] && st[3])
		puts("YES");
	else
		puts("NO");
}

int main()
{
	int T;
	cin >> T;
	
	while(T --)
		solve();
		
	return 0;
}

B. Also Try Minecraft

题目链接

链接

题目描述

输入

输出

样例

输入

7 6
10 8 9 6 8 12 7
1 2
1 7
4 6
7 1
3 5
4 2

输出

2
10
0
7
3
1

题目大意

给你n个数,m组x,y。判断从a[x]a[y]两个数之间的减少量是多少。
比如样例中1 7,判断从a[1]a[7]中的减少的数是多少
[10 - 8]+[9 - 6]+[12 - 7] = 2 + 3 + 5 = 10

思路

按照题意进行模拟会造成超时,所以可以采用前缀和。
需要对m组数据进行分类,因为有的是从左到右,有的是从右到左。
left[i] = left[i-1] + max(0ll , a[i] - a[i+1])
right[i] = right[i-1] + max(0ll , a[i+1] - a[i])
其中0ll表示long long状态下的0

模拟代码(会TLE)

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <iostream>

using namespace std;

const int N = 1e5+10;

int n , m;
int a[N];

int main()
{
	cin >> n >> m;
	for(int i = 1;i <= n;i ++)
		cin >> a[i];
	
	while(m --)
	{
		int x , y;
		cin >> x >> y;
		
		int cnt = 0;
		if(x < y)//从左到右 
		{
			
			for(int i = x;i < y;i ++)
				if(a[i] > a[i+1])
					cnt += a[i] - a[i+1];
			
		}
		else if(x > y)//从右往左 
		{
			
			for(int i = x;i > y;i --)
				if(a[i] > a[i-1])
					cnt += a[i] - a[i-1];
		}
		
		cout << cnt << endl;
	}
	
	
	return 0;
}

前缀和代码

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>

using namespace std;

typedef long long ll;

const int N = 1e5+10;

ll a[N];
ll le[N] , ri[N];
ll n , m , x , y;

int main()
{
	cin >> n >> m;
	
	for(int i = 1;i <= n;i ++)
		cin >> a[i];
	
	memset(le,0ll,sizeof(le));
	memset(ri,0ll,sizeof(ri));
	
	for(int i = 1;i < n;i ++)
	{
		le[i] = le[i-1] + max(0ll , a[i] - a[i+1]);
		ri[i] = ri[i-1] + max(0ll , a[i+1] - a[i]);
	}
	
	while(m --)
	{
		cin >> x >> y;
		
		if(x < y)
			cout << le[y-1] - le[x-1] << endl;
		else
			cout << ri[x-1] - ri[y-1] << endl;
	}
	
	return 0;
}

有关【CodeForces】Educational Codeforces Round 132 (Rated for Div. 2)的更多相关文章

  1. Educational Codeforces Round 146 (Rated for Div. 2)(B,E详解) - 2

    题外话:抑郁场,开局一小时只出A,死活想不来B,最后因为D题出锅ura才保住可怜的分。但咱本来就写不到DB-LongLegs(数论)本题题解法一学自同样抑郁的知乎作者幽血魅影的题解,有讲解原理。法二来着知乎巨佬cup-pyy(大佬说《不难发现》呜呜)题意三种操作:向上走mmm步向右走mmm步给自己一次走的步数加111,即使得m=m+1m=m+1m=m+1问从(0,0)(0,0)(0,0)走到(a,b)(a,b)(a,b)的最小操作次数,值得注意的是操作三不可逆。解析假设我们最终一步的大小增长到mmm,那么在这个过程中我能以[1,m][1,m][1,m](当步数增长到该数时)之间的任何数字向上或

  2. c++ - _stat 返回不可能的 errno 代码 132 - 2

    我有以下程序来读取存在的文件:constchar*path="C:\\Users\\myname\\AppData\\Roaming\\Technology\\plus\\fs\\D\\TECH\\CUSTOM\\LOG.XML";struct_statlInfo;interr=_stat(path,&lInfo);if(err==0){return(lInfo.st_mode&_S_IFDIR)!=0;}else{_get_errno(&err);printf("Error:%d\n",err);}根据documentation,在这个特定文件上,我得到err==132,其中_sta

  3. Android SDK 在 android.widget.PopupWindow$1.onScrollChanged(PopupWindow.java :132) - 2

    谷歌开发团队的人能解释一下如何避免在pre-ics设备上发生这种崩溃吗?在我的例子中,ListView项目上的ImageButton是PopupWindow的anchor,用于创建下拉菜单。我已经尝试了popup.dismiss()、popup=null等所有方法,但似乎没有什么能阻止适配器重置时出现问题。我收到以下异常:FATALEXCEPTION:mainjava.lang.NullPointerExceptionatandroid.widget.PopupWindow$1.onScrollChanged(PopupWindow.java:132)05-2117:02:27.736

  4. Codeforces Round 866 (Div. 2) - 2

    A.Yura'sNewName题意:给出一个仅由_或^组成的字符串,你可以在任意位置添加_或^字符,使得字符串满足:任意字符要么属于^_^的一部分,要么属于^^的一部分。求最少添加的字符数量。分析:对于_我们只需处理没有组成^_^的_:①如果_在首位置且左边没有^则添加^②如果_在尾位置且右边没有^则添加^③如果_在中间部分且右边没有^则添加^当字符串只有一个^时末尾添加一个^code:#includeusingnamespacestd;intmain(){ std::ios::sync_with_stdio(false); cin.tie(0),cout.tie(0); intt; cin

  5. 132.【前端】webpack 搭建 React 应用环境(一) - 2

    自从接触前端开发以来,严格来说是从接触React开发以来,一直用的create-react-app脚手架。用起来确实爽,啥也不用干,直接上业务代码(这其实也就是脚手架的意义所在)。随着技术的进阶,是时候抛开脚手架工具自建一个React应用开发环境了。该环境支持的技术场景:1.支持React2.支持typescript3.支持scss4.支持调试热更新一、创建项目目录npminitgitinit二、安装依赖npminstall--save-dev@babel/core@babel/cli@babel/preset-env@babel/preset-reactnpminstall--save-de

  6. Educational Codeforces Round 145 Div. 2 题解 - 2

    目录A.Garland(签到)题面翻译思路:代码B.PointsonPlane(数学)题面翻译思路:代码C.SumonSubarray(构造)题面翻译:思路:代码D.BinaryStringSorting题面翻译思路:代码A.Garland(签到)Youhaveagarlandconsistingof 4 coloredlightbulbs,thecolorofthe i-thlightbulbis si.Initially,allthelightbulbsareturnedoff.Yourtaskistoturnallthelightbulbson.Youcanperformthefollo

  7. Educational Codeforces Round 145 Div. 2 题解 - 2

    目录A.Garland(签到)题面翻译思路:代码B.PointsonPlane(数学)题面翻译思路:代码C.SumonSubarray(构造)题面翻译:思路:代码D.BinaryStringSorting题面翻译思路:代码A.Garland(签到)Youhaveagarlandconsistingof 4 coloredlightbulbs,thecolorofthe i-thlightbulbis si.Initially,allthelightbulbsareturnedoff.Yourtaskistoturnallthelightbulbson.Youcanperformthefollo

  8. Educational Codeforces Round 132 div.2 A-F题解 - 2

    视频讲解:TBDA.ThreeDoors题目大意有333个门和333把对应的钥匙。其中222把钥匙分别在222扇门后,111把在手上。打开门才能获得门后的钥匙,问能否打开所有的门。题解判断前两次开的门后,是否有钥匙即可。参考代码#includeusingnamespacestd;typedeflonglongll;intmain(){ intT,x,a[5],now; scanf("%d",&T); while(T--) { scanf("%d%d%d%d",&x,&a[1],&a[2],&a[3]); now=3^2^1^a[1]^a[2]^a[3]; if(a[now]==0||a[

  9. Educational Codeforces Round 132 div.2 A-F题解 - 2

    视频讲解:TBDA.ThreeDoors题目大意有333个门和333把对应的钥匙。其中222把钥匙分别在222扇门后,111把在手上。打开门才能获得门后的钥匙,问能否打开所有的门。题解判断前两次开的门后,是否有钥匙即可。参考代码#includeusingnamespacestd;typedeflonglongll;intmain(){ intT,x,a[5],now; scanf("%d",&T); while(T--) { scanf("%d%d%d%d",&x,&a[1],&a[2],&a[3]); now=3^2^1^a[1]^a[2]^a[3]; if(a[now]==0||a[

  10. Codeforces CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) A-D 题解 - 2

    A.Two0-1Sequences 大致翻译:两个长度为n和m的二进制序列a和b(题目保证n>=m)两个操作:op1: 改变a(2)为min(a(1),a(2)),并且移除a(1)op2: 改变a(2)为max(a(1),a(2)),并且移除a(1)每次操作后,原先的a(i)变成a(i+1),长度减少1,即前移。  a二进制序列能否通过这两个操作变成b二进制序列?解题思路:刚开始想的是判断a2后缀跟a1后缀是否相同,再判断,a1前面有没有1和0(因为有1和0,就表示op1和op2可以随意完成)。写的时候又陆陆续续发现需要几个特判,想a1长度为1等。于是就debug,慢慢发现只要前面有a2的第一

随机推荐