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+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

题意:一个人从体重s要减肥到t,有d周期的减肥计划,计算需要多少天能减肥成功。如果永远不可能成功输出-1

先在第一轮周期中遍历每一天,如果s<=t   就说明成功了,直接输出

如果没有,一轮后计算出周期和 ,如果是正数那么不可能减肥成功

然后得考虑减的最多的那天情况,如果在最后一个周期中减肥成功了,但是可能在最小值那里就成功了,所以我们在最后一个周期中也得逐个计算

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define rep(i,a,n) for(int i=a;i<n;++i)
#define readc(x) scanf("%c",&x)
#define read(x) scanf("%d",&x)
#define sca(x) scanf("%d",&x)
#define sca2(x,y) scanf("%d%d",&x,&y)
#define sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define print(x) printf("%d\n",x)
#define mst(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&-x
#define lson(x) x<<1
#define rson(x) x<<1|1
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<int,int> P;
const int INF =0x3f3f3f3f;
const ll inf = 99999999999;
const int mod = 1e9+7;
const int MAXN = 105;
const int maxn =1000005;
using namespace std;
int t,d;
ll s;
int a[maxn];
int main(){
  scanf("%lld",&s);
  sca2(t,d);
  ll mi = inf; // 最小值
  ll sum = 0; // 周期的和
  for(int i = 0; i < d; i++){
    sca(a[i]);
    sum += a[i];
    if(s + sum <= t){
      printf("%d\n", i + 1);
      return 0;
    }
    mi = min(mi,sum);
  }
  if(sum >= 0){
    printf("-1\n");
    return 0;
  }
  ll cnt = 1;
  while(cnt * sum + mi + s > t){
      cnt++;
  }
  ll day = cnt * d;
  s += cnt * sum;
  for(int i = 0; i < d; i++){
    if(s + a[i] > t){
      day++;
      s += a[i];
    }
    else{
      day++;
      break;
    }
  }
  printf("%lld\n",day);
  return 0;
}
发布了89 篇原创文章 · 获赞 6 · 访问量 7926

猜你喜欢

转载自blog.csdn.net/kl782636177/article/details/89289690