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

3 4
2 3 4
1 1 1

Sample Output

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。

自己的超时想法:

#include<stdio.h>
#include<algorithm>
//#include<string.h>
//#include<math.h>
//#include<string>
//#include<iostream>
#include<queue>
//#include<stack>
//#include<map>
//#include<vector>
//#include<set>
using namespace std;
typedef long long LL;
#define mid (L+R)/2
#define lson rt*2,L,mid
#define rson rt*2+1,mid+1,R
const int maxn = 1e3 + 30;
const LL INF = 0x3f3f3f3f;
const LL mod = 9973;
typedef long long LL;
typedef unsigned long long ULL;
struct Num{
int b, c, prod, id;
bool operator < (const Num & rhs)const{
return prod > rhs.prod;
}
}nums[maxn];
int A[maxn], B[maxn], ans[maxn];
priority_queue<Num>PQ;
int Scan() { //输入外挂
int res = 0, flag = 0;
char ch;
if((ch = getchar()) == '-') flag = 1;
else if(ch >= '0' && ch <= '9') res = ch - '0';
while((ch = getchar()) >= '0' && ch <= '9')
res = res * 10 + (ch - '0');
return flag ? -res : res;
}
void Out(int a) { //输出外挂
if(a < 0) { putchar('-'); a = -a; }
if(a >= 10) Out(a / 10);
putchar(a % 10 + '0');
}
int main(){
int n,m;
while(scanf("%d %d",&n,&m)!=EOF){
while(!PQ.empty()) PQ.pop();
for(int i = 1; i <= n; ++i){
scanf("%d",&A[i]);
// A[i] = Scan();
nums[i].c = 1;
}
for(int i = 1; i <= n; i++){
scanf("%d",&nums[i].b);
// nums[i].b = Scan();
nums[i].id = i;
nums[i].prod = nums[i].b * (nums[i].c*nums[i].c);
PQ.push(nums[i]);
}
Num nm = PQ.top(); PQ.pop();
ans[nm.id]++;
Num tmp;
for(int i = 1; i < m; ++i){
if(PQ.empty()){
ans[nm.id] += m-i; break;
}
// cout<<PQ.size()<<" ++++"<<endl;
tmp = PQ.top();
// printf("%d %d %d++\n",tmp.id,tmp.c,tmp.prod);
nm.c++;
nm.prod = nm.b*nm.c*nm.c;
if(nm.c > A[nm.id]){
PQ.pop();
nm = tmp;
ans[nm.id] = nm.c; continue;
}
if(tmp.prod < nm.prod){
PQ.pop();
PQ.push(nm);
nm = tmp;
ans[nm.id] = nm.c;
}else{
ans[nm.id] = nm.c;
}
}
int res = 0;
for(int i = 1; i <= n; ++i){
// printf("%d\n",ans[i]);
res += nums[i].b * ans[i] * ans[i];
ans[i] = 0;
}
//printf("%d\n",res);
Out(res);
puts("");
}
return 0;
}

  

大牛的解法:

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<string>
#include<iostream>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
using namespace std;
typedef long long LL;
#define mid (L+R)/2
#define lson rt*2,L,mid
#define rson rt*2+1,mid+1,R
const int maxn = 1e6 + 30;
const LL INF = 0x3f3f3f3f;
const LL mod = 9973;
typedef long long LL;
typedef unsigned long long ULL;
int a[maxn],b[maxn] ,cost[maxn];
int main(){
int n, m;
while(scanf("%d%d",&n,&m)!=EOF){
for(int i = 1; i <= n; ++i){
scanf("%d",&a[i]);
}
for(int i = 1; i <= n; ++i){
scanf("%d",&b[i]);
}
int cnt = 0;
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= a[i]; ++j){
cost[cnt] = (2*j-1)*b[i];
cnt++;
}
}
sort(cost,cost+cnt);
LL res = 0;
for(int i = 0; i < m; ++i){
res += cost[i];
}
printf("%lld\n",res);
}
return 0;
}

  

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. C# 给图片添加透明的文字、图片水印

    #region 添加水印 /// <summary> /// 添加文字水印 /// </summary> /// <param name="image" ...

  2. Redis缓存穿透、缓存雪崩

    缓存穿透 缓存穿透是指查询一个一定不存在的数据,由于缓存是不命中时被动写的,并且出于容错考虑,如果从存储层查不到数据则不写入缓存,这将导致这个不存在的数据每次请求都要到存储层去查询,失去了缓存的意义. ...

  3. angular 输出属性

    import { Component, OnInit, EventEmitter, Output } from '@angular/core'; @Component({ selector: 'app ...

  4. 基于 cookie 的 node 中间层灰度流程的一些思考

    此文已由作者申国骏授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 前言 关于灰度发布的意义此处就不进行介绍了,可以先读下这两篇文章 <微服务部署:蓝绿部署.滚动部署.灰 ...

  5. 《C#多线程编程实战》2.7 CountDownEvent

    这个同步线程的类大概是东北的. 很有意思. 会限定你的线程使用的次数,更形象一点的像是你妈,提前准备好你要使用的线程的次数,用不完还不高兴那种的. 使用顺序基本就是 实例化  填充线程的启动次数 使用 ...

  6. C语言条件编译(#if,#ifdef,#ifndef,#endif,#else,#elif)

    1.条件编译介绍 条件编译(conditional compiling)命令指定预处理器依据特定的条件来判断保留或删除某段源代码.例如,可以使用条件编译让源代码适用于不同的目标系统,而不需要管理该源代 ...

  7. 解决 Github用户名 变为 invalid-email-address 问题

    解决 Github用户名 变为 invalid-email-address 问题 If the identity used for this commit is wrong, you can fix ...

  8. 2. C/C++笔试面试经典题目二

    1. C和C++中struct有什么区别? [参考答案] [解析]C中的struct没有保护行为,没有public,private,protected,内部不能有函数,但可以有函数指针. 2. C++ ...

  9. vue模块拖拽效果

    正巧在之前面试中遇到问实现拖拽效果 当时面试的时候简单回答了实现的方式与逻辑. 现在闲来无事,把这个东西实现了一下. 原理很简单,写的很方便. 数据驱动,建立一个数组,数组初始长度为1 拖动触发时,添 ...

  10. Python之freshman01

    列表与元组.字典 1.列表list:["ele1","ele2","ele3","ele0"] 列表是一组任意类型的值, ...