跳到正文

个人记录

4.22周二下午训练 -- 东塘407总结

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

文章信息

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

版本

  1. v1当前

P3817 小A的糖果 && P1055 [NOIP 2008 普及组] ISBN 号码

小A的糖果 错误代码:

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"

int const N=1e5+10;
int n,x,an[N],ans;

signed main(){
	cin>>n>>x;
	for(int i=1;i<=n;i++)	cin>>an[i];
	for(int i=2;i<=n;i++){
		if(an[i-1]+an[i]>x){
			int xx=an[i-1]+an[i]-x;
			if(an[i]-xx>=0)		an[i]-=xx;
			else if(an[i-1]-xx>=0)		an[i-1]-=xx;
			ans+=xx;
		}
	}
	cout<<ans;
	return 0;
}

ISBN 号码 错误代码:

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"

int an[15],t,ans;

signed main(){
	string s;
	cin>>s;
	for(int i=0;i<s.size();i++){
		if(s[i]=='-')	continue;
		an[++t]=s[i]-'0';
	}
	for(int i=1;i<t;i++){
		ans+=an[i]*i;
		ans%=11;
	}
	if(ans==an[t])	cout<<"Right";
	else	cout<<s.substr(0,s.size()-1)<<ans;
	return 0;
}

这两道题太粗心了,第一道题没有处理第一个数;第二道题没判断10的情况,太可惜了!

小A的糖果 正确代码:

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"

int const N=1e5+10;
int n,x,an[N],ans;

signed main(){
	cin>>n>>x;
	for(int i=1;i<=n;i++)	cin>>an[i];
	for(int i=1;i<=n;i++){//一定要处理第一个数
		if(an[i-1]+an[i]>x){
			int xx=an[i-1]+an[i]-x;
			an[i]-=xx;
			ans+=xx;
		}
	}
	cout<<ans;
	return 0;
}

ISBN 号码 正确代码:

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"

int an[15],t,ans;

signed main(){
	string s;
	cin>>s;
	for(int i=0;i<s.size();i++){
		if(s[i]=='-')	continue;
		if(s[i]=='X')	an[++t]=10;
		else	an[++t]=s[i]-'0';
	}
	for(int i=1;i<t;i++){
		ans+=an[i]*i;
		ans%=11;
	}
	char cans;
	if(ans==10)	cans='X';
//判断10的情况 
	else	cans=ans+'0';
	if(cans==s[s.size()-1])	cout<<"Right";
	else	cout<<s.substr(0,s.size()-1)<<cans;
	return 0;
}

这两道题就是太粗心了,下次要更仔细一点。

P2058 [NOIP 2016 普及组] 海港 && P1582 倒水 && P10577 [蓝桥杯 2024 国 A] 兔子集结

主要搞前几道题去了,后面的题不仅时间不够,后面两道题还没看懂。

海港 正确代码:

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"

int const N=1e5+10;
int n,t,k,guo[N],ans;

struct str{
	int s,t;
}h;

queue<str> sp; 

signed main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>t>>k;
		while(sp.size()){//检查有没有船被时代抛弃
			h=sp.front();//拿出船检查
			if(h.t+86400<=t){//船被时代抛弃了
				guo[h.s]--;//船上的人也要减去
				if(guo[h.s]==0)	ans--;//如果这个国的人没有一个在海港了,那国家种类要减一
				sp.pop();//把这艘船踢出海港
				continue;
			}
			break;
		}
		for(int i=1;i<=k;i++){
			int x;
			cin>>x;
			sp.push({x,t});//驶入新的船
			guo[x]++;//把船上的人的国家记下
			if(guo[x]==1)	ans++;//如果有新国家的人,国家种类加一
		}
		cout<<ans<<endl;
	}
	return 0;
}

倒水 正确代码:

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"

int n,k,ans;

int num1(int n){//查找n的二进制中1的数量
	int s=0;
	while(n){
		int a=n%2;
		if(a%2)	s++;
		n/=2;
	}
	return s;
}

int num_one_1(int n){//查找n的二进制中第一个1的位置
	int s=0;
	while(n){
		if(n%2)	return s;
		n/=2;
		s++;
	}
}

signed main(){
	cin>>n>>k;
	while(num1(n)>k){//如果瓶子的数量大于k
		int t=num_one_1(n);
		ans+=(1<<t);//加上新的瓶子
		n+=(1<<t);//更新瓶子的数量
	}
	cout<<ans;
	return 0;
}

兔子集结 正确代码:

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"

int const N=1e5+10;
int n,f[N];

struct str{
	int p,id;
}an[N];

bool cmp1(str a,str b){
	return a.p<b.p;
}

bool cmp2(str a,str b){
	return a.id<b.id;
}

int find(int x){//并查集查找函数 
	if(f[x]==x)	return x;
	return f[x]=find(f[x]);
}

signed main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>an[i].p;
		an[i].id=i;
	}
	sort(an+1,an+n+1,cmp1);//先按位置排一遍序 
	//f[i]表示i往f[i]跳,1号兔子只能往2号跳,n号兔子只能往n-1号跳,其它兔子往离自己距离最小的兔子跳
	f[1]=2,f[n]=n-1;
	for(int i=2;i<n;i++){
		int l=an[i-1].p,m=an[i].p,r=an[i+1].p;
		if(m-l<=r-m)	f[i]=i-1;
		else	f[i]=i+1;
	}
	for(int i=2;i<=n;i++){
		if(f[i-1]==i && f[i]==i-1){//如果相邻两个点成环 
			f[i-1]=i-1,f[i]=i;//f[i]变为自己 
			an[i].p=an[i-1].p=(an[i-1].p+an[i].p)/2;//它们就一定在它俩最中间的位置集结 
		}
	}
	for(int i=1;i<=n;i++){
		//f[i]不是自己的i,它最终会在自己根节点的位置完成集结 
		if(f[i]!=i){
			int root=find(i);
			an[i].p=an[root].p;//注意:root只是自己根节点的编号!!! 
		}
	}
	sort(an+1,an+n+1,cmp2);//最后再按原顺序排回来 
	for(int i=1;i<=n;i++)	cout<<an[i].p<<" ";
	return 0;
}

这几道题反映出我的时间管理不好并且看懂题的能力还不够,以后需要把时间管理好,然后再多多刷题,尽量一下就能看懂题并知道思路。

原文