问题 B: Slimming Plan

Chokudai loves eating so much. However, his doctor Akensho told him that he was overweight, so he finally decided to lose his weight.

Chokudai made a slimming plan of a D-day cycle. It is represented by D integers w0,…,wD−1. His weight is S on the 0-th day of the plan and he aims to reduce it to T (S>T). If his weight on the i-th day of the plan is x, it will be x+wi%D on the (i+1)-th day. Note that i%D is the remainder obtained by dividing i by D. If his weight successfully gets less than or equal to T, he will stop slimming immediately.

If his slimming plan takes too many days or even does not end forever, he should reconsider it.

Determine whether it ends or not, and report how many days it takes if it ends.

输入

The input consists of a single test case formatted as follows.

S T D
w0…wD−1
The first line consists of three integers S, T, D (1≤S,T,D≤100,000,S>T). The second line consists of D integers w0,…,wD−1(−100,000≤wi≤100,000 for each i).

输出

If Chokudai’s slimming plan ends on the d-th day, print d in one line. If it never ends, print −1.

样例输入
65 60 3
-2 3 -4

样例输出
4

#include<bits/stdc++.h>
using namespace std;
long long w[100005];
int n;
int main()
{
    long long sub,x,y;
    cin>>x>>y>>n;
    sub=x-y;
    int a;
    int minn=1000000000;
    int sign;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a);
        w[i]=w[i-1]+a;
        if(minn>w[i])///那一天最多减多少
        {
            minn=w[i];
            sign=i;
        }
        if(-w[i]>=sub) {cout<<i<<endl;return 0;}
    }
    if(w[n]>=0) cout<<-1<<endl;
    else
    {
        long long cha=sub+minn;
        if(cha%w[n]==0) cout<<cha/(-w[n])*n+sign;
        else///如果不整除,那么还要再进行一个减肥周期
        {
            int cycle=cha/(-w[n])+1;///所以减肥周期加一
            int subnow=sub+cycle*w[n];///总共要减的扣掉cycle个周期减的
            for(int i=1;i<=n;i++)
            {
                if(subnow+w[i]<=0) {cout<<i+cycle*n<<endl; return 0;}
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值