Link: https://www.nowcoder.com/acm/contest/84/B
Source: Niuke.com
There are n trees on the street, numbered 1...n, and the height of the ith tree is a
i
.
Define that n trees are beautiful if and only if
1. for all i, a i = a n-i+1 ;
2. for 1 <= i < n / 2 (not divisible), a i + 1 = a i + 1;
say "2 3 4 5 5 4 3 2" and "1 2 3 2 1" are pretty but "1 3 3 1" and "1 2 3 1" are not.
Now please modify the height of at least a few trees (can be larger or smaller), so that these trees are beautiful.
Define that n trees are beautiful if and only if
1. for all i, a i = a n-i+1 ;
2. for 1 <= i < n / 2 (not divisible), a i + 1 = a i + 1;
say "2 3 4 5 5 4 3 2" and "1 2 3 2 1" are pretty but "1 3 3 1" and "1 2 3 1" are not.
Now please modify the height of at least a few trees (can be larger or smaller), so that these trees are beautiful.
Enter description:
The first line contains an integer n representing the number of trees ( 1 <= n <= 100,000).
The second line of n integers represents the height of the tree ( 1 <= a
i
<= 100,000)。
Output description:
Output an integer representing the number of heights of the least modified tree.
Example 1
enter
3 2 2 2
output
1
Example 2
enter
4 1 2 2 1
output
0
I haven't done a thinking problem for a long time.
It feels useless.
The slope of this problem is known.
Solving by slope is very fast,
pay attention to the case of negative array subscripts.
So I added 10W to the subscript.
1 #include <cstdio> 2 #include <algorithm> 3 #include <vector> 4 #include <queue> 5 #include <cstring> 6 #include <string> 7 using namespace std; 8 const int maxn = 2e5 + 10; 9 int a[maxn], b[maxn]; 10 int main() { 11 int n; 12 while(scanf("%d", &n) != EOF) { 13 for (int i = 1 ; i <= n ; i++ ) 14 scanf("%d", &a[i]); 15 memset(b, 0, sizeof(b)); 16 for (int i = 1 ; i <= n ; i++) { 17 if (i <= n / 2) b[a[i] - i + 100000]++; 18 else b[a[i] - (n - i + 1) + 100000]++; 19 } 20 int ans = 0; 21 for (int i = 0 ; i < 200000 ; i++ ) { 22 ans = max(ans, b[i]); 23 } 24 printf("%d\n", n - ans); 25 } 26 return 0; 27 }