标签:code data nas 1.5 desc containe inline decide creating
We are all familiar with sorting algorithms: quick sort, merge sort, heap sort, insertion sort, selection sort, bubble sort, etc. But sometimes it is an overkill to use these algorithms for an almost sorted array.
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
3 3 2 1 7 3 3 2 1 5 3 1 4 1 5Sample Output
YES YES NO
判断数列是否几乎非递增或非递减,“几乎”就是删掉一个数字后变成了非递增或非递减。
思路判断数列是最大增数列或最大减数列是否大于等于len-1就行,增数列和减数列可以有连续相等的。
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <algorithm> 5 using namespace std; 6 const int N = 1e5+10; 7 const int Inf = 1<<30; 8 int a[N], dp[N]; 9 int t, n; 10 int main(){ 11 scanf("%d",&t); 12 while(t--) { 13 scanf("%d",&n); 14 for(int i = 0; i < n; i ++){ 15 scanf("%d",&a[i]); 16 dp[i] = Inf; 17 } 18 for(int i = 0; i < n; i ++) { 19 *upper_bound(dp,dp+n,a[i]) = a[i]; 20 } 21 int ans = upper_bound(dp,dp+n,Inf-1) - dp; 22 for(int i = 0; i < n; i ++) dp[i] = Inf; 23 for(int i = 0; i < n/2; i ++){ 24 swap(a[i],a[n-i-1]); 25 } 26 for(int i = 0; i < n; i ++) { 27 *upper_bound(dp,dp+n,a[i]) = a[i]; 28 } 29 int ans1 = upper_bound(dp,dp+n,Inf-1) - dp; 30 if(ans >= n-1 || ans1 >= n-1) cout << "YES\n"; 31 else cout << "NO\n"; 32 memset(a,0,sizeof(a)); 33 } 34 return 0; 35 }
1 5 1 4 2 5 -12 4 -12 1 2 4Sample Output
2
求最大公共增数列。
就是最长递增子序列和最长公共子序列的结合,dp判断下就性。
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 using namespace std; 5 int a[550], b[550], dp[550][550]; 6 int main() { 7 int t; 8 scanf("%d",&t); 9 while(t--) { 10 int n, m; 11 scanf("%d",&n); 12 for(int i = 0; i < n; i ++) scanf("%d",&a[i]); 13 scanf("%d",&m); 14 for(int i = 0; i < m; i ++) scanf("%d",&b[i]); 15 for(int i = 0; i < n; i ++) { 16 for(int j = 0; j < m; j ++) { 17 if(a[i] == b[j]) dp[i+1][j+1] = dp[i][j] + (b[j] > b[j-1]); 18 else dp[i+1][j+1] = max(dp[i][j+1],dp[i+1][j]); 19 } 20 } 21 cout << dp[n][m] << endl; 22 if(t)printf("\n"); 23 memset(dp,0,sizeof(dp)); 24 memset(a,0,sizeof(a)); 25 memset(b,0,sizeof(b)); 26 } 27 return 0; 28 }
apple peach ananas banana pear peachSample Output
appleach bananas pearch
求一长度最小的字符串,要包含输入的两个字符串。
最长公共子序列,不过要标记下。
1 // #include <iostream> 2 // #include <stdio.h> 3 // #include <string.h> 4 // using namespace std; 5 // char str1[110], str2[110], dp[110][110]; 6 // int main(){ 7 // while(scanf("%s%s",str1,str2)!=EOF) { 8 // int len1 = strlen(str1), len2 = strlen(str2); 9 // for(int i = 0; i < len1; i ++) { 10 // for(int j = 0; j < len2; j ++) { 11 // if(str1[i] == str2[j]) dp[i+1][j+1] = dp[i][j]+1; 12 // else dp[i+1][j+1] = max(dp[i+1][j],dp[i][j+1]); 13 // } 14 // } 15 // int ans1 = 0, ans2 = 0; 16 // for(int i = 1; i <= len2; i ++){ 17 // if(dp[len1][i] == i) ans2 = i; 18 // } 19 // for(int i = 1; i <= len1; i ++){ 20 // if(dp[i][len2] == i) ans1 = i; 21 // } 22 // if(ans1 > ans2){ 23 // printf("%s%s\n",str2,str1+ans1); 24 // }else printf("%s%s\n",str1,str2+ans2); 25 // memset(str2,0,sizeof(str2)); 26 // memset(str1,0,sizeof(str1)); 27 // memset(dp,0,sizeof(dp)); 28 // } 29 // return 0; 30 // } 31 32 33 34 #include <iostream> 35 #include <stdio.h> 36 #include <string.h> 37 using namespace std; 38 const int N = 110; 39 int dp[N][N], mark[N][N], len1, len2; 40 char str1[N], str2[N]; 41 42 void LCS(){ 43 memset(dp,0,sizeof(dp)); 44 for(int i = 0; i <= len1; i ++) 45 mark[i][0] = 1; 46 for(int i = 0; i <= len2; i ++) 47 mark[0][i] = -1; 48 for(int i = 1; i <= len1; i ++){ 49 for(int j = 1; j <= len2; j ++){ 50 if(str1[i-1] == str2[j-1]){ 51 dp[i][j] = dp[i-1][j-1]+1; 52 mark[i][j] = 0; 53 }else if(dp[i-1][j] >= dp[i][j-1]){ 54 dp[i][j] = dp[i-1][j]; 55 mark[i][j] = 1; 56 }else { 57 dp[i][j] = dp[i][j-1]; 58 mark[i][j] = -1; 59 } 60 } 61 } 62 } 63 void printLCS(int i, int j) { 64 if(!i && !j)return; 65 if(mark[i][j] == 0){ 66 printLCS(i-1,j-1); 67 printf("%c",str1[i-1]); 68 }else if(mark[i][j] == 1){ 69 printLCS(i-1,j); 70 printf("%c",str1[i-1]); 71 }else { 72 printLCS(i,j-1); 73 printf("%c",str2[j-1]); 74 } 75 } 76 int main() { 77 while(scanf("%s%s",str1,str2)!=EOF){ 78 len1 = strlen(str1); len2 = strlen(str2); 79 LCS(); 80 printLCS(len1,len2); 81 printf("\n"); 82 } 83 return 0; 84 }
3 1 3 2 4 1 2 3 4 4 3 3 2 1 0Sample Output
4 10 3
求一增子序列,并把它的值相加,总和最大的数。
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 using namespace std; 5 const int N = 1010; 6 int dp[N], a[N], n; 7 int main(){ 8 while(scanf("%d",&n)&&n) { 9 for(int i = 0; i < n; i ++) scanf("%d",&a[i]); 10 int ans = 0; 11 for(int i = 0; i < n; i ++) { 12 dp[i] = a[i]; 13 for(int j = 0; j < i; j ++) { 14 if(a[i] > a[j]) dp[i] = max(dp[i],dp[j]+a[i]); 15 } 16 ans = max(dp[i],ans); 17 } 18 printf("%d\n",ans); 19 memset(dp,0,sizeof(dp)); 20 memset(a,0,sizeof(a)); 21 } 22 return 0; 23 }
标签:code data nas 1.5 desc containe inline decide creating
原文地址:http://www.cnblogs.com/xingkongyihao/p/7230740.html