Three-school joint training [NOIP simulation] to find

topic

"I have a wish, I hope to find you through everything."

This is a two-dimensional plane world. There are n special fruits on the plane. I start from the (0,0) point and hope to get as many fruits as possible, but for some special reason, I have only three ways of movement ( Suppose I am currently at (x,y)):

1. I can go to (x+1,y)

2. I can go to (x,y+1)

3. I can go to (x+1, y+1)

Now I need your help to find out the maximum number of fruits I can get.

For 70% of the data 1<=n<=1000

For 100% data 1<=n<=100000, -10^9<=x,y<=10^9

 

answer:

I discretized it and sorted it by x, and I was looking for the LIS of y, using tree-like array optimization.

#include <bits/stdc++.h>
using namespace std;
struct haha{
    int x;
    int y;
}lala[100010],fin[100010],tmp[100010];
bool cmp(haha x,haha y)
{
    if(x.x==y.x) return x.y<y.y;
    return x.x<y.x;
}
bool cmp2(haha x,haha y)
{
    if (xy==yy) return xx< yx;
    return xy< yy;
}
int n;
inline int lowbit(register int x)
{
    return x&(-x);
}
int c[1000010];
inline void add(register int x,register int v)
{
    while(x<=n){
        c[x]=max(c[x],v);
        x+=lowbit(x);
    }
    return;
}
inline int ask(int x)
{
    register int res=0;
    while(x>0){
        res=max(res,c[x]);
        x-=lowbit(x);
    }
    return res;
}
int f[100010];
int main()
{
    cin>>n;
    int num=0;
    for(register int i=1;i<=n;i++){
        int a,b;
        scanf("%d%d",&a,&b);
        if(a<0||b<0){
            continue;
        }
        lala[ ++num].x= a;
        lala[num].y = b;
    }
    n = num      
    sort(lala+1,lala+1+n,cmp);
    for(register int i=1;i<=n;i++){
        tmp[i]=lala[i];
        tmp[i].x=i;
    }
    sort(tmp+1,tmp+1+n,cmp2);
    tmp[0].y=999999999;
    for(register int i=1;i<=n;i++){
        fin[i]=tmp[i];
        if(tmp[i].y==tmp[i-1].y){
            end[i].y =end[i- 1 ].y;
        }
        else{
            end[i].y = i;
        }
    }
    sort(fin+1,fin+1+n,cmp);
    for(register int i=1;i<=n;i++){
        int now=fin[i].y;
        int found=ask(now);
        int op=found+1;
        f [i] = op;
        add(now,op);
    }
    register int maxn=0;
    for(register int i=1;i<=n;i++){
        maxn=max(maxn,f[i]);
    }
    cout<<maxn;
}
/*
8
0 1
1 0
1 5
2 5
3 4
3 1
5 1
5 4
*/

 

Reprinted in: https://www.cnblogs.com/kamimxr/p/11563495.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326446583&siteId=291194637