很牛很牛的贪心
jiazhichen844 · · 题解
链接
我们记
诶不是,这是不是直接贪贪就绷住了……诶不对,怎么贪啊?
一种想法是:我们设当前还未拍的照片对应的区间是
这个问题最早在 1981 年被 Garey,Johnson,Simons 和 Tarjan 解决并证明,以下是他们给出的做法。
考虑一件事情,若存在区间
像
但是,考虑两个照片的区间
容易发现,任意一个照片的集合
我们考察禁止区间间的包含关系,发现本质不同的右端点只有
我们从大往小枚举
如何维护此信息?
如果扫每个
挂一下原题解:solution。
时间复杂度
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,t;
struct node
{
int l,r;
bool operator < (const node a) const
{
if(l==a.l)
return r<a.r;
return l<a.l;
}
}a[10005];
int r[10005],l[10005],st[10005],p[10005];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>t;
for(int i=1;i<=n;i++)
cin>>a[i].l>>a[i].r,r[i]=a[i].r;
sort(r+1,r+n+1,greater<int>()),sort(a+1,a+n+1);
for(int i=1;i<=n;i++)
l[i]=a[i].l,st[i]=r[i],p[i]=n;
for(int i=n;i>=1;i--)
{
for(int j=1;j<=n;j++)
{
if(r[j]>=a[i].r)
{
st[j]-=t;
while(p[j]>i&&st[j]<a[p[j]].l)
{
st[j]=min(st[j],l[p[j]]);
p[j]--;
}
if(st[j]<a[i].l)
{
cout<<"no\n";
return 0;
}
l[i]=min(l[i],st[j]-t);
}
}
}
cout<<"yes\n";
}