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

题意+题解

一个人从体重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;
}

upc组队赛7 Slimming Plan的更多相关文章

  1. Slimming Plan

    问题 B: Slimming Plan 时间限制: 1 Sec  内存限制: 128 MB 题目描述 Chokudai loves eating so much. However, his docto ...

  2. upc组队赛5 Ingenious Lottery Tickets【排序】

    Ingenious Lottery Tickets 题目描述 Your friend Superstitious Stanley is always getting himself into trou ...

  3. upc组队赛3 Chaarshanbegaan at Cafebazaar

    Chaarshanbegaan at Cafebazaar 题目链接 http://icpc.upc.edu.cn/problem.php?cid=1618&pid=1 题目描述 Chaars ...

  4. upc组队赛1 过分的谜题【找规律】

    过分的谜题 题目描述 2060年是云南中医学院的百年校庆,于是学生会的同学们搞了一个连续猜谜活动:共有10个谜题,现在告诉所有人第一个谜题,每个谜题的答案就是下一个谜题的线索....成功破解最后一个谜 ...

  5. upc组队赛1 不存在的泳池【GCD】

    不存在的泳池 题目描述 小w是云南中医学院的同学,有一天他看到了学校的百度百科介绍: 截止到2014年5月,云南中医学院图书馆纸本藏书74.8457万册,纸质期刊388种,馆藏线装古籍图书1.8万册, ...

  6. upc组队赛1 黑暗意志【stl-map】

    黑暗意志 题目描述 在数千年前潘达利亚从卡利姆多分离之时,迷雾笼罩着这块新形成的大陆,使它不被外来者发现.迷雾同样遮蔽着这片大陆古老邪恶的要塞--雷神的雷电王座.在雷神统治时期,他的要塞就是雷电之王力 ...

  7. upc组队赛1 闪闪发光 【优先队列】

    闪闪发光 题目描述 一所位于云南昆明的中医药本科院校--云南中医学院. 因为报考某专业的人数骤减,正面临着停招的危机. 其中有九名少女想到一条妙计--成为偶像, 只要她们成为偶像,学校的名气便会增加, ...

  8. upc组队赛1 流连人间的苏苏

    流连人间的苏苏 题目描述 苏苏在做红尘仙的任务时,发现坐落于风景秀丽.四季如春的昆明市的云南中医学院. 没过多久,苏苏就喜欢上了这个学校.以致于苏苏忘了回涂山的时间,现在她只剩下d天的时间待在云南中医 ...

  9. upc组队赛18 THE WORLD【时间模拟】

    THE WORLD 题目链接 题目描述 The World can indicate world travel, particularly on a large scale. You mau be l ...

随机推荐

  1. Tomcat安装后修改路径方法

    tomcat+mysql+jdk 本地已经安装tomcat.mysql.jdk服务,需要更改安装目录由D盘改为C盘,方法如下 一.停止tomcat.mysql服务 二.安装文件由D盘拷贝到C盘,原D盘 ...

  2. oo_project_2java数据类型范围及测试

    数据类型范围测试 一.数字常数的编译问题 java中的常量数字默认以int型编译 如: long a = 1234567890; //十位 long b = 12345678900; //默认数据为i ...

  3. 【awk】 判断是不是纯ascii串

    筛选出纯ascii串: awk '{ l = length($0); for (i = l; i > 0; i--) { if (substr($0,i,1) > "\177&q ...

  4. 1.如何在JMeter中使用JUnit

    您是否需要在测试过程中使用JUnit? 要回答这个问题,我们先来看看单元测试. 单元测试是软件测试生命周期中测试的最低分辨率. 运行单元测试时,需要在应用程序中使用最小的可测试功能,将其与其他代码隔离 ...

  5. nginx的原理

    Nginx会按需同时运行多个进程:一个主进程(master)和几个工作进程(worker),配置了缓存时还会有缓存加载器进程 (cache loader)和缓存管理器进程(cache manager) ...

  6. java环境--JDK和Tomcat在linux上的安装和配置

    Tomcat在Linux上的安装与配置 以下使用的Linux版本为: Redhat Enterprise Linux 7.0 x86_64,Tomcat版本为tomcat-7.0.54.1.下载JDK ...

  7. std::list erase

    conns.erase(remove(conns.begin(), conns.end(), conn), conns.end());

  8. 使用nano编辑器进行查找和替换

    笔者自己常用的是vim.这里nano的使用场景就是刚安装好Ubuntu系统,想要替换更新源,这时候还没装上vim.所以,学一下如何用nano查找和替换,可以解决这个小问题. 首先sudo nano / ...

  9. Vue.js(五)

    前后端交互概述与URL地址格式 JS中常见的异步调用: 定时任务 ajax 事件函数 接口调用方式: 原生ajax 基于jQuery的ajax fetch axios url 地址格式: 传统的url ...

  10. websocket 中使用Service层的方法

    创建公共Utils 类 ApplicationContextRegister @Component @Lazy(false) public class ApplicationContextRegist ...