SGU 114

來源:互聯網
上載者:User

標籤:blog   io   for   div   amp   size   log   return   

分析得,選取的電視塔的位置應當位於各城市的加權中位元位置的右邊最近的城市位置。

理由:對於上述位置A,假設它右邊城市+它本身的人數為rp,它左邊城市的人數為lp,由定義rp>=lp。

所以,如果向左移動D距離,則右邊城市不滿意度增加D*rp,左邊減少D*lp<=D*rp,不合算。

同理,向右移動D距離時,同樣有D*rp<=D*lp,不合算。

所以,上述位置為最佳位置。

輸出時應注意輸出格式:有五位小數。

#include "stdio.h"#include "string.h"#include "stdlib.h"struct city{int p,x;}c[15010];int cmp(const void *a,const void *b){return (*(city *)a).x-(*(city *)b).x;}int main(){int i,N,sum=0,tmp=0,dist=0;scanf("%d",&N);for(i=0;i<N;i++){scanf("%d%d",&c[i].x,&c[i].p);sum+=c[i].p;}qsort(c,N,sizeof(c[0]),cmp);for(i=0;i<N;i++){tmp+=c[i].p;if(tmp>=sum/2+sum%2){printf("%d.00000",c[i].x);break;}}return 0;}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.