我又来发帖子了
mid=(l+r+1)>>1 和不加1有什么区别(除了更新l和r上的)
有的题目前者对后者不对,有的反之,我一直的理解是+1适合用来求出最大值,不加一适合求出最小值。
(fi代表的是长度为i的结尾值最小的)
但是我在求最大上升子序列的时候我认为二分f[i]是为了求出最大的i所以就用了+1的更新方式但是错了,改成不加一的就对了。
题目是DP题目
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6;
int n,jis,f[N],cnt;
struct node{
int a,b;
}w[100010];
bool cut(node q,node p){
if(q.a!=p.a) return q.a>p.a;
return q.b>p.b;
}
bool judge(int x,int w)
{
if(w>f[x]) return 1;
return 0;
}
signed main(){
cin>>n;
for(int i=1;i<=n;i++) cin>>w[i].a>>w[i].b;
sort(w+1,w+1+n,cut);
//以下是DP
f[1]=w[1].b;cnt++;
for(int i=2;i<=n;i++)
{
int l=1,r=cnt+1,mid;
f[r]=-1;
while(l<r)
{
mid=(l+r)>>1;
if(judge(mid,w[i].b)) l=mid+1;
else r=mid;
}
f[l]=w[i].b; cnt=max(cnt,l);
}
cout<<cnt;
}