T1:
没什么好说的,按照题意模拟即可
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
int t, n, sum[200005];
string s;
int main(){
cin >> t;
while(t--){
cin >> n >> s;
s = " " + s;
for(int i = 1; i <= n; i++){
if(s[i] == '(') sum[i] = 1;
else sum[i] = -1;
}
int flag = 1;
for(int i = 1; i <= n; i++)
sum[i] += sum[i - 1];
if(sum[n]){
cout << "No\n";
continue;
}
for(int i = 1; i <= n; i++){
if(sum[i] == -1 && sum[i + 1] == 0 && flag){
flag--;
sum[i] = 1;
}
}
bool p = 1;
for(int i = 1; i <= n; i++)
if(sum[i] < 0) p = 0;
if(!p) cout << "No\n";
else cout << "Yes\n";
}
return 0;
}T2:
很典,但是还是学到了新东西
对于第一问, 很显然,直接对原序列做一个 最长不降子序列 即可。
第二问就有一些比较巧妙地转换了,大部分人会直接告诉你这要求 最长上升子序列 就行,但我们需要加以严谨的证明。
我们转换一下题意,它是问我们最少需要多少条 不降子序列 才能覆盖整个区间,我们设 最长上升子序列 的长度为 ,答案为 。
1.我们可以发现,因为每一个系统只能拦截一条 不降子序列 所以对于 最长上升子序列 中的每一个点, 都需要给它单独开一个系统, 所以 。
2.对于每一个单独的系统, 由于每一套拦截的都是一条 不降子序列 所以在每一条系统内都可以选择出一点, 由此构造出一条 上升子序列 , 而其又不能大于 最长上升子序列 的长度, 所以 。
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, maxn, a[N], dp[N];
struct bit{
int c[N];
void clear(){
memset(c, 0, sizeof c);
}
void update(int x, int i){
for(; i <= (int)5e4; i += i & -i) c[i] = max(c[i], x);
}
int query(int i){
int res = 0;
for(; i; i -= i & -i) res = max(res, c[i]);
return res;
}
} tr;
int main(){
cin.tie(0)->sync_with_stdio(0);
while(cin >> a[++n]);
n--;
for(int i = 1; i <= n; i++) dp[i] = 1;
for(int i = n; i >= 1; i--){
dp[i] = tr.query(a[i]) + 1;
tr.update(dp[i], a[i]);
}//倒着做最长不降子序列
maxn = 0;
for(int i = 1; i <= n; i++) maxn = max(maxn, dp[i]);
cout << maxn << '\n';
tr.clear();
for(int i = 1; i <= n; i++) dp[i] = 1;
for(int i = 1; i <= n; i++){
dp[i] = tr.query(a[i] - 1) + 1;
tr.update(dp[i], a[i]);
}//正着着做最长上升子序列
maxn = 0;
for(int i = 1; i <= n; i++) maxn = max(maxn, dp[i]);
cout << maxn << '\n';
return 0;
}T3:
我们要找到长度为 时, 每个长度为 的子段中的最小值的最大值。
对于这种题,我们应该多转换视角, 不要让区间去找最小值, 而是看最小值可以找那些区间
对于每个最小值,很显然如果在 长度为 的区间内是最小值, 则在长度为 的区间内都可以作为区间最小值来竞争最大值。所以我们可以维护出,每个点 最长能成为最小值的区间长度 ,显然可以使用单调栈处理出每个点的左右最大覆盖范围 和 , 则 ,则每个点可以尝试去更新 ,且对于每一个 都可以去更新 的答案,倒叙处理即可
#include<bits/stdc++.h>
#define pii pair<int, int>
using namespace std;
const int N = 2e6 + 5;
int n, top, a[N], stk[N], l[N], r[N], len[N], ans[N];
pii b[N];
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
sort(b + 1, b + 1 + n, greater<pii>());
for(int i = 1; i <= n; i++){
while(top && a[stk[top]] > a[i]){
r[stk[top]] = i - 1;
top--;
}
stk[++top] = i;
}
for(int i = 1; i <= n; i++) if(!r[i]) r[i] = n;
while(top) top--;
for(int i = n; i >= 1; i--){
while(top && a[stk[top]] > a[i]){
l[stk[top]] = i + 1;
top--;
}
stk[++top] = i;
}
for(int i = 1; i <= n; i++) if(!l[i]) l[i] = 1;
for(int i = 1; i <= n; i++) len[i] = r[i] - l[i] + 1;
for(int i = 1; i <= n; i++) ans[len[i]] = max(ans[len[i]], a[i]);
for(int i = n; i >= 1; i--) ans[i] = max(ans[i], ans[i + 1]);
for(int i = 1; i <= n; i++) cout << ans[i] << ' ';
return 0;
}T4:
可以直接把栈横过来,就会发现这可以看作一个区间,线段树维护即可,但太慢了,我们要追求更极致的速度
我们发现暴力慢是因为 和 太多了,所以考虑分类讨论
- ,则后面的没有意义,单独处理
- ,毫无意义,只能用来占位
- ,暴力维护,直接查询,最多查询 次
对于 和 都分别开一个栈来维护即可,注意记录每个数在栈中的位置
注意边界
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 1e5 + 5, inf = 1ll << 32;
struct node{
int val, pos;
} stk[N];
int zero[N], cnt, top, tmp, q;
signed main(){
cin.tie(0)->sync_with_stdio(0);
cin >> q;
for(int i = 1, op, x; i <= q; i++){
cin >> op;
if(op == 1){
cin >> x;
top++;
if(x == 0){
zero[++tmp] = top;
}else if(x > 1){
cnt++;
stk[cnt].val = x, stk[cnt].pos = top;
}
}else if(op == 2){
if(!top) continue;
if(top == zero[tmp])
tmp--;
else if(top == stk[cnt].pos)
cnt--;
top--;
}else{
cin >> x;
if(x > top){
cout << "ERROR";
}else{
if(tmp && top - zero[tmp] + 1 <= x){
cout << 0;
}else{
int ans = 1, p = 0;
while(top - stk[cnt - p].pos + 1 <= x){
ans *= stk[cnt - p].val;
if(ans >= inf) break;
p++;
}
if(ans >= inf) cout << "OVERFLOW";
else cout << ans;
}
}
cout << endl;
}
}
return 0;
}