18: Array C

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 586  Solved: 104
[Submit][Status][Web Board]

Description

Giving two integers  and  and two arrays  and  both with length , you should construct an array  also with length  which satisfied:

1.0≤CiAi(1≤in)

2.

and make the value S be minimum. The value S is defined as:

Input

There are multiple test cases. In each test case, the first line contains two integers n(1≤n≤1000) andm(1≤m≤100000). Then two lines followed, each line contains n integers separated by spaces, indicating the array Aand B in order. You can assume that 1≤Ai≤100 and 1≤Bi≤10000 for each i from 1 to n, and there must be at least one solution for array C. The input will end by EOF.

Output

For each test case, output the minimum value S as the answer in one line.

Sample Input

  1. 3 4
  2. 2 3 4
  3. 1 1 1

Sample Output

  1. 6

HINT

In the sample, you can construct an array [1,1,2](of course [1,2,1] and [2,1,1] are also correct), and  is 6.

题目大意:给你n,m。表示a数组和b数组。然后让你选m个数组成c数组,保证c[i] <= a[i],要求sigma(c[i]*c[i]*b[i])求和最小。可能没描述不太清楚,自己再看看题吧。。。

解题思路:自己想了一个mlogn的做法,但是一直超时到死。。。赛后听大牛的解法,的确很巧妙ORZ。

自己的超时想法:

  1. #include<stdio.h>
  2. #include<algorithm>
  3. //#include<string.h>
  4. //#include<math.h>
  5. //#include<string>
  6. //#include<iostream>
  7. #include<queue>
  8. //#include<stack>
  9. //#include<map>
  10. //#include<vector>
  11. //#include<set>
  12. using namespace std;
  13. typedef long long LL;
  14. #define mid (L+R)/2
  15. #define lson rt*2,L,mid
  16. #define rson rt*2+1,mid+1,R
  17. const int maxn = 1e3 + 30;
  18. const LL INF = 0x3f3f3f3f;
  19. const LL mod = 9973;
  20. typedef long long LL;
  21. typedef unsigned long long ULL;
  22. struct Num{
  23. int b, c, prod, id;
  24. bool operator < (const Num & rhs)const{
  25. return prod > rhs.prod;
  26. }
  27. }nums[maxn];
  28. int A[maxn], B[maxn], ans[maxn];
  29. priority_queue<Num>PQ;
  30. int Scan() { //输入外挂
  31. int res = 0, flag = 0;
  32. char ch;
  33. if((ch = getchar()) == '-') flag = 1;
  34. else if(ch >= '0' && ch <= '9') res = ch - '0';
  35. while((ch = getchar()) >= '0' && ch <= '9')
  36. res = res * 10 + (ch - '0');
  37. return flag ? -res : res;
  38. }
  39. void Out(int a) { //输出外挂
  40. if(a < 0) { putchar('-'); a = -a; }
  41. if(a >= 10) Out(a / 10);
  42. putchar(a % 10 + '0');
  43. }
  44. int main(){
  45. int n,m;
  46. while(scanf("%d %d",&n,&m)!=EOF){
  47. while(!PQ.empty()) PQ.pop();
  48. for(int i = 1; i <= n; ++i){
  49. scanf("%d",&A[i]);
  50. // A[i] = Scan();
  51. nums[i].c = 1;
  52. }
  53. for(int i = 1; i <= n; i++){
  54. scanf("%d",&nums[i].b);
  55. // nums[i].b = Scan();
  56. nums[i].id = i;
  57. nums[i].prod = nums[i].b * (nums[i].c*nums[i].c);
  58. PQ.push(nums[i]);
  59. }
  60. Num nm = PQ.top(); PQ.pop();
  61. ans[nm.id]++;
  62. Num tmp;
  63. for(int i = 1; i < m; ++i){
  64. if(PQ.empty()){
  65. ans[nm.id] += m-i; break;
  66. }
  67. // cout<<PQ.size()<<" ++++"<<endl;
  68. tmp = PQ.top();
  69. // printf("%d %d %d++\n",tmp.id,tmp.c,tmp.prod);
  70. nm.c++;
  71. nm.prod = nm.b*nm.c*nm.c;
  72. if(nm.c > A[nm.id]){
  73. PQ.pop();
  74. nm = tmp;
  75. ans[nm.id] = nm.c; continue;
  76. }
  77. if(tmp.prod < nm.prod){
  78. PQ.pop();
  79. PQ.push(nm);
  80. nm = tmp;
  81. ans[nm.id] = nm.c;
  82. }else{
  83. ans[nm.id] = nm.c;
  84. }
  85. }
  86. int res = 0;
  87. for(int i = 1; i <= n; ++i){
  88. // printf("%d\n",ans[i]);
  89. res += nums[i].b * ans[i] * ans[i];
  90. ans[i] = 0;
  91. }
  92. //printf("%d\n",res);
  93. Out(res);
  94. puts("");
  95. }
  96. return 0;
  97. }

  

