zoukankan      html  css  js  c++  java
  • spoj GCJ1C09C Bribe the Prisoners

    题目链接:

    http://www.spoj.com/problems/GCJ1C09C/

    题意:

    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, N. N 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 

    2
    8 1
    3
    20 3
    3 6 14

    Output 

    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。

    实现:

     1 #include <cstdio>
     2 #include <iostream>
     3 #include <algorithm>
     4 #include <cstring>
     5 using namespace std;
     6 
     7 const int MAXP = 10000, MAXQ = 100, INF= 0x3f3f3f3f;
     8 int n, p, q, a[MAXP + 5], dp[MAXQ + 5][MAXQ + 5];
     9 
    10 int solve()
    11 {
    12     memset(dp, 0, sizeof(dp));
    13     a[0] = 0;
    14     a[q + 1] = p + 1;
    15     for (int j = 2; j <= q + 1; j++)
    16     {
    17         for (int i = 0; i <= q + 1 - j; i++)
    18         {
    19             dp[i][i + j] = INF;
    20             for (int k = i + 1; k < i + j; k++)
    21                 dp[i][i + j] = min(dp[i][i + j], dp[i][k] + dp[k][i + j]);
    22             dp[i][i + j] += a[i + j] - a[i] - 2;
    23         }
    24     }
    25     return dp[0][q + 1];
    26 }
    27 
    28 int main()
    29 {
    30     cin >> n;
    31     for (int t = 1; t <= n; t++)
    32     {
    33         cin >> p >> q;
    34         for (int i = 1; i <= q; i++)
    35         {
    36             scanf("%d", &a[i]);
    37         }
    38         cout << "Case #" << t << ": " << solve() << endl;
    39     }
    40     return 0;
    41 }
  • 相关阅读:
    常见 Web 安全攻防总结
    传统方式接口测试返回值json验证
    Springboot中RestTemplate -- 用更优雅的方式发HTTP请求
    mock简单的json返回
    MySQL数据库学习笔记(五)----MySQL字符串函数、日期时间函数
    MySQL数据库学习笔记(四)----MySQL聚合函数、控制流程函数(含navicat软件的介绍)
    MySQL数据库学习笔记(三)----基本的SQL语句
    MySQL数据库学习笔记(一)----MySQL 5.6.21的安装和配置(setup版)
    python实现广度优先搜索
    php递归
  • 原文地址:https://www.cnblogs.com/wangyiming/p/6388094.html
Copyright © 2011-2022 走看看