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 ...
随机推荐
- "共振式”项目管理
"共振式”项目管理--是我第一个提出的吗?:) 脑子里突然想到项目管理的一些事情,然后想到项目其实是有节奏的,项目中的人员其实如果找到了这个节奏,踏准了节奏,一切将是顺风顺水. 刚准备动笔时 ...
- Java-NIO(九):管道 (Pipe)
Java NIO 管道是2个线程之间的单向数据连接.Pipe有一个source通道和一个sink通道.数据会被写到sink通道,从source通道读取. 代码使用示例: @Test public vo ...
- Eclipse中JavaSwing图形插件安装
1.在百度中搜索WindowBuilder,找到http://www.eclipse.org/windowbuilder/ 2.点击Download调转到页面: 因为我的eclipse版本是 3.点击 ...
- c#:ThreadPool实现并行分析,并实现线程同步结束
背景: 一般情况下,经常会遇到一个单线程程序时执行对CPU,MEMORY,IO利用率上不来,且速度慢下问题:那么,怎么解决这些问题呢? 据我个人经验来说有以下两种方式: 1.并行.多线程(Parall ...
- CentOS 6.8下二级域名及目录的绑定
二级域名对应目录的绑定: 第一步: 开启mod_rewrite模块,默认是开启的,这里可以查下是否开启 终端输入:vim /etc/httpd/conf/httpd.conf 回车 查看188行:L ...
- fiddler实现手机抓包
对fiddler相关配置 1.允许fiddler捕获http协议,打开fiddler客户端,菜单框选择Tools->Options->HTTPS,勾选图中三项 2.允许远程连接,Tools ...
- 使用Autofac,提示重写成员“Autofac.Integration.Mvc.AutofacDependencyResolver.GetService(System.Type)”时违反了继承安全性规则。重写方法的安全可访问性必须与所重写方法的安全可访问性匹配。
接触Autofac大概有2天左右,第2天,亲自动手搭建demo,搭完,以为大功告成的时候,提示了这个错误,网上找了很多方法,都没有解决. 为以后的朋友,避免踩坑,分享一下我的解决方法. Dmeo我是新 ...
- [LeetCode] Kill Process 结束进程
Given n processes, each process has a unique PID (process id) and its PPID (parent process id). Each ...
- 重拾Python(5):数据读取
本文主要对Python如何读取数据进行总结梳理,涵盖从文本文件,尤其是excel文件(用于离线数据探索分析),以及结构化数据库(以Mysql为例)中读取数据等内容. 约定: import numpy ...
- pyqt4 写动画不能播放问题集合
最近在学习动画,真的真的是血泪史,百度基本是0资源,各种在谷歌外国大佬的英文中躺过一个一个血坑....... 这是随便写的一个动画功能调试窗口..... 问题现象: 点击食灵,没反应,写的动画不能生成 ...