【Japan Alumni Group Summer Camp 2017】 B.Slimming Plan

本文介绍了一个基于循环减肥计划的算法挑战,目标是帮助Chokudai通过调整饮食来达到理想的体重。文章详细解释了如何根据特定规则计算减肥周期,包括在正向和负向计划中的策略,以及如何确定减肥计划是否能成功结束。

题目描述

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

 

大体题意上是开始减肥计划,这个计划会循环进行,然后存的时候存这个人当前这一天所积累的增减量,然后用两个变量记录每一次循环的最大减少量(最小量)和跑一遍计划能够减少的体重,判断。

规则1:

如果这个减肥计划是正的,就遍历一遍循环。看看能不能在中间就减肥成功了,如果不能,则是-1

规则2:

如果这个减肥计划是负的,就用一个while,然后在while里面判断如果当前的最大减少量能够减到规定体重,则直接循环找那天,如果不能就减去一遍计划的减少量,然后再判断。

极有可能是样例对了然后跑wa,需要多想几组数据。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<stack>
using namespace std;
vector<long long> v;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
    long long s,t,d;
    cin>>s>>t>>d;
    long long w;
    long long sum = 0;
    long long eachm = 1e+9;
    long long eachf = 0;
    for(long long i=0;i<d;i++){
        cin>>w;
        sum+=w;
        v.push_back(sum);
        if(sum<eachm){
            eachm = sum;
        }
    } 
    eachf = sum;
    s-=t;
    if(eachf>=0){
        for(long long i=0;i<d;i++){
            if(s+v[i]<=0){
                cout<<i+1<<endl;
                return 0;
            }
        }
        cout<<-1<<endl;
    }
    else{
        long long ns = s;
        long long ss = 0;
        while(1){
            if(ns+eachm<=0){
                for(long long i=0;i<d;i++){
                    if(ns+v[i]<=0){
                        ns = 0;
                        ss+=i+1;
                        break;
                    }
                }
                break;
            }   
            else{
                ns+=eachf;
                ss+=d;
            }
        }
        cout<<ss<<endl;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值