Sweety

Practice makes perfect

导航

第五届山东省ACM Weighted Median

Posted on 2016-05-11 22:42  蓝空  阅读(172)  评论(0编辑  收藏  举报

Weighted Median


简单排序求和

不过这里有个处理0.5这样的数小技巧,先乘2,之后按照整数操作就行了。

#include<cstdio>  
#include<cstring>  
#include<cmath>  
#include<cstdlib>  
#include<iostream>  
#include<algorithm>  
#include<vector>  
#include<map>  
#include<queue>  
#include<stack>  
#include<string>  
#include<map>  
#include<set>  
#include<ctime>  
#define eps 1e-6  
#define MAX 10000005  
#define INF 0x3f3f3f3f  
#define LL long long  
#define pii pair<string,int>  
#define rd(x) scanf("%d",&x)  
#define rd2(x,y) scanf("%d%d",&x,&y)  
const int dir[][2] = { {-1, 0}, {0, -1}, { 1, 0 }, { 0, 1 } };  
using namespace std;  
  
struct T{  
  int x;  
  int w;  
}arr[MAX];  
  
bool cmp(T a,T b){  
      return a.x<b.x;  
}  
int main(){  
  int n;  
  LL sum,temp;  
  int s;  
  while(~rd(n)){  
    sum=0;  
    temp=0;  
    for(int i=0;i<n;i++)  
        rd(arr[i].x);  
    for(int i=0;i<n;i++)  
        rd(arr[i].w);  
    for(int i=0;i<n;i++)  
        sum+=arr[i].w,arr[i].w=arr[i].w<<1;  
    sort(arr,arr+n,cmp);  
    s=arr[0].x;  
    for(int i=0;i<n,temp<sum;s = arr[i].x,i++)  
        temp+=arr[i].w;  
  
    printf("%d\n",s);  
  }  
  return 0;  
}