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.

样例输入

  1. 2
  2. 3 2
  3. 5 3 4
  4. 3 8 5
  5. 4 2
  6. 6 2 8 2
  7. 10 18 12 4

样例输出

  1. 40
  2. 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[];
  10. int comp(my x,my y)
  11. {
  12. if(x.wei<y.wei)
  13. return ;
  14. return ;
  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=;i<=n;i++)
  26. scanf("%d",&f[i].su);
  27. for(int i=;i<=n;i++)
  28. scanf("%d",&f[i].wei);
  29. sort(f+,f+n+,comp);
  30. ans1=,t1=;
  31. for(int i=;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=;
  40. for(int i=;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==)
  52. {
  53. ans3=;
  54. }
  55. else
  56. {
  57. ans3=;
  58. for(int i=;i<=n;i++)
  59. if(i!=k-)
  60. {
  61. if(ans3<1LL*f[i].su*(f[i].wei+f[k-].wei))
  62. ans3=1LL*f[i].su*(f[i].wei+f[k-].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 ;
  71. }
  1.  

Family Gathering at Christmas(思维题)的更多相关文章

  1. zoj 3778 Talented Chef(思维题)

    题目 题意:一个人可以在一分钟同时进行m道菜的一个步骤,共有n道菜,每道菜各有xi个步骤,求做完的最短时间. 思路:一道很水的思维题, 根本不需要去 考虑模拟过程 以及先做那道菜(比赛的时候就是这么考 ...

  2. cf A. Inna and Pink Pony(思维题)

    题目:http://codeforces.com/contest/374/problem/A 题意:求到达边界的最小步数.. 刚开始以为是 bfs,不过数据10^6太大了,肯定不是... 一个思维题, ...

  3. ZOJ 3829 贪心 思维题

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题.自己智商不够.不敢搞,想着队友智商 ...

  4. 洛谷P4643 [国家集训队]阿狸和桃子的游戏(思维题+贪心)

    思维题,好题 把每条边的边权平分到这条边的两个顶点上,之后就是个sb贪心了 正确性证明: 如果一条边的两个顶点被一个人选了,一整条边的贡献就凑齐了 如果分别被两个人选了,一作差就抵消了,相当于谁都没有 ...

  5. C. Nice Garland Codeforces Round #535 (Div. 3) 思维题

    C. Nice Garland time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  6. PJ考试可能会用到的数学思维题选讲-自学教程-自学笔记

    PJ考试可能会用到的数学思维题选讲 by Pleiades_Antares 是学弟学妹的讲义--然后一部分题目是我弄的一部分来源于洛谷用户@ 普及组的一些数学思维题,所以可能有点菜咯别怪我 OI中的数 ...

  7. UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)

    UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There W ...

  8. HDU 1029 Ignatius and the Princess IV / HYSBZ(BZOJ) 2456 mode(思维题,~~排序?~~)

    HDU 1029 Ignatius and the Princess IV (思维题,排序?) Description "OK, you are not too bad, em... But ...

  9. cf796c 树形,思维题

    一开始以为是个树形dp,特地去学了..结果是个思维题 /* 树结构,设最大点权值为Max,则答案必在在区间[Max,Max+2] 证明ans <= Max+2 任取一个点作为根节点,那么去掉这个 ...

随机推荐

  1. Problem03 水仙花数

    题目:打印出所有的"水仙花数"."水仙花数"是指一个三位数,其各位数字立方和等于该数本身. 例如:153是一个"水仙花数",因为153=1的 ...

  2. 13-----BBS论坛

    BBS论坛(十三) 13.1点击更换图形验证码 (1)front/signup.html <div class="form-group"> <div class= ...

  3. 下载,安装oracle数据库以及navicat连接数据库

    一.学习时所遇问题: 1.在下载之前以为oracle不是免费的,但是后来才知道oracle对于个人学习时是免费的,可以到官网下载安装.在下载时由于要注册oracle官网,所以尝试了好几遍,才成功下载o ...

  4. 使用EventBus实现兄弟组件之间的通信

    使用EventBus实现兄弟组件之间的通信 需求:为了实现菜单折叠的效果,例如http://blog.gdfengshuo.com/example/work/#/dashboard header组件和 ...

  5. 安装Chrome插件Markdown Preview Plus

    1.在谷歌应用商店,安装Chrome插件Markdown Preview Plus   2.设置Markdown Preview Plus (1)鼠标左键该拓展插件 (2)鼠标右键该插件 3.将mar ...

  6. This blog Test the Open Live Writer

    1. We print HELLOWORLD when we first learned to code, I want to Write this blog to test the software ...

  7. MySQL如何查询当月数据

    自己写了一个比较简单效率又高的方法,分享给大家: WHERE addTime BETWEEN DATE_FORMAT(NOW(),'%Y-%m-01') AND NOW() 方法就是过滤 本月1日到当 ...

  8. Win7无线路由Win8/8.1开启网络承载DOS命令笔记本电脑当无线路由器

    以下废话,先吐槽几句 这个功能其实在Win7+系统都内置了,不要再用什么某豹.某大师.某Soft之类的开启无线网络,这些软件都是骗你们安装一些垃圾软件的. 另外吐槽 某度wifi.某数字wifi都是垃 ...

  9. C# params 动态参数

    public delegate void Action(params object[] args); 再简单的东西都要强迫自己记录了,前段时间硬盘坏了,资料全没了,也没有备份,太痛苦了,那么多资料全没 ...

  10. Spring课程 Spring入门篇 4-1 Spring bean装配(下)之bean定义及作用域注解实现

    课程链接: 1 概述 2 代码演练 3 代码解析 1 概述 1.1 bean注解相关 a context:component-scan标签使用 问:该标签的作用是什么? 答:该标签作用是支持注解,在x ...