问题 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;
}

Slimming Plan的更多相关文章

  1. upc组队赛7 Slimming Plan

    Slimming Plan 题目描述 Chokudai loves eating so much. However, his doctor Akensho told him that he was o ...

  2. 测试计划(Test Plan)

    测试计划(Test Plan) 版权声明:本文为博主原创文章,未经博主允许不得转载. 测试计划的概念: 测试计划是一个文档,描述了进行测试的测试范围,测试策略和方法,测试资源和进度.是对整个测试活动进 ...

  3. SQL Tuning 基础概述02 - Explain plan的使用

    1.explain plan的使用 SQL> explain plan for delete from t_jingyu; Explained. SQL> select * from ta ...

  4. POJ2175 Evacuation Plan

    Evacuation Plan Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4617   Accepted: 1218   ...

  5. New Plan!

    很久无写过blogs,荒废得差不多了,在博客园虽开bolg 5年多,但由于自己工作的问题,从开始的热情记录,到冷却冰冻,再到现在重拾起来,有一番感受:从大学刚毕业的制作网页菜鸟,开始接触DIV,CSS ...

  6. 分析oracle的执行计划(explain plan)并对对sql进行优化实践

    基于oracle的应用系统很多性能问题,是由应用系统sql性能低劣引起的,所以,sql的性能优化很重要,分析与优化sql的性能我们一般通过查看该sql的执行计划,本文就如何看懂执行计划,以及如何通过分 ...

  7. 【转】Oracle 执行计划(Explain Plan) 说明

    转自:http://blog.chinaunix.net/uid-21187846-id-3022916.html       如果要分析某条SQL的性能问题,通常我们要先看SQL的执行计划,看看SQ ...

  8. MySQL慢查询Explain Plan分析

    Explain Plan 执行计划,包含了一个SELECT(后续版本支持UPDATE等语句)的执行 主要字段 id 编号,从1开始,执行的时候从大到小,相同编号从上到下依次执行. Select_typ ...

  9. Timusoj 1982. Electrification Plan

    http://acm.timus.ru/problem.aspx?space=1&num=1982 1982. Electrification Plan Time limit: 0.5 sec ...

随机推荐

  1. Android平台targetSdkVersion设置及动态权限

    --关于Android动态权限和targetSdkVersion Android系统自6.0开始,提供动态权限机制,对于敏感权限(存储,定位,录音,拍照,录像等),需要在APP运行过程中动态向用户申请 ...

  2. python基础1 print()函数

    一切程序的开始都是由print('hello world') 了解print()函数 输出python之歌 print(''' 优美胜于丑陋明了胜于晦涩简洁胜于复杂复杂胜于凌乱扁平胜于嵌套间隔胜于紧凑 ...

  3. awk 实战

    awk 一些好玩的用法.有什么不错的点子可以留言,发挥出awk牛逼功能 分离mac地址 ifconfig wlan0 | grep eth | awk '{n=split($2,arr,": ...

  4. 免登陆下载jdk

    linux下使用wget免登陆获取jdk对应版本gz包 wget --no-check-certificate --no-cookies --header "Cookie: oracleli ...

  5. Oracle查询重复数据并删除,只保留一条记录

    前言 项目中,在“资源目录-在线编目”中,资源项子表存在多条重发数据,需要进行数据清理,删除重发的数据,最终只保留一条相同的数据. 操作的表名:R_RESOURCE_DETAILS 操作步骤 一.重复 ...

  6. !!在js中的用法

    var obj = ""; //undefinedconsole.log(!!obj);  //false(强制转换成Boolean类型)

  7. NABCD分析---校园服务

    N(需求): 大学生活中,很多琐碎的小事浪费同学时间精力.我们的APP本着为同学服务的宗旨,解决生活中各方面的问题,同学们可以在APP上发布各种信息,例如兼职,二手买卖等等. A(做法): 用户打开A ...

  8. JAVA获取汉字拼音首字母

    package com.common.util; import java.io.UnsupportedEncodingException; /** * 取得给定汉字串的首字母串,即声母串 * Titl ...

  9. 与图论的邂逅03:Lengauer-Tarjan

    回想一下,当我们在肝无向图连通性时,我们会遇到一个神奇的点——它叫割点.假设现在有一个无向图,它有一个割点,也就是说把割点删了之后图会分成两个联通块A,B.设点u∈A,v∈B,在原图中他们能够互相到达 ...

  10. Hibernate的工作原理及使用的原因

    一.工作原理: 1.读取并解析配置 2.读取并解析映射信息,创建Session Factory 3.打开Session 4.创建事务Transation 5.持久化操作 6.提交事务 7.关闭Sess ...