打卡第一天 - 洛谷P1868 饥饿的奶牛 - 2026 - 6 - 14
AC代码:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int M = 3e6 + 6; // 右边界
vector<int> a[M]; // 表示以i结尾的左边界
long long f[M];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m_l = 0;
cin >> n;
for (int i = 1; i <= n; i++) {
int x, y;
cin >> x >> y;
x++;
y++;
m_l = max(m_l, y);
a[y].push_back(x);
}
for (int i = 1; i <= M; i++) {
long long t = 0;
for (auto j : a[i])
t = max(t, f[j - 1] + i - j + 1);
f[i] = max(f[i - 1], /* 不吃这一堆草 */ t);
}
cout << f[m_l];
return 0;
}
因为第一次,比较仓促,就先只贴AC代码吧

浙公网安备 33010602011771号