大牛的解法:

  1. #include<stdio.h>
  2. #include<algorithm>
  3. #include<string.h>
  4. #include<math.h>
  5. #include<string>
  6. #include<iostream>
  7. #include<queue>
  8. #include<stack>
  9. #include<map>
  10. #include<vector>
  11. #include<set>
  12. using namespace std;
  13. typedef long long LL;
  14. #define mid (L+R)/2
  15. #define lson rt*2,L,mid
  16. #define rson rt*2+1,mid+1,R
  17. const int maxn = 1e6 + 30;
  18. const LL INF = 0x3f3f3f3f;
  19. const LL mod = 9973;
  20. typedef long long LL;
  21. typedef unsigned long long ULL;
  22. int a[maxn],b[maxn] ,cost[maxn];
  23. int main(){
  24. int n, m;
  25. while(scanf("%d%d",&n,&m)!=EOF){
  26. for(int i = 1; i <= n; ++i){
  27. scanf("%d",&a[i]);
  28. }
  29. for(int i = 1; i <= n; ++i){
  30. scanf("%d",&b[i]);
  31. }
  32. int cnt = 0;
  33. for(int i = 1; i <= n; ++i){
  34. for(int j = 1; j <= a[i]; ++j){
  35. cost[cnt] = (2*j-1)*b[i];
  36. cnt++;
  37. }
  38. }
  39. sort(cost,cost+cnt);
  40. LL res = 0;
  41. for(int i = 0; i < m; ++i){
  42. res += cost[i];
  43. }
  44. printf("%lld\n",res);
  45. }
  46. return 0;
  47. }

  

HZAU 18——Array C——————【贪心】的更多相关文章

  1. CodeForces-721D-Maxim and Array(优先队列,贪心,分类讨论)

    链接: https://vjudge.net/problem/CodeForces-721D 题意: Recently Maxim has found an array of n integers, ...

  2. CF1479B Painting the Array(贪心+DP)

    题目大意:给你一个序列,让你提取出一个子序列A,剩余的部分组成子序列B,现定义seg(x)表示把序列x中相邻的相同数合并成一个数后,序列x的长度,分别求seg(A)+seg(B)的最大值和最小值,n= ...

  3. hzau 1209 Deadline(贪心)

    K.Deadline There are N bugs to be repaired and some engineers whose abilities are roughly equal. And ...

  4. Codeforces 402D Upgrading Array:贪心 + 数学

    题目链接:http://codeforces.com/problemset/problem/402/D 题意: 给你一个长度为n的数列a[i],又给出了m个“坏质数”b[i]. 定义函数f(s),其中 ...

  5. Codeforces G. Nick and Array(贪心)

    题目描述: Nick had received an awesome array of integers a=[a1,a2,…,an] as a gift for his 5 birthday fro ...

  6. Codeforces Round #581 (Div. 2) B. Mislove Has Lost an Array (贪心)

    B. Mislove Has Lost an Array time limit per test1 second memory limit per test256 megabytes inputsta ...

  7. upc 组队赛18 STRENGTH【贪心模拟】

    STRENGTH 题目链接 题目描述 Strength gives you the confidence within yourself to overcome any fears, challeng ...

  8. Codeforces Round #696 (Div. 2) C. Array Destruction (贪心,multiset)

    题意:有\(n\)个数,首先任选一个正整数\(x\),然后在数组中找到两个和为\(x\)的数,然后去掉这两个数,\(x\)更新为两个数中较大的那个.问你最后时候能把所有数都去掉,如果能,输出最初的\( ...

  9. js对象 数组Array详解 (参照MDN官网:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find)

    一:数组的创建方式: 1.采用直接量创建 var arr = [];//创建一个空数组 var arr2 = [1,2,3];//创建一个有三个元素的数组 2.采用构造函数创建 a.var arr1 ...

随机推荐

  1. Ecliplse导入maven项目applicationContext.xml报错:Referenced file contains errors (http://www.springframework.org/schema/context/spring-context-3.1.xsd). For more information, right click on the message in

    刚刚导入的maven项目的Spring配置文件报错: 大体意思是说: 引用的文件包含错误(http://www.springframework.org/schema/context/springing ...

  2. 03process对象的其他方法属性

    一 Process对象的join方法 在主进程运行过程中如果想并发地执行其他的任务,我们可以开启子进程,此时主进程的任务与子进程的任务分两种情况 情况一:在主进程的任务与子进程的任务彼此独立的情况下, ...

  3. 20165219第4次实验《Android程序设计》实验报告

    20165219第4次实验<Android程序设计>实验报告 一.实验内容及步骤 (一)Android Stuidio的安装Hello world测试 要求 参考http://www.cn ...

  4. Weekly Contest 117

    965. Univalued Binary Tree A binary tree is univalued if every node in the tree has the same value. ...

  5. 洛谷P4013 数字梯形问题(费用流)

    传送门 两个感受:码量感人……大佬nb…… 规则一:$m$条路径都不相交,那么每一个点只能经过一次,那么考虑拆点,把每一个点拆成$A_{i,j}$和$B_{i,j}$,然后两点之间连一条容量$1$,费 ...

  6. 文件操作 - 整体操作&文件搜索

    文件操作 - 整体操作 1.touch 作用:创建普通文件 格式:touch file1 [file2] 2.cp 作用:拷贝文件 格式:cp 源文件 目标文件 3.rm 作用:删除文件 格式:rm ...

  7. Samba服务为例、简单了解

    先.关掉SElinux.防火墙. ---------------------------- 安装rpm包(主): samba-3.6.9-164.el6.x86_64.rpm 启动检测:samba服务 ...

  8. 条目七《如果容器中包含了通过new操作创建的指针,切记在容器对象析构前将指针delete掉》

    如果容器中包含了通过new操作创建的指针,切记在容器对象析构前将指针delete掉 在STL中容器是智能的,可以在容器销毁时自动调用容器里对象的析构函数来销毁容器存储的对象. STL的容器虽然比较智能 ...

  9. Could not get lock /var/lib/apt/lists/lock

    执行: apt-get update 出现 Could not get lock /var/lib/apt/lists/lock 解决办法: 查询与apt相关的进程 ps -e | grep apt ...

  10. class __init__()

    python 先定义函数才能调用 类是客观对象在人脑中的主观映射,生产对象的模板,类相当于盖房的图纸,生产工具的模具 模板 类:属性.方法 __init__() 这个方法一般用于初始化一个类但是 当实 ...