【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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值