标签:round for ble void 排序 离散化 date while eof
ss
1010 YJJ‘s Salesman(dp,树状数组,降维,离散化)
将二维降成一维,一般就是按照第一维度排好序,然后扫描点,此时某点之前扫描过的点的第一维度不会大于该点的第一维!这样就不用考虑第一维度了!!!
#include<bits/stdc++.h> #define per(i,a,b) for(int i=a;i<=b;i++) #define mod 1000000007 using namespace std; typedef long long ll; const int inf =0x3f3f3f3f; const double eps=1e-8; #define siz 100005 struct Node{int x,y,v;}node[siz]; int T,n,num[siz],dp[siz],a[siz],tot; void init() { memset(num,0,sizeof(num)); memset(dp,0,sizeof(dp)); } int lb(int u){return u&-u;} void update(int u,int k) { for(int i=u;i<=tot;i+=lb(i)){ num[i]=max(num[i],k); } } int get_query(int u) { int res=0; for(int i=u;i>0;i-=lb(i)){res=max(res,num[i]);} return res; } bool cmp(Node a,Node b){if(a.x==b.x)return a.y<b.y;else return a.x<b.x;} int main() { scanf("%d",&T); while(T--){ init(); scanf("%d",&n); per(i,1,n){scanf("%d %d %d",&node[i].x,&node[i].y,&node[i].v);} //离散化,记录,排序,去重,重新赋值 tot=0; for(int i=1;i<=n;i++)a[tot++]=node[i].y; sort(a,a+tot); tot=unique(a,a+tot)-a;//离散化,即只把用到的点记录下来,然后把原来的值换成新的离散值,减小循环量 for(int i=1;i<=n;i++){node[i].y=lower_bound(a,a+tot,node[i].y)-a+1;} sort(node+1,node+n+1,cmp); per(i,1,n)dp[i]=node[i].v;// int ans=0,p=1; for(int i=1;i<=n;i++){ while(p<i && node[p].x<node[i].x){ update(node[p].y,dp[p]); p++; } dp[i]=get_query(node[i].y-1)+node[i].v; ans=max(ans,dp[i]); } printf("%d\n",ans); } return 0; }
标签:round for ble void 排序 离散化 date while eof
原文地址:https://www.cnblogs.com/WindFreedom/p/9535877.html