zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    c语言博客作业09
    c语言|博客作业08
    C语言|博客作业07
    C语言|博客作业06
    C语言|博客作业05
    C语言|博客作业04
    C语言|博客作业03
    关于Vue.js里面输入框在v-model之后如果给其绑定属性赋初始值导致绑定数据不响应问题
    前后端分离开发模式中关于前端取得分页数据时的分页问题(前端使用ant design pro)
    《电子病案在病案管理中存在问题及对策》文献阅读随笔
  • 原文地址:https://www.cnblogs.com/renjianshige/p/7554617.html
Copyright © 2011-2022 走看看