Bribe the Prisoners SPOJ - GCJ1C09C

本文探讨了一种基于动态规划和剪枝策略解决的贿赂囚犯问题。在一个直线上排列的监狱中,当一个囚犯被释放时,其邻居将通过窗户得知并传递此消息,直到消息到达边界或空牢房。为了避免愤怒的囚犯破坏牢房,必须向他们支付金币作为贿赂。文章提供了一个算法,用于计算在一系列囚犯释放计划中所需的最小金币数量。

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

滴答滴答---题目链接 

贿赂囚犯 Bribe the prisoners ( 动态规划+剪枝)

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.

 

#include<stdio.h>
#include<iostream>
#include<math.h>
using namespace std;
int p,Q ;
//区间动态规划
//bribe the prisoner
//定义一个二维数组。依次用来填充最小的花费。
int dp[109][109];//表示从第i个填充到j个时的最小花费。
//同时定义一个存放罪犯的数组。
int a[109];
int f;
const int inf=0x3f3f3f;
void solve()
{
    a[0]=0;
    a[Q+1]=p+1;//为了解决边界问题。
    for(int i=0; i<=Q; i++)
        dp[i][i+1]=0;//初始化,因为所有的从i到i+1的花费除去边界都是0;
    //循环求解。定义w表示区间的范围,w=2表示跨度为2的情况,也就是该区间里面只有一个要释放的犯人
    for(int w=2; w<=Q+1; w++)
    {
        //每次选的范围都是w,从i到j 的范围内的最小值等于从i到K加从第k到j的最小值。
        for(int i=0; i+w<=Q+1; i++)
        {
            //此处用到的k恰是其中的中值。
            int j=i+w,tmp=inf;//tmp用来保存当前区间的当前最好情况的花费金币数
            for(int k=i+1; k<j; k++)
                tmp=min(tmp,dp[i][k]+dp[k][j]);
            dp[i][j]=tmp+a[j]-a[i]-2;//此处就是当前区间最小值。
        }
    }
    printf("Case #%d: %d\n",f,dp[0][Q+1]);
}
int main()
{
    int t;
    scanf("%d",&t);
    f=0;
    while(t--)
    {
        scanf("%d%d",&p,&Q);
        for(int i=1; i<=Q; i++)
            scanf("%d",&a[i]);
        f++;
        solve();
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值