Coderforces 192 B

弄两个数组a,b,将a数组复制到b,先把b排序,然后从依次减去b数组的数值,小于0的不能走,看指针是否能从1到达n,当第一个满足条件得数出现时即为听别人说

此题也可用动态规划,不过我没想出来。。。T_T.....

View Code
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 
 5 
 6 int cmp(const void *a,const void *b)
 7 {
 8     return *(int *)a - *(int *)b;
 9 }
10    
11 int main()
12 {
13     int i,j,k,n;
14     int a[1001],b[1001];
15         
16     scanf("%d",&n);
17     memset(b,0,sizeof(b));
18     for(i=1; i<=n; i++)
19     {
20        scanf("%d",&a[i]);
21        b[i] = a[i];
22     }  
23     qsort(b,n,sizeof(b[0]),cmp); 
24     k = 1;
25     for(j=n; j>0; j--)
26     {
27        for(i=1; i<=n; i++)
28          a[i] = a[i] - b[j];
29       if(a[1]>=0 && a[n]>=0)
30       {
31         for(i=1; k<=n,i<=n; i++)
32         {
33           if(a[k+1]>=0 && k+1<=n)
34           { 
35               k = k+1;
36           }                 
37           if(a[k+2]>=0 &&k+2<=n)
38           {
39              k = k+2;
40           }  
41           if(a[k+1]<0&&a[k+2]<0)
42              break;   
43        }   
44        if(k == n)
45        {
46            printf("%d\n",b[j]); 
47            break;
48        }
49       } 
50       for(i=1; i<=n; i++)
51         a[i] += b[j];   
52      }          
53     //system("pause");
54     return 0;
55 }    

 

你可能感兴趣的:(code)