• Bribe the Prisoners SPOJ


    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 hisother 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.

    题解:

      区间dp水题,套路状态dp[l][r]表示将l到r的点全部搞完的最小代价,因为这个状态转移会重复所以考虑加一个限制条件,不包括两个端点。

      那么我们就套路枚举断点,暴力转移,dp[l][r]=min(dp[l][k]+dp[k][r]+w[r]-w[l]-2)减去2是因为不算端点,要加两个关键点0和n,答案就是dp[0][n](因为不考虑端点),我是写的记忆化搜索,自然一点,如果for的话先枚举一个len就可以了。

    代码:

    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <cmath>
    #include <iostream>
    #define ll long long
    #define MAXN 500
    using namespace std;
    int n,q,a[MAXN],w[MAXN],b[MAXN][MAXN];
    ll dp[MAXN][MAXN];
    
    ll dfs(int l,int r){
        if(b[l][r]) return dp[l][r];
        if(l+1==r) return 0;
        b[l][r]=1;
        ll tmp=1<<30;
        for(int i=l+1;i<=r;i++){
            tmp=min(tmp,dfs(l,i)+dfs(i,r)+w[r]-w[l]-2);
        }
        dp[l][r]=tmp;
        return tmp;
    }
    
    int main()
    {    
        int t;cin>>t;int Case=0;
        while(t--){
            scanf("%d%d",&n,&q);
            memset(b,0,sizeof(b));
            memset(dp,127,sizeof(dp));
            memset(a,0,sizeof(a));
            memset(w,0,sizeof(w));
            for(int i=1;i<=q;i++) scanf("%d",&a[i]);
            a[++q]=0,a[++q]=n+1;
            sort(a+1,a+q+1);
            for(int i=1;i<=q;i++) w[i]=a[i];
            int k=unique(w+1,w+q+1)-w-1;
            for(int i=1;i<=q;i++) a[i]=lower_bound(w+1,w+k+1,a[i])-w;
            printf("Case #%d: %lld
    ",++Case,dfs(1,k));
        }
        return 0;
    }
  • 相关阅读:
    121devg笔记 003
    java并发和排序的简单例子(Runnable+TreeSet)
    java的ConCurrentHashMap
    tortoisegit 配置ssh登录
    springboot支持http2
    MySQL数据库远程按日期备份windows脚本
    java web项目 jetty容器启动时,访问.jsp页面失败的问题。
    Oracle解决ora01653 无法通过1024扩展 解决Oracle表空间扩展问题
    vue3 [Vue warn]: onMounted is called when there is no active component instance to be associated with
    vue echarts There is a chart instance already initialized on the dom
  • 原文地址:https://www.cnblogs.com/renjianshige/p/7554617.html
Copyright © 2020-2023  润新知