#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;
}
错误原因
暴力枚举导致超时
第四题
题目大意
判断t组数据中每n个字符串中所有单个字符是否能整除n
解题思路
输入t组数据,每次输入n个字符串,判断字符串中说有单个字符出现的数量是否能整除n,输出Yes或No
AC代码
#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;
}
错误原因
考试的时候没想到可以这样写
第六题
题目大意
输入一个字符串,根据每个字符或数字代表的意思并执行最后输出执行后的总数
解题思路
用while输入字符串,并用一个长整型数组记录每次操作后的数字,最后输出
AC代码
#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;
}