跳到正文

个人记录

4.22总结

GeorgeDeng约 15 分钟
原文
文章信息与版本

文章信息

发布
2025年4月26日
收录
2025年12月12日
更新
2026年11月6日
篇幅
约 15 分钟

版本

  1. v1当前

又双叒叕挂分惨烈。

A:小 A 的糖果

赛时代码:

C++
#include <iostream>
using namespace std;

int n,x;
int a[100005];
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>n>>x;
	for(int i = 1;i<=n;i++){
		cin>>a[i];
	}
	int ans = 0;
	for(int i = 2;i<=n;i++){
		if(a[i]+a[i-1]>x){
			ans+=(a[i]+a[i-1]-x);
			a[i]-=a[i]+a[i-1]-x;
		}
	}
	cout<<ans;
	return 0;
}

错误点在应该计算是否需要减少两端的糖果。

正确代码:

C++
#include <iostream>
#define int long long
using namespace std;

int n,x;
int a[100005];
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>n>>x;
	for(int i = 1;i<=n;i++){
		cin>>a[i];
	}
	int ans = 0;
	for(int i = 2;i<=n;i++){
		if(a[i]+a[i-1]>x){
			ans+=(a[i]+a[i-1]-x);
            if(i==2&&a[i]<a[i]+a[i-1]-x){
                a[i-1] = a[i]+a[i-1]-x-a[i];
                a[i] = 0;
            }else a[i]-=a[i]+a[i-1]-x;
		}
	}
	cout<<ans;
	return 0;
}

B:ISBN 号码

赛时代码:

C++
#include <iostream>
using namespace std;

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	string s;
	cin>>s;
	int ans = 0;
	int cnt = 1;
	for(int i = 0;i<s.length()-1;i++){
		if(s[i]!='-'){
			ans+=(s[i]-'0')*cnt;
			cnt++;
		}
	}
	ans%=11;
	int yingai = s[s.length()-1];
	if(yingai=='X'){
		yingai = 10;
	}else{
		yingai-='0';
	}
	if(ans==yingai){
		cout<<"Right";
	}else{
		for(int i = 0;i<s.length()-1;i++){
			cout<<s[i];
		}
		cout<<ans;
	}
	return 0;
}

错误点在最后输出的时候要输出 X,而我没有输出 X。

正确代码:

C++
#include <iostream>
#define int long long
using namespace std;

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	string s;
	cin>>s;
	int ans = 0;
	int cnt = 1;
	for(int i = 0;i<s.length()-1;i++){
		if(s[i]!='-'){
			ans+=(s[i]-'0')*cnt;
			cnt++;
		}
	}
	ans%=11;
	int yingai = s[s.length()-1];
	if(yingai=='X'){
		yingai = 10;
	}else{
		yingai-='0';
	}
	if(ans==yingai){
		cout<<"Right";
	}else{
		for(int i = 0;i<s.length()-1;i++){
			cout<<s[i];
		}
		if(ans!=10) cout<<ans;
        else cout<<"X";
	}
	return 0;
}

C: Superbull S

一道最大生成树问题。

赛时代码:

C++
#include <iostream>
#include <algorithm>
using namespace std;

int n;
int a[2005];
int fa[2005];
struct node{
	int f,t,w;
}edge[4000005];
int find(int x){
	if(fa[x]==x){
		return x;
	}
	return fa[x] = find(fa[x]);
}
void unionn(int x,int y){
	x = find(x),y = find(y);
	if(x!=y) fa[y] = x;
}
bool cmp(node x,node y){
	return x.w>y.w;
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>n;
	for(int i = 1;i<=n;i++){
		cin>>a[i];
	}
	int m = 0;
	for(int i = 1;i<=n;i++){
		for(int j = i+1;j<=n;j++){
			m++;
			edge[m].f = i,edge[m].t = j;
			edge[m].w = a[i]^a[j];
		}
	}
	sort(edge+1,edge+1+m,cmp);
	int ans = 0;
	int cnt = 0;
	for(int i = 1;i<=n;i++) fa[i] = i;
	for(int i = 1;i<=m&&cnt<n-1;i++){
		int tx = find(edge[i].f),ty = find(edge[i].t);
		if(tx==ty) continue;
		ans+=edge[i].w;
		cnt++;
		unionn(tx,ty);
	}
	cout<<ans;
	return 0;
}

错误点在于要开 long long。队伍 ID 最大值是 230−12^{30}-1,与另外一个数异或,会爆 int。没看数据范围,导致错误。

正确代码:

C++
#include <iostream>
#include <algorithm>
#define int long long
using namespace std;

