【解题报告】洛谷P1410 子序列
题目链接
https://www.luogu.com.cn/problem/P1410
思路
我们考虑动态规划
设 f[i] 表示到 i 的最长的递减的长度
发现,如果 f[i] 大于 2 的话,这个序列就不成立
反之,这个序列成立
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <string> using namespace std; int n; int main() { while(cin>>n) { int a[2005],f[2005]; bool flag=false; for(int i=0;i<n;i++) { cin>>a[i]; f[i]=1; for(int j=i-1;j>=0;j--) { if(a[i]<a[j]) f[i]=max(f[i],f[j]+1); } if(f[i]>2) flag=true; } if(flag) cout<<"No!"<<'\n'; else cout<<"Yes!"<<'\n'; } return 0; }
|