Codeforces Round #850 (Div. 2, based on VK Cup 2022 - Final Round)C. Monsters (easy version)

题目描述:

In a computer game, you are fighting against n monsters. Monster number i has ai health points, all ai are integers. A monster is alive while it has at least 1 health point.

You can cast spells of two types:

Deal 1 damage to any single alive monster of your choice.

Deal 1 damage to all alive monsters. If at least one monster dies (ends up with 0 health points) as a result of this action, then repeat it (and keep repeating while at least one monster dies every time).

Dealing 1 damage to a monster reduces its health by 1.

Spells of type 1 can be cast any number of times, while a spell of type 2 can be cast at most once during the game.

What is the smallest number of times you need to cast spells of type 1 to kill all monsters?

大意:

在电脑游戏中,你正在与n个怪物战斗。怪物编号i有ai生命值,所有ai都是整数。怪物至少有1点生命值时仍然活着。

你可以施放两种法术:

对你选择的任何一个活着的怪物造成1点伤害。

对所有活着的怪物造成1点伤害。如果至少有一个怪物因该动作死亡(最终获得0点生命值),则重复该动作(并在每次至少有一只怪物死亡时继续重复)。

对怪物造成1点伤害会降低其生命值1。

1类法术可以施放任意次数,而2类法术在游戏中最多只能施放一次。

为了杀死所有怪物,你需要施放1类法术的最小次数是多少?

输入格式

Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤10^4). The description of the test cases follows.

Each test case consists of two lines. The first line contains a single integer n (1≤n≤2⋅10^5) — the number of monsters.

The second line contains n integers a1,a2,…,an (1≤ai≤n) — monsters' health points.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅10^5.

每个测试包含多个测试用例。第一行包含测试用例数t(1≤t≤10^4)。测试用例的描述如下。

每个测试用例由两行组成。第一行包含一个整数n(1≤n≤2⋅10^5)-怪物的数量。

第二行包含n个整数a1,a2,…,an(1≤ai≤n)-怪物的生命值。

保证所有测试用例的n之和不超过2⋅10^5。

输出格式:

For each test case, print a single integer — the smallest number of times you need to cast spells of type 1 to kill all monsters.

对于每个测试用例,如果无法进行这样的配对,请打印“否”。

对于每一个测试用例,打印一个整数-您需要使用1类法术来杀死所有怪物的最小次数。

输入输出样例

输入 #1

2

3

3 1 2

6

4 1 5 4 1 1

输出 #1

0

4

思路:

将生命值排序,确保后一个的生命值比前面多1或0(当前一个与这个相同,且前一个未变的时候),并将操作次数求和即可

代码:

#include<bits/stdc++.h>
using namespace std;


int main(){
	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		int a[n+1]={};
		for(int i=1;i<=n;i++){
			cin>>a[i];
		}
		sort(a+1,a+n+1);
		long long sum=0;
		for(int i=1;i<=n;i++){
			if(a[i]-a[i-1]<=1){
				continue;
			}
			else{
				sum+=a[i]-a[i-1]-1;
				a[i]=a[i-1]+1;
			}
		}
		printf("%lld\n",sum);
	}

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值