Family Gathering at Christmas(思维题)

时间:2023-11-25 19:21:02

Family Gathering at Christmas

时间限制: 1 Sec  内存限制: 128 MB
提交: 13  解决: 4
[提交] [状态] [讨论版] [命题人:admin]

题目描述

Every year, Alice’s family has a gathering at Christmas, at a family member’s place. The members would like to choose a host so that every member can reach the host’s place without spending too much time, where the time for traveling depends on the geographical distance between the two places and the way of transportation. For example, going to a place on foot usually takes longer time than by bicycle. Since a host has to spend much effort to get everything ready, the family decides not to have the gathering at one’s place so often. For members who are not the host, they have to prepare a dish for the party. However, all of them are too busy to cook on their own so they always go to the central city to buy a dish right before the party.
Alice suggests a way for her family to determine the gathering place. First, she associates each member with a weight, which quantifies the way of transportation. Then, the time needed for each non-host member m to reach the host is
wm · (dm + dh ),

where wm is the weight of the member, d m is the distance from the member’s place to the central city, and d h is the distance from the central city to the host’s place. Then she associates each member’s place with a key, which is the longest time needed for a non-host member to reach the place. To decide the host, she picks a small number k, and choose a place with the kth smallest key. Please develop an efficient algorithm to help Alice find the kth smallest key.

输入

The first line of the input is the number of instances to be tested. There are at most 10 instances. An instance consists of 3 lines. The first line contains two integers, n and k.
The second line contains n integers, w1 , . . . , wn , and the third line contains d1 , . . . , dn . Two consecutive integers in a line are separated by a space.

输出

The output for each instance is an integer, which is the kth smallest key.

样例输入

2
3 2
5 3 4
3 8 5
4 2
6 2 8 2
10 18 12 4

样例输出

40
132
有的人用线段树做的,懂不起!
AC代码:
 #include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef struct{
int su,wei;
}my;
int n,k;
my f[];
int comp(my x,my y)
{
if(x.wei<y.wei)
return ;
return ;
}
long long ans1,ans2,ans3,num,maxn,minn;
int t1;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)
scanf("%d",&f[i].su);
for(int i=;i<=n;i++)
scanf("%d",&f[i].wei);
sort(f+,f+n+,comp);
ans1=,t1=;
for(int i=;i<=n;i++)
if(i!=k)
{
if(ans1<1LL*f[i].su*(f[i].wei+f[k].wei))
ans1=1LL*f[i].su*(f[i].wei+f[k].wei),t1=i;
}
if(t1>k)
{
ans2=;
for(int i=;i<=n;i++)
if(i!=t1)
{
if(ans2<1LL*f[i].su*(f[i].wei+f[t1].wei))
ans2=1LL*f[i].su*(f[i].wei+f[t1].wei);
}
}
else
{
ans2=5000000000LL;
}
if(k==)
{
ans3=;
}
else
{
ans3=;
for(int i=;i<=n;i++)
if(i!=k-)
{
if(ans3<1LL*f[i].su*(f[i].wei+f[k-].wei))
ans3=1LL*f[i].su*(f[i].wei+f[k-].wei);
}
}
num=ans1+ans2+ans3;
minn=min(ans1,min(ans2,ans3));
maxn=max(ans1,max(ans2,ans3));
printf("%lld\n",num-minn-maxn);
}
return ;
}