【题目链接】:http://codeforces.com/contest/793/problem/A

【题意】



每次你可以对1..n中的任意一个数字进行减少k操作;

问你最后可不可能所有的数字都变成一样的;

可能的话输出最小操作次数;

【题解】



a[x1]-k*x1=t

a[x2]-k*x2=t

x1是对a[x1]的操作次数..以此类推

则有

a[x1]-a[x2]=k*(x2-x1)

可知a[x1]-a[x2]必然为k的倍数否则无解;

则升序排;

相邻两个数字的差必然要求为k的倍数;

即%k==0

然后全都变成最小的那个数显然是最优的.



【Number Of WA】



0



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1e5+100; LL n,k;
LL a[N]; int main()
{
//freopen("F:\\rush.txt","r",stdin);
ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf not use
cin >> n >> k;
rep1(i,1,n) cin >> a[i];
sort(a+1,a+1+n);
rep1(i,1,n-1)
{
LL t = a[i+1]-a[i];
if (t%k!=0)
return cout << -1 << endl,0;
}
LL ans = 0;
rep1(i,1,n)
{
LL temp = a[i]-a[1];
ans+=(temp/k);
}
cout << ans << endl;
return 0;
}

【codeforces 793A】Oleg and shares的更多相关文章

  1. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  2. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  3. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  4. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  5. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  6. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

  7. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

  8. 【Codeforces 670C】 Cinema

    [题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...

  9. 【codeforces 515D】Drazil and Tiles

    [题目链接]:http://codeforces.com/contest/515/problem/D [题意] 给你一个n*m的格子; 然后让你用1*2的长方形去填格子的空缺; 如果有填满的方案且方案 ...

随机推荐

  1. 【Allwinner ClassA20类库分析】4.GPIO类的使用

        从本节起,開始使用ClassA20类库完毕操作外设的功能,请先在https://github.com/tjCFeng/ClassA20下载ClassA20类库. 封装的目的就是简化操作,试想一 ...

  2. Extension Methods (C# Programming Guide)

    https://msdn.microsoft.com/en-us//library/bb383977.aspx private static void Dump(this ArraySegment&l ...

  3. C# 数据库备份与还原 小妹做了一个winform系统,需要对sql2000数据库备份和还原(小妹妹你太狠了)

      成功了,把代码帖出来给大家看看,跟我刚开始帖出来的差不多,是需要杀掉进程的,我之前调用的存储过程,可能有点问题,现在改成sql了/// <summary>        /// 数据库 ...

  4. c# 命令行下编译c#文件 // c# file类读写文件

    c# 命令行下编译c#文件 2010-03-01 15:02:14|  分类: c# 学习|字号 订阅     在 开始  ——>程序 ——>vstool中打开vs2008命令提示. 通过 ...

  5. C# 实现DataGridView分页功能

    C#实现DataGridView分页功能 2010-07-17 13:45:42|  分类: C#|字号 订阅     从界面可以看到,在设计时需要一个DataGridView.BindingNavi ...

  6. code+12月月赛 火锅盛宴

    时间限制: 2.0 秒 空间限制: 512 MB 题目背景 SkyDec和YJQQQAQ都是Yazid的好朋友.他们都非常喜欢吃火锅.有一天,他们聚在一起,享受一场火锅盛宴. 题目描述 在这场火锅盛宴 ...

  7. 98.Ext.form.Label组件的基本用法

    转自:https://www.cnblogs.com/kelly/archive/2009/06/05/1496897.html 本篇介绍Ext.form.Label组件的基本用法: 这里通过上一篇介 ...

  8. 通过DOM实现点击隐藏父元素

    HTML代码简单如下: <ul id='ul1'> <li><a href="javascript:">1</a></li&g ...

  9. Cracking the Coding Interview 4.8

    You are given a binary tree in which each node contains a value. Design an algorithm to print all pa ...

  10. # 深入理解Redis(二)——内存管理的建议与技巧

    引语 随着使用Redis的深入,我们不可避免的需要深入了解优化Redis的内存,本章将重点讲解Redis的内存优化之道,同时推荐大家阅读memory-optimization一文. 想要高效的使用Re ...