跳到正文

个人记录

7.31总结

Little_d约 3 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年4月6日
篇幅
约 3 分钟

版本

  1. v1当前

B3667 求区间所有后缀最大值的位置

错误原因

没开 unsigned long long。

AC代码

C++
#include<bits/stdc++.h>
#define int unsigned long long
using namespace std;
int n,k;
int a[1000005];
deque<int>q;
signed main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n>>k;
    for(int i=1;i<=n;i++)cin>>a[i];
    for(int i=1;i<=n;i++){
        if(!q.empty()&&q.front()+k-1<i)q.pop_front();
        while(!q.empty()&&a[i]>a[q.back()])q.pop_back();
        q.push_back(i);
        if(i>=k)cout<<q.size()<<'\n';
    }
    return 0;
}

P8094 [USACO22JAN] Cow Frisbee S

错误原因

数组开小了

AC代码

C++
#include<bits/stdc++.h>
#define int unsigned long long
using namespace std;
int n;
int a[1000005];
stack<int>s;
int ans=0;
signed main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=1;i<=n;i++){
		while(!s.empty()&&a[s.top()]<a[i]){
			ans+=i-s.top()+1;
			s.pop();
		}
		if(!s.empty())ans+=i-s.top()+1;
		s.push(i);
	}
	cout<<ans;
	return 0;
}

P1823 [COI 2007] Patrik 音乐会的等待

错误原因

时间来不及,最后没测样例就交上去了,答案忘记累加了。

AC代码

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
int a[500005];
struct node{
	int val,num;
};
stack<node>s;
int ans;
signed main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=1;i<=n;i++){
		node t={a[i],1};
		while(!s.empty()&&s.top().val<=a[i]){
			ans+=s.top().num;
			if(s.top().val==a[i])t.num+=s.top().num;
			s.pop();
		}
		if(!s.empty())ans++;
		s.push(t);
	}
	cout<<ans;
	return 0;
}
原文