跳到正文

个人记录

S14 题解

keep_on约 8 分钟
原文

这是 2026年2月21日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2026年2月21日
更新
2026年2月21日
篇幅
约 8 分钟

版本

  1. v2当前
  2. v1

比赛描述:

div.1 链接:https://www.luogu.com.cn/contest/305469 .
div.2 链接:https://www.luogu.com.cn/contest/305470 .

2A 二力平衡 /balance

非常简单,对于每一组测试数据,将力的大小原样输出、再对方向进行分讨即可。注意开 long long 。

给出标程:

C++
#include <bits/stdc++.h>
using namespace std;
long long T,f;
char c;
int main()
{
	for(cin>>T;T;T--) {
		cin>>f>>c;
		cout<<f<<' ';
		puts(c=='E'?"W":c=='W'?"E":c=='N'?"S":c=='S'?"N":c=='U'?"D":"U");
	}
	return 0;
}

2B 数字统计 /counting

也很简单,枚举 [l,r][l,r] 中的每一个整数,并判断是否符合要求即可。注意输入顺序。

给出标程:

C++
#include <bits/stdc++.h>
using namespace std;
int l,r,p1,p2,q1,q2,ans;
int main()
{
    cin>>l>>r>>p1>>p2>>q1>>q2;
    for(int i=l;i<=r;i++) if(i%p1==q1||i%p2==q2) ans++;
    cout<<ans<<'/n';
    return 0;
}

2C/1A 打字练习 /delete

比较基础的字符串处理题。

观察到前两个测试点没有任何特殊语句,于是考虑 cos 复读机,可以获得 40 pts40 \ pts 。

对于 CapsLock 我们可以遍历字符串中的每一个字符,判断是不是大写字母或小写字母,并对其更改。我们可以开一个布尔变量 flgflg 记录当前 CapsLock 是不是打开状态,稍微特判一下即可。

对于 Delete ,我们可以开一个数组暂时存储下将要输出的字符串,并且开一个变量 toptop 记录栈顶,每次遇到 Delete 时令 toptop 减一。注意实现时不要让 toptop 变成负数。

给出标程:

C++
#include <bits/stdc++.h>
using namespace std;
int n,top;
bool flg;
string a[105],tmp;
string t(string x) {
    for(int i=0;i<x.size();i++) {
        if(x[i]>='a'&&x[i]<='z') x[i]=x[i]-'a'+'A';
        else if(x[i]>='A'&&x[i]<='Z') x[i]=x[i]-'A'+'a';
    }
    return x;
}
int main()
{
    cin>>n;
    for(;n;n--) {
        cin>>tmp;
        if(tmp=="CapsLock") flg=(flg?0:1);
        else if(tmp=="Delete"&&top) top--;
        else a[++top]=(flg?t(tmp);tmp);
    }
    for(int i=1;i<=top;i++) cout<<a[i]<<'/n';
    return 0;
}

2D/1B 马到成功 /explore

有点难度的模拟题。

为了方便可以开一个结构体储存坦克的参数。

没有攻击操作时比较好处理,先贴一段代码(work() 函数负责逐刻处理坦克的行动)。

C++
const int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
int n,t,ans;
struct tank {
	int x,y,at,df,fx,hp,d;
	string mode;
}a[55];
void work(int mo) {
	for(int i=1;i<=n;i++) {
		if(a[i].hp<=0) continue;
		a[i].x+=dx[a[i].d]; a[i].y+=dy[a[i].d];
		if(a[i].mode[mo]=='N') continue;
		if(a[i].mode[mo]=='L') a[i].d=(a[i].d+3)%4;
		if(a[i].mode[mo]=='R') a[i].d=(a[i].d+1)%4;
		if(a[i].mode[mo]=='F') a[i].hp+=a[i].fx;

接下来考虑攻击操作。

题目给出的描述非常坑人,如果直接模拟的话,需要对方向进行可爱的构史大分讨。

考虑一个更简单的方法,我们可以模拟炮弹飞行的过程,每飞行一次就枚举一下有没有打到坦克,处理代码如下:

C++
if(a[i].mode[mo]=='A') {
	int hx=a[i].x,hy=a[i].y;
	bool flg=0;
	while(abs(hx)<=100&&abs(hy)<=100) {
		hx+=dx[a[i].d]; hy+=dy[a[i].d];
		for(int j=1;j<=n;j++)
			if(a[j].x==hx&&a[j].y==hy&&a[j].hp>0) {
				flg=1; a[j].hp-=max(0,a[i].at-a[j].df);
			}
		if(flg) break;
	}
}

综上来看,这道题非常考验代码实现能力,稍有不慎就会出错。

整场 div.2 很考验基础。

给出标程:

C++
#include <bits/stdc++.h>
using namespace std;
const int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
int n,t,ans;
struct tank {
	int x,y,at,df,fx,hp,d;
	string mode;
}a[55];
void work(int mo) {
	for(int i=1;i<=n;i++) {
		if(a[i].hp<=0) continue;
		a[i].x+=dx[a[i].d]; a[i].y+=dy[a[i].d];
		if(a[i].mode[mo]=='N') continue;
		if(a[i].mode[mo]=='L') a[i].d=(a[i].d+3)%4;
		if(a[i].mode[mo]=='R') a[i].d=(a[i].d+1)%4;
		if(a[i].mode[mo]=='F') a[i].hp+=a[i].fx;
		if(a[i].mode[mo]=='A') {
			int hx=a[i].x,hy=a[i].y;
			bool flg=0;
			while(abs(hx)<=100&&abs(hy)<=100) {
				hx+=dx[a[i].d]; hy+=dy[a[i].d];
				for(int j=1;j<=n;j++)
					if(a[j].x==hx&&a[j].y==hy&&a[j].hp>0) {
						flg=1; a[j].hp-=max(0.a[i],at-a[j].df);
					}
				if(flg) break;
			}
		}
	}
}
int main()
{
	cin>>n>>t;
	for(int i=1,x,y,at,df,fx,hp,d;i<=n;i++) {
		string mode;
		cin>>x>>y>>at>>df>>fx>>hp>>d>>mode;
		a[i]={x,y,at,df,fx,hp,d,mode};
	}
	for(int i=0;i<t;i++) work(i);
	cout<<a[1].x<<' '<<a[1].y<<' '<<max(0,a[1].hp)<<'\n';
	for(int i=2;i<=n;i++) if(a[i].hp>0) ans++;
	cout<<ans<<'\n';
	return 0;
}

1C 序列统计 /sequence

简要题意:给定一个 1∼n1 \sim n 的排列 aa,求出其有多少个长度为奇数的子段满足中位数是 kk,1≤k≤n≤2×1051 \le k \le n \le 2 \times 10^5。

引理 1:记 kk 出现的位置为 pospos,合法的子段为 [l,r][l,r],则必然有 1≤l≤pos≤r≤n1 \le l \le pos \le r \le n,证明过程留给读者。

那么我们可以想到一个很显然的做法,枚举每一个满足引理 1 的子段,把它粘下来并排序,检查其中位数是不是 kk,时间复杂度是 O(n3log⁡n)O(n^3 \log n),跑不满,可以拿到 30 pts30 \ pts。