最长上升子序列
Almost Sorted Array
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 7686 Accepted Submission(s): 1800
We say an array is sorted if its elements are in non-decreasing order or non-increasing order. We say an array is almost sorted if we can remove exactly one element from it, and the remaining array is sorted. Now you are given an array a1,a2,…,an, is it almost sorted?
1≤T≤2000
2≤n≤105
1≤ai≤105
There are at most 20 test cases with n>1000.
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstdlib> 5 #include <cstring> 6 #include <string> 7 #include <deque> 8 #include <map> 9 #include <vector> 10 using namespace std; 11 #define ll long long 12 #define N 100009 13 #define M 1000000000 14 #define gep(i,a,b) for(int i=a;i<=b;i++) 15 #define gepp(i,a,b) for(int i=a;i>=b;i--) 16 #define gep1(i,a,b) for(ll i=a;i<=b;i++) 17 #define gepp1(i,a,b) for(ll i=a;i>=b;i--) 18 #define mem(a,b) memset(a,b,sizeof(a)) 19 #define ph push_back 20 int t,n; 21 int a[N],dp[N]; 22 int main() 23 { 24 scanf("%d",&t); 25 while(t--) 26 { 27 scanf("%d",&n); 28 gep(i,1,n) scanf("%d",&a[i]); 29 int l1=1,l2=1l; 30 dp[l1]=a[1]; 31 gep(i,2,n){ 32 if(a[i]>=dp[l1]){ 33 l1++; 34 dp[l1]=a[i]; 35 } 36 else{ 37 int pos=upper_bound(dp+1,dp+1+l1,a[i])-dp; 38 dp[pos]=a[i]; 39 } 40 } 41 dp[l2]=a[n]; 42 gepp(i,n-1,1){ 43 if(a[i]>=dp[l2]){ 44 l2++; 45 dp[l2]=a[i]; 46 } 47 else{ 48 int pos=upper_bound(dp+1,dp+1+l2,a[i])-dp; 49 dp[pos]=a[i]; 50 } 51 } 52 if(l1>=n-1||l2>=n-1){ 53 printf("YES\n"); 54 } 55 else{ 56 printf("NO\n"); 57 } 58 } 59 return 0; 60 }
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstdlib> 5 #include <cstring> 6 #include <string> 7 #include <deque> 8 #include <map> 9 #include <vector> 10 #include <stack> 11 using namespace std; 12 #define ll long long 13 #define N 100009 14 #define M 1000000000 15 #define gep(i,a,b) for(int i=a;i<=b;i++) 16 #define gepp(i,a,b) for(int i=a;i>=b;i--) 17 #define gep1(i,a,b) for(ll i=a;i<=b;i++) 18 #define gepp1(i,a,b) for(ll i=a;i>=b;i--) 19 #define mem(a,b) memset(a,b,sizeof(a)) 20 #define ph push_back 21 int n,a[N],dp[N]; 22 int pre[N]; 23 stack<int>s; 24 vector<int>ve; 25 int main() 26 { 27 scanf("%d",&n); 28 gep(i,1,n) scanf("%d",&a[i]); 29 gep(i,1,n) dp[i]=1; 30 mem(pre,-1); 31 //递增 32 gep(i,2,n){ 33 gep(j,1,i-1){ 34 if(a[i]>a[j]&&dp[i]<dp[j]+1){ 35 dp[i]=dp[j]+1;//1~i 36 pre[i]=j; 37 } 38 } 39 } 40 while(j!=-1){ 41 s.push(a[j]); 42 j=pre[j]; 43 } 44 int v=s.top(); 45 s.pop(); 46 printf("%d",v); 47 while(!s.empty()){ 48 int v=s.top(); 49 s.pop(); 50 printf(" %d",v); 51 } 52 printf("\n"); 53 /* 54 5 55 6 3 5 2 9 56 3 5 9 57 */ 58 59 //递减 60 gepp(i,n-1,1){ 61 gep(j,i+1,n){ 62 if(a[i]>a[j]&&dp[i]<dp[j]+1){ 63 dp[i]=dp[j]+1;//i~n 64 pre[i]=j; 65 } 66 } 67 } 68 int MAX=0,j=1; 69 gep(i,1,n){ 70 if(MAX<dp[i]){ 71 MAX=dp[i]; 72 j=i; 73 } 74 } 75 while(j!=-1){ 76 ve.ph(a[j]); 77 j=pre[j]; 78 } 79 gep(i,0,ve.size()-1){ 80 printf("%d%c",ve[i],i==ve.size()-1?'\n':' '); 81 } 82 /* 83 5 84 5 1 3 2 6 85 5 3 2 86 */ 87 88 return 0; 89 }
//ACM训练联盟周赛
Teemo starts to do homework everyday. Today, he meets a hard problem when doing his homework.
There's an array A which contains n integers(for every 1<=i<=n, A[i] = 1 or A[i]= 2), you can choose an interval [l,r](1<=l<=r<=n), then reverse it so that the length of the longest non-decreasing subsequence of the new sequence is maximum.
Input Format
- The first line of the input contains an integer T(1=<T<=10), giving the number of test cases.
- For every test case, the first line contains an integer n(1<=n<=2000). The second line contains n integers. The i th integer represents A[i](1<=A[i]<=2).
Output Format
Print a single integer, which means the maximum possible length of the longest non-decreasing subsequence of the new sequence.
样例输入
1 4 1 2 1 2
样例输出
4
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstdlib> 5 #include <cstring> 6 #include <string> 7 #include <deque> 8 #include <map> 9 #include <vector> 10 #include <stack> 11 using namespace std; 12 #define ll long long 13 #define N 2009 14 #define M 1000000000 15 #define gep(i,a,b) for(int i=a;i<=b;i++) 16 #define gepp(i,a,b) for(int i=a;i>=b;i--) 17 #define gep1(i,a,b) for(ll i=a;i<=b;i++) 18 #define gepp1(i,a,b) for(ll i=a;i>=b;i--) 19 #define mem(a,b) memset(a,b,sizeof(a)) 20 #define ph push_back 21 int t,n,dpx[N][N],dpy[N][N]; 22 int dp[N],a[N]; 23 int l; 24 int main() 25 { 26 scanf("%d",&t); 27 28 while(t--) 29 { 30 scanf("%d",&n); 31 gep(i,1,n) scanf("%d",&a[i]); 32 mem(dpx,0);mem(dpy,0); 33 gep(i,1,n){ 34 l=0; 35 mem(dp,0); 36 gep(j,i,n){ 37 if(a[j]>=dp[l]){ 38 l++; 39 dp[l]=a[j]; 40 } 41 else{ 42 int pos=upper_bound(dp+1,dp+1+l,a[j])-dp; 43 dp[pos]=a[j]; 44 } 45 dpx[i][j]=l;//i~j的最长上升子序列 46 } 47 } 48 gepp(i,n,1){ 49 l=0; 50 mem(dp,0); 51 gepp(j,i,1){ 52 if(a[j]>=dp[l]){ 53 l++; 54 dp[l]=a[j]; 55 } 56 else{ 57 int pos=upper_bound(dp+1,dp+1+l,a[j])-dp; 58 dp[pos]=a[j]; 59 } 60 dpy[j][i]=l;//j~i的最长下降子序列 61 } 62 } 63 int MAX=0; 64 gep(i,1,n){ 65 gep(j,1,n){ 66 MAX=max(MAX,dpx[1][n]-dpx[i][j]+dpy[i][j]);//reverse 67 } 68 } 69 printf("%d\n",MAX); 70 } 71 return 0; 72 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现