Google Code Jam 2010 Round 1B Problem B. Picking Up Chicks

本文探讨了一群沿直线道路向东奔跑的鸡,在限定时间内至少K只鸡到达终点的问题。使用移动起重机通过交换位置的方式帮助鸡群前进,目标是最小化交换次数。文中提供了解决方案的算法实现。
https://code.google.com/codejam/contest/635101/dashboard#s=p1
 

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, CC test cases follow. Each test case starts with 4 integers on a line -- NKB 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 
 

Output 
 
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
Case #1: 0
Case #2: 2
Case #3: IMPOSSIBLE

 

Solution

vector<int>X;
vector<int>V;
vector<pair<int, int>>ck;

int solve(int N, int K, int B, int T1)
{
    int sw = 0;
    int slow = 0;
    for (int i = N - 1; i >= 0; i--) {
        if (ck[i].first + T1 * ck[i].second >= B) {
            K--;
            sw += slow;
            
            if (!K)
                break;
            
        } else {
            slow++;
        }
    }
    
    if (K)
        return -1;
    
    return sw;
}

int main()
{
    freopen("in.in", "r", stdin);
    if (WRITE_OUT_FILE)
        freopen("out.out", "w", stdout);
    
    int T;
    scanf("%d\n", &T);
    if (!T) {
        cerr << "Check input!" << endl;
        exit(0);
    }
    
    for (int t = 1; t <= T; t++) {
        if (WRITE_OUT_FILE)
            cerr << "Solving: #" << t << " / " << T << endl;
        
        int N, K, B, T1;
        scanf("%d %d %d %d\n", &N, &K, &B, &T1);
        
        X.clear();
        V.clear();
        ck.clear();
            
        for (int i = 0; i < N; i++) {
            int x;
            scanf("%d", &x);
            
            X.push_back(x);
        }

        for (int i = 0; i < N; i++) {
            int v;
            scanf("%d", &v);
            
            V.push_back(v);
        }
        
        for (int i = 0; i < N; i++) {
            ck.push_back(pair<int, int>(X[i], V[i]));
        }
        
        sort(ck.begin(), ck.end());
        
        auto result = solve(N, K, B, T1);
        
        if (result >= 0) {
            printf("Case #%d: %d\n", t, result);
        } else {
            printf("Case #%d: IMPOSSIBLE\n", t);
        }
        
        
    }
    
    fclose(stdin);
    if (WRITE_OUT_FILE)
        fclose(stdout);
    
    return 0;
}

 

转载于:https://www.cnblogs.com/fatlyz/p/3680793.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值