Trees ——CodeForces
来源:互联网 发布:苹果5s网络显示e 编辑:程序博客网 时间:2024/04/29 17:35
On Bertown’s main street n trees are growing, the tree number i has the height of ai meters (1 ≤ i ≤ n). By the arrival of the President of Berland these trees were decided to be changed so that their heights formed a beautiful sequence. This means that the heights of trees on ends (the 1st one and the n-th one) should be equal to each other, the heights of the 2-nd and the (n - 1)-th tree must also be equal to each other, at that the height of the 2-nd tree should be larger than the height of the first tree by 1, and so on. In other words, the heights of the trees, standing at equal distance from the edge (of one end of the sequence) must be equal to each other, and with the increasing of the distance from the edge by 1 the tree height must also increase by 1. For example, the sequences “2 3 4 5 5 4 3 2” and “1 2 3 2 1” are beautiful, and ‘1 3 3 1” and “1 2 3 1” are not.
Changing the height of a tree is a very expensive operation, using advanced technologies invented by Berland scientists. In one operation you can choose any tree and change its height to any number, either increase or decrease. Note that even after the change the height should remain a positive integer, i. e, it can’t be less than or equal to zero. Identify the smallest number of changes of the trees’ height needed for the sequence of their heights to become beautiful.
Input
The first line contains integer n (1 ≤ n ≤ 105) which is the number of trees. The second line contains integers ai (1 ≤ ai ≤ 105) which are the heights of the trees.
Output
Print a single number which is the minimal number of trees whose heights will have to be changed for the sequence to become beautiful.
Example
Input
3
2 2 2
Output
1
Input
4
1 2 2 1
Output
0
题意大概就是把队列修改成对称且连续递增1的,求需要修改的最小数目,做法就是枚举固定一个点,搜索以这个点为基准修改成目标状态的不需要修改的数量,并且这些不用修改的点以后枚举就可以跳过了(因为它们属于同一个免修改块),因此加一个bool数组记录,枚举搜索一边就可以找到最大的免修改值,用n减去免修改值就是答案了。
#include<iostream>#include<algorithm>#include<string.h>#include<string>#include<cmath>#include<stdio.h>using namespace std;int n;int a[100005];bool v[100005];int main(){ //total-freenum,find the max of freenum scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); } if(n==1){printf("0");return 0;} int ans=n; int maxfree=0; for(int i=1;i<=n/2;i++){ if(v[i])continue; v[i]=1; if(a[i]<i)continue; int maxn=0; for(int j=i;j<=n/2;j++){ if(a[j]-a[i]==j-i){ v[j]=1; maxn++; } if(a[n-j+1]-a[i]==j-i){ v[n-j+1]=1; maxn++; } } if(n%2==1){if(a[n/2+1]-a[i]==n/2+1-i){maxn++;}} maxfree=max(maxfree,maxn); } int k=n/2; if(n%2==1)k++; for(int i=n;i>k;i--){ if(v[i])continue; v[i]=1; if(a[i]<n-i+1)continue; int maxn=0; for(int j=i;j>k;j--){ if(a[j]-a[i]==i-j){ v[j]=1; maxn++; } if(a[n-j+1]-a[i]==i-j){ v[n-j+1]=1; maxn++; } } if(n%2==1){if(a[n/2+1]-a[i]==i-(n/2+1)){maxn++;}} maxfree=max(maxfree,maxn); } printf("%d",ans-maxfree); return 0;}
。。。似乎有更快的做法= =
- Trees ——CodeForces
- Trees CodeForces
- DP方程——Coloring Trees ( Codeforces Round #369 (Div. 2) C )
- Codeforces Round #369 (Div. 2) C. Coloring Trees —— DP
- codeforces C.Coloring Trees
- codeforces C. Coloring Trees
- 【打CF,学算法——二星级】Codeforces Round #312 (Div. 2) A Lala Land and Apple Trees
- codeforces 264E Roadside Trees
- codeforces Trees in a Row
- codeforces Round_369 C. Coloring Trees
- codeforces 711C - Coloring Trees
- Codeforces 711C Coloring Trees
- Trees in a Row CodeForces
- Okabe and Banana Trees CodeForces
- OpenCV——Calonder Descriptor, Generic Trees and Randomized Trees
- HDU2215 ——Maple trees
- Uva712——S-Trees
- codeforces#236_div2_B Trees in a Row
- iOS安装包瘦身方法?
- struct 在另一源文件使用
- Google开源C++单元测试框架gTest 2:gTest断言
- redis数据持久化的策略
- candy
- Trees ——CodeForces
- 折半查找
- Play Basketball
- MySQL 高可用:mysql+mycat实现数据库分片(分库分表)
- 5大育儿网站
- linux入门:简易shell
- jquery处理字符串的总结
- 对PHP学习的一些总结
- iOS 保持界面流畅的技巧