跳到正文

个人记录

8.26-407-模拟考

AC_Goose约 5 分钟
原文
文章信息与版本

文章信息

发布
2025年8月27日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 5 分钟

版本

  1. v2当前
  2. v1

8.26-407-模拟考

T1 交换

题意分析

两个swap即可。

代码

C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int x,y,z;
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>x>>y>>z;
	swap(x,y);
	swap(x,z);
	cout<<x<<' '<<y<<' '<<z;
	return 0;
}

T2 猜猜芭蕾舞剧将如何结束

题意分析

用一种类似于双指针的方法,左指针表示舞台左侧,右指针表示舞台右侧,每一次输入就更新,再判断边界。

代码

C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N=1005;
int r,n;
int d[N];
int cl,cr;
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>r>>n;
	cl=0;
	cr=r;
	while(n--){
		int d;
		cin>>d;
		cl+=d;
		cr+=d;
		cl=min(cl,r);
		cl=max(cl,0ll);
		cr=min(cr,r);
		cr=max(cr,0ll);
	}
	if(cl==cr){
		cout<<cr;
	}else{
		cout<<-1;
	}
	return 0;
}

T3 落落的去的数学问题五

题意分析

第一眼考虑打表,但打出来的表972KBKB,根本交不上去。

其实,每一个数的下面,有三种可能,要么下一位比自己小一,要么大一,要么相等。但是99要特判。那么我们使用一个广搜实现即可,先把一到九压进去,然后取出队头,加上下一位,再压进去。

代码

C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int k;
int cnt;
int bfs(){
	queue<int> Q;
	Q.push(1);
	Q.push(2);
	Q.push(3);
	Q.push(4);
	Q.push(5);
	Q.push(6);
	Q.push(7);
	Q.push(8);
	Q.push(9);
	while(Q.size()){
		int u=Q.front();
		Q.pop();
		cnt++;
		if(cnt==k){
			return u;
		}
		for(int i=(u%10)-1;i<=(u%10)+1;i++){
			if(i<0||i>9){
				continue;
			}
			Q.push(u*10+i);
		}
	}
}
signed main(){
	cin>>k;
	cout<<bfs();
	return 0;
}

T4 操作2

题意分析

我们输入之后把数组排序,然后求出前缀和。因为要改变成xx使用找到比xx小的,再找比他大的,然后去前缀和差。

代码

C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N=2e5+5;
int n,q;
int a[N];
int pre[N];
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>q;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	sort(a+1,a+n+1);
	for(int i=1;i<=n;i++){
		pre[i]=pre[i-1]+a[i];
	}
	for(int i=1;i<=q;i++){
		int x;
		cin>>x;
		int pos2=upper_bound(a+1,a+n+1,x)-a;
		int pos1=lower_bound(a+1,a+n+1,x)-a-1;
		int ans=0;
		int tmp=x*pos1;
		int cnt=pre[pos1];
		ans=ans+abs(cnt-tmp);
		cnt=pre[n]-pre[pos2-1];
		tmp=x*(n-pos2+1);
		ans=ans+abs(cnt-tmp);
		cout<<ans<<endl;
	}
	return 0;
}
原文