gym101138F(数学,暴力)

F. GukiZ Height
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

GukiZ loves hiking in the mountains. He starts his hike at the height 0 and he has some goal to reach, initially equal to h.

The mountains are described by a sequence of integers A0, A1, ..., An - 1. In the first day GukiZ will change his height by A0, in the second day by A1, and so on. Mountains are a regular structure so the pattern repeats after the first n days. In general, in the i-th day GukiZ will change his height by A(i - 1)%n.

Additionally, GukiZ will become more and more tired and in the i-th day his goal will decrease by i. For example, after first three days his goal will be equal to h - 1 - 2 - 3 = h - 6.

Note that A may contain negative elements, what represents going down from some hill. Moreover, GukiZ's height may become negative, and so does his goal!

You can assume that both GukiZ's height and goal change at the same moment (immediately and simultaneously) in the middle of a day.

Once GukiZ is at the height not less than his goal, he ends his hike. Could you calculate the number of days in his hike?

Input

The first line of the input contains two integers n and h (1 ≤ n ≤ 1051 ≤ h ≤ 109) — the length of the array A and the initial goal, respectively. 

The second line contains n integers A0, A1, ..., An - 1 ( - 109 ≤ Ai ≤ 109).

Output

In a single line print the number of days in the GukiZ's hike.

It can be proved that for any valid input the answer exists.

Examples
input
3 45
7 -4 5
output
7
input
1 1
0
output
1
Note

GukiZ starts at height 0 with goal 45. We can describe his hike as follows:

  1. After the first day he is at height 7 and his goal is 44
  2. Height 3, goal 42
  3. Height 8, goal 39
  4. Height 15, goal 35
  5. Height 11, goal 30
  6. Height 16, goal 24
  7. Height 23, goal 17

After the 7-th day Gukiz's height is not less than his goal so he ends his hike.


题意:

一个登山运动员,n座山,目标高度为h,然后给出n个数,运动员当前高度为前面所有山的高度和(注意山是重复的,第n座山后面是第一座山)。

同时,在第i天他的目标高度会见小i

当当前高度大于等于目标高度时,停止登山

求第几天结束。


题解:

先判断会不会在第一轮某一天就结束

如果没有,那么假设进行x轮后结束,枚举结束是在第x+i(1<=i<=n)天,然后对每个i算出最小的x,列一个不等式即可。

注意精度。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll inf=0x3ffffffffffff;
const ll mod=1000000007;
const int maxn=1e5+10;
ll a[maxn];
ll sum=0;
typedef long double db;
int main()
{
    int n;
    ll h;
    scanf("%d%I64d",&n,&h);
    rep(i,1,n+1) scanf("%I64d",&a[i]),sum+=a[i];
    ll ans=inf;
    ll h1=0;
    rep(i,1,n+1)
    {
        h1+=a[i];
        if(h1>=h-i*(i+1)/2)
        {
            printf("%d\n",i);
            return 0;
        }
        db b=((db)i/n+1.0/2/n+(db)sum/n/n);
        db c=((db)i*(i+1)/2-h+h1)/n/n;
        db t=sqrtl(b*b-2*c);
        t=t-b;
        ans=min(ans,(ll)(i+ceill(t)*n));
    }
    printf("%I64d\n",ans);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值