bzoj1664

来源:互联网 发布:ftp同步数据库备份 编辑:程序博客网 时间:2024/05/18 17:24

标程 :
树状数组优化

#include<bits/stdc++.h>using namespace std;#define FOR(i,s,t) for(int i=(s);i<=(t);i++)inline int read(void){    int x = 0, c, f = 1;    do{c=getchar();if(c=='-')f=-1;}while(c<'0'||c>'9');    do{x=x*10+c-'0';c=getchar();}while(c>='0'&&c<='9');    return x * f;}const int N = 11000;struct node{    int x, y;    bool operator < (const node &t) const {        return x != t.x ? x < t.x : y < t.y;    }} e[N];int n, f[N];#define lb(x) (x&-x)const int M = 100000;int cnt[M + 10];inline void Updata(int pos, int x) {    for (int i = pos; i <= M; i += lb(i))        cnt[i] = max(cnt[i], x);}inline int Query(int pos) {    int ret = 0;    for (int i = pos; i; i ^= lb(i)) {        ret = max(ret, cnt[i]);    } return ret;}int main(){    n = read();    for (int i = 1; i <= n; i++) {        e[i].x = read(), e[i].y = e[i].x + read() - 1;    }    sort(e+1,e+n+1);    int ans = 0;    for (int i = 1; i <= n; i++) {        f[i] = Query(e[i].x - 1);        ans = max(ans, ++f[i]);        Updata(e[i].y, f[i]);    }    cout << ans << endl;}

数据范围出小了, 所以暴力踩标程:

#include<bits/stdc++.h>using namespace std;#define FOR(i,s,t) for(int i=(s);i<=(t);i++)inline int read(void){    int x = 0, c, f = 1;    do{c=getchar();if(c=='-')f=-1;}while(c<'0'||c>'9');    do{x=x*10+c-'0';c=getchar();}while(c>='0'&&c<='9');    return x * f;}const int N = 11000;struct node{    int x, y;    bool operator < (const node &t) const {        return x != t.x ? x < t.x : y < t.y;    }} e[N];int n, f[N];int main(){    n = read();    for (int i = 1; i <= n; i++) {        e[i].x = read(), e[i].y = e[i].x + read() - 1;    }    sort(e+1,e+n+1);    int ans = 0;    for (int i = 1; i <= n; i++) {        for (int j = i - 1; j; j--)            if (e[i].x > e[j].y) f[i] = max(f[i], f[j]);        ans = max(ans, ++f[i]);    }    cout << ans << endl;}