问题 B: Slimming Plan
时间限制: 1 Sec 内存限制: 128 MB
提交: 808 解决: 76
[提交] [状态] [命题人:admin]
题目描述
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
将每个周期的差算出来,而不是用一个一个的减,这样只需要求出一个那个最小的差值,然后用周期来判断,最后在最后一个周期精确答案即可
#include <iostream>
#include <cstdio>
#include <set>
using namespace std;
typedef long long ll;
using namespace std;
ll arr[100000+6];
ll dis[100000+6];
ll s,t,d;
int main()
{
cin>>s>>t>>d;
ll s1=s;
ll mins=1000006;
for(int i=0;i<d;i++)
{
cin >>arr[i];
}
dis[0]=arr[0];
if(dis[0]<mins)
mins=dis[0];
//cout<<mins<<endl;
for(int i=2;i<d;i++)
{
dis[i]=dis[i-1]+arr[i];
if(dis[i]<mins)
mins=dis[i];
}
//cout<<mins<<endl;
for(int i=0;i<d;i++)
{
s+=arr[i%d];
// cout <<s<<endl;
if(s<=t)
{
cout<<i+1<<endl;
return 0;
}
}
ll x=s1-s;
// cout<<s1<<endl;
// cout<<s<<endl;
//cout<<x<<endl;
if(s>=s1)
{
cout <<-1<<endl;
return 0;
}
else
{
ll k=d;
//cout<<k<<endl;
while(s+mins>t)
{
//cout<<s<<endl;
s=s-x;
//cout<<s<<endl;
k+=d;
//cout<<k<<endl;
}
// cout <<x<<" s"<<s<<" k"<<k<<endl;
while(s>t)
{
s+=arr[k%d];
// cout<<s<<endl;
k++;
// cout<<k<<endl;
}
cout<<k<<endl;
}
return 0;
}