#include <stdio.h> #include <string.h> #include <algorithm>
const int MAXN = 100005; const int lim = 10;
int n; int pos[MAXN], f[MAXN][15]; bool flag;
inline int min(int a, int b) { if( a < 0 ) return b; return a < b ? a : b; }
int main() { //freopen("in.txt","r", stdin); while( scanf("%d", &n), n) { flag = false; for(int i = 0; i < n; i++) { scanf("%d", pos + i); if( i >= 2 && pos[i] - pos[i - 2] > lim) flag = true; } if( flag ) { puts("-1"); continue; } memset(f, -1, sizeof( f)); f[1][ pos[1] - pos[0] ] = 0; for(int i = 1; i < n; i ++) { for(int j = 1; j <= lim; j ++) { for(int k = i + 1; k <= i + lim && k < n; k ++) { if( f[i][j] >= 0 && pos[k] - (pos[i] - j) <= lim ) { f[k][ pos[k] - pos[i] ] = min(f[k][ pos[k] - pos[i] ], f[i][j] + 1); } } } } int ans = 999999999; for(int i = 1; i <= lim; i ++) { ans = min(f[n-1][i], ans); } printf("%d\n", ans); } //while(1); return 0; }
|