河南萌新联赛2025第(一)场:河南工业大学——M.无聊的子序列 · znzryb
## 思路讲解
**给定 r 和 s,任何长度至少为(r − 1)(s − 1) + 1 的互异实数序列都包含长度为 r 的单调递增子序列或长度为 s 的单调递减子序列。**
$L=(r-1)*(s-1)+1 =2*2+1=5$
**我们来分解一下这个定义:**
- **子序列 (Subsequence)**:从原序列中,按照原来的顺序,拿掉零个或多个元素后,剩下的元素组成的序列。例如,在序列 {3, **1**, **4**, 5, **2**} 中,{1, 4, 2} 就是一个子序列。
- **单调递增 (Monotonically Increasing)**:序列中的每个数都比它前面的数要大。例如 {2, 5, 8, 10}。
- **单调递减 (Monotonically Decreasing)**:序列中的每个数都比它前面的数要小。例如 {9, 6, 3, 1}。
所以,这个定理告诉你,只要你的序列足够长,你就一定能从中找到一个特定长度的、要么是持续上升、要么是持续下降的子序列。
## AC代码
[https://ac.nowcoder.com/acm/contest/view-submission?submissionId=78240193](https://ac.nowcoder.com/acm/contest/view-submission?submissionId=78240193)
::: 源代码
```cpp
// Problem: 无聊的子序列
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/113664/M
// Memory Limit: 512 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
#define all(vec) vec.begin(),vec.end()
#define pb push_back
#define SZ(a) ((int) a.size())
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define debug(var) cerr << #var <<":"<<var<<"\n";
#define lson(var) (var<<1)
#define rson(var) ((var<<1)+1)
using namespace std;
using ll = long long;using ull = unsigned long long;
using DB=double;using LD=long double;
// using i128 = __int128;
using pdd = pair<DB,DB>;using plb = pair<ll,bool>;
using pll = pair<ll,ll>;using vll=vector<ll>;
using arr3 = array<ll,3> ;using arr2 = array<ll,2>;
constexpr ll MAXN=static_cast<ll>(1e6)+10,INF=static_cast<ll>(1e17)+9; // 1e18+9也是素数
constexpr ll MAXM=(ll)1e6+10;constexpr ll MAXV=(ll)1e6+10;
constexpr ll mod=static_cast<ll>(1e9)+7;
constexpr double eps=1e-8;const double pi=acos(-1.0);
ll N,M,Q,X,K,T,lT,A[MAXN];
/*
*/
ll get3(ll x){
if(x+2>N) return 0;
if(A[x]<= A[x+1] && A[x+1]<=A[x+2])return 0;
if(A[x]>=A[x+1] && A[x+1]>=A[x+2]) return 0;
return 1;
}
ll get4(ll x){
if(x+3>N) return 0;
if(A[x]<= A[x+1] && A[x+1]<=A[x+2])return 0;
if(A[x]>=A[x+1] && A[x+1]>=A[x+2]) return 0;
if(A[x]<= A[x+1] && A[x+1]<=A[x+3])return 0;
if(A[x]>= A[x+1] && A[x+1]>=A[x+3])return 0;
if(A[x]>=A[x+2] && A[x+2]>=A[x+3]) return 0;
if(A[x]<=A[x+2] && A[x+2]<=A[x+3]) return 0;
if(A[x+1]>=A[x+2] && A[x+2]>=A[x+3]) return 0;
if(A[x+1]<=A[x+2] && A[x+2]<=A[x+3]) return 0;
return 1;
}
inline void __(){
cin>>N;
FOR(i,1,N){
cin>>A[i];
}
ll ans=N+N-1;
FOR(i,1,N){
ans+=get3(i)+get4(i);
}
cout<<ans<<"\n";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
// cin>>lT;
// while(lT--)
__();
return 0;
}
/*
*/
```
:::
## 心路历程(WA,TLE,MLE……)