贪心&二分 CodeForces999D 贪心&二分&set

本文介绍了一种算法,用于解决通过最少的操作次数使数组中元素的特定余数分布均匀的问题,并提供了一个高效的实现示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D. Equalize the Remainders

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given an array consisting of nn integers a1,a2,…,ana1,a2,…,an, and a positive integer mm. It is guaranteed that mm is a divisor of nn.

In a single move, you can choose any position ii between 11 and nn and increase aiai by 11.

Let's calculate crcr (0≤r≤m−1)0≤r≤m−1) — the number of elements having remainder rr when divided by mm. In other words, for each remainder, let's find the number of corresponding elements in aa with that remainder.

Your task is to change the array in such a way that c0=c1=⋯=cm−1=nmc0=c1=⋯=cm−1=nm.

Find the minimum number of moves to satisfy the above requirement.

Input

The first line of input contains two integers nn and mm (1≤n≤2⋅105,1≤m≤n1≤n≤2⋅105,1≤m≤n). It is guaranteed that mm is a divisor of nn.

The second line of input contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1090≤ai≤109), the elements of the array.

Output

In the first line, print a single integer — the minimum number of moves required to satisfy the following condition: for each remainder from 00to m−1m−1, the number of elements of the array having this remainder equals nmnm.

In the second line, print any array satisfying the condition and can be obtained from the given array with the minimum number of moves. The values of the elements of the resulting array must not exceed 10181018.

Examples

input

Copy

6 3
3 2 0 6 10 12

output

Copy

3
3 2 0 7 10 14 

input

Copy

4 2
0 1 2 3

output

Copy

0
0 1 2 3 

 

题意:

有数a[1]-a[n],有m是n的因子,每+1算1步,让c[r],r即a[i]%m,作为a数组每个数%m余r的个数,使得c[0]=……=c[m-1]=n/m至少需要几步。

 

嘤首先要看出来这是贪心。

贪心的思想是遍历过程中,如果a[[i]%m=r,且c[r]未达n/m,不需要+1,计入;否则,找到最近的未满的r加至那里。其正确性可以模拟一下,没有任何问题;如果不选择最近的数而是+1之类的,后面的再补上来,步数只会>=这种方案。

一开始直接只是贪心暴力写,T了。

然后就用set做二分:

将计够n/m的余数都放在set集合中,计数结束就从集合中删除,查找最近的未满余数时,用lower_bound做二分,记得特判如果是最大的(s.rbegin())就找最前面的这样子。

另外数据范围,开int是会wa的emmm

 

#include <cstdio>
#include <cstring>
#include <set>
#include <algorithm>
using namespace std;

const int maxn=2e5+20;
const int maxm=1e5+20;

int main()
{
	int n,m;
	long long a[maxn],r,x;
	long long c[maxn];	//为什么数据范围都要开这么大啊啊啊啊
	long long ans=0;
	set <long long> s;

	memset(c,0,sizeof(c));
	scanf("%d%d",&n,&m);
	for(int i=0;i<m;++i)
		s.insert(i);
	for(int i=1;i<=n;++i){
		scanf("%lld",&a[i]);
		if(m==1) continue;
		r=a[i]%m;
		if(r>*s.rbegin())
			x=*s.begin();
		else
			x=*s.lower_bound(r);//lower_bound(s.begin(),s.end(),r);
		if(++c[x]==n/m)
			s.erase(x);
		ans+=(x-r+m)%m;
		a[i]+=(x-r+m)%m;
	}
	printf("%lld\n",ans);
	for(int i=1;i<=n;++i)
		printf("%lld ",a[i]);

	return 0;
}

 

### Codeforces 题目解答思路与方法 #### Monsters and Spells 的解答思路 对于题目 *Monsters And Spells* ,其核心在于模拟怪物受到伤害的过程并判断最终能否击败所有怪物。此过程涉及到贪心算法的应用,具体来说是在每一轮攻击中尽可能多地减少怪物的生命值。 为了实现这一目标,可以先按照怪物初始生命值降序排列,然后依次处理每一个怪物,在每次施放技能时优先选择能造成最大伤害的方式。通过这种方式能够确保在有限的能量下最大化总伤害输出[^1]。 ```cpp #include &lt;bits/stdc++.h&gt; using namespace std; int main() { int t; cin &gt;&gt; t; while(t--) { long long n, h, a, b, k; cin &gt;&gt; n &gt;&gt; h &gt;&gt; a &gt;&gt; b &gt;&gt; k; vector&lt;pair&lt;long long,int&gt;&gt; monsters(n); for(int i = 0; i &lt; n; ++i){ cin &gt;&gt; monsters[i].first; // 生命值 monsters[i].second = i; } sort(monsters.rbegin(), monsters.rend()); // 按照生命值从高到低排序 bool canDefeatAll = true; for(auto&amp; m : monsters) { if(m.first &gt; someFunctionToCalculateDamage(h,a,b,k)){ canDefeatAll = false; break; } } cout &lt;&lt; (canDefeatAll ? &quot;YES\n&quot; : &quot;NO\n&quot;); } } ``` 上述代码片段展示了如何读取输入数据并对怪物按生命值进行排序,之后遍历这些已排序的数据来决定是否有可能战胜所有的敌人。 #### Sequence 数字序列生成逻辑分析 针对 *Sequence* 这一问题,则采取了一种完全不同的策略。考虑到直接计算会遇到性能瓶颈以及难以预测的结果模式,转而探索是否存在周期性的特性成为了解决方案的关键所在。经过观察发现随着数值的增长确实出现了重复现象,这意味着一旦找到了这样的循环节就可以快速定位任意位置上的元素而不必逐项构建整个列表[^3]。 ```python def find_nth_number(n): sequence = [] current_num = 1 seen = {} while True: str_form = &#39;&#39;.join(sorted(str(current_num))) if str_form in seen: loop_start_index = seen[str_form] non_loop_part_length = len(sequence[:loop_start_index]) relative_position_within_cycle = (n - non_loop_part_length - 1) % \ (len(sequence) - non_loop_part_length) return int(&#39;&#39;.join(sorted(str(sequence[relative_position_within_cycle])))) seen[str_form] = len(sequence) sequence.append(current_num) next_value_options = set([current_num * 2, int(&#39;&#39;.join(sorted(str(current_num))))]) current_num = min(next_value_options.difference(set(sequence)), default=current_num + 1) print(find_nth_number(15)) # 输出应为1156 ``` 这段 Python 实现首先尝试建立直到检测到第一个重复项为止的部分序列;接着利用模运算找到给定索引 `n` 对应在环内的确切位置,并据此返回相应的整数值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值