Slimming Plan |
Time Limit: 10000ms, Special Time Limit:25000ms, Memory Limit:524288KB |
Total submit users: 45, Accepted users: 13 |
Problem 14105 : No special judgement |
Problem description |
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. |
Input |
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). |
Output |
If Chokudai's slimming plan ends on the d-th day, print d in one line. If it never ends, print -1. |
Sample Input |
Sample Input 1 65 60 3 -2 3 -4 Sample Input 2 65 60 3 -2 10 -3 Sample Input 3 100000 1 1 -1 Sample Input 4 60 59 1 -123 |
Sample Output |
Output for Sample Input 1 4 Output for Sample Input 2 -1 Output for Sample Input 3 99999 Output for Sample Input 4 1 |
Problem Source |
JAG Practice Contest for ACM-ICPC Asia Regional 2017, AtCoder, 2017-09-24 |
题意:某人要减肥,给定起始重量和终止重量,制定每天减肥增肥重量,循环执行该计划,问是否可以减肥成功,不能输出-1,能输出最早的天数
题解:首先吐槽一下湖大oj,结构体放构造函数无限rt,rt了十几发后一个个排除才排除出来,然后long long有问题,让我又wa了几发,怀疑人生啊!!!破OJ!
本题如果在第一轮某一天小于等于0,输出最小的天数
如果第一轮不存在小于等于0的情况,且sum(数组1 ~ d)>=0,则不可能减肥成功,输出-1,否则循环处理,每次循环看当前循环轮数*sum(数组1~d) + 第一轮中最小的状态是否小于等于0,是则输出当前循环轮数 * d + 最小的x(满足s + a[x-1] + 循环论述*sum小于等于0)
以上
AC代码
Source Code
#include <iostream>
#include <stdio.h>
#include <algorithm>
#define ll __int64
using namespace std;
const ll maxn = 111111;
ll a[maxn];
struct node{
ll id, x;
};
node q[111111];
bool cmp1(node a1, node a2){
if(a1.x != a2.x)
return a1.x < a2.x;
return a1.id < a2.id;
}
int main(){
ll s, t, d, sum = 0, num1 = 0, min1 = maxn * maxn * 200;
scanf("%I64d %I64d %I64d", &s, &t, &d);
for(ll i = 0; i < d; i++)
scanf("%I64d", &a[i]), sum += a[i];
s-=t;
for(ll i = 0; i < d; i++){
if(s + a[i] <= 0){
printf("%I64d\n", i + 1);
return 0;
}
s += a[i];
q[i].id = i + 1;
q[i].x = s;
min1 = min(min1, s);
num1++;
}
if(sum >= 0){
printf("-1\n");
}
else{
ll now = 1;
while(1){
if(sum * now + min1 <= 0){
ll ans = maxn * maxn * 100;
for(ll i = 0; i < d; i++){
if(sum * now + q[i].x <= 0 && ans > (now * d + q[i].id)){
ans = now * d + q[i].id;
}
}
printf("%I64d\n", ans);
return 0;
}
now++;
}
}
return 0;
}