题目链接:
题意:
题解:
二分
从上一个位置到达这个位置,可以通过上一个位置推出当前位置必须到达的最小值now,如果now小于a[i]就无所谓了,因为肯定行,直接更新下一个now;如果now-a[i]大于了当前二分出的伸展极限就不行。看了别人代码,还是看了很久,还是太菜啊。。
代码:
1 #include2 using namespace std; 3 typedef long long ll; 4 #define MS(a) memset(a,0,sizeof(a)) 5 #define MP make_pair 6 #define PB push_back 7 const int INF = 0x3f3f3f3f; 8 const ll INFLL = 0x3f3f3f3f3f3f3f3fLL; 9 inline ll read(){10 ll x=0,f=1;char ch=getchar();11 while(ch<'0'||ch>'9'){ if(ch=='-')f=-1;ch=getchar();}12 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}13 return x*f;14 }15 //16 const int maxn = 1e5+10;17 18 int n,a[maxn];19 20 bool check(int x){21 int now=-INF; // 可能x很大,对于a[2]可以到的最小值a[1]-x+1可以变得很负22 for(int i=1; i<=n; i++){23 if(now-a[i]>x) return false;24 now = max(now+1,a[i]-x+1); // now表示当前值a[i]可以变到的最小的值+1之后就是a[i+1]的一个必须变到的值,a[i]-x+1也是,取他们的最大值就是a[i+1]必须要到达的最小值 如果a[i+1]>now,就一定可以,更新下一个now的时候,就是a[i+1]-x+1了。25 }26 return true;27 }28 29 int main(){30 int T = read();31 for(int cas=1; cas<=T; cas++){32 n = read();33 34 for(int i=1; i<=n; i++){35 a[i] = read();36 }37 38 int ans = 0;39 int l = 0, r = 1e6;40 while(l <= r){41 int mid = (l+r)/2;42 if(check(mid)) ans=mid,r=mid-1;43 else l=mid+1;44 }45 46 cout << "Case #" << cas << ":\n" << ans << endl;47 }48 49 return 0;50 }