跳到正文

题解

题解:P16426 「YLLOI-R4-T2」听妈妈的话

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

文章信息

发布
2026年5月4日
收录
2026年5月5日
更新
2026年11月6日
篇幅
约 1 分钟
题目
P16426

版本

  1. v2当前
  2. v1

洛谷P16426

这道题就是一道典型的贪心,我们只需要让公鸡与母鸡相邻就好了喵。
但是要注意的一点就是egg们的孵化时间不一定小于 tt,所以要特判一下的。
然后关键点就在后来下蛋数量,每相邻的一对小鸡们在 tt 时间内孵的蛋为这个式子:

t−max(a[i],a[i+1])t - max(a[i], a[i+1])

最后累加起来就好了喵,还要注意开long long哦
代码如下

C++
#include <bits/stdc++.h>
//天翼3G太快辣
#define pb push_back
#define endl '\n'
#define NaHCO3 return
#define qwq 0;
#define Skywings
#define maxn 1000010
#define int long long
using namespace std;
int n, t, a[maxn], ans;
signed main() {
	cin >> n >> t;
	for (int i = 1; i <= n; i++) cin >> a[i];
	for (int i = 1; i < n; i++) {
		if (a[i] >= t || a[i+1] >= t) continue;
		ans += (t - max(a[i], a[i+1]));
	}
	cout << ans << endl;

	NaHCO3 qwq
}
原文