【贪心,LIS】洛谷P1233 木棍加工

链接

https://www.luogu.org/problemnew/show/P1233

大意

给定一些木棍的长度和宽度,现要安排加工这些木棍的顺序,使得总等待时间最少

思路

这道题有点像球赛。。。

首先,我们现满足长度和宽度中的一个,为什么这个一定成立呢?因为长度接上与宽度加上对答案的贡献都是1,那么我们尽可能使其中一种先满足,在求出最多能满足另一种的方案,其实要满足另一种的方案,能接上的长度即为最长上升序列的长度,而我们是要求不能接上的长度,所以我们可以求出最长不上升序列即为最终答案

代码

#include<cstdio>
#include<algorithm>
using namespace std;int n,f[5001],ans;
struct node
{
    int st,ed;
}w[5001];
inline bool cmp(node x,node y){return x.st>y.st;}
signed main()
{
    scanf("%d",&n);
    for(register int i=1;i<=n;i++) scanf("%d%d",&w[i].st,&w[i].ed);
    sort(w+1,w+1+n,cmp);//排序
    for(register int i=1;i<=n;i++)
    if(w[i].ed>f[ans]) f[++ans]=w[i].ed;
    else *lower_bound(f+1,f+1+ans,w[i].ed)=w[i].ed;
    printf("%d",ans);//输出
}

猜你喜欢

转载自blog.csdn.net/xuxiayang/article/details/81669840
今日推荐