Jeanny
寂兮,寥兮,独立不改,周行而不殆

C.一道非常好的函数入门题

一句话题意:每次改变某一个位置的字符,询问ABC的个数。

#include <bits/stdc++.h>
using namespace std;
char s[200005],c;
int n,m,cnt,p;
int chk(int l){
    if(l >= 1 && l+2 <=n && s[l] == 'A' && s[l+1] == 'B' && s[l+2] == 'C')
        return 1;
    else 
        return 0;
}
int main(){
    cin>>n>>m>>(s+1);
    for(int i = 1; i+2 <= n; i++){
        if(chk(i)) cnt++;
    }
    for(int i = 1; i <= m; i++){
        cin>>p>>c;
        if(chk(p) || chk(p-1) || chk(p-2)) cnt--;
        s[p] = c;
        if(chk(p) || chk(p-1) || chk(p-2)) cnt++;
        cout<<cnt<<endl;
    }
    return 0;
}

 

D.独立想出来的。前缀和+单调栈

在i和j之间没有比a[j]更高的,问每一个位置后序有几个满足这个条件。

5
2 1 4 3 5
3 2 2 1 0
对于 i=1 ,满足条件的j整数2,3,5有三个,4位置不可以的原因是中间有数字4大于3
#include <bits/stdc++.h>
#include <set>
using namespace std;
const int N = 200005;
int h,n,a[N],sum[N],s[N];
stack<int> q;
//降序
//5 3 1 4  
//5 3 1 6
//2
//1 2 3 4
int main(){
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin>>n;
    for(int i = 1; i <= n; i++){
        cin>>a[i];
        while(!q.empty() && a[i] > a[q.top()]) q.pop();
        if(!q.empty()) h = q.top();
        else h = 1;//栈为空 
        if(i != h) s[h]++,s[i]--;  
        q.push(i);  
    }
    for(int i = 1; i <= n; i++){
        sum[i] += sum[i-1] + s[i];
        cout<<sum[i]<<" ";
    }
    return 0;
}
/*
5
% 2 1 4 3 5
*/

 

 

 

posted on 2024-09-22 11:41  Jeanny  阅读(18)  评论(0)    收藏  举报