CF351A Jeff and Rounding 思维
Jeff got 2n real numbers a1, a2, ..., a2n as a birthday present. The boy hates non-integer numbers, so he decided to slightly "adjust" the numbers he's got. Namely, Jeff consecutively executes n operations, each of them goes as follows:
- choose indexes i and j (i ≠ j) that haven't been chosen yet;
- round element ai to the nearest integer that isn't more than ai (assign to ai: ⌊ ai ⌋);
- round element aj to the nearest integer that isn't less than aj (assign to aj: ⌈ aj ⌉).
Nevertheless, Jeff doesn't want to hurt the feelings of the person who gave him the sequence. That's why the boy wants to perform the operations so as to make the absolute value of the difference between the sum of elements before performing the operations and the sum of elements after performing the operations as small as possible. Help Jeff find the minimum absolute value of the difference.
The first line contains integer n (1 ≤ n ≤ 2000). The next line contains 2n real numbers a1, a2, ..., a2n (0 ≤ ai ≤ 10000), given with exactly three digits after the decimal point. The numbers are separated by spaces.
In a single line print a single real number — the required difference with exactly three digits after the decimal point.
3
0.000 0.500 0.750 1.000 2.000 3.000
0.250
3
4469.000 6526.000 4864.000 9356.383 7490.000 995.896
0.279
In the first test case you need to perform the operations as follows: (i = 1, j = 4), (i = 2, j = 3), (i = 5, j = 6). In this case, the difference will equal |(0 + 0.5 + 0.75 + 1 + 2 + 3) - (0 + 0 + 1 + 1 + 2 + 3)| = 0.25.
假设小数部分是x的话,向下取整为-x,向上为1-x;
可以发现不论是向下还是向上都是 -x,那么小数部分就可以统一处理;
那么问题就是当向上取整时,会+1----->求1的个数;
那么枚举就行了;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 200005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ int n;
int m; int main() {
//ios::sync_with_stdio(0);
cin >> n;
m = 2 * n;
double tmp;
int numeq = 0;
double sum = 0.0;
int numdb = 0;
for (int i = 1; i <= m; i++) {
rdlf(tmp);
ll intmp = (ll)tmp;
if (intmp == tmp)numeq++;
else {
sum += 1.0*(tmp - intmp);
numdb++;
}
}
int minn = min(n, numeq);
double ans = inf;
for (int i = 0; i <= minn; i++) {
ans = min(ans, (double)fabs(n - i - sum));
}
printf("%.3lf\n", 1.0*ans);
return 0;
}
CF351A Jeff and Rounding 思维的更多相关文章
- Codeforces Round #204 (Div. 2)->C. Jeff and Rounding
C. Jeff and Rounding time limit per test 1 second memory limit per test 256 megabytes input standard ...
- CodeForces 352C. Jeff and Rounding(贪心)
C. Jeff and Rounding time limit per test: 1 second memory limit per test: 256 megabytes input: stan ...
- CF&&CC百套计划3 Codeforces Round #204 (Div. 1) A. Jeff and Rounding
http://codeforces.com/problemset/problem/351/A 题意: 2*n个数,选n个数上取整,n个数下取整 最小化 abs(取整之后数的和-原来数的和) 先使所有的 ...
- codeforces A. Jeff and Rounding (数学公式+贪心)
题目链接:http://codeforces.com/contest/351/problem/A 算法思路:2n个整数,一半向上取整,一半向下.我们设2n个整数的小数部分和为sum. ans = |A ...
- cf C. Jeff and Rounding
http://codeforces.com/contest/352/problem/C 题意:给予N*2个数字,改变其中的N个向上进位,N个向下进位,使最后得到得数与原来数的差的绝对值最小 对每一个浮 ...
- Codeforces Round #204 (Div. 2) C. Jeff and Rounding——数学规律
给予N*2个数字,改变其中的N个向上进位,N个向下进位,使最后得到得数与原来数的差的绝对值最小 考虑小数点后面的数字,如果这些数都非零,则就是 abs(原数小数部分相加-1*n), 多一个0 则 m ...
- CF 351A - Jeff and Rounding DP
http://codeforces.com/problemset/problem/351/C 题意:有2*n个浮点数a1,a2,a3...a2*n,把他们分成n队,对于每对<A,B>,对A ...
- CodeForces 352C Jeff and Rounding
题意 有一个含有\(2n(n \leqslant2000)\)个实数的数列,取出\(n\)个向上取整,另\(n\)个向下取整.问取整后数列的和与原数列的和的差的绝对值. 就是说,令\(a\)为原数列, ...
- 数学思维——cf351A
把每个值的各种贡献算一下即可 /* ai的小数部分为xi,向下取整对答案贡献为xi 向上取整对答案的贡献是xi-1,如果这个数是0,那么对答案的贡献是xi,即如果0向上取整就可以免去-1 然后sum{ ...
随机推荐
- SUSE 开启ssh、telnet
SSH 1. /etc/ssh/sshd_config [SSH的配置文件] 2. SuSEfirewall2 stop #关闭防火墙 如图,输入命令 vi /etc/ssh/sshd_config ...
- paramiko监控 windows服务器 被监控服务器只需要安装openssh服务即可基于wmic完成大部分监控
#!/usr/bin/python #-*- coding: UTF-8 -*- #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...
- node与vue结合的前后端分离跨域问题
第一点:node作为服务端提供数据接口,vue使用axios访问接口, 安装axios npm install axios --save 安装完成后在main.js中增加一下配置: import ax ...
- VxVM如何扩展和收缩卷及文件系统
1. 同时扩展卷和文件系统 先用vxassist命令检查DG可用空间 [root@rhelnode1 ~]# vxassist -g testdg maxsize Maximum volume siz ...
- Shell编程进阶 1.3data命令
date命令是显示日期时间的命令 date 2016年 01月 01日 星期五 15:05:01 CST 修改时间的选项是 -s date -s "2016-01-01 12:56:10&q ...
- 关于JS正则表达式的一篇文章(转载)
原文:http://www.cnblogs.com/xujh/archive/2008/08/21/1273525.html <input onkeypress="return ...
- Markdown编辑器及图床推荐
Typora和自动图床工具 Typora 地址 ,极致简洁,界面很漂亮,最重要的是所见即所得 百度云搬运 密码:xi01 自动图床工具 需要七牛云做图床,感谢作者,详见博客 使用方法,只需两步即可完成 ...
- IP及端口号
IP:代表一台机器 端口号:每一个程序都有一个端口号与之对应 一个域名对应一个虚拟主机
- Codeforces #505(div1+div2) C Plasticine zebra
题意:给你一段字符串,可以选择任意多的位置,每个位置会反转两边的字符串,问交错的字符串最长是多长? 思路:找规律,仔细分析样例1.假设位置为 1 2 3 4 5 6 7 8 9,反转之后会发现答案是7 ...
- No bean class specified on bean definition 解决方案
调试Spring项目出现No bean class specified on bean definition异常 但是控制台并没有给出其他相关信息了 这个时候可以在AbstractBeanDefini ...