跳到正文

个人记录

8月7日数据结构改错

Hu_Bin_h_101约 5 分钟
原文
文章信息与版本

文章信息

收录
2026年8月9日
更新
2026年8月10日
篇幅
约 5 分钟

版本

  1. v1当前

T3

题目大意

有 N 辆遥控车,K 辆损坏,求在 1~N 中,选一段长度 B 的连续车,最少要修好其中多少辆坏车

解题思路

这道题用前缀和算法,读入每一辆坏车的编号t,设置a[t]=1做标记,枚举所有长度为 b 的连续区间,最后求它的最小值

AC代码

C++
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int a[N], pre[N];
int main() {
	//freopen("car.in","r",stdin);
	//freopen("car.out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int n, b, k;
    cin >> n >> b >> k;
    for (int i = 0; i < k; i++) {
        int t;
        cin >> t;
        a[t] = 1;
    }
    for (int i = 1; i <= n; i++) {
        pre[i] = pre[i - 1] + a[i];
    }
    int mi = k + 1;
    for (int i = 1; i + b - 1 <= n; i++) {
        int sum = pre[i + b - 1] - pre[i - 1];
        mi = min(mi, sum);
    }
    cout << mi;
    return 0;
}

错误原因

暴力枚举导致超时TLE

T4

题目大意

n 个工具箱,可跨箱任意交换零件,判断能否让所有工具箱零件完全一样。每种零件的总数量必须能被 n 整除,能则输出 YES,否则 NO

解题思路

AC代码

C++
#include<bits/stdc++.h>
using namespace std;
int t;
int main(){
	ios::sync_with_stdio(0),cin.tie(0);
	cin>>t;
	int n;
	while(t--){
		cin>>n;
		map<char,int> mp;
		for(int i=1;i<=n;i++){
			string s;
			cin>>s;
			for(int i=0;i < s.size();i++){
				mp[s[i]]++;
			}
		}
		bool fl = 1;
		for(auto it : mp){
			if(it.second % n == 0){
				continue;
			}
			else{
				fl = 0;
			}
		}
		if(fl == 1){
			cout<<"YES"<<endl;
		}else{
			cout<<"NO"<<endl;
		}
	}
	return 0;
}

错误原因

直接输出的样例骗了10分

T6

题目大意

输入一个字符串,按照每个字符或数字代表的意思并执行并记录最后输出执行后的总数

解题思路

用while输入字符串,并用一个长整型数组记录每次操作后的数字,最后输出执行后获得的数字

AC代码

C++
#include <bits/stdc++.h>
using namespace std;
const int N=1005;
long long st[N];
int top=0;
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    string s;
    while (cin >> s) {
        if (s == "C") {
            if(top>0) top--;
        }else if (s == "D") {
            if(top>0){
                st[top]=st[top-1]*2;
                top++;
            }
        }else if (s == "+") {
            if(top>=2){
                long long a=st[top-1];
                long long b=st[top-2];
                st[top]=a+b;
                top++;
            }
        }else{
            st[top]=stoll(s);
            top++;
        }
    }
    long long ans=0;
    for(int i=0;i<top;i++){
        ans+=st[i];
    }
    cout<<ans<<endl;
    return 0;
}

错误原因

考试时马虎大意而导致对题目要求理解模糊

原文