zoukankan      html  css  js  c++  java
  • 51Nod1110 距离之和最小 V3

    Problem

    X轴上有N个点,每个点除了包括一个位置数据X[i],还包括一个权值W[i]。点P到点P[i]的带权距离 = 实际距离 * P[i]的权值。求X轴上一点使它到这N个点的带权距离之和最小,输出这个最小的带权距离之和。

    Solution

    中位数最小,加权可以看成多个点。

    Code

    #include<iostream>
    #include<stdio.h>
    #include<algorithm>
    #include<map>
    #include<queue>
    #include<vector>
    #include<cstring>
    #include<stack>
    #define mem(ss) memset(ss,0,sizeof(ss))
    #define fo(d,s,t) for(int d=s;d<=t;d++)
    #define fo0(d,s,t) for(int d=s;d>=t;d--)
    typedef long long ll;
    typedef long double ld;
    typedef __int128 lll;
    const ll mod=1e9+7;
    #define io_opt ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
    using namespace std;
    ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
    inline int read(){int data=0;char ch=0;while (ch<'0' || ch>'9') ch=getchar();while (ch>='0' && ch<='9') data=data*10+ch-'0',ch=getchar();return data;}
    ll n,cnt1,cnt2,ansx,ans;
    struct E{
        int x,w;
    }e[10020];
    int cmp(E x,E y){
        return x.x<y.x;
    }
    int main(){
        io_opt;
        cin>>n;
        fo(i,1,n){
            cin>>e[i].x>>e[i].w;
            cnt1+=e[i].w;
        }
        sort(e+1,e+1+n,cmp);
        cnt1=cnt1/2+1;
        fo(i,1,n){
            cnt2+=e[i].w;
            if(cnt2>=cnt1){
                ansx=e[i].x;
                break;
            }
        }
        fo(i,1,n){
            ans+=abs(ansx-e[i].x)*e[i].w;
        }
        cout<<ans<<endl;
        return 0;
    }
    
  • 相关阅读:
    基于应用外壳的架构
    示例代码和环境配置
    获取元素位置信息:getBoundingClientRect
    nodejs学习记录
    网页整理 --- 要换工作了,把这一堆网页先存起来
    删除网页上的广告
    周数的处理
    十六进制
    [例]字体改变,文章位置不变
    haslayout和BFC
  • 原文地址:https://www.cnblogs.com/sz-wcc/p/11663881.html
Copyright © 2011-2022 走看看