跳到正文

题解

P11371 题解

ikunTLE约 1 分钟
原文
文章信息与版本

文章信息

发布
2024年12月1日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 1 分钟
获赞
12
题目
P11371

版本

  1. v1当前

题目传送门

思路

为了使其操作次数最少,应该把所有的小朋友的糖果数都增加为糖果最多的小朋友的糖果数。设 mm 为序列 aa 中的最大值。若 (m−ai) mod k≠0(m-a_i)\bmod k\ne 0,则无论怎么添加糖果都无法使所有的糖果数相等;否则统计 m−aik\frac{m-a_i}{k} 的总和。

注意事项

  • 不开 long long 见祖宗。

AC CODE

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
int read(){int x=0;char f=1,ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();return x*f;}
const int N=1e5+10;
int a[N];
signed main(){
	int n=read(),k=read(),m=0;
	for(int i=1;i<=n;++i){
		a[i]=read();
		m=max(m,a[i]);
	}
	int ans=0;
	for(int i=1;i<=n;++i)
		if((m-a[i])%k)
			return printf("NO\n"),0;
		else ans+=(m-a[i])/k;
	printf("YES %lld\n",ans);
	return 0;
}
原文
存档评论1 条抓取于