Minimize the error CodeForces - 960B
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined . You have to perform exactly k1 operations on array A and exactly k2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after k1 operations on array A and k2operations on array B have been performed.
Input
The first line contains three space-separated integers n (1 ≤ n ≤ 103), k1 and k2 (0 ≤ k1 + k2 ≤ 103, k1 and k2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.
Second line contains n space separated integers a1, a2, ..., an ( - 106 ≤ ai ≤ 106) — array A.
Third line contains n space separated integers b1, b2, ..., bn ( - 106 ≤ bi ≤ 106)— array B.
Output
Output a single integer — the minimum possible value of after doing exactly k1 operations on array A and exactly k2 operations on array B.
Examples
2 0 0
1 2
2 3
2
2 1 0
1 2
2 2
0
2 5 7
3 4
14 4
1
Note
In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)2 + (2 - 3)2 = 2.
In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)2 + (2 - 2)2 = 0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)2 + (4 - 4)2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)2 + (4 - 5)2 = 1.
【题目概述】
给你两个数字序列a,b,每个序列长度都为n,然后E=∑(a[i]-b[i])^2。现在你可以改变a,b序列中的元素k1次和k2次,每次可以使一个元素加一或者减一。使得改变结束之后E的值最小
【思路阐述】
每一次的操作都会使差值变化,+1或-1,目标是使差值离0越近越好,那么当存在有大于0的差值时,就让改差值减一,如果当所有差值为0的时,就让其中一个(就第一个)差值加一。
#include<bits/stdc++.h>
using namespace std;
struct node{
int a;
int b;
int dif;
}num[]; bool cmp(node a,node b) {
return a.dif > b.dif;
} int main() {
int n,k1,k2;
while(~scanf("%d %d %d",&n,&k1,&k2)) {
for(int i = ; i < n; i++) scanf("%d",&num[i].a);
for(int i = ; i < n; i++) {
scanf("%d",&num[i].b);
num[i].dif = abs(num[i].a - num[i].b);
}
sort(num,num+n,cmp);
int op = k1 + k2;
int count = ;
for(int i = ; i < op; i++) {
if(num[].dif == ) num[].dif++;
else num[].dif--;
count++;
sort(num,num+n,cmp);
}
long long int ans = ;
for(int i = ; i < n; i++) {
ans += pow(num[i].dif,);
} cout<<ans<<endl;
}
return ;
}
Minimize the error CodeForces - 960B的更多相关文章
- 【codeforces】【比赛题解】#960 CF Round #474 (Div. 1 + Div. 2, combined)
终于打了一场CF,不知道为什么我会去打00:05的CF比赛…… 不管怎么样,这次打的很好!拿到了Div. 2选手中的第一名,成功上紫! 以后还要再接再厉! [A]Check the string 题意 ...
- 19 Error handling and Go go语言错误处理
Error handling and Go go语言错误处理 12 July 2011 Introduction If you have written any Go code you have pr ...
- # ML学习小笔记—Where does the error come from?
关于本课程的相关资料http://speech.ee.ntu.edu.tw/~tlkagk/courses_ML17.html 错误来自哪里? error due to "bias" ...
- 李宏毅机器学习课程---3、Where does the error come from
李宏毅机器学习课程---3.Where does the error come from 一.总结 一句话总结:机器学习的模型中error的来源是什么 bias:比如打靶,你的瞄准点离准心的偏移 va ...
- (转) Summary of NIPS 2016
转自:http://blog.evjang.com/2017/01/nips2016.html Eric Jang Technology, A.I., Careers ...
- Propagation of Visual Entity Properties Under Bandwidth Constraints
1. Introduction The Saga of Ryzom is a persistent massively-multiplayer online game (MMORPG) release ...
- Deep Learning in a Nutshell: History and Training
Deep Learning in a Nutshell: History and Training This series of blog posts aims to provide an intui ...
- (转)The AlphaGo Replication Wiki
The AlphaGo Replication Wiki 摘自:https://github.com/Rochester-NRT/RocAlphaGo/wiki/01.-Home Contents : ...
- (转)The Road to TensorFlow
Stephen Smith's Blog All things Sage 300… The Road to TensorFlow – Part 7: Finally Some Code leave a ...
随机推荐
- Linux:日期用法,及格式定义
在shell脚本中经常会需要获取当前日期的地方,linux的系统时间在shell里是可以直接调用系统变量: 获取今天时期---`date +%Y%m%d` 或 `date +%F` 或 $(date ...
- 算子:sample(false, 0.1)抽样数据
抽样示例操作: scala> import org.apache.spark.sql.hive.HiveContext import org.apache.spark.sql.hive.Hive ...
- 类相关的BIF
1.>>> issubclass(C,A)#判断c是A的子类,返回真假 2.>>> isinstance(b1,B) #判断c1是B类的实例化对象,返回真假 3.& ...
- 字符串分割方法split()函数
>>> data = '1000,小甲鱼,男'>>> data.split(',')['1000', '小甲鱼', '男'] str.split('以什么为标志进行 ...
- Java知识体系纲要
最近一段时间,把Java主要涉及到的大概念都大致学习了一遍,为了让自己能够更好地形成对Java知识体系的整体把握,先把学过的知识点添加到自己画的思维导图上. 整个Java知识体系的划分,我自己主要将它 ...
- 原来你是这样的Promise
1. Promise简介 promise是异步编程的一种解决方案,它出现的初衷是为了解决回调地狱的问题. 打个比方,我需要: --(延迟1s)--> 输出1 --(延迟2s)--> 输出2 ...
- 线程的同步控制(Synchronization)
临界区(Critical Sections) 摘要 临界区(Critical Section) 用来实现"排他性占有".适合范围时单一进程的各线程之间. 特点 一个局部对象,不是一 ...
- 从零开始系列之vue全家桶(4)带新手小白一起搭建第一个个人网站项目
未经允许,严禁转载,全文由blackchaos提供. 在安装好了前面大部分需要的插件,我们开始进行第一个个人项目.结合vue+vuex+vue-cli+vue-router+webpack使用. 1. ...
- 原生Js操作DOM
查找DOM .querySelectorAll(),它接受包含一个CSS选择器的字符串参数,返回一个表示文档中匹配选择器的所有元素的NodeList元素. .querySelector(),返回第一个 ...
- 推荐一个c++小巧开源且跨平台的图像解码库
该图像解码库仅仅三个文件. 图像处理封装: spot.cpp spot.h 解码库实现: spot.c 支持图片文件格式如下: File format Read Write BMP files yes ...