跳到正文

个人记录

5.29总结

JWM918约 4 分钟
原文
文章信息与版本

文章信息

发布
2026年6月1日
收录
2026年6月1日
更新
2026年11月14日
篇幅
约 4 分钟

版本

  1. v1当前

5.29总结

课堂总结

P16465{\color{Red}{P16465}} [GKS 2013 Practice] Moist

题目传送门(比赛)

考察知识点

  • 模拟
  • 字符串

错误原因

  • WA{\color{Red}{WA}}:错误使用双重循环进行交换比较,导致移动次数计算错误

错误思路

用双重循环进行比较,统计答案

错误代码(0分){\color{Red}{(0分)}}:

错误代码
C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int t,n,ans;
string s[105];
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>t;
	for(int x=1;x<=t;x++){
		ans=0;
		cin>>n;
		for(int i=1;i<=n+1;i++){
			getline(cin,s[i]);
		}
		for(int i=1;i<=n;i++){
			for(int j=i+1;j<=n+1;j++){
				if(s[j]<s[i]){
					swap(s[i],s[j]);
					ans++;
				}
			}
		}
		cout<<"Case #"<<x<<": "<<ans<<endl;
	}
	return 0;
}

正确思路

维护当前已扫描的最大字符串,从第二张牌开始,若当前牌小于最大字符串则花费+1+1,否则更新最大字符串为当前牌

正确代码(10分){\color{Red}{(10分)}}:

正确代码
C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int t,n,ans;
string s,st;
signed main(){
	cin>>t;
	for(int x=1;x<=t;x++){
		ans=0;
		cin>>n;
		getchar();//吸收换行符
		for(int i=1;i<=n;i++){
			getline(cin,s);
			if(i==1)st=s;//第一张牌作为初始最大值
			else{
				if(s<st)ans++;//比当前最大值小:移动(花费+1)
				else st=s;//更新最大值
			}
		}
		cout<<"Case #"<<x<<": "<<ans<<endl;
	}
	return 0;
}

P16220{\color{Orange}{P16220}} [ECUSTPC 2025] 浮点

题目传送门(比赛)

考察知识点

  • 位运算
  • 构造

错误原因

  • 没有解题思路

正确思路

对于每个nn,找到大于nn的最小22的幂作为起始值,然后输出连续的nn个整数。这样构造出的序列的相邻两数的最高二进制位相同,从而满足条件

正确代码(AC){\color{Green}{(AC)}}:

正确代码
C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int t,n;
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>t;
	while(t--){
		cin>>n;
	    int m=n;
		int res=1;
        //求出大于n的最小2的幂
		while(m){
			m/=2;
			res*=2;
		}
        //输出答案
		for(int i=res;i<res+n;i++){
			cout<<i<<" ";
		}
		cout<<endl;
	}
	return 0;
}
原文