Codeforces 352C Jeff and Rounding【dp】

来源:互联网 发布:软件开发流程模版 编辑:程序博客网 时间:2024/06/10 07:16

A. Jeff and Rounding
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 thanai (assign toai:ai ⌋);
  • round element aj to the nearest integer that isn't less thanaj (assign toaj: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 numbersa1, 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
30.000 0.500 0.750 1.000 2.000 3.000
Output
0.250
Input
34469.000 6526.000 4864.000 9356.383 7490.000 995.896
Output
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


题目大意:

一共有N*2个数,其中我们需要进行N次操作,每次操作选取两个数,使得一个向上取整,一个向下取整。问最终得到的序列和原序列的差的绝对值最小是多少。


思路:


1、考虑dp,其一共有以下这些元素需要考虑到维度当中去:

①当前处理到第几个数。

②当前数是向上取整还是向下取整。

③处理到当前数一共向上取整了多少次,向下取整了多少次。

那么考虑设定dp【i】【j】【2】,其中表示处理到第i位,已经向上取整了j次,当前操作为:0(向下取整),1(向上取整)的和。

使得这个和与原序列加和到第i位上的和绝对值的差最小。


2、那么接下来考虑状态转移方程:
①if(fabs(dp[i-1][j][0]+floor(a[i])-sum[i])<fabs(dp[i-1][j][1]+floor(a[i])-sum[i]))dp[i][j][0]=dp[i-1][j][0]+floor(a[i]);

else dp[i][j][0]=dp[i-1][j][1]+floor(a[i]);

表示当前这个数向下取整的最优方案。

②if(fabs(dp[i-1][j-1][0]+ceil(a[i])-sum[i])<fabs(dp[i-1][j-1][1]+ceil(a[i])-sum[i]))dp[i][j][1]=dp[i-1][j-1][0]+ceil(a[i]);

else dp[i][j][1]=dp[i-1][j-1][1]+ceil(a[i]);

表示当前这个数向上取整的额最优方案。


3、注意初始化。


Ac代码:

#include<stdio.h>#include<string.h>#include<algorithm>#include<iostream>#include<math.h>using namespace std;double sum[5005];double a[5005];double dp[4002][2002][2];void init(){    for(int i=0;i<=4001;i++)    {        for(int j=0;j<=2001;j++)        {            dp[i][j][0]=dp[i][j][1]=1000000000000000;        }    }}int main(){    int n;    while(~scanf("%d",&n))    {        init();        memset(sum,0,sizeof(sum));        for(int i=1;i<=n*2;i++)        {            scanf("%lf",&a[i]);            sum[i]=sum[i-1]+a[i];        }        dp[1][0][0]=floor(a[1]);        dp[1][1][1]=ceil(a[1]);        for(int i=2;i<=n*2;i++)        {            for(int j=0;j<=n&&j<=i;j++)            {                if(j==0)                {                    dp[i][j][0]=dp[i-1][j][0]+floor(a[i]);                    continue;                }                if(fabs(dp[i-1][j][0]+floor(a[i])-sum[i])<fabs(dp[i-1][j][1]+floor(a[i])-sum[i]))                {                    dp[i][j][0]=dp[i-1][j][0]+floor(a[i]);                }                else dp[i][j][0]=dp[i-1][j][1]+floor(a[i]);                 if(fabs(dp[i-1][j-1][0]+ceil(a[i])-sum[i])<fabs(dp[i-1][j-1][1]+ceil(a[i])-sum[i]))                {                    dp[i][j][1]=dp[i-1][j-1][0]+ceil(a[i]);                }                else dp[i][j][1]=dp[i-1][j-1][1]+ceil(a[i]);            }        }        printf("%.3lf\n",min(fabs(dp[n*2][n][0]-sum[n*2]),fabs(dp[n*2][n][1]-sum[n*2])));    }}/*10.001 0.00220.001 0.002 0.003 0.004*/



0 0