011 school recruit Zhenti exercise flowers (US Mission)

Flowers

Title Description
Park N has a garden, the garden each initially have no flowers, garden gardener will number from 1 to N and plans numbered's just kind of the garden i A_i flowers, he would choose a daily interval [L , R] (1≤L≤R≤N) and numbered L to R in the garden in a variety of flower, so gardeners take at least how many days to complete the project?

Input Description:
The first line contains an integer N, 1≤N≤10 ^ 5.
The second line contains N space-separated integers A_1 to A_N, 0≤A_i≤10 ^ 4.

Output Description: The
output complete the minimum number of days required for the program.

A thought: greedy (AC)

1 if __name__ == '__main__':
2     N = int(input())
3     ary = list(map(int,input().strip().split()))
4     cnt = 0
5     for i in range(1,N):
6         if ary[i-1] > ary[i]:
7             cnt += ary[i-1] - ary[i]
8     print(cnt + ary[N-1])

 

Thinking two: Divide and Conquer (only by 60%)

 1 def findMinIndex(ary):
 2     minValue = sys.maxsize
 3     minIndex = -1
 4     for i in range(len(ary)):
 5         if ary[i] < minValue:
 6             minValue = ary[i]
 7             minIndex = i
 8     for i in range(len(ary)):
 9         ary[i] -= minValue
10     return ary,minIndex,minValue
11 
12 def calFlowers(ary,L,R):
13     if L == R:
14         return ary[L]
15     elif L < R:
16         ary2,minI,minV = findMinIndex(ary[L:R+1])
17         left = calFlowers(ary2,0,minI-1)
18         right = calFlowers(ary2,minI+1,len(ary2)-1)
19         return left + right + minV
20     else:
21         return 0
22         
23 if __name__ == '__main__':
24     N = int(input())
25     ary = list(map(int,input().strip().split()))
26     
27     result = calFlowers(ary,0,N-1)
28     print(result)

 

Guess you like

Origin www.cnblogs.com/asenyang/p/11100600.html