gpt4 book ai didi

Find the minimum possible number of elements greater than x that can be left after performing m operations of incrementing n elements(找出在执行递增n个元素的m个操作后可以剩余的大于x的最小可能数量的元素)

转载 作者:bug小助手 更新时间:2023-10-27 20:05:18 25 4
gpt4 key购买 nike



Given the x, n, m, and the sorted array a (each element of a is not greater than x). One operation is incrementing n elements of a (adding 1 to each of chosen element). The question is, what is the minimum number of elements that are greater than x can be left after performing m operations?

给定x、n、m和排序数组a(a的每个元素不大于x)。一种操作是递增a的n个元素(所选元素的每个加1)。问题是,在执行m次运算后,可以留下的大于x的元素的最小数量是多少?


Is it possible to find the answer without actually modifying the array / creating another array?
I tried to work out a formula: n - ((len(a) * x - sum(a)) / m), but it doesn't seem to be correct. If I were to solve this manually by hand, I would just use the sliding window from the back of the array, making all the elements equal to x, but I don't think I can afford doing this in program due to the time limits. So any piece of advice will be useful, thanks.

有没有可能在不实际修改数组/创建另一个数组的情况下找到答案?我试着算出一个公式:n -((len(a)* x - sum(a))/ m),但似乎不正确。如果我手动解决这个问题,我会从数组的后面使用滑动窗口,使所有元素都等于x,但由于时间限制,我认为我不能在程序中这样做。任何建议都是有用的,谢谢。


更多回答

Assuming that you can chose any n elements (not just a contiguous subarray), then the correct answer is to find the y elements closest to x, and increment those elements in every operation. Where y is the answer you're looking for.

假设您可以选择任何n个元素(不仅仅是连续的子数组),那么正确的答案是找到最接近x的y个元素,并在每次操作中递增这些元素。其中y是您要寻找的答案。

@user3386109 but you need to increment exactly n elements each time. Plus, choosing only those elements that are the closest and incrementing them all the way m times isn't a good idea obviously.

@user3386109,但每次需要恰好递增n个元素。此外,只选择那些最接近的元素,并将它们一直递增到m倍,显然不是一个好主意。

I didn't mean to imply that you only increment y elements. Certainly you need to increment n elements in each operation. But the y elements that will be greater than x should be incremented all m times.

我的意思并不是说您只增加y元素。当然,您需要在每个操作中增加n个元素。但是大于x的y元素应该全部递增m次。

优秀答案推荐

The total amount of incrementing to be done is n*m.

要完成的递增总数为n*m。


The best way to arrange it to to divide the array a into three regions.

排列它的最佳方式是将阵列a分成三个区域。



  1. The area where a[i] + m <= x. They get incremented every time, and will absorb m of the increments.

  2. The area where x < a[i] + m but we won't increment them past x. They can absorb x - a[i] of the increments.

  3. The area where x < a[i] + m but we WILL increment them past x. They can be incremented every time and will absorb m of the increments. Which is m - x + a[i] more increments than when included in the second region.


If a has fewer than n elements, throw an exception. The problem is impossible. We can't find n elements in a for a single update.

如果a的元素少于n个,则抛出异常。这个问题是不可能的。对于单个更新,我们在a中找不到n个元素。


So working from the smallest to the largest we figure out how many increments are absorbable by putting things into the first and second groups. If that is enough, the answer is 0.

所以从最小到最大,我们计算出有多少增量是通过把东西放进第一组和第二组来吸收的。如果这是足够的,答案是0。


If we need more incrementing, go through a from the largest to the smallest element. Keep adding m - x + a[i] until we've reached or exceeded n * m increments. The number that you put into group 3 is your answer.

如果我们需要更多的增量,从最大的元素到最小的元素。继续增加m-x+a[i],直到我们达到或超过n*m个增量。你在第三组输入的数字就是你的答案。


更多回答

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com