Oleg and shares CodeForces
来源:互联网 发布:bp网络和卷积神经网络 编辑:程序博客网 时间:2024/06/06 20:22
Oleg the bank client checks share prices every day. There are n share prices he is interested in. Today he observed that each second exactly one of these prices decreases by k rubles (note that each second exactly one price changes, but at different seconds different prices can change). Prices can become negative. Oleg found this process interesting, and he asked Igor the financial analyst, what is the minimum time needed for all n prices to become equal, or it is impossible at all? Igor is busy right now, so he asked you to help Oleg. Can you answer this question?
Input
The first line contains two integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ 109) — the number of share prices, and the amount of rubles some price decreases each second.
The second line contains n integers a1, a2, …, an (1 ≤ ai ≤ 109) — the initial prices.
Output
Print the only line containing the minimum number of seconds needed for prices to become equal, of «-1» if it is impossible.
Example
Input
3 3
12 9 15
Output
3
Input
2 2
10 9
Output
-1
Input
4 1
1 1000000000 1000000000 1000000000
Output
2999999997
Note
Consider the first example.
Suppose the third price decreases in the first second and become equal 12 rubles, then the first price decreases and becomes equal 9 rubles, and in the third second the third price decreases again and becomes equal 9 rubles. In this case all prices become equal 9 rubles in 3 seconds.
There could be other possibilities, but this minimizes the time needed for all prices to become equal. Thus the answer is 3.
In the second example we can notice that parity of first and second price is different and never changes within described process. Thus prices never can become equal.
In the third example following scenario can take place: firstly, the second price drops, then the third price, and then fourth price. It happens 999999999 times, and, since in one second only one price can drop, the whole process takes 999999999 * 3 = 2999999997 seconds. We can note that this is the minimum possible time.
分析前后两项的关系可以推出来,如果(a[i]-a[i-1])%k!=0,那么-1.
此外,根据上面的关系,想要相等,那么都变成最小的那个是最优的
#include<iostream>using namespace std;#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<stdlib.h>#include<vector>#include<queue>#include<deque>#include<map>#include<set>#include<time.h>#define pi(x,y) printf("%d%c",(x),(y));#define pin(x) printf("%d\n",(x));#define si(x) scanf("%d",&(x))#define sii(x,y) scanf("%d%d",&(x),&(y))#define s3(x,y,z) scanf("%d%d%d",&(x),&(y),&(z))#define rep(x,y,z) for(int (x)=(y);(x)<(z);++(x))#define dep(x,y,z) for(int (x)=(y)-1;(x)>=(z);--(x))#define read int TcaseN;scanf("%d",&TcaseN);for(int Tcase=1;Tcase<=TcaseN;++Tcase)#define cls(x,y) memset((x),(y),sizeof((x)));#define pb(x) push_back(x)#define mp(x,y) make_pair((x),(y))#define max3(value_a,value_b,value_c) max(max(value_a,value_b),value_c)#define min3(value_a,value_b,value_c) min(min(value_a,value_b),value_c)#define GT(x) (x)=clock();#define fin(x) freopen(x,"r",stdin);#define fout(x) freopen(x,"w",stdout);///In This You Can Define Long Integer Type#define LONGTYPE long longtypedef LONGTYPE LL;typedef unsigned LONGTYPE ULL;const int maxint=((~((unsigned)(0)))>>1);const LL maxll=((~((unsigned LONGTYPE)(0)))>>1);const int inf=0x3f3f3f3f;const double PI=acos(-1.0);const int N=100005;int n,k;int a[N];int main() {#ifdef tangge clock_t tSTART,tEND,t3; GT(tSTART);#endif // tangge /*Input:*/ scanf("%d%d",&n,&k); int ans=inf,cut=0; for(int i=0;i<n;++i){ scanf("%d",&a[i]); if(i){ if((a[i]-a[i-1])%k!=0){ ans=-1; } } } if(~ans){ sort(a,a+n); long long ans=0; for(int i=1;i<n;++i){ ans+=(a[i]-a[0])/k; } printf("%lld\n",ans); }else{ puts("-1"); }#ifdef tangge GT(tEND); printf("%.8lf\n",(tEND-tSTART)/1000.0);#endif // tangge return 0;}
- Oleg and shares CodeForces
- Oleg and shares (Codeforces
- Oleg and shares CodeForces
- Codeforces 793A Oleg and shares 题解
- codeforces 793A Oleg and shares
- 【Codeforces 793 A. Oleg and shares】
- A. Oleg and shares
- CF-Tinkoff Challenge-Elimination Round-A-Oleg and shares
- Codeforces Round #380A - Interview with Oleg
- CodeForces 738A - Interview with Oleg(水题)
- codeforces 738A Interview with Oleg
- 【57.97%】【codeforces Round #380A】Interview with Oleg
- codeforces - 738A - Interview with Oleg(字符串处理)
- Codeforces 729A Interview with Oleg 简单暴力
- [ 线段树 网络流 ] Codeforces793G Oleg and chess
- Mount Windows (CIFS) LAN Shares and Overcome Libreoffice/.doc Issues
- Codeforces #380(Div.2)A.Interview with Oleg【模拟】水题
- A. Interview with Oleg
- c++作业6
- 解析json字符串
- springMVC
- EOJ 3246实验室传染病(单调栈)@
- 计算机英语·I
- Oleg and shares CodeForces
- Spring成长之路
- oracle并行计算
- unity3d 协程
- Android学习九 ContentProvider内容提供者相关
- C#并行运算 Parallel.Invoke、Parallel.For、Parallel.Foreach性能测试及示例
- 6种常用的迭代器 ostream_iterator,istream_iterator,reverse_iterator,back_insert_iterator,front_insert_itera)
- 各类时间格式
- Android面试题-IntentService源码分析