Codeforces 352C Jeff and Rounding【dp】

探讨了如何通过特定操作使数值序列的调整前后总和之差最小化的问题,涉及到数学中的取整运算。

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 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
3
0.000 0.500 0.750 1.000 2.000 3.000
Output
0.250
Input
3
4469.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])));
    }
}
/*
1
0.001 0.002
2
0.001 0.002 0.003 0.004
*/



引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.youkuaiyun.com/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值