首页 > 其他分享 >Codeforces Round 863 (Div. 3) A-C 赛后思路复盘

Codeforces Round 863 (Div. 3) A-C 赛后思路复盘

时间:2023-04-05 13:46:33浏览次数:40  
标签:string int 863 pos long Codeforces solve y1 Round

A (思维)

思路:观察样例可知数越大放在前面越优。遍历字符串,判断当前位置的数字和要插入的数字的关系,如果要插入的数大于当前数,那么就插入到当前数的前面。string里有一个insert函数,可以把指定字符串插入到指定下标之前。
在原串下标为pos的字符前插入字符串str
basic_string& insert (pos, str);
str从下标为pos1开始数的n个字符插在原串下标为pos的字符前
basic_string& insert (pos,str, pos1, n);
在原串下标为pos的字符前插入n个字符c
basic_string& insert (pos, n, c);
参考自:(https://blog.csdn.net/weixin_45845039/article/details/108909704)

点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N= 2 * 100010;
int a[N];
void solve()
{
	int n,k;
	cin >> n >> k;
	string s;
	cin >> s;
	bool f = 0;
	string s1 = to_string(k);
	for(int i=0; i < s.size(); i ++){
		int h = s[i]-'0';
		if(k > h){
			s.insert(i,s1);
			f = 1;
			break;
		}
	}
	if(f==0) cout << s << k <<'\n';
	else cout << s << '\n';
}

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

B (思维、规律)

思路:在纸上模拟样例可找到规律,因为初始点所在的层数是固定的,因此我们可以找到起始点到正方形边缘的距离,同理也可以找到终点离边缘的距离。这两个相对距离的差值就是我们要移动的最小步数。

点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N= 2 * 100010;
int a[N];
void solve()
{
	int n;
	int x1,y1,x2,y2;
	cin >> n >> x1 >> y1 >> x2 >> y2;
	int ka,kb,kc,kd;
	if(x1 > y1){
		ka = y1 - 1;
		kb = n - x1;
	}else {
		ka = x1 -1;
		kb = n- y1;
	}
	int a1 = min(ka,kb);
	if(x2 > y2){
		kc = y2 - 1;
		kd = n - x2;
	}else{
		kc = x2 -1;
		kd = n - y2;
	}
	int a2 = min(kc,kd);
	int ans = abs(a1-a2);
	cout << ans <<'\n';
}

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

C(模拟、贪心)

思路:我们可以每次贪心的选取最小的数,每个\(b_i\)的值只会被\(a_i\)和\(a_{i+1}\)影响,因此我们遍历\(b\)数组,直接构造答案数组,并且每次构造第\(a_{i+1}\)位的时候都预先把值设置为0,因为第\(a_{i+1}\)位的值也会影响到\(b_{i+1}\)的值,所以我们先贪心的把最小的填进去肯定是最优的。然后在构造第\(b_{i+1}\)位的时候,如果当前\(b_i\)的值填到上一位不会影响\(b_{i-1}\)的值,那么再把\(b_i\)填到上一位去最优。
举例:当b为 3 4 4 5时,先将a填入3 0 这时如果\(a_2\)填入4,那么\(max(a_1,a_2)\)就等于4,和\(b_1\)的值冲突,因此4不能填到\(a_2\),只能填到\(a_3\),后面的数同理。
代码:

点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N= 2 * 100010;
int a[N];
int ans[N];
void solve()
{
	int n;
	cin >> n;
	for(int i=1; i <= n-1; i ++) cin >> a[i];

	int cnt = 1;
	ans[0] = 1e9;
	bool f =0;
	for(int i = 1; i <= n-1; i ++) {
		if(i==1){
			if(a[i] ==0) {
				ans[i] = a[i];
				ans[i + 1] = a[i];
			}else {
				ans[i] = a[i];
				ans[i+1] = 0;
			}
			continue;
		}
		if(max(a[i],ans[i-1]) >a[i-1]){
			ans[i+1] =a[i];
		}else if(max(a[i],ans[i-1]) <= a[i-1]){
			ans[i] = a[i];
			ans[i+1] = 0;
		}
	}
	
	for (int i = 1; i <= n; i ++)
		cout << ans[i] << ' ';
	cout << '\n';
}

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

标签:string,int,863,pos,long,Codeforces,solve,y1,Round
From: https://www.cnblogs.com/neko-yingying/p/17289273.html

相关文章

  • cf-div.3-863d
    题目链接:https://codeforces.com/contest/1811/problem/D思维题,昨天被E题搞太久了,这题认真想的话应该可以出的。思路:不断循环,判断x和y是否在合法区间内。代码:#include<bits/stdc++.h>usingnamespacestd;constintN=2e5+10;longlongfib[70];voidsolve(){int......
  • codeforces round 862
    A.和洛谷上的删数思路一致,后者是找峰顶,这个是找谷底从前到后枚举每一位与要添加的数比大小,如果要添加的数<=该位的数,就继续枚举,否则就将这个数添加在其前面B.需要移动的步数=两个点所在的层数之差的绝对值,只要计算出所在层数就可以一开始没想明白怎么算这个层数,先把每个......
  • Codeforces Round 861 (Div. 2)
    Preface这场感觉都是一个礼拜前补题打的了,但由于上周末事情比较多都没来得及写题解因此可能题意都记得不是很清楚了,就简略地谈一谈吧A.LuckyNumbers不难想到直接暴力从左端点枚举到右端点并对每个数进行暴力判断一个很naive的结论就是当答案为\(9\)时直接输出即可,然后我们......
  • Background Removal obs
            BackgroundRemoval/PortraitSegmentation/VirtualGreen-screenv0.5.16GotodownloadAuthorroyshilkrotCreationdateApr15,2021TagsbackgroundgreenscreenportraitremovalsegmentationOverviewU......
  • Codeforces Round 862 A-E
    CodeforcesRound862(Div.2)先简单写一下A-E的题解。A异或的经典性质:\(x\oplusx=0\)。B显然要把字典序最小的那个字母放到最前面。如果这个字母出现了很多次,那么应该选择最后一次出现的位置。这也很容易证明。C联立以后计算一下就行了。比赛的时候爆了一次int。......
  • Codeforces Round 717 (Div. 2) B. AGAGA XOOORRR(位运算)
    https://codeforces.com/contest/1516/problem/B题目大意:给定长度为n的数组a,问我们能不能一直选择两个相邻的元素进行异或后,删除这两个值,把异或值留下来,最后剩下>=2个数字,它们都是相同的?可以做到输出YES,不能的话输出NO。input23022423110outputYESNO题......
  • CodeTON Round 4 (Div. 1 + Div. 2, Rated, Prizes!)-C
    参考了佬的c题题解思路,感觉很巧妙,记录一下https://zhuanlan.zhihu.com/p/618685370#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongconstintN=2*100010;inta[N];voidsolve(){ intn,c,d; cin>>n>>c>>d; set<int>se......
  • Codeforces Round 862 (Div. 2) A-D题解
    比赛地址A.WeNeedtheZero题意:给出一个数组,对任意1<=i<=n,令bi=aix,问是否存在x,使得b<sub>1</sub>b2...bn=0Solution如果n为奇数,那么x一定存在,因为偶数个x异或得到的是0,直接令x=0(a<sub>1</sub>a2...an)即可如果n为偶数,那么x取任何值都不会影响结果,所以只用看a1a<sub>2</sub......
  • Codeforces Round 862 (Div. 2) (4.2)
    CodeforcesRound862(Div.2)A-WeNeedtheZero思路:某个数被异或两次相当于没变,即判断n的奇偶性;n为偶数时判断所有数异或后的数是否为0,若为0,输出任意数;n为奇数时答案为所有数异或后的值#include<bits/stdc++.h>usingnamespacestd;typedefpair<int,int>PII;consti......
  • Codeforces Round 862 (Div. 2)A-C思路复盘
    感觉这场前三题都简单,复盘一下赛时的脑回路QAQ,c二分wa了四发赛后才过的血亏A题意:问是否能找到一个数x,有\(b_i=a_i⊕x\),使得\(b\)数组的总异或和为0。思路:赛时模拟样例可以发现先把a数组的总异或和求出来假设为x,然后由异或性质可知相同为0,不同为1,可知这个x可能就是答案。然......