GCJ 2009 Round 1C Bribe the Prisoners

探讨一个囚犯被释放后如何通过最优顺序释放其他囚犯来最小化金币贿赂总成本的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Bribe the Prisoners

no tags 

  

Problem

In a kingdom there are prison cells (numbered 1 to P) built to form a straight line segment. Cells number i and i+1 are adjacent, and prisoners in adjacent cells are called "neighbours." A wall with a window separates adjacent cells, and neighbours can communicate through that window.

All prisoners live in peace until a prisoner is released. When that happens, the released prisoner's neighbours find out, and each communicates this to his other neighbour. That prisoner passes it on to his other neighbour, and so on until they reach a prisoner with no other neighbour (because he is in cell 1, or in cell P, or the other adjacent cell is empty). A prisoner who discovers that another prisoner has been released will angrily break everything in his cell, unless he is bribed with a gold coin. So, after releasing a prisoner in cell A, all prisoners housed on either side of cell A - until cell 1, cell P or an empty cell - need to be bribed.

Assume that each prison cell is initially occupied by exactly one prisoner, and that only one prisoner can be released per day. Given the list of Q prisoners to be released in Q days, find the minimum total number of gold coins needed as bribes if the prisoners may be released in any order.

Note that each bribe only has an effect for one day. If a prisoner who was bribed yesterday hears about another released prisoner today, then he needs to be bribed again.

Input

The first line of input gives the number of cases, NN test cases follow. Each case consists of 2 lines. The first line is formatted as

P Q

where P is the number of prison cells and Q is the number of prisoners to be released.
This will be followed by a line with Q distinct cell numbers (of the prisoners to be released), space separated, sorted in ascending order.

Output

For each test case, output one line in the format

Case #X: C
where X is the case number, starting from 1, and C is the minimum number of gold coins needed as bribes.

Limits

1 ≤ N ≤ 100
Q ≤ P
Each cell number is between 1 and P, inclusive.

Large dataset

1 ≤ P ≤ 10000
1 ≤ Q ≤ 100

Sample


Input 
 

Output 
 
2
8 1
3
20 3
3 6 14
Case #1: 7
Case #2: 35

Note

In the second sample case, you first release the person in cell 14, then cell 6, then cell 3. The number of gold coins needed is 19 + 12 + 4 = 35. If you instead release the person in cell 6 first, the cost will be 19 + 4 + 13 = 36.

题意:

一个监狱里有P个并排着的牢房。从左至右依次编号为1,2,...,P。最初所有的牢房里都住着一个囚犯。相邻的两个牢房之间可以互通信息。

现在要释放一些囚犯。如果释放某个牢房里的囚犯,其相邻的牢房里的囚犯就会知道,因而发生暴动。所以,释放某个囚犯时,必须要贿赂两旁相邻牢房的囚犯一枚金币。另外,为了防止释放的消息在相邻牢房间传开,不仅两旁直接相邻的牢房,所有可能听到消息的囚犯,即直到空牢房为止或直到监狱两端为止,此间的所有囚犯都必须给一枚金币。

现在要释放Q名囚犯。如果选择所需金币数量尽量少的顺序释放,最少需要多少枚金币?

分析:DP算法

dp[i][j]表示的是,将从a[i]号囚犯到a[j]号囚犯(不含两端的囚犯)的连续部分里的所有囚犯都释放时,所需的最少金币总数。

为了更方便的处理两端的情况,我们把左端当成0号囚犯,右端当成Q + 1号囚犯。这样,dp[0][Q + 1]就是答案。

#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 10000 + 10;
const int INF = 10000000;

int P, Q, a[maxn];    //A中保存输入数据,下标从1开始
int dp[maxn][maxn];   //dp[i][j] := 释放(i, j)所需的金币
int main()
{
    int T;
    scanf("%d", &T);
    for (int cas = 1; cas <= T; cas++){
        scanf("%d%d", &P, &Q);
        for (int i = 1; i <= Q; i++){
            scanf("%d", &a[i]);
        }

        //为了方便,将两端加入a中
        a[0] = 0;
        a[Q + 1] = P + 1;

        //初始化
        for (int q = 0; q < Q; q++){
            dp[q][q + 1] = 0;
        }

        //从短的区间开始填充dp
        for (int w = 2; w <= Q + 1; w++){
            for (int i = 0; i + w <= Q + 1; i++){
                //计算dp[i][j]
                int j = i + w, t = INF;
                //枚举最初释放的囚犯,计算最小的费用
                for (int k = i + 1; k < j; k++){
                    t = min(t, dp[i][k] + dp[k][j]);
                }

                //最初的释放还需要与所释放囚犯无关的a[j] - a[i] - 2枚金币
                dp[i][j] = t + a[j] - a[i] - 2;
            }
        }
        printf("Case #%d: %d\n", cas, dp[0][Q + 1]);
    }
    return 0;
}



### C语言实现WGS84与GCJ-02坐标系之间转换 为了实现在C语言中完成WGS84到GCJ-02的坐标转换,通常会采用特定的数学模型来调整经纬度数值。下面给出了一种基于已知公式的实现方式。 #### 定义辅助函数用于计算偏移量 ```c #include <math.h> #define PI 3.1415926535897932384626 double transformLat(double x, double y) { double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(fabs(x)); ret += (20.0 * sin(6.0 * x * PI) + 20.0 * sin(2.0 * x * PI)) * 2.0 / 3.0; ret += (20.0 * sin(y * PI) + 40.0 * sin(y / 3.0 * PI)) * 2.0 / 3.0; ret += (160.0 * sin(y / 12.0 * PI) + 320 * sin(y * PI / 30.0)) * 2.0 / 3.0; return ret; } double transformLon(double x, double y) { double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(fabs(x)); ret += (20.0 * sin(6.0 * x * PI) + 20.0 * sin(2.0 * x * PI)) * 2.0 / 3.0; ret += (20.0 * sin(x * PI) + 40.0 * sin(x / 3.0 * PI)) * 2.0 / 3.0; ret += (150.0 * sin(x / 12.0 * PI) + 300.0 * sin(x / 30.0 * PI)) * 2.0 / 3.0; return ret; } ``` #### 主要转换逻辑 通过上述定义的帮助函数`transformLat()` 和 `transformLon()`, 可以进一步构建完整的转换过程: ```c void wgs84ToGcj02(double wgLat, double wgLon, double* gcjLat, double* gcjLon){ if(outOfChina(wgLat, wgLon)){ *gcjLat = wgLat; *gcjLon = wgLon; return ; } double dLat = transformLat(wgLon - 105.0, wgLat - 35.0); double dLon = transformLon(wgLon - 105.0, wgLat - 35.0); double radLat = wgLat / 180.0 * PI; double magic = sin(radLat); magic = 1 - 0.00669342162296594323 * magic * magic; double sqrtMagic = sqrt(magic); dLat = (dLat * 180.0) / ((6378245 * (1 - 0.00669342162296594323)) / (magic * sqrtMagic) * PI); dLon = (dLon * 180.0) / (6378245 / sqrtMagic * cos(radLat) * PI); *gcjLat = wgLat + dLat; *gcjLon = wgLon + dLon; }[^3] bool outOfChina(double lat, double lon){ // 判断是否在中国范围内 if(lon < 72.004 || lon > 137.8347) return true; if(lat < 0.8293 || lat > 55.8271) return true; return false; } ``` 此代码片段展示了如何利用给定的纬度(`wgLat`)和经度(`wgLon`)作为输入参数,并返回对应的GCJ-02标准下的新位置[`gcjLat`,`gcjLon`]。对于不在中国境内的地点,则不做任何变换处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值