首页 > 其他分享 >Educational Codeforces Round 166 (Rated for Div. 2)

Educational Codeforces Round 166 (Rated for Div. 2)

时间:2024-06-02 20:59:27浏览次数:10  
标签:Educational Rated cin int tt Codeforces should ans array

A. Verify Password

题目描述

Monocarp is working on his new site, and the current challenge is to make the users pick strong passwords.

Monocarp decided that strong passwords should satisfy the following conditions:

  • password should consist only of lowercase Latin letters and digits;
  • there should be no digit that comes after a letter (so, after each letter, there is either another letter or the string ends);
  • all digits should be sorted in the non-decreasing order;
  • all letters should be sorted in the non-decreasing order.

Note that it’s allowed for the password to have only letters or only digits.

Monocarp managed to implement the first condition, but he struggles with the remaining ones. Can you help him to verify the passwords?

分析:

要求数字必须出现在字母的前面,且字母与数字分别要按照升序排列

代码:

#include<bits/stdc++.h>
using namespace std;
 
int main()
{
	int tt;
	cin >> tt;
	while(tt--)
	{
		string a;
		int n;
		cin >> n;
		cin >> a;
		int flag=0,OK=0;
		char flag1='0',flag2='a';
		for(char q:a)
		{
			if(q>='0'&&q<='9') 
			{
				if(flag==1) //如果这个数字出现在字母之后
				{
					cout << "NO" << endl;
					OK=1;
					break;
				}
				if(q>=flag1) //判断是否按照升序排列
				{
					flag1=q;
				}
				else
				{
					cout << "NO" << endl;
					OK=1;
					break;
				}
			}
			else if(q>='a'&&q<='z')
			{
				flag=1;
				if(q>=flag2)
				{
					flag2=q;
				}
				else
				{
					cout << "NO" << endl;
					OK=1;
					break;
				}
			}
		}
		if(!OK) cout << "YES" << endl;
	}
	return 0;
 } 

B. Increase/Decrease/Copy

You are given two integer arrays: array a a a of length n n n and array b b b of length n + 1 n+1 n+1.

You can perform the following operations any number of times in any order:

  • choose any element of the array a a a and increase it by 1 1 1;
  • choose any element of the array a a a and decrease it by 1 1 1;
  • choose any element of the array a a a, copy it and append the copy to the end of the array a a a.

Your task is to calculate the minimum number of aforementioned operations (possibly zero) required to transform the array a a a into the array b b b. It can be shown that under the constraints of the problem, it is always possible.

分析:

要把a数组转换为b数组,要在a中挑选出一个数字复制到末尾,要尽量使复制过去的数字与b[n]相差较小。因为可以按照任意次序进行操作,所以能从a数组中挑选出的数字包括a[i]-b[i]区间中的所有数字。最后操作数再加上a[i]-b[i]的绝对值。

例如:
4
4 2 1 2
2 1 5 2 3

用例3中,a[0]->b[0],过程为4-1=3,3-1=2,在此过程中出现了数字3也就是b[n],因此,我们可以直接将3复制到a数组的末尾。

代码:

#include<bits/stdc++.h>
using namespace std;
 
const int N =2*1e5+10;
long long int a[N],b[N];
 
int main()
{
	int tt;
	cin >> tt;
	while(tt--)
	{
		int n;
		cin >> n;
		for(int i=0;i<n;i++) cin >> a[i];
		for(int i=0;i<=n;i++)cin >> b[i];
		long long int ans=1e9;
		for(int i=0;i<n;i++) 
		{
	        ans=min(ans,abs(a[i]-b[n]));
	        ans=min(ans,abs(b[i]-b[n]));
	        if(min(a[i],b[i])<=b[n]&&b[n]<=max(a[i],b[i])) //判断区间中是否有b[n]
	        {
	            ans=0;
	        }
       }
	    for(int i=0;i<n;i++)
	    ans+=abs(a[i]-b[i]);
		cout << ans+1 << endl;
	}
	return 0;
 } 

24/5/31

标签:Educational,Rated,cin,int,tt,Codeforces,should,ans,array
From: https://blog.csdn.net/2301_79426686/article/details/139361801

