zrq495
www.zrq495.com
题目描述:

        There is one Master In ACM_DIY called "白衣少年"(White) whose motto is "I can HOLD ANY FEMALE". Since White is really busy with HOLDING FEMALES, he has no idea of the work that is given by his boss(MALE, of course), so can you help him to solve such a simple and really easy problem: 

        Array A has N positive integers,for each A[i] (0<=i<N, indicating the i-th integer in the array A), it fits in a 32-bit signed integer ),find the closest number less than A[i] (if the distance is the same,we prefer the left one).

        If you can solve this problem, White may give you some "tips"(You know better!)

输入:

        T cases (1<=T<=5)

        For each case, the first line give an integer N(1<= N <= 10^6),then the second line has the array with N integers. (All the integers are guaranteed to fit in 32-bit signed integer)

输出:

        For each case, print one line with N space-seperated integers, where the i-th integer is the number who is less that A[i] and is closest to i if exists, otherwise it is 0. 

样例输入:
3
3
2 1 3
3
2 3 1
4
5 7 3 6
样例输出:
1 0 1
1 2 0
3 5 0 3

注意数组的大小和数组是否越界。

 1 #include<stdio.h>
 2 int a[1000002];
 3 int main()
 4 {
 5     int T, i, j, rlen, llen, n, min;
 6     scanf("%d", &T);
 7     while(T--)
 8     {
 9         scanf("%d", &n);
10         for(i=0; i<n;i++)
11         {
12             scanf("%d", &a[i]);
13             if (i==0)
14                 min=a[i];
15             if (a[i]<min)
16                 min=a[i];
17         }
18         for (i=0;i<n; i++)
19         {
20             if (min == a[i])
21             {
22                 printf("0");
23                 if (i!=n-1)
24                     printf(" ");
25                 else
26                     printf("\n");
27                 continue;
28             }
29             llen=rlen=0;
30             for (j=i+1; j<n; j++)
31             {
32                 if (a[i]>a[j])
33                 {
34                     rlen=j-i;
35                     break;
36                 }
37             }
38             for (j=i-1; j>=0; j--)
39             {
40                 if (a[i] > a[j])
41                 {
42                     llen=i-j;
43                     break;
44                 }
45             }
46             //printf("%d   %d\n", llen, rlen);
47             if (llen == 0 && rlen == 0)
48                 continue;
49             if (llen <= rlen)
50             {
51                 if (llen == 0)
52                     printf("%d", a[i+rlen]);
53                 else
54                     printf("%d", a[i-llen]);
55             }
56             else
57             {
58                 if (rlen == 0)
59                     printf("%d", a[i-llen]);
60                 else
61                     printf("%d", a[i+rlen]);
62             }
63             if (i!=n-1)
64             {
65                 printf(" ");
66             }
67             else
68             {
69                 printf("\n");
70             }
71         }
72     }
73     return 0;
74 }

 

posted on 2012-05-26 20:44  zrq495  阅读(275)  评论(0编辑  收藏  举报