跳到正文

个人记录

7.28-课堂总结

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

文章信息

发布
2025年7月29日
收录
2025年12月12日
更新
2026年11月17日
篇幅
约 5 分钟
获赞
1

版本

  1. v1当前

课堂总结

B3758 [信息与未来 2021] 括号序列

错误原因:没思路

考察知识点:栈

解题思路:

定义两个正整数top和cnt,定义一个长度为2010的字符数组st,输入一个字符串n,然后遍历字符串,如果n[i]等于’(‘top++,cnt++,st[top]=’(‘否则再判断,如果cnt>0 cnt—,top++,st[top]=’)‘否则top++,st[top]=’(‘,top++,st[top]=’)‘遍历结束后再从1循环到cnt,top++,st[top]=’)‘循环结束后再从1循环到top输出st[i]

正确代码:

C++
#include<bits/stdc++.h>
using namespace std;
int top,cnt;
char st[2010];
int main(){
    string n;
    cin>>n;
    for(int i=0;i<n.size();i++){
        if(n[i]=='('){//是左括号
            top++;
            cnt++;//数量++
            st[top]='(';//进栈
        }
        else {//是右括号
            if(cnt){//有左括号
                cnt--;//数量--
                top++;
                st[top]=')';//进栈
            }
            else{//没有左括号
                top++;
                st[top]='(';//进栈
                top++;
                st[top]=')';//进栈
            }
        }
    }
    for(int i=1;i<=cnt;i++){//还有多余的右括号
        top++;
        st[top]=')';
    }
    for(int i=1;i<=top;i++){//输出
        cout<<st[i];
    }
}

P7505 「Wdsr-2.5」小小的埴轮兵团

错误原因:没思路

考察知识点:栈

解题思路:

定义两个正整数t=1和h=1,定义两个长度为300005的正整数数组s和a,输入三个正整数n,k,m,然后从1循环到n,输入a[i],给a数组排序,然后再从1循环到n,s[t]=a[i],t++,定义一个long long类型的sum=0 然后while(m—){定义一个正整数op,输入op,如果op==1{定义一个正整数x,输入x,sum+=x,while(t>h){如果s[t-1]+sum>k,t—否则break}},如果op==2{定义一个正整数x,输入x,sum-=x,while(h>t){如果s[t-1]+sum<-k,h++否则break}},如果op==3{输出t-h和endl}}

正确代码:

C++
#include<bits/stdc++.h>
using namespace std;
int s[300005],a[300005],h=1,t=1;
int main(){
    int n,k,m;
    cin>>n>>m>>k;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    sort(a+1,a+n+1);
    for(int i=1;i<=n;i++){
        s[t]=a[i];
        t++;
    }
    long long sum=0;
    while(m--){
        int op;
        cin>>op;
        if(op==1){
            int x;
            cin>>x;
            sum+=x;
            while(t>h){
                if(s[t-1]+sum>k)t--;
                else break;
            }
        }
        else if(op==2){
            int x;
            cin>>x;
            sum-=x;
            while(h<t){
                if(s[h]+sum<-k)h++;
                else break;
            }
        }
        else{
            cout<<t-h<<endl;
        }
    }
    return 0;
}

测谎仪

  1. [✔]my老师是大帅哥
  2. [❌]这篇文章是我乱写的
  3. [✔||❌] 6

图片

原文