相关文章

  • 【AI Generated】NaiveUI实战
    NaiveUI实战目录引言准备工作NaiveUI基础3.1安装与配置3.2快速开始常用控件4.1Button按钮4.2Input输入框4.3Select选择框4.4Checkbox复选框4.5Radio单选框4.6Switch开关4.7DatePicker日期选择器4.8TimePicker时间选择器4.9Form表单4.1......
  • 【AI Generated】从零学习Vue.js
    从零学习Vue.js目录引言准备工作Vue.js基础3.1Vue实例3.2模板语法3.3数据绑定3.4计算属性和侦听器3.5Class与Style绑定3.6条件渲染3.7列表渲染3.8事件处理3.9表单输入绑定Vue.js组件4.1组件基础4.2组件注册4.3父子组件通信4.4插槽4.5......
  • Codeforces Round 949 (Div. 2)
    目录写在前面ABCD写在最后写在前面比赛地址:https://codeforces.com/contest/1976妈的昨晚硬撑打了场edu上午实验下午爬山考试困困困妈的什么二进制场,C吃了个爽呃呃写得什么史山A二进制。一个显然的想法是选择区间\([l,r]\)中质因数次数之和最大的数。特别指出了限制......
  • Codeforces Round 949 (Div. 2)
    榜单#提交者=*ABCDEF1(2055)gutongxing20261388-1488900A#include<bits/stdc++.h>usingnamespacestd;intT,n,m;signedmain(){ scanf("%d",&T); while(T--){ scanf("%d%d",&n,&m); printf(&quo......
  • Educational Codeforces Round 166 (Rated for Div. 2)
    目录写在前面ABCD写在最后写在前面比赛地址:https://codeforces.com/contest/1976满课,并且48小时之内只睡了8h。本来不想打的,但是手痒就上小号打了,然而唐唐唐掉大分呃呃A签到。感谢isdigit函数。///*By:Luckyblock*/#include<bits/stdc++.h>#defineLLlonglon......
  • Educational Codeforces Round 151 (Rated for Div. 2) E
    链接凌晨两点半突然醒了。。然后睡不着了。。躺了一个半小时决定起来啃题解。花了一个小时弄懂了。但是要怎么自己想到还没想好。这个属于计数dp的范围了,我不是很熟悉了。题目大意:有n个盒子,里面装了一些球,球的数量大于等于1且小于n。可以进行一种操作,每次操作可以把一个球移......
  • codeforces round 948(Div2)
    A题目过简单,略B.构造+二进制点击查看代码#include<bits/stdc++.h>#defineLLlonglongLLx,ans[40];boolyes[40];intmain(){std::ios::sync_with_stdio(0),std::cin.tie(0);intT;std::cin>>T;while(T--){std::cin>>x;for(LLi......
  • Codeforces Round 948 (Div. 2)
    A.LittleNikita题意:\(n\)步操作,\(+1\)或\(-1\),最终结果是否等于\(m\)思路:设\(+1\)的操作次数为\(x\),\(-1\)的操作次数为\(y\)\[x+y=n\\x-y=m\]\[x=(n+m)/2\\y=(n-m)/2\]\((n-m)\)和\((n+m)\)均为偶数,即\(n\)和\(m\)均为偶数或同为奇数,且\(n>=m\)代码:voidsolve()......
  • Codeforces Round 948 (Div. 2) B - C
    总结:做了A,B,然后开局A看错题wa了一发,B出的又很慢,所以掉大分。总的来说还是c没开出来。B.BinaryColouring1.题目大意:给你一个int范围内的数x,要求构造一个二进制串,能有-1、1、0,二进制串的值不能出现两个连续的地方不为0,二进制串的值要等于x。2.思路分析:我们可以发现,对于x的......
  • POSEIDON: Privacy-Preserving Federated NeuralNetwork Learning
    写在最前面,感觉这一篇的技术更贴近于密码学,所以部分核心技术读起来比较吃力。仅供大家参考哇~Abstract—Inthispaper,weaddresstheproblemofprivacypreservingtrainingandevaluationofneuralnetworksinanN-party,federatedlearningsetting.Weproposea......