| 编号 | 分数 |
|---|---|
| B | 100 |
| C | |
| D | |
| E | |
| F |
总结
T1
题目中的L和R可以用栈查询的左右侧第一个大于自己的数的下标,所以可以从到枚举套公式即可
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
constexpr ll N = 3e5+5,M = 5*N,K = 5000;
stack<ll> s,ss;
ll n,a[M],ans[M],asn[M];
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
for(ll i = 1;i<=n;i++){//输入和初始化
cin >> a[i];
ans[i] = 0;
asn[i] = n+1;
}
for(ll i = n;i>=1;i--){//右
while(s.size()&&a[i]<a[s.top()]){
ans[s.top()] = i;
s.pop();
}
s.push(i);
}
for(ll i = 1;i<=n;i++){//左
while(ss.size()&&a[i]<a[ss.top()]){
asn[ss.top()] = i;
ss.pop();
}
ss.push(i);
}
ll out = 0;
for(ll i = 1;i<=n;i++){
out = max(out,(asn[i]-ans[i]-1)*a[i]);//公式
}
cout << out;
return 0;
}T2
使用栈来寻找左右侧第一个比自己大的数,再累加,最后统计能量即可
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
constexpr int N=1e6+5,M=5*N,K=5000;
stack<int> sa,sb;
int n,a[N],b[N],k[N];
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
for(int i = 1;i<=n;i++){//输入
cin >> a[i] >> b[i];
}
for(int i = 1;i<=n;i++){//左
while(sa.size()&&a[i]>a[sa.top()]){
k[i]+=b[sa.top()];//累加能量
sa.pop();
}
sa.push(i);
}
for(int i = n;i>=1;i--){//右
while(sb.size()&&a[i]>a[sb.top()]){
k[i]+=b[sb.top()];//累加
sb.pop();
}
sb.push(i);
}
int maxn=0;
for(int i = 1;i<=n;i++){
maxn=max(maxn,k[i]);//最大值
}
cout << maxn;
return 0;
}T3
用单调栈在数列中匹配即可
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
constexpr ll N = 1e5+5,M = 5*N,K = 5000;
stack<ll> s;
ll n,a[M],ans[M];
int main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin >> n;
for(ll i = 1;i<=n;i++){
cin >> a[i];
}
ll out=0;
for(ll i = 1;i<=n;i++){
ans[i]=1;//初始化(放上一个循环里效果不变)
while(s.size()&&a[i]>=a[s.top()]){
out+=ans[s.top()];//增加答案
if(a[i]==a[s.top()]) ans[i]=ans[s.top()]+1;//ans叠加
s.pop();
}
if(s.size()) out++;//累加答案
s.push(i);
}
cout << out;
return 0;
}T4
因为输入数据中的宽度不用考虑,所以只需要考虑高度并计算答案即可 每次计算哪里能贴,哪里要贴即可
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
constexpr int N = 1e5+5,M = 5*N,K = 5000;
stack<int> s;
int n,a[M],ans;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin >> n;
for(int i = 1,x;i<=n;i++){
cin >> x >> a[i];
}
for(int i = 1;i<=n;i++){
while(s.empty()==0&&a[i]<=a[s.top()]){
if(a[i]==a[s.top()])ans--;//因为这个墙的高度和上一个一样,所以减少与后面的ans++抵消
s.pop();
}
ans++;//增加
s.push(i);
}
cout << ans;//输出
return 0;
}T5
可以先删除最小的,再把剩余的位数删除
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
constexpr int N=1e5+5,M=5*N,K=5000;
int n,k;
string num;
int main() {
cin >> n >> k >> num;
stack<int> s;
for(int i = 0;i<num.size();i++) {
while(k>0&&s.size()&&num[i]>num[s.top()]){//栈删除
s.pop();
k--;
}
s.push(i);
}
while(k>0){
s.pop();//删除
k--;
}
string ans;//计算答案
while(s.size()){//累加
ans=ans+num[s.top()];
s.pop();
}
reverse(ans.begin(),ans.end());//翻转答案
cout << ans;
return 0;
}