AcWing 730. 机器人跳跃问题
原题链接
中等
作者:
好烂人
,
2025-03-24 22:10:57
·江苏
,
所有人可见
,
阅读 3
#include<iostream>
using namespace std;
const int N = 1e5 + 10;
int n;
int a[N];
int count(int t, int max)
{
for(int i = 0; i < n; i ++)
{
if(t > max || t < 0) break;
else{
if(t >= a[i]) t = t + (t - a[i]);
else t = t - (a[i] - t);
}
}
return t;
}
int main()
{
cin>>n;
for(int i = 0; i < n; i ++)
cin>>a[i];
int max = 0;
for(int i = 0; i < n ; i ++)
{
if(a[i] > max) max = a[i];
}
int l = 0; int r = 1e5 + 10;
int mid = 0;
while(l < r)
{
mid = l + r >> 1;
int u = count(mid, max);
if(u >= 0){
r = mid;
}else{
l = mid + 1;
}
}
cout<<l;
return 0;
}
o(n^2)--------------------------------------------------------------------------
#include<iostream>
using namespace std;
const int N = 1e5 + 10;
int n;
int a[N];
int count(int t, int max)
{
for(int i = 0; i < n; i ++)
{
if(t > max || t < 0) break;
else{
if(t >= a[i]) t = t + (t - a[i]);
else t = t - (a[i] - t);
}
}
return t;
}
int main()
{
cin>>n;
for(int i = 0; i < n; i ++)
cin>>a[i];
int max = 0;
for(int i = 0; i < n ; i ++)
{
if(a[i] > max) max = a[i];
}
for(int i = 1; i < N; i ++)
{
if(count(i, max) >= 0){
cout<<i;
break;
}
}
return 0;
}