Solution
and this tutorial on Binary Indexed Tree...
Tutorial
binary indexed tree in this problem is used to get V
state:
dp[i] - minimum dolls sequence at position i, the same with LIS's state
transition:
dp[i]=max(dp[i], V+1)
V is max(dp[k]), for all k<i
/* UVas */
/*****************************************************************************/
struct doll {
int d[2];
inline bool operator<(const doll &o) const {
if (d[0]==o.d[0])
return d[1]>o.d[1];
return d[0]<o.d[0];
}
} D[20000];
int t, m;
int read(int i, int *a) {
int r=0;
while (i) {
r=max(r, a[i]);
i-=i&(-i);
}
return r;
}
void updt(int i, int *a, int l, int arg) {
while (i<=l) {
a[i]=max(a[i], arg);
i+=i&(-i);
}
}
int main(void) {
scanf("%d", &t);
while (t--) {
scanf("%d", &m);
for (int i=0; i<m; i++)
scanf("%d %d", &D[i].d[0], &D[i].d[1]);
sort(D, D+m);
int d[20000]={};
int h[2][10001]={};
int r=0;
for (int i=0; i<m; i++) {
for (int j=0; j<2; j++) {
int f=read(10001-D[i].d[j], h[j]);
d[i]=max(d[i], f+1);
updt(10001-D[i].d[j], h[j], 10001, d[i]);
}
r=max(r, d[i]);
}
printf("%d\n", r);
}
}
/*****************************************************************************/
No comments :
Post a Comment