zoukankan      html  css  js  c++  java
  • 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.

    Input

    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.

    Output

    In a single line print a single real number — the required difference with exactly three digits after the decimal point.

    Examples
    Input
    Copy
    3
    0.000 0.500 0.750 1.000 2.000 3.000
    Output
    Copy
    0.250
    Input
    Copy
    3
    4469.000 6526.000 4864.000 9356.383 7490.000 995.896
    Output
    Copy
    0.279
    Note

    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
    ", 1.0*ans);
    	return 0;
    }
    
    EPFL - Fighting
  • 相关阅读:
    vs 文件头自动添加注释
    .NET开发人员必知的八个网站
    鼠标移动到曲线图上显示值
    显示器分辨率不同,部分winform控件在其他机器上显示不全
    dotnetcharting 的简单使用
    C#Winform中ToolTip的简单用法,
    SQL Server 2008 报错:已成功与服务器建立连接,但是在登录前的握手期间发生错误
    开源组件整理
    java实现控件的移动及使用鼠标改变控件大小
    基础知识点七
  • 原文地址:https://www.cnblogs.com/zxyqzy/p/10200367.html
Copyright © 2011-2022 走看看