思路:右端点从小到大遍历,靠右种树。
卡在这个循环for (int k = a[i].r; k >= a[i].l && cnt>0; k–)
…&&cnt>0 ✔
…&&cnt❌
// https://loj.ac/problem/10001
#include <bits/stdc++.h>
#define N 5005
using namespace std;
bool used[30004];
struct node
{
int l, r, w;
bool operator<(const node &b) const
{
return r < b.r;
}
} a[N];
int main()
{
int h, n;
cin >> n >> h;
memset(used, 0, sizeof(used));
for (int i = 0; i < h; i++)
{
cin >> a[i].l >> a[i].r >> a[i].w;
}
sort(a, a + h);
int ans = 0;
for (int i = 0; i < h; i++)
{
int cnt = a[i].w;
for (int k = a[i].r; k >= a[i].l ; k--)
{
cnt -= used[k];
}
// cout << cnt << endl;
for (int k = a[i].r; k >= a[i].l && cnt>0; k--)
{
if (!used[k])
{
used[k] = 1;
ans++;
cnt--;
}
}
}
cout << ans<< endl;
system("pause");
return 0;
}