int n;
int a[2005];
int fa[2005];
struct node{
	int f,t,w;
}edge[4000005];
int find(int x){
	if(fa[x]==x){
		return x;
	}
	return fa[x] = find(fa[x]);
}
void unionn(int x,int y){
	x = find(x),y = find(y);
	if(x!=y) fa[y] = x;
}
bool cmp(node x,node y){
	return x.w>y.w;
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>n;
	for(int i = 1;i<=n;i++){
		cin>>a[i];
	}
	int m = 0;
	for(int i = 1;i<=n;i++){
		for(int j = i+1;j<=n;j++){
			m++;
			edge[m].f = i,edge[m].t = j;
			edge[m].w = a[i]^a[j];
		}
	}
	sort(edge+1,edge+1+m,cmp);
	int ans = 0;
	int cnt = 0;
	for(int i = 1;i<=n;i++) fa[i] = i;
	for(int i = 1;i<=m&&cnt<n-1;i++){
		int tx = find(edge[i].f),ty = find(edge[i].t);
		if(tx==ty) continue;
		ans+=edge[i].w;
		cnt++;
		unionn(tx,ty);
	}
	cout<<ans;
	return 0;
}

D: 海港

赛时代码:

C++
#include <iostream>
using namespace std;

int rest[100005];
bool flag[100005];
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	int n;
	cin>>n;
	int cnt = 0;
	int maxn = 0;
	for(int i = 1;i<=n;i++){
		int t,k;
		cin>>t>>k;
		for(int j = 1;j<=k;j++){
			int x;
			cin>>x;
			maxn = max(maxn,x);
			for(int i = 1;i<=maxn;i++){
				if(t-rest[i]+1>86400&&flag[i]) cnt--,flag[i] = false;
			}
			if(!flag[x]) cnt++,flag[x] = true;
			rest[x] = t;
		}
		cout<<cnt<<endl;
	}
	return 0;
}

因为没想出正解,所以写了个暴力拿到 70pts70pts。

正确代码:

C++
#include <iostream>
#include <vector>
#define int long long
using namespace std;

struct node{
	int timee,country;
}q[300005];
int cnt[100005],ans;
int head,tail;
int n;
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cin>>n;
    int ans = 0;
    for(int i = 1;i<=n;i++){
        int t,k;
        cin>>t>>k;
        for(int j = 1;j<=k;j++){
            int x;
            cin>>x;
            q[++tail] = {t,x};
            cnt[x]++;
            if(cnt[x]==1) ans++;
        }
        while(head<tail&&t-q[head].timee>=86400){
			cnt[q[head].country]--;
			if(cnt[q[head].country]==0) ans--;
			head++;
		}
		cout<<ans<<endl;
    }
    return 0;
}

E: 倒水

一道较难结论题。

赛时代码:

(赛时好像蒙了一个结论,错了)

C++
#include <iostream>
#include <cmath>
using namespace std;

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	int n,k;
	cin>>n>>k;
	n-=k;
	int tmp = 0;
	while(pow(k,tmp)<n&&k!=1){
		tmp++;
	}
	if(k==1){
		cout<<n-1;
		return 0;
	}
	cout<<n<<' '<<tmp<<endl;
	cout<<min(n-pow(k,tmp-1),pow(k,tmp)-n);
	return 0;
}

正确代码:

C++
#include <iostream>
#include <cmath>
#define int long long
using namespace std;

int lowcnt(int x){
    int cnt = 0;
    while(x){
        if(x%2==1) cnt++;
        x>>=1;
    }
    return cnt;
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	int n,k;
	cin>>n>>k;
    int cnt = 0;
    int tmp = n;
    while(tmp){
        if(tmp%2==1) cnt++;
        tmp/=2;
    }
    if(n>k&&k>cnt){
        return !(cout<<0);
    }
    if(k>n) return !(cout<<k-n);
    int ans = 0;
    while(lowcnt(n)>k){
        ans+=(n&(0-n));
        n+=(n&(0-n));
    }
    cout<<ans;
	return 0;
}

F: 兔子集结

赛时写了个暴力,没想出正解。

赛时代码:

C++
#include <iostream>
#include <algorithm>
using namespace std;

struct node{
	int pla,id;
	int choz;
};
node p[100005];
int n;
bool cmp(node a,node b){
	return a.pla<b.pla;
}
bool cmp2(node a,node b){
	return a.id<b.id;
}
bool check(){
	for(int i = 1;i<=n;i++){
		if(p[p[i].choz].pla!=p[i].pla) return false;
	}
	return true;
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>n;
	for(int i = 1;i<=n;i++){
		cin>>p[i].pla;
		p[i].id = i;
	}
	sort(p+1,p+1+n,cmp);
	for(int i = 1;i<=n;i++){
		if(i==1){
			p[i].choz = 2;
			continue;
		}else if(i==n){
			p[i].choz = n-1;
			break;
		}
		if(p[i].pla-p[i-1].pla<=p[i+1].pla-p[i].pla){
			p[i].choz = i-1;
		}else{
			p[i].choz = i+1;
		}
	}
	while(!check()){
		for(int i = 1;i<=n;i++){
			if(p[p[i].choz].pla==p[i].pla) continue;
			if(p[p[i].choz].pla-p[i].pla==1&&p[i].choz>i) continue;
			if(p[p[i].choz].pla<p[i].pla) p[i].pla--;
			else p[i].pla++;
		}
	}
	sort(p+1,p+1+n,cmp2);
	for(int i = 1;i<=n;i++){
		cout<<p[i].pla<<' ';
	}
	return 0;
}

剩下的可以戳这里。

TheEnd.The End.
原文