codeforces 1561 C. Deep Down Below(1300)

该博客主要介绍了一种解决Codeforces编程竞赛中关于连通洞穴问题的算法。作者首先阐述了题目要求,即找到每个洞穴所需的最小战斗力和通关后的战斗力,并形成排序对。接着,他们提出了一个解决方案,通过排序和比较相邻洞穴的战斗力,调整战斗力以确保连通性。如果无法直接连通,则增加战斗力。最后,博客展示了实现该算法的C++代码片段,并输出了所需最小总战斗力。
摘要由CSDN通过智能技术生成

链接:https://codeforces.ml/problemset/problem/1561/C

题意:自己上机翻;

题解:

找出每个洞穴通关所需的最小战斗力和通关后的战斗力,组成一对,对所有这样的对子排序,然后能连起来,连不起来加战斗力,具体看代码;

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
bool bmp(pair<ll, ll>a, pair<ll, ll>b)
{
	return a.first < b.first;
}
pair<ll, ll>a[100005];
int main()
{
	ll t;
	cin >> t;
	while (t--)
	{
		ll m, n;
		cin >> m;
		ll cnt = 0;
		while (m--)
		{
			cin >> n;
			ll p = 0;
			for (ll i = 0; i < n; i++)
			{
				ll x;
				cin >> x;
				p = max(p, x - i + 1);
			}
			a[cnt].first = p;
			a[cnt].second = p + n;
			cnt++;
		}
		sort(a, a + cnt, bmp);
		//for (int i = 0; i < cnt; i++)
		//{
		//	cout << a[i].first << " " << a[i].second << endl;
		//}
		ll k = a[0].first;
		for (int i = 1; i < cnt; i++)
		{
			if (a[i - 1].second < a[i].first)
			{
				k += a[i].first - a[i - 1].second;
			}
			else
			{
				int temp = a[i].second - a[i].first;
				a[i].first = a[i - 1].second;
				a[i].second = a[i].first + temp;
			}
		}
		cout << k << endl;

	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值