Code Jam 2010 Round 1B Problem A

简介:

Problem B. Picking Up Chicks

Problem

A flock of chickens are running east along a straight, narrow road. Each one is running with its own constant speed. Whenever a chick catches up to the one in front of it, it has to slow down and follow at the speed of the other chick. You are in a mobile crane behind the flock, chasing the chicks towards the barn at the end of the road. The arm of the crane allows you to pick up any chick momentarily, let the chick behind it pass underneath and place the picked up chick back down. This operation takes no time and can only be performed on a pair of chicks that are immediately next to each other, even if 3 or more chicks are in a row, one after the other.

Given the initial locations (Xi) at time 0 and natural speeds (Vi) of the chicks, as well as the location of the barn (B), what is the minimum number of swaps you need to perform with your crane in order to have at least K of the N chicks arrive at the barn no later than time T?

You may think of the chicks as points moving along a line. Even if 3 or more chicks are at the same location, next to each other, picking up one of them will only let one of the other two pass through. Any swap is instantaneous, which means that you may perform multiple swaps at the same time, but each one will count as a separate swap.

Input

The first line of the input gives the number of test cases, C. C test cases follow. Each test case starts with 4 integers on a line -- N, K, B and T. The next line contains the Ndifferent integers Xi, in increasing order. The line after that contains the N integers Vi. All distances are in meters; all speeds are in meters per second; all times are in seconds.

Output

For each test case, output one line containing "Case #x: S", where x is the case number (starting from 1) and S is the smallest number of required swaps, or the word "IMPOSSIBLE".

Limits

1 ≤ C ≤ 100; 1 ≤ B ≤ 1,000,000,000; 1 ≤ T ≤ 1,000; 0 ≤ Xi < B; 1 ≤ Vi ≤ 100; All the Xi's will be distinct and in increasing order.

Small dataset

1 ≤ N ≤ 10; 0 ≤ K ≤ min(3, N);

Large dataset

1 ≤ N ≤ 50; 0 ≤ K ≤ N;

Sample


Input 

3
5 3 10 5
0 2 5 6 7
1 1 1 1 4
5 3 10 5
0 2 3 5 7
2 1 1 1 4
5 3 10 5
0 2 3 4 7
2 1 1 1 4

Output

Case #1: 0
Case #2: 2
Case #3: IMPOSSIBLE


解题思路:一道多叉树水题。跟之前做的电话号码匹配类似,而且更简单。


class Prefix {
    Map<String, Prefix> sub = new HashMap<String, Prefix>();

    int put(String s) {
        if (s.length() == 0)
            return 0;
        int index = s.indexOf('/', 1);
        if (index < 0)
            index = s.length();
        String s1 = s.substring(0, index);
        String s2 = s.substring(index);
        int ret = 0;
        if (!sub.containsKey(s1)) {
            Prefix child = new Prefix();
            sub.put(s1, child);
            ret++;
        }
        return ret + sub.get(s1).put(s2);
    }
}

void run() {
    int C = sc.nextInt();
    for (int c = 1; c <= C; c++) {
        int N = sc.nextInt();
        int M = sc.nextInt();
        Prefix prefix = new Prefix();
        for (int i = 0; i < N; i++) {
            prefix.put(sc.next());
        }
        int count = 0;
        for (int i = 0; i < M; i++) {
            count += prefix.put(sc.next());
        }
        System.out.println(String.format("Case #%d: %d", c, count));
    }
}


目录
相关文章
|
缓存 算法
07、Netty学习笔记—(聊天业务优化:参数调优)(二)
07、Netty学习笔记—(聊天业务优化:参数调优)(二)
07、Netty学习笔记—(聊天业务优化:参数调优)(二)
|
弹性计算 安全
停止使用云服务通常需要遵循以下步骤
停止使用云服务通常需要遵循以下步骤
860 2
|
运维 监控 JavaScript
鸿蒙next版开发:分析JS Crash(进程崩溃)
在HarmonyOS 5.0中,JS Crash指未处理的JavaScript异常导致应用意外退出。本文详细介绍如何分析JS Crash,包括异常捕获、日志分析和典型案例,帮助开发者定位问题、修复错误,提升应用稳定性。通过DevEco Studio收集日志,结合HiChecker工具,有效解决JS Crash问题。
505 4
|
SQL 安全 网络安全
守护数字资产:服务器迁移期间的安全挑战与对策
【10月更文挑战第4天】在数字化转型的浪潮中,服务器迁移成为企业不可避免的任务。然而,迁移过程中的安全挑战不容忽视。本文从安全考量的角度,探讨了服务器迁移期间可能遇到的安全问题,并提供了相应的对策和代码示例。
284 3
|
人工智能 搜索推荐 UED
还没排上SearchGPT?比Perplexity更好用的国产开源平替了解一下?
【8月更文挑战第24天】近日发布的一项研究成果提出了一种革新性的信息检索系统——MindSearch,该系统通过模仿人脑思维方式,有效解决了传统信息检索方法面对复杂查询时的不足。MindSearch利用多代理框架,将用户查询拆解成子问题逐步扩展查询图谱,实现复杂查询的精准定位;通过多层次信息检索,整合不同网页中的相关数据,提高信息提取的准确率;并且能高效处理大规模网页,3分钟内即可检索300多个网页。实验显示,MindSearch不仅提升了响应的深度与广度,还在封闭及开放式问答中表现出色,更符合用户的偏好。不过,MindSearch仍面临查询意图理解、噪音处理及可扩展性等方面的挑战。
219 4
|
机器学习/深度学习 算法
XGBoost中正则化的9个超参数
本文探讨了XGBoost中多种正则化方法及其重要性,旨在通过防止过拟合来提升模型性能。文章首先强调了XGBoost作为一种高效算法在机器学习任务中的应用价值,并指出正则化对于缓解过拟合问题的关键作用,具体包括降低模型复杂度、改善泛化能力和防止模型过度适应训练数据。随后,文章详细介绍了四种正则化方法:减少估计器数量(如使用`early_stopping_rounds`)、使用更简单的树(如调整`gamma`和`max_depth`)、采样(如设置`subsample`和`colsample`)以及收缩(如调节`learning_rate`, `lambda`和`alpha`)。
397 0
XGBoost中正则化的9个超参数
|
存储 分布式计算 Hadoop
Hadoop 运行的三种模式
【8月更文挑战第31天】
1198 0
|
人工智能 Android开发 iOS开发
最新版ps2023免费安装包网盘下载地址Adobe Photoshop2023
Adobe Photoshop Elements 是 Adobe 公司是继 Photoshop 之后全新推出的图像编辑、照片修饰和 Web 图形解决方案。它界面友好,易于使用,功能强大,主要针对商业用户、摄影爱好者,相比专业软件定价较低。
3655 0
|
Cloud Native 前端开发 Java
【微服务36】分布式事务Seata源码解析四:图解Seata Client 如何与Seata Server建立连接、通信
【微服务36】分布式事务Seata源码解析四:图解Seata Client 如何与Seata Server建立连接、通信
875 0
【微服务36】分布式事务Seata源码解析四:图解Seata Client 如何与Seata Server建立连接、通信