Party All the Time
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Problem Description
In the Dark forest, there is a Fairy kingdom where all the spirits will go together and Celebrate the harvest every year. But there is one thing you may not know that they hate walking so much that they would prefer to stay at home if they need to walk a long way.According to our observation,a spirit weighing W will increase its unhappyness for S3*W units if it walks a distance of S kilometers.
Now give you every spirit's weight and location,find the best place to celebrate the harvest which make the sum of unhappyness of every spirit the least.
Input
The first line of the input is the number T(T<=20), which is the number of cases followed. The first line of each case consists of one integer N(1<=N<=50000), indicating the number of spirits. Then comes N lines in the order that x[i]<=x[i+1] for all i(1<=i<N). The i-th line contains two real number : Xi,Wi, representing the location and the weight of the i-th spirit. ( |xi|<=106, 0<wi<15 )
Output
For each test case, please output a line which is "Case #X: Y", X means the number of the test case and Y means the minimum sum of unhappyness which is rounded to the nearest integer.
Sample Input
1
4
0.6 5
3.9 10
5.1 7
8.4 10Sample Output
Case #1: 832
题意
小精灵不愿意走路,走路的话会使他们产生不高兴值,如果小精灵走的路是S体重是W那么他走S路程产生的不高兴值是ans=(S^3)*W;现在森林里要开一个庆祝会,给出各个小精灵的坐标(注意这里是一维的)和体重,求在哪里庆祝会使小精灵的不高兴值最小,输出这个最小的不高兴值。样例中第一行是样例个数,每个样例的第一行是小精灵的个数n,接下的n行是每个小精灵的坐标和体重。输出只有一个整数,即最小的不高兴值(四舍五入)。
解题思路
三分模板题
AC代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5+5;
ll pnum[maxn],nnum[maxn]; // 存正数和负数
bool cmp(ll x,ll y)
{
return abs(x) > abs(y);
}
int main()
{
int n,m;
int p=1,ne=1;
ll t;
scanf("%d %d",&n,&m);
for(int i=1;i<=n+m+1;i++) {
scanf("%lld",&t);
if(t >= 0)
pnum[p++] = t;
else
nnum[ne++] = t;
}
sort(pnum+1,pnum+p,cmp);
sort(nnum+1,nnum+ne,cmp);
ll ans=0;
if(ne-1 >= m) {
for(int i=1;i<=m;i++)
ans -= nnum[i];
for(int i=m+1;i<ne;i++)
ans += nnum[i];
for(int i=1;i<p;i++)
ans += pnum[i];
}
else {
for(int i=1;i<ne;i++)
ans -= nnum[i];
ans -= pnum[p-1];
for(int i=1;i<p-1;i++)
ans += pnum[i];
}
printf("%lld",ans);
}