[第三次训练]Hard problem

来源:互联网 发布:java如何编译成class 编辑:程序博客网 时间:2024/05/15 08:58

Hard problem

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

4 6 5 4 
4 2 
1 2 3 4

Sample Output

OH,NO

HINT


解题报告

此题是一个猫吃老鼠的问题,猫只能吃个头依次减小的。给一个吃的老鼠的数目K,找出第K只吃掉的老鼠最大的值。此题解题方法如下:

例:
老鼠值   9 8 6  5  7 4 2 4 1
那能吃掉的老鼠为:
第一只: 因为前面没有比9大的了,那么就只能吃这一只老鼠,那么第一个为1;
第二只:因为前面的9>8,故可以通过9-8这样来吃,那么第二个就为2;
第三只:因为8>6,所以可以通过(9-8)这一路来吃,那么就是9-8-6,又因为9>6,那么也可以9-6这样吃,显然前一种方式的吃法长度长些,那么第三个就为3;
第四只:因为6>5,所以可以通过(9-8-6)这一路来吃,那么就是9-8-6-5,又因为8>5,那么路径就为9-8-5,最后9>5,所以第三条路径为9-5,显然第一种方式最长,那么第四个就为4;
第五只,根据上面的方法有一条路径 9-8-7,所以第五个就为3;
第六只,路径有 9-8--6-5-4,9-8-6-4,9-8-4,9-4;所以路径最长的为(9-8-6-5-4),那么第六个就为5;
第七只,路径有 9-8-6-5-4-2,9-8-7-2,9-8-6-5-2,9-8-6-2,9-8-2,9-2,那么第七个就为6;
依次类推,可以得到如下的一条值
能吃掉的老鼠 1 2 3 4 3 5 6 6 7
如果要寻找吃掉三只老鼠的那个最大的值,那么就是6(路径9-8-6)

这样既可解决此题,代码如下:
#include<stdio.h>#include<string.h>int a[100010],b[10010]={0};int main(){    //freopen("E.in","r",stdin);    //freopen("out.txt","w",stdout);    int t,n,k,i,j,ans,sum;    scanf("%d",&t);    while(t--)    {        memset(a,0,sizeof(a));        memset(b,0,sizeof(b));        scanf("%d%d",&n,&k);        for(i=0;i<n;i++)            scanf("%d",&a[i]);        if(n<k)        {printf("OH,NO\n");continue;}        b[0]=1;        for(i=1;i<n;i++)        {            sum=0;            for(j=i-1;j>=0;j--)            {                if(a[i]<=a[j])                    if(b[j]>=sum)                        {                            sum=b[j];                        }            }            b[i]=sum+1;        }        ans=0;        for(i=0;i<n;i++)        {            if(b[i]==k&&a[i]>ans)                ans=a[i];        }        if(ans==0)            printf("OH,NO\n");        else            printf("%d\n",ans);    }    return 0;}