7.27单调栈测试
100+30+100+100+60=390
T1
Accepted.
思路
模板。
代码
#include<bits/stdc++.h>
#define ll long long
#define N (int)(3e6 + 10)
using namespace std;
stack<int> stk;
int a[N], ans[N];
void work(){
int n;
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
for(int i = 1; i <= n; i++){
while(stk.size() && a[stk.top()] < a[i]){
ans[stk.top()] = i;
stk.pop();
}
stk.push(i);
}
for(int i = 1; i <= n; i++){
cout << ans[i] << " ";
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int oT_To = 1;
// cin >> oT_To;
while(oT_To--){
work();
}
return 0;
}
T2
Wrong Answer 30pts.
思路
比较一个数的大小从高位比起,所以可以使用单调不增栈,多于每个数都删掉栈顶中比它小的数,直到删除次数为0为止,最后输出栈中所有元素。
需要注意:如果最后删除次数没达到k,那么只输出前n-k个数(这个k是最初的k,不是删完后的k)。
死因
没有考虑到没删够的情况,并且存下标的单调栈用了字符类型。
代码
#include<bits/stdc++.h>
#define ll long long
#define N (int)(3e6 + 10)
using namespace std;
stack<int> stk;
stack<char> stk2;
char a[N];
void work(){
int n, k;
cin >> n >> k;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
int cnt = 0;
for(int i = 1; i <= n; i++){
while(stk.size() && a[stk.top()] < a[i]){
cnt++;
if(cnt <= k){
stk.pop();
}else{
break;
}
}
stk.push(i);
}
while(stk.size()){
stk2.push(a[stk.top()]);
stk.pop();
}
cnt = 0;
while(stk2.size()){
cnt++;
if(cnt > n - k){
break;
}
cout << stk2.top();
stk2.pop();
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int oT_To = 1;
// cin >> oT_To;
while(oT_To--){
work();
}
return 0;
}
T3
Accepted.
思路
对于区间去算贡献并不好算,枚举区间还会超时,我们可以换个方式,对于每个最小值去算贡献。使用单调栈找出每个数左边和右边第一个比他小的数的下标,用l[i]和r[i]来表示,在l[i]+1到r[i]-1这个区间内,这个数一定是最小的,所以这个数的贡献就是(r[i]-1-l[i]-1)*a[i],打擂台记录最大值即可。
需要注意:l[i]应初始化为0,r[i]应初始化为n+1。
代码
#include<bits/stdc++.h>
#define ll long long
#define N (int)(3e5 + 10)
using namespace std;
stack<int> stk;
ll a[N], l[N], r[N];
void work(){
int n;
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
l[i] = 1, r[i] = n;
}
for(int i = 1; i <= n; i++){
while(stk.size() && a[stk.top()] > a[i]){
r[stk.top()] = i - 1;
stk.pop();
}
stk.push(i);
}
for(int i = n; i >= 1; i--){
while(stk.size() && a[stk.top()] > a[i]){
l[stk.top()] = i + 1;
stk.pop();
}
stk.push(i);
}
ll ans = 0;
for(int i = 1; i <= n; i++){
ans = max(ans, (r[i] - l[i] + 1) * a[i]);
}
cout << ans;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int oT_To = 1;
// cin >> oT_To;
while(oT_To--){
work();
}
return 0;
}
T4
Accepted.
思路
找出每个数左边和右边第一个小于它的数(这一步与上一题一样),长度小于等于r[i]-1-l[i]-1的区间强度至多是a[i],但是暴力更新所有长度会TLE,于是智商不够线段树来凑我们可以使用线段树来维护不同长度区间强度的最小值。
代码
#include<bits/stdc++.h>
#define ll long long
#define N (int)(2e5 + 10)
using namespace std;
stack<int> stk;
int a[N], l[N], r[N];
int tr[N * 4], lt[N * 4];
void pushup(int u){
tr[u] = max(tr[u * 2], tr[u * 2 + 1]);
}
void pushdown(int u){
int v1 = u * 2, v2 = u * 2 + 1;
tr[v1] = max(tr[v1], lt[u]);
tr[v2] = max(tr[v2], lt[u]);
lt[v1] = max(lt[v1], lt[u]);
lt[v2] = max(lt[v2], lt[u]);
lt[u] = 0;
}
void update(int l, int r, int s, int t, int u, int k){
if(l <= s && r >= t){
tr[u] = max(tr[u], k);
lt[u] = max(lt[u], k);
return;
}
int mid = (s + t) / 2;
if(lt[u]){
pushdown(u);
}
if(l <= mid){
update(l, r, s, mid, u * 2, k);
}
if(r > mid){
update(l, r, mid + 1, t, u * 2 + 1, k);
}
pushup(u);
}
int getmx(int id, int s, int t, int u){
if(s == id && s == t){
return tr[u];
}
int mid = (s + t) / 2;
if(lt[u]){
pushdown(u);
}
if(id <= mid){
return getmx(id, s, mid, u * 2);
}
return getmx(id, mid + 1, t, u * 2 + 1);
}
void work(){
int n;
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
l[i] = 1, r[i] = n;
}
for(int i = 1; i <= n; i++){
while(stk.size() && a[stk.top()] > a[i]){
r[stk.top()] = i - 1;
stk.pop();
}
stk.push(i);
}
while(stk.size()){
stk.pop();
}
for(int i = n; i >= 1; i--){
while(stk.size() && a[stk.top()] > a[i]){
l[stk.top()] = i + 1;
stk.pop();
}
stk.push(i);
}
for(int i = 1; i <= n; i++){
int len = r[i] - l[i] + 1;
update(1, len, 1, n, 1, a[i]);
}
for(int i = 1; i <= n; i++){
cout << getmx(i, 1, n, 1) << " ";
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int oT_To = 1;
// cin >> oT_To;
while(oT_To--){
work();
}
return 0;
}
T5
Time Limit Exceeded 60pts.
可恶的捆绑!
思路
对于数组中的第i个数为左端点的区间,其右端点只能在它右边第一个比他小的数的左边。用单调栈找出,并用数组 rmn 存储每个数右边第一个比他小的数的位置。由于每个区间的右端点必须要比左端点的数小,所以我们可以用一个 rmx 数组,用于记录右边第一个比它大的数的位置(也使用单调栈找出那个数)。同时,为了满足题目中分割的区间尽可能少的要求,每个区间应尽可能的大。为了达成这个条件,在求以 l 为左端点区间的右端点 r 时,可以从 l 开始,让 r=l,每次跳到下一个离它最近,但比他大的数的位置上 rmx[r],直到跳出 rmn[l] 时停止。在求下一个区间时,我们只需要让 l=r+1,r=l,并重复上面的操作即可。
死因
写了个偏暴力,TLE了。
代码
#include<bits/stdc++.h>
#define ll long long
#define N (int)(3e5 + 10)
using namespace std;
stack<int> stk;
int a[N], rmn[N], rmx[N];
void work(){
int n;
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
rmn[i] = n + 1, rmx[i] = n + 1;
}
for(int i = 1; i <= n; i++){
while(stk.size() && a[stk.top()] > a[i]){
rmn[stk.top()] = i;
stk.pop();
}
stk.push(i);
}
while(stk.size()){
stk.pop();
}
for(int i = 1; i <= n; i++){
while(stk.size() && a[stk.top()] <= a[i]){
rmx[stk.top()] = i;
stk.pop();
}
stk.push(i);
}
int ans = 0, l = 1, r = 1;
while(l <= n){
while(r <= n){
int nxt = rmx[r];
if(nxt < rmn[l]){
r = nxt;
}else{
break;
}
}
l = r + 1;
r = l;
ans++;
}
cout << ans;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int oT_To = 1;
// cin >> oT_To;
while(oT_To--){
work();
}
return 0;
}