CodeForces 960 B.Minimize the error
2018-04-09 15:48
246 查看
题意
给你两个数字序列a,b,每个序列长度都为n,然后E=∑(a[i]-b[i])^2。现在你可以改变a,b序列中的元素k1次和k2次,每次可以使一个元素加一或者减一。使得改变结束之后E的值最小。
题解
用一个数组来存入abs(a[i]-b[i])的值,有k1+k2的操作(+1/-1),以绝对值从小到大排,大于的-1,小于0的+1,暴力解决,不会超时,然后在遍历平方加起来。
给你两个数字序列a,b,每个序列长度都为n,然后E=∑(a[i]-b[i])^2。现在你可以改变a,b序列中的元素k1次和k2次,每次可以使一个元素加一或者减一。使得改变结束之后E的值最小。
题解
用一个数组来存入abs(a[i]-b[i])的值,有k1+k2的操作(+1/-1),以绝对值从小到大排,大于的-1,小于0的+1,暴力解决,不会超时,然后在遍历平方加起来。
#include <bits/stdc++.h> using namespace std; const int maxn = 1005; long long number1[maxn], number2[maxn], ans[maxn]; bool cmp(long long a, long long b){ return abs(a) < abs(b); } int main(){ int n, k1, k2; while(cin>>n>>k1>>k2){ memset(ans, 0, sizeof(ans)); memset(number2, 0, sizeof(number2)); memset(number1, 0, sizeof(number1)); for(int i = 0; i < n; i++){ cin>>number1[i]; } for(int i = 0; i < n; i++){ cin>>number2[i]; } for(int i = 0; i < n; i++){ ans[i] = abs(number1[i]-number2[i]); } sort(ans, ans+n, cmp); int t = k1 + k2; while(t){ if(ans[n-1] < 0){ ans[n-1]++; } else{ ans[n-1]--; } t--; sort(ans, ans+n, cmp); } long long sum = 0; for(int i = 0; i < n; i++){ sum += ans[i]*ans[i]; } cout<<sum<<endl; } }
相关文章推荐
- You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version
- Codeforces 189 C. Kalila and Dimna in the Logging Industry
- use notifyicon to minimize the image onto the toolbar
- Error while calling the Roles.AddUserToRole (error message: Cannot resolve collation conflict fo
- Swift报错解决" 'try' and the error is not handled"
- IE8 下 HTML Parsing Error: Unable to modify the parent container element before the child element is closed (KB927917)
- 1716438 - Error :" Failed to load database information Details: The Database connector crdb_bwquery.
- Error running demo: This version of Android Studio is incompatible with the Gradle Plugin used.
- 【33.33%】【codeforces 681D】Gifts by the List
- VMware安装时Error 1324. The path My Documents contains a invalid character的原因和解决方法
- The connection to adb is down, and a severe error has occured.
- 【63.63%】【codeforces 724A】Checking the Calendar
- Mac下eclipse 启动时出现An error has occurred. See the log file的问题
- Failed to load resource: the server responded with a status of 500 (Internal Server Error)
- Steps to change the default location of SQL Server /SQL Server Agent - Error log files
- codeforces 505C Mr. Kitayuta, the Treasure Hunter(DP)
- Open CHM Error:"Cannot open the file:mk:@MSITStore----" on XP of EN version
- 关于Code Sign error:The identity 'iPhone distribution' doesn't match any valid certificate private ..
- Android开发 Error:The number of method references in a .dex file cannot exceed 64K.
- error: Please reinstall the libzip distribution