B3667 求区间所有后缀最大值的位置
错误原因
没开 unsigned long long。
AC代码
#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代码
#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代码
#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;
}