Code Jam 2010 Round 1A Problem B

简介:

Problem B. Make it Smooth

Problem

You have a one-dimensional array of N pixels. Each pixel has a value, represented by a number between 0 and 255, inclusive. The distance between two pixels is the absolute difference of their numbers. You can perform each of the following operations zero or more times: With cost D, delete any pixel, so its original neighbors become neighboring pixels. With cost I, insert one pixel of any value into any position -- either between two existing pixels, or before the first pixel, or after the last pixel. You can change the value of any pixel. The cost is the absolute difference of the old value of the pixel and the new value of the pixel. The array is smooth if any neighboring pixels have distance at most M. Find the minimum possible cost of a sequence of operations that makes the array smooth.

Note: The empty array -- the array containing no pixels -- is considered to be smooth.

Input

The first line of the input gives the number of test cases, T. T test cases follow, each with two lines. The first line is in the form "D I M N", the next line contains N numbers ai: the values of the pixels from left to the right.

Output

For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1), and y is the minimum cost to make the input array smooth.

Limits

All the numbers in the input are integers. 1 ≤ T ≤ 100 0 ≤ D, I, M, ai ≤ 255 Small dataset 1 ≤ N ≤ 3.

Large dataset

1 ≤ N ≤ 100.

Sample


Input 

2
6 6 2 3
1 7 5
100 1 5 3
1 50 7

Output 

Case #1: 4
Case #2: 17


Explanation

In Case #1, decreasing the 7 to 3 costs 4 and is the cheapest solution. In Case #2, deleting is extremely expensive; it's cheaper to insert elements so your final array looks like [1, 6, 11, 16, 21, 26, 31, 36, 41, 46, 50, 45, 40, 35, 30, 25, 20, 15, 10, 7].

解题思路:

最近遇到的一些动态规划问题中感觉最难的一个,思考明白后让我对动态规划的理解更深刻了。思路是这个数组中的每个数字在最小代价时,不确定大小,但是在0-255范围内把每种不同组合的情况都考虑到,找出其中最小的代价。在找的过程中,可以先找数组前1个的最小代价,然后利用前1个的结论找前2个的最小代价,也就是用动态规划的思想递归求解。在定义数组a[N],存放输入的N个记录。定义dp[N+1][256],例如dp[i+1][j]的意义是,a的子数组a[0:i]中最后一个像素变为j时的最小代价。可能的情况有:

  1. 删除a[i+1],那么cost=dp[i][j] + D,即a[i]变为j的代价加上删除a[i+1]代价。

  2. 修改a[i+1]变为j,那么需要考虑a[i]不同情况,我们使用辅助变量k表示a[i]的不同情况,0<=k<=255。cost=dp[i][k]+insertcost+movecost,其中movecost指的是将a[i+1]修改为j的代价,insertcost指的是将k到j之间通过插入使之平滑的代价。

求出删除a[i+1]与k取不同值时修改a[i+1]的代价的最小值,即为a[i+1][j]的值。接下来只需要找出dp[N][0:255]中的最小值,即为答案。


void run() {
    int C = sc.nextInt();
    double ratio = (1 + sqrt(5)) / 2;
    for (int c = 1; c <= C; c++) {
        int a1 = sc.nextInt();
        int a2 = sc.nextInt();
        int b1 = sc.nextInt();
        int b2 = sc.nextInt();
        long count = 0;
        for (int i = a1; i <= a2; i++) {
            double up = i * ratio;
            double down = i / ratio;
            if (b1 >= up || b2 <= down)
                count += b2 - b1 + 1;
            else {
                if (b1 <= down)
                    count += max(0, (int) floor(down) - b1 + 1);
                if (b2 >= up)
                    count += max(0, b2 - (int) ceil(up) + 1);
            }
        }
        System.out.println(String.format("Case #%d: %d", c, count));
    }
}


目录
相关文章
|
数据挖掘
深入分析:ERP系统的优势与劣势
深入分析:ERP系统的优势与劣势
947 3
|
机器学习/深度学习 监控 安全
网络安全产品之认识入侵防御系统
由于网络安全威胁的不断演变和增长。随着网络技术的不断发展和普及,网络攻击的种类和数量也在不断增加,给企业和个人带来了巨大的安全风险。传统的防火墙、入侵检测防护体系等安全产品在面对这些威胁时,存在一定的局限性和不足,无法满足当前网络安全的需求。入侵防御系统(IPS)作为一种主动防御的解决方案应运而生。它可以实时检测和防御网络流量中的恶意攻击和威胁,通过串接的方式部署在网络中,对入侵行为进行实时阻断,从而极大地降低了入侵的危害。
742 1
|
资源调度
Vue3+vite个人博客网站从0-1(项目环境搭建)
Vue3+vite个人博客网站从0-1(项目环境搭建)
927 0
Vue3+vite个人博客网站从0-1(项目环境搭建)
|
DataWorks API 数据库
DataWorks操作报错合集之在使用 OceanBase (OB) 作为数据源进行数据集成时遇到报错,该如何排查
DataWorks是阿里云提供的一站式大数据开发与治理平台,支持数据集成、数据开发、数据服务、数据质量管理、数据安全管理等全流程数据处理。在使用DataWorks过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
|
存储 负载均衡 定位技术
现代数据库系统中的数据分片策略与优化
数据分片在现代数据库系统中扮演着关键角色,特别是在面对海量数据和高并发访问的情况下。本文探讨了数据分片的基本概念、常见的分片策略(如水平分片与垂直分片)、以及如何通过优化和选择合适的分片策略来提升数据库系统的性能和可扩展性。
|
机器学习/深度学习 人工智能 自然语言处理
Java中的自然语言处理应用案例分析
Java中的自然语言处理应用案例分析
|
Kubernetes Cloud Native Java
大规模 Kubernetes 集群故障注入的利器-ChaosBlade
本文将主要介绍 ChaosBlade 在 Kubernetes 中故障注入的底层实现原理、版本优化过程以及大规模应用演练测试。01
940 108
大规模 Kubernetes 集群故障注入的利器-ChaosBlade
|
存储 Python Windows
文件位置标记与定位:概念、方法与实现
文件位置标记与定位:概念、方法与实现
317 0
|
前端开发 JavaScript Java
基于Springboot+Vue实现智慧停车场管理系统
基于Springboot+Vue实现智慧停车场管理系统
190 0
|
Linux Windows 网络安全
阿里云服务器如何上传下载文件
1、链接到公网ip 2、使用rz、sz语法进行上传、下载   如果没有rz、sz,则给服务器里安装这两个包 yum install lrzsz 安装完毕即可使用rz,sz是便是Linux/Unix同Windows进行ZModem文件传输的命令行工具。
7738 1