Hard problem
1022: Hard problem
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 43 Solved: 12
Description
The cat gets N mice from dreamone, and she can choose K mice from them as the order which is listed from
left to right for dinner. But there is a limitation that the second mouse is no bigger than the first one, the third
one is no bigger than the second one….the Kth one is no bigger than the (K-1) th one. Actually, there is always
not a single method to choose the K mice from the N mice as the way described above; we can assume there
is M ways. This time, the cat of dreamone’s has thought another hard problem:
For each way, there is always a value for the Kth mouse. She wants to know the biggest value for the Kth
mouse of all the M ways. Can you get it? Of course, not all of you have understood the idea, so there is an
example blew:
We can assume N=4, K=2.
The N (N=4) numbers represented the N mice are given blew from left to right:
4 6 5 4
According to the rules above, we can get four ways for choosing K mice, such as:
4 4; 6 5; 6 4; 5 4
So the answer is 5.because the value 5 is the biggest one of the four ways for the Kth number.
If the cat can not solve the problem as quickly as she can, she will feel very boring, so she turns to you, a
topcoder of SWUST, for help. Can you help her?
Input
The first line of input will be a positive integer indicating how many test cases will be included (T). Each of
the next T cases will contain two parts:
The first part: two integer N, K (1<=N<=10000, 1<=K<=10)
The second part: N numbers (which is no larger than 1000000) represented the N mice from left to right.
Output
For each test, you should output the biggest value for the Kth numbers of all the manners. If you can not find
any way to choose the Kth mouse, just output “OH, NO” instead.
Sample Input
2
4 2
4 6 5 4
4 2
1 2 3 4
Sample Output
5
OH,NO
思路:dp问题,找好状态转移方程。
代码:
#include<iostream> #include<stdio.h> #include<cstring> using namespace std; #define MAX 10000 int line[MAX+10]; int num[11]; int dp[MAX+10][11]; int get_min(int a,int b) { return a>b?b:a; } int get_max(int a,int b) { return a>b?a:b; } int main() { //freopen("E.in","r",stdin); //freopen("E1.out","w",stdout); int n,m,t,i,j,flag,ans; scanf("%d",&n); while(n--) { flag=0;ans=0; scanf("%d%d",&m,&t); for(i=0;i<m;i++) scanf("%d",&line[i]); if(t>m) printf("OH,NO\n"); else { memset(num,-1,sizeof(num)); memset(dp,-1,sizeof(dp)); num[0]=line[0]; for(i=0;i<m;i++) { dp[i][0]=line[i]; int temp=get_min(i,t); for(j=temp;j>=1;j--) { if(num[j-1]==-1) continue; else if(num[j-1]>=line[i]) { dp[i][j]=line[i]; num[j]=get_max(num[j],line[i]); } } num[0]=get_max(num[0],line[i]); } for(i=t-1;i<m;i++) { if(dp[i][t-1]>ans) { ans=dp[i][t-1]; flag=1; } } if(flag) printf("%d\n",ans); else printf("OH,NO\n"); } } return 0; }
补充:综合编程 , 其他综合 ,