Educational Codeforces Round 76 (Rated for Div. 2)

给定一排学生,其中有两个竞争对手,目标是通过最多x次相邻学生交换操作来最大化两者之间的距离。输出在限制交换次数下,两人最大可能的距离。另外两个问题涉及到数字变换和寻找最短的支配子数组。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A - Two Rival Students
You are the gym teacher in the school.
There are n students in the row. And there are two rivalling students among them. The first one is in position a, the second in position b. Positions are numbered from 1 to n from left to right.
Since they are rivals, you want to maximize the distance between them. If students are in positions p and s respectively, then distance between them is |p−s|.
You can do the following operation at most x times: choose two adjacent (neighbouring) students and swap them.
Calculate the maximum distance between two rivalling students after at most x swaps.
Input
The first line contains one integer t (1≤t≤100) — the number of test cases.
The only line of each test case contains four integers n, x, a and b (2≤n≤100, 0≤x≤100, 1≤a,b≤n, a≠b) — the number of students in the row, the number of swaps which you can do, and positions of first and second rivaling students respectively.
Output
For each test case print one integer — the maximum distance between two rivaling students which you can obtain.
Example
Input
3
5 1 3 2
100 33 100 1
6 0 2 3
Output
2
99
1
Note
In the first test case you can swap students in positions 3 and 4. And then the distance between the rivals is equal to |4−2|=2.
In the second test case you don’t have to swap students.
In the third test case you can’t swap students

题目大意:给一个n,从1-n个人,然后有两个人a,b为竞争对手,可以这n个人可以每次相邻两个人交换一次,可以交换x次,问他们两个竞争对手交换若干次后最远距离为多少。(在第i,j个位置距离就为|i-j|)

解题思路:如果他们两个距离加上可以交换的次数之后距离没有超过n-1的话,那么他们两个最远距离一定时交换x次的距离,因为每交换一次距离都加1,但是如果他们两个距离加上交换次数大于n的话,那么最远的距离就是从1-n的距离,就是n-1。

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	int t,n,x,a,b,MAX,MIN;
	cin>>t;
	while(t--)
	{
		cin>>n>>x>>a>>b;
		if(abs(a-b)+x>n-1) cout<<n-1<<endl;
		else cout<<abs(a-b)+x<<endl;
	}
	return 0;
 } 

B - Magic Stick
Recently Petya walked in the forest and found a magic stick.
Since Petya really likes numbers, the first thing he learned was spells for changing numbers. So far, he knows only two spells that can be applied to a positive integer:
If the chosen number a is even, then the spell will turn it into 3a2;
If the chosen number a is greater than one, then the spell will turn it into a−1.
Note that if the number is even and greater than one, then Petya can choose which spell to apply.
Petya now has only one number x. He wants to know if his favorite number y can be obtained from x using the spells he knows. The spells can be used any number of times in any order. It is not required to use spells, Petya can leave x as it is.
Input
The first line contains single integer T (1≤T≤104) — the number of test cases. Each test case consists of two lines.
The first line of each test case contains two integers x and y (1≤x,y≤109) — the current number and the number that Petya wants to get.
Output
For the i-th test case print the answer on it — YES if Petya can get the number y from the number x using known spells, and NO otherwise.
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
Example
Input
7
2 3
1 1
3 6
6 8
1 2
4 1
31235 6578234
Output
YES
YES
NO
YES
NO
YES
YES

题目大意:给一个数字x和一个数字y,如果x是偶数,x可以变为(3*x)/2,如果x>1,x可以变为x-1,如果x既为偶数又大于1,那么可以选择变换的方式,问x经过若干次变化,能否变为y。

解题思路:可以发现,(3x)/2这个变化是让数字变大的,而x-1是让数字变小的,而且只要x大于1那么就可以进行x-1这个操作,所以,如果x>=y的话,那么一定可以从x变为y(一直-1-1-1-1…)如果x<y的话,那么就要考虑两种情况,一种是不管怎么变化,它都只能在某个范围内扩大,那么如果y大于它这个范围它就不能变换,2,3就是一个例子,2经过(3x)/2变化还是2,而3不是偶数,只能进行-1,3-1=2,所以3和2是一类,那么如果x=2或者x=3,y只要大于3,就一定不可能,其他的都可以进行扩大(如果为奇数),然后减为偶数的时候又可以扩大,偶数的话可以直接扩大,当超过y的时候一直减1就可以了。

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	int t,n,x,a,b,MAX,MIN;
	cin>>t;
	while(t--)
	{
		cin>>n>>x>>a>>b;
		if(abs(a-b)+x>n-1) cout<<n-1<<endl;
		else cout<<abs(a-b)+x<<endl;
	}
	return 0;
 } 

