Family Gathering at Christmas(思维题)

 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代码:
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 using namespace std;
 5 typedef struct{
 6     int su,wei;
 7 }my;
 8 int n,k;
 9 my f[40005];
10 int comp(my x,my y)
11 {
12     if(x.wei<y.wei)
13         return 1;
14     return 0;
15 }
16 long long ans1,ans2,ans3,num,maxn,minn;
17 int t1;
18 int main()
19 {
20     int T;
21     scanf("%d",&T);
22     while(T--)
23     {
24         scanf("%d%d",&n,&k);
25         for(int i=1;i<=n;i++)
26             scanf("%d",&f[i].su);
27         for(int i=1;i<=n;i++)
28             scanf("%d",&f[i].wei);
29         sort(f+1,f+n+1,comp);
30         ans1=0,t1=0;
31         for(int i=1;i<=n;i++)
32         if(i!=k)
33         {
34             if(ans1<1LL*f[i].su*(f[i].wei+f[k].wei))
35                 ans1=1LL*f[i].su*(f[i].wei+f[k].wei),t1=i;
36         }
37         if(t1>k)
38         {
39             ans2=0;
40             for(int i=1;i<=n;i++)
41             if(i!=t1)
42             {
43                 if(ans2<1LL*f[i].su*(f[i].wei+f[t1].wei))
44                     ans2=1LL*f[i].su*(f[i].wei+f[t1].wei);
45             }
46         }
47         else
48         {
49             ans2=5000000000LL;
50         }
51         if(k==1)
52         {
53             ans3=0;
54         }
55         else
56         {
57             ans3=0;
58             for(int i=1;i<=n;i++)
59             if(i!=k-1)
60             {
61                 if(ans3<1LL*f[i].su*(f[i].wei+f[k-1].wei))
62                     ans3=1LL*f[i].su*(f[i].wei+f[k-1].wei);
63             }
64         }
65         num=ans1+ans2+ans3;
66         minn=min(ans1,min(ans2,ans3));
67         maxn=max(ans1,max(ans2,ans3));
68         printf("%lld\n",num-minn-maxn);
69     }
70     return 0;
71 }
View Code
 

猜你喜欢

转载自www.cnblogs.com/lglh/p/9582929.html
今日推荐