所以代码只通过了例子,不保证代码能ac #include <iostream> #include <cmath> using namespace std; int dis(int mid_x, int mid_y, int x[], int y[],int start,int end) { int step = 0; for (int i=start;i<end;i++){ step += abs(mid_x - x[i]) + abs(mid_y - y[i]); } return step; }; int get_step(int x[], int y[],int start,int end){ int min_x = 1000; int max_x = 0; int min_y = 1000; int max_y = 0; for (int i=start;i<end;i++){ max_x = max(x[i],max_x); min_x = min(x[i],min_x); max_y = max(y[i],max_y); min_y = min(y[i],min_y); } int mid_x = (min_x + max_x)/2; int mid_y = (min_y + max_y)/2; int last_mid_x = mid_x+1; int last_mid_y = mid_y+1; int min_step=10000; while(max_x-min_x>=1){ while (max_y-min_y>=1){ if (dis(mid_x,mid_y,x,y,start,end)>dis(mid_x,max_y,x,y,start,end)){ min_y = mid_y; }else{ max_y = mid_y; } min_step = min(dis(mid_x,mid_y,x,y,start,end),dis(mid_x,max_y,x,y,start,end)); last_mid_y = mid_y; if (last_mid_y==(min_y + max_y)/2){ break; } mid_y = (min_y + max_y)/2; }; if(dis(mid_x,mid_y,x,y,start,end) > dis(max_x,mid_y,x,y,start,end)) min_x = mid_x; else max_x = mid_x; min_step = min(dis(mid_x,mid_y,x,y,start,end) , dis(max_x,mid_y,x,y,start,end)); last_mid_x = mid_x; if (last_mid_x==(min_x + max_x)/2){ break; } mid_x = (min_x + max_x)/2; } return min_step; } void get_steps(int x[], int y[],int n) { cout<<0<<' '; for (int i=2;i<n;i++){ int min_step = 1000; for(int j=0;j<n-i;j++){ min_step = min(min_step,get_step(x,y,j,j+i)); } cout<<min_step<<' '; } cout<<get_step(x,y,0,n)<<endl; }; int main() { int n; cin >> n; int x[n], y[n]; for (int j = 0; j < n; j++) { cin >> x[j]; } for (int j = 0; j < n; j++) { cin >> y[j]; } get_steps(x,y,n); return 0; }
点赞 7

相关推荐

11-06 10:58
已编辑
门头沟学院 嵌入式工程师
双非25想找富婆不想打工:哦,这该死的伦敦腔,我敢打赌,你简直是个天才,如果我有offer的话,我一定用offer狠狠的打在你的脸上
点赞 评论 收藏
分享
牛客网
牛客企业服务