C - Dominated Subarray
Let’s call an array t dominated by value v in the next situation.
At first, array t should have at least 2 elements. Now, let’s calculate number of occurrences of each number num in t and define it as occ(num). Then t is dominated (by v) if (and only if) occ(v)>occ(v′) for any other number v′. For example, arrays [1,2,3,4,5,2], [11,11] and [3,2,3,2,3] are dominated (by 2, 11 and 3 respectevitely) but arrays [3], [1,2] and [3,3,2,2,1] are not.
Small remark: since any array can be dominated only by one number, we can not specify this number and just say that array is either dominated or not.
You are given array a1,a2,…,an. Calculate its shortest dominated subarray or say that there are no such subarrays.
The subarray of a is a contiguous part of the array a, i. e. the array ai,ai+1,…,aj for some 1≤i≤j≤n.
Input
The first line contains single integer T (1≤T≤1000) — the number of test cases. Each test case consists of two lines.
The first line contains single integer n (1≤n≤2⋅105) — the length of the array a.
The second line contains n integers a1,a2,…,an (1≤ai≤n) — the corresponding values of the array a.
It’s guaranteed that the total length of all arrays in one test doesn’t exceed 2⋅105.
Output
Print T integers — one per test case. For each test case print the only integer — the length of the shortest dominated subarray, or −1 if there are no such subarrays.
Example
Input
4
1
1
6
1 2 3 4 5 1
9
4 1 2 4 5 4 3 2 1
4
3 3 3 3
Output
-1
6
3
2
Note
In the first test case, there are no subarrays of length at least 2, so the answer is −1.
In the second test case, the whole array is dominated (by 1) and it’s the only dominated subarray.
In the third test case, the subarray a4,a5,a6 is the shortest dominated subarray.
In the fourth test case, all subarrays of length more than one are dominated.

题目大意:一个数组,计算每个数字出现的次数,求出现两次相同的数最近的距离。
解题思路:每出现一个新的数就将它第一次的位置存到一个新的数组中,如果第二次出现就用这个位置减去第一次的位置,然后和ans取最小,然后将第二次的位置重新更新,取到最后如果ans还是INF,那么就是没有这个引领数字,就输出“-1”,否则输出ans+1.(2 3 4 2 5 6 第1个和第4个都是2,ans=4-1=3,距离为3+1,所以是ans+1).

#include<iostream>
#include<cstring>
using namespace std;
#define INF 0x3f3f3f3f
const int maxn=2e5+10;
int a[maxn];
int b[maxn];
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		memset(b,0,sizeof(b));
		int ans=INF,n;
		cin>>n;
		for(int i=1;i<=n;i++)
			cin>>a[i];
		for(int i=1;i<=n;i++)
		{
			if(b[a[i]]==0) b[a[i]]=i;
			else
			{
				ans=min(ans,i-b[a[i]]);
				b[a[i]]=i;
			}
		}
		if(ans==INF) cout<<"-1"<<endl;
		else cout<<ans+1<<endl;
	}
	return 0;
}
"educational codeforces round 103 (rated for div. 2)"是一个Codeforces平台上的教育性比赛,专为2级选手设计评级。以下是有关该比赛的回答。 "educational codeforces round 103 (rated for div. 2)"是一场Codeforces平台上的教育性比赛。Codeforces是一个为程序员提供竞赛和评级的在线平台。这场比赛是专为2级选手设计的,这意味着它适合那些在算法和数据结构方面已经积累了一定经验的选手参与。 与其他Codeforces比赛一样,这场比赛将由多个问题组成,选手需要根据给定的问题描述和测试用例,编写程序来解决这些问题。比赛的时限通常有两到三个小时,选手需要在规定的时间内提交他们的解答。他们的程序将在Codeforces的在线评测系统上运行,并根据程序的正确性和效率进行评分。 该比赛被称为"educational",意味着比赛的目的是教育性的,而不是针对专业的竞争性。这种教育性比赛为选手提供了一个学习和提高他们编程技能的机会。即使选手没有在比赛中获得很高的排名,他们也可以从其他选手的解决方案中学习,并通过参与讨论获得更多的知识。 参加"educational codeforces round 103 (rated for div. 2)"对于2级选手来说是很有意义的。他们可以通过解决难度适中的问题来测试和巩固他们的算法和编程技巧。另外,这种比赛对于提高解决问题能力,锻炼思维和提高团队合作能力也是非常有帮助的。 总的来说,"educational codeforces round 103 (rated for div. 2)"是一场为2级选手设计的教育性比赛,旨在提高他们的编程技能和算法能力。参与这样的比赛可以为选手提供学习和进步的机会,同时也促进了编程社区的交流与合作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值