2025 超新星战队 B 队暑假集训模拟赛 day1
A. LIS
题面
给一个数组
-
设当前索引为
i := L ,答案为1 。 -
对于
i < j \leq R ,选取最小的j ,满足a_j > a_i 。如果找不到j ,则结束程序。 -
让
i := j ,并将答案增加1 。然后跳转到步骤 2。
求
输入格式
第一行包含一个整数
对于每组测试数据:
第一行包含一个整数
接下来一行包含
输出格式
对于每组测试数据,输出一行包含一个整数,表示答案。
数据范围
对于所有数据,保证
思路
先固定
设
设
设
设
实现
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int N = 400000 + 1;
int a[N + 1], stk[N + 1], f[N + 1];
void solve() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
a[n + 1] = 1 << 30;
f[n + 1] = 0;
int top = 1;
stk[top] = n + 1;
for (int i = n; i; --i) {
while (top && a[stk[top]] <= a[i]) {
--top;
}
f[i] = n - i + 1 + f[stk[top]];
stk[++top] = i;
}
cout << accumulate(f + 1, f + n + 1, 0ll) << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
时空复杂度:均为