问题 B: Slimming Plan

问题 B: Slimming Plan

时间限制: 1 Sec  内存限制: 128 MB
 

题目描述

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+w(i%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 th

 

an 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

 

这个题让我白白debug了四个小时鸭!!疯狂咆哮,结果突然有人说不用取模就能过,然后就试了一下,果然!

实名diss中石油的数据和题面,明明是却来一个X+Wi%D,哭唧唧o(╥﹏╥)o

其实如果题面正确的话,题意很好懂。就是他想要减肥,一个周期d天,周期内的第i天会增加wi的体重(负的就是减轻了),问他最少多少天可以减到,如果减不到输出-1

用一个前缀和数组和记录一下第i天可以减多少,再用Min记录一下一个周期内最多减多少(因为可能会反弹导致增加)所以不一定是周期末减的最多

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define INFL 0x3f3f3f3f3f3f3f3f

const int maxn=1e6+5;
ll w[maxn];
ll sum[maxn];//减肥数前缀和数组 从考试到第i天可以减多少
int main()
{
    ll s,t,d;
    scanf("%lld%lld%lld",&s,&t,&d);
    ll Min=INFL;//一个周期内可以减少的最多是多少
    for(int i=1;i<=d;i++)
    {
        scanf("%lld",&w[i]);
        sum[i]=sum[i-1]+w[i];
        Min=min(Min,sum[i]);
    }

    ll res=s-t;//应该减去的重量
    bool flag=false;
    for(int i=1;i<=d;i++)//一个周期内
    {
        if(res+sum[i]<=0)
        {
            printf("%d\n",i);
            flag=true;
            break;
        }
    }
    if(!flag)//多于一个周期
    {
        if(sum[d]>=0)//一周周期里 体重不会减轻 肯定完成不了
            printf("-1\n");
        else
        {
            ll day=d;//减肥天数
            ll ress=res+sum[d];//剩余的需要减的
            while(ress+Min>0)//在一个周期内完不成,看需要多少个整周期
            {
                day+=d;//时间+1个周期
                ress+=sum[d];//减1个周期可以减轻的重量 
            }
            //经过几个周期后,在这个周期内可以完成
            for(int i=1;i<=d;i++)
            {
                if(ress+sum[i]<=0)
                {
                    day+=i;
                    printf("%lld\n",day);
                    break;
                }
            }
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值