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. Chrome 强行修改配置

    大约有两个月没写了,一是最近这两个月还挺忙,更重要的是也没有遇到什么好玩的,或者是要记录的,今天无意间遇到一个非技术问题:Chrome设置的问题. 问题描述: chrome 在下载文件时,默认情况下是 ...

  2. 用于拆解和组合PDF中各个对象的shell脚本

    拆解脚本 header_start=0 header_len=15 xref_start=$(strings -a -t d $1 | grep -e "\bxref\b" | a ...

  3. [题解]Print a 1337-string...-数学(codeforces 1202D)

    题目链接:https://codeforces.com/problemset/problem/1202/D 题意: 构造一串只由 ‘1’,‘3’,‘7’ 组成的字符串,使其 ‘1337’ 子序列数量为 ...

  4. 【题解】LCIS

    题目描述 给定两个整数序列,写一个程序求它们的最长上升公共子序列. 输入格式 每个序列用两行表示,第一行是长度L,第二行是该序列. 输出格式 在第一行,输出该LCIS的长度.第二行,输出该LCIS. ...

  5. 不要62(数位dp)

    题目传送门 不要62 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  6. POJ:3371 Connect the Cities(最小生成树)

    http://acm.hdu.edu.cn/showproblem.php?pid=3371 AC代码: /** /*@author Victor /* C++ */ #include <bit ...

  7. 42.Flatten Binary Tree to Linked List

    Level:   Medium 题目描述: Given a binary tree, flatten it to a linked list in-place. For example, given ...

  8. db2序列

    CREATE SEQUENCE <sequence-name> AS data-type   默认 As Integer START WITH <numeric-constant&g ...

  9. css3中的scroll-behavior属性

    scroll-behavior属性 当用户手动导航或者 CSSOM scrolling API 触发滚动操作时,CSS 属性 scroll-behavior 为一个滚动框指定滚动行为,当用户通过鼠标滑 ...

  10. 转:KVM 虚拟机的克隆

    KVM 虚拟机的克隆 首先把需要克隆的源虚拟机先关闭,然后使用以下命令来进行克隆,注意我这里使用的是相对路径.   virsh shutdown VM02 virt-clone -o VM02 -n ...