int a[100005],b[100005];//预处理坐标P到原点之间的地标个数 int aa[50005],bb[50005];//存储地标 int l,r; int lm,rm;//两端最远距离 int lc,rc;//两端地标个数 int ans=0; //1 2两种情况 if(t>=lm) ans=max(ans,lc); else ans=max(ans,a[t]); if(t>=rm) ans=max(ans,rc); else ans=max(ans,b[t]); //3 4两种情况 for(int i=1;i<=lc;i++){ if(t>aa[i]*2){//足够一个来回才有意义更新答案 int g=t-aa[i]*2; int res=a[aa[i]]; if(g>=rm)res+=rc;//如果返回后也超过另一方向 else res+=b[g]; ans=max(ans,res); } } for(int i=1;i<=rc;i++){ if(t>bb[i]*2){ int g=t-bb[i]*2; int res=b[bb[i]]; if(g>=lm)res+=lc; else res+=a[g]; ans=max(ans,res); } }
int n,m; int a[50]; int b[50];//a数组的前缀和 int vis[50]; int ans; //put * in position e voiddfs(int e,int d,int sum){//e是位置 d是深度 sum是局部答案 if(d==m){ ans=max(ans,sum*(b[n]-b[e])); return; } for(int i=e+1;i<=n;i++){ if(!vis[i]){ vis[i]=1; dfs(i,d+1,sum*(b[i]-b[e])); vis[i]=0; } } } dfs(0,0,1);//是k+1个数的乘积 所以sum初始化为1
int a[10005]; int ru[10005]; vector<int>d[10005]; int ans; int t[10005]; intdfs(int u){ if(t[u]!=0)//记忆化搜索 return t[u]; int res=0; for(int i=0;i<d[u].size();i++){ int v=d[u][i]; //u -> v res=max(res,dfs(v)); } return t[u]=res+a[u]; }
How to exchange the nodes without changing their values? from the side to another: u could see[pre, cur, cur->next] it’s easy to know when “cur” gets NULL, the “pre” is at the end of linked list. we could store “cur->next” in temp then makes “cur” at front of “pre” so now “pre” turned to “cur” and “cur” turned to the temp’s value(used to be “cur->next”)