跳到正文

题解

2025年11月20日 廖老师第九次课

zhangnanzhe150223约 8 分钟
原文
文章信息与版本

文章信息

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

版本

  1. v2当前
  2. v1

题目信息

题目难度一开始的分数结束时的分数
T696513 [ABC432B] Permute to Minimize普及−100100
T696508 [CF2166A] Same Difference普及−0100
T696683 粉兔普及−0100
T684839 植物大战僵尸普及−0100

T1 T696513 [ABC432B] Permute to Minimize

题意分析

给你一个nn,要求将nn重新排列并且使其没有前导零。

思路

  1. 输入nn。
  2. 将nn分解为一个个数(xx)。
  3. 将xx按升序排序。
  4. 重新排列。
  5. 输出结果。

代码

C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int x[10];
signed main()
{
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	int n;
    //1. 输入n。
	cin>>n;
    //2. 将n分解为一个个数(x)。
	string a=to_string(n);
	for(int i=0;i<a.size();i++)x[i+1]=(a[i]-'0');
    //3. 将x按升序排序。
	sort(x+1,x+a.size()+1);
    //4. 重新排列。
	for(int i=a.size();i>=1;i--)
	{
		if(x[i]!=0&&x[i-1]==0&&i-1!=0)swap(x[i],x[i-1]);
	}
    //5. 输出结果。
	for(int i=1;i<=a.size();i++)cout<<x[i];
	return 0;
}

T2 T696508 [CF2166A] Same Difference

题意分析

有tt组测试样例,给你一个长度为nn的字符串ss,要求求出使每个字符相同所需的最少操作次数。

思路

  1. 输入tt。
  2. 输入nn和ss。
  3. 进行计数。
  4. 输出结果。

代码

C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
signed main()
{
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	int t;
    //1. 输入t。
	cin>>t;
	while(t--)
	{
		int n,sum=0;
		string s;
        //2. 输入n和s。
		cin>>n>>s;
        //3. 进行计数。
		for(int i=0;i<n-1;i++)
		{
			if(s[i]!=s[n-1])sum++;
		}
        //4. 输出结果。
		cout<<sum<<endl;
	}
	return 0;
}

T3 T696683 粉兔

题意分析

给你一个n×mn\times m的矩形,矩形中有NN只雄粉兔和MM只雌粉兔,求有多少只粉兔至少有三条逃跑通道。

思路

  1. 输入n,m,Nn,m,N和MM。
  2. 用循环看粉兔的周围有没有异性粉兔。
  3. 进行计数。
  4. 输出合格的粉兔合格的粉兔的数量。

代码

C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
char a[505][505];
signed main()
{
    //1. 输入n,m,N和M。
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	int n,m,N,M,cnt=0;
	cin>>n>>m>>N>>M;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			cin>>a[i][j];
		}
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
          //2. 用循环看粉兔的周围有没有异性粉兔。
			if(a[i][j]=='F')
			{
				int f1=1,f2=1,f3=1,f4=1;
				for(int l=1;l<=j;l++)
				{
					if(a[i][l]=='M')f1=0;
				}
				for(int l=j;l<=m;l++)
				{
					if(a[i][l]=='M')f2=0;
				}
				for(int h=1;h<=i;h++)
				{
					if(a[h][j]=='M')f3=0;
				}
				for(int h=i;h<=n;h++)
				{
					if(a[h][j]=='M')f4=0;
				}
          //3. 进行计数。
				if(f1+f2+f3+f4>=3)cnt++;
			}
			if(a[i][j]=='M')
			{
				int f1=1,f2=1,f3=1,f4=1;
          //2. 用循环看粉兔的周围有没有异性粉兔。
				for(int l=1;l<=j;l++)
				{
					if(a[i][l]=='F')f1=0;
				}
				for(int l=j;l<=m;l++)
				{
					if(a[i][l]=='F')f2=0;
				}
				for(int h=1;h<=i;h++)
				{
					if(a[h][j]=='F')f3=0;
				}
				for(int h=i;h<=n;h++)
				{
					if(a[h][j]=='F')f4=0;
				}
          //3. 进行计数。
				if(f1+f2+f3+f4>=3)cnt++;
			}
		}
	}
    //4. 输出合格的粉兔合格的粉兔的数量。
	cout<<cnt;
	return 0;
}

T4 T684839 植物大战僵尸Permute to Minimize

题意分析

给你nn只zombie,告诉你每只zombie出现的时间以及在出现的行数(可能会同时出现同一位置的僵尸),问至少需要多少坚果才能消灭所有的zombie。

思路

  1. 设st结构体。
  2. 设cmp结构体排序函数。
  3. 输入nn和每只zombie出现的时间以及在出现的行数。
  4. 这一行没扔过坚果保龄球。
  5. 这一行扔过坚果保龄球。
  6. 输出结果。

代码

C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using amespace std;
//1. 设st结构体。
struct st
{
	int hs,sj;
}s[2005];
//2. 设cmp结构体排序函数。
int cmp(st s1,st s2)
{
	if(s1.hs==s2.hs)return s1.sj<s2.sj;
	return s1.hs<s2.hs;
}
int t[10];
signed main()
{
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    //3. 输入n和每只zombie出现的时间以及在出现的行数。
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)cin>>s[i].hs>>s[i].sj;
	sort(s+1,s+n+1,cmp);
	int cnt=0;
	for(int i=1;i<=n;i++)
	{
        //4. 这一行没扔过坚果保龄球。
		if(t[s[i].hs]==0)
		{
			t[s[i].hs]=s[i].sj;
			cnt++;
		}
        //5. 这一行扔过坚果保龄球。
		else
		{
			if(s[i].sj-t[s[i].hs]>=60)
			{
				t[s[i].hs]=s[i].sj;
				cnt++;
			}
		}
	}
    //6. 输出结果。
	cout<<cnt;
	return 0;
}
原文