【codeforces 793A】Oleg and shares
【题目链接】: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的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
- 【Codeforces 670C】 Cinema
[题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...
- 【codeforces 515D】Drazil and Tiles
[题目链接]:http://codeforces.com/contest/515/problem/D [题意] 给你一个n*m的格子; 然后让你用1*2的长方形去填格子的空缺; 如果有填满的方案且方案 ...
随机推荐
- SQL Server 2005数据库定期备份(非常详细)与 SQL Server 2005数据库备份定期清理
SQL Server 2005数据库定期备份 分类: SQL Server 20052011-01-06 16:25 3320人阅读 评论(1) 收藏 举报 sql server数据库sqlserv ...
- DCloud-MUI:事件管理
ylbtech-DCloud-MUI:事件管理 极简的JS函数 1.返回顶部 1.事件绑定 除了可以使用addEventListener()方法监听某个特定元素上的事件外, 也可以使用.on()方法实 ...
- (Go)10.流程控制示例
package main import ( "math/rand" "fmt" ) func main() { //var n int n := rand.In ...
- 使用JQuery制作幻灯片(轮播图)
1.首先看一下目录结构 images文件夹放所需要播放的图片. js文件夹放jquery库和main.js 2.html代码: <!DOCTYPE html> <html lang= ...
- DotnetCore安装介绍
微软的DotnetCore发布至今,已经有段时间了,我们都非常关注我们它能为我们带来什么,我们能通过它做什么?要解决这些问题,不仅仅需要基本的了解意外,还需要知道是开发环境是怎么搭建的,接下来我们就一 ...
- 【寒假集训系列DAY.1】
Problem A. String Master(master.c/cpp/pas) 题目描述 所谓最长公共子串,比如串 A:“abcde”,串 B:“jcdkl”,则它们的最长公共子串为串 “cd” ...
- MySQL安装for windows
======MySQL安装 for windows====== 版本5.7.X MySQL服务器帮助我们来管理文件的操作 MySQL软件 - 服务器端软件 - 服务端程序 - 解析指令 - 对文件的操 ...
- Django分页器及自定义分页器
Django的分页器 view from django.shortcuts import render,HttpResponse # Create your views here. from app0 ...
- List 练习
(List)已知有一个Worker 类如下: public class Worker { private int age; private String name; private double sa ...
- 【SQL】通过rowid查找及删除重复记录
新建T表如下: SQL> select * from t; X Y ---------- -- 1 a 1 a 1 a 2 ...