Cows party

Title Description

Lunar New Year is coming soon, cows plans to hold a party to celebrate the New Year. However, cows do not like to go too far from the road, this will give their party a negative mood, when a cow negative index Wi, he participated in walking distance to the desired party si, then he will give the party bring Si3 * Wi negative emotions. All locations where the cows are in a straight line, all cows known coordinates and negative index, seeking to determine how the meeting place, so that all parties bring cows to minimize the sum of negative emotions, negative emotions minimum output sum.

Entry

The first row contains an integer Ca (Ca <= 20), expressed Ca set of test data.

For each test: The first line contains an integer n (1 <= n <= 50000), represents the number of cows. The next n lines contains two floating-point Si and wi (-106 <= Si <= 106, 0 <Wi <15).

Export

For each test case, the output "Case #c: ans", where c represents the number of test data, the minimum value of negative emotions of the ANS and the result is rounded to an integer.

Sample input

1
5
0.9 2
1.4 4
3.1 1
6.2 1
8.3 2

Sample Output

Case #1: 300

Plus about convex function convex function convex function equal to one-third, bare, you can define your own use permit

#include<iostream>
#include<cstdio>
#include<cstring>
#define ll long long
#define ld double
using namespace std;
int T,n;
ld sm1=0,sm2=0,sm3=0,sm4=0;
ld w[50020],s[50020];
ld abs(ld x){
    return x>0?x:-x;
}
ld pow(ld x,int k){
    ld cur=1;
    for(int i=1;i<=k;i++){
        cur*=x;
    }
    return cur;
}
ll ans(ld x){
    ld cur=x-(ll)x;
    if(cur<0.5) return (ll)x;
    return (ll)x+1;
}
ld smstp(ld ss){
    ld cur=0;
    for(int i=1;i<=n;i++){
        cur+=pow(abs(s[i]-ss),3)*w[i];
    }
    return cur;
}
int main(){
    /*ld pla;
     
    for(pla=0.9;pla<=8.3;pla+=0.1)
        cout<<pla<<":"<<ans(pow(abs(0.9-pla),3)*2+pow(abs(1.4-pla),3)*4+pow(abs(3.1-pla),3)*13+pow(abs(6.2-pla),3)*1+pow(abs(8.3-pla),3)*2)<<endl;
    */
    cin>>T;
    for(int I=1;I<=T;I++){
        memset(s,0,sizeof(s));
        memset(w,0,sizeof(w));
        scanf("%d",&n);
        ld l=1e7,r=-1e7;
        for(int i=1;i<=n;i++){
            scanf("%lf%lf",&s[i],&w[i]);
            //cin>>s[i]>>w[i];
            //cout<<s[i]<<' '<<w[i]<<endl;
            l=min(s[i],l);
            r=max(s[i],r);
        }
        //cout<<l<<" "<<r<<endl;
        //cout<<r-l<<endl;
        while(r-l>0.00001){
            ld m1=l+(r-l)/3.0;
            ld m2=r-(r-l)/3.0;
            if(smstp(m1)<smstp(m2)){
                r=m2;
            }
            else{
                l=m1;
            }//cout<<(int)l<<" "<<(int)r<<endl;
        }
        cout<<"Case #"<<I<<": "<<ans(smstp(l))<<endl;
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/sz-wcc/p/11071981.html