标签:
这是道带权中位数的题,自己一想好像暑假的时候在 noi-openjudge 那个网站写题的时候看过类似的,那个时候的我蒟蒻, 不知道怎么办? 现在重新想起了这一类题,找个时间攻一下吧!毕竟这道题不难, (只要从左往右扫一次,和从右往左扫一次,我再一次可耻地看了题解),思考了一下二维的情况,应该不难,只是把x坐标和y坐标分开而已,加油!
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #define rep(i,j,k) for(int i = j; i <= k; i++) 5 #define down(i,j,k) for(int i = j; i >= k; i--) 6 using namespace std; 7 8 struct node{ 9 double lc, lp, rc, rp, dist, s, p; 10 bool operator < (const node& rhs ) const { 11 return dist < rhs.dist; 12 } 13 string name; 14 } c[6000]; 15 16 int main() 17 { 18 int n = 0, ans = 0; 19 while( cin>>c[n].p>>c[n].dist>>c[n].name ){ 20 n++; 21 } 22 sort(c,c+n); 23 c[0].lp = c[0].p, c[0].lc = 0, c[n-1].rc = 0, c[n-1].rp = c[n-1].p; 24 rep(i,1,n-1){ 25 c[i].lp = c[i-1].lp + c[i].p; 26 c[i].lc = c[i-1].lc + (c[i-1].lp * (c[i].dist - c[i-1].dist)); 27 } 28 down(i,n-2,0){ 29 c[i].rp = c[i+1].rp + c[i].p; 30 c[i].rc = c[i+1].rc + (c[i+1].rp * (c[i+1].dist - c[i].dist)); 31 32 } 33 rep(i,1,n-1){ 34 if( c[i].lc + c[i].rc < c[ans].lc+ c[ans].rc ) ans =i; 35 } 36 cout<<c[ans].name<<endl; 37 return 0; 38 }
标签:
原文地址:http://www.cnblogs.com/83131yyl/p/5094353.html