青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

GoogleCodejam2009 Round 1C Bribe the Prisoners

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, 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
QP
Each cell number is between 1 and P, inclusive.

Small dataset

1 ≤ P ≤ 100
1 ≤ Q ≤ 5

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.


題目分析:
從直覺上來說,這是一道動態規劃的題目,關鍵是如何建立狀態遞推。有一個很明顯的規律是,釋放一個牢房的犯人,只能影響到左邊第一個空的牢房和右邊第一個空的牢房,而與其它的無關。所以,釋放了一個囚犯,整個連續的牢房被分成了2段,而這兩段又都可以看成是單獨的兩個互不影響的一段,這樣,腦子里有一種很直覺的想法就是,這是一棵類似于二叉樹的結構,這顆二叉樹最后的形態就決定了最終的結果。
我們從第二個Case為例子介紹算法:
1.首先對該題所提到的監獄增加兩個cell,0號和P + 1號, 這兩個cell是不存在的,只是為了增加程序的可編寫和公式的一致,我們用一個vector v 來存儲釋放囚犯的監獄號,v按照從小到達的順序排列,如第二個例子中,v(0...4) = (0,3, 6, 14, 21)
2. 我們用F[i][j]表示 從v[i]到v[j]這一段中所需要的賄賂金最小值,那么,F[0][4]就是最后需要的結果, 代表從v[0]到v[4]也就是從(0, 21)這之間的牢房中釋放囚犯所需要的錢(不包括邊界,實際需要錢賄賂的囚犯在1到20號房子中)
3. 例如第三個例子,F[0][4] = Min(F[0][i] + F[i][3]) + (v[4] - v[0] - 2) , 其可以中i = 1, 2, 3
    可以從第三個例子中看到,無論你選擇釋放哪一個囚犯,所需的金額都是一定的,正好是這段之間住人的牢房數 - 1
4.更加抽象出最終的公式, 我們用L代表左邊界, R代表右邊界
    F[L][R] = Min(F[L][i] + F[i][R]) + v[R] - V[L] - 2,   i = L + 1, L + 2, ……R- 1,  當L + 1 != R時
    F[L][R] = 0, 當L + 1 == R時, 這個就是上面提到的二叉樹的葉子節點
   通過這個公式就可以得到最終的結果了。

 1#include <iostream>
 2#include <vector>
 3#include <queue>
 4#include <string>
 5#include <algorithm>
 6#include <set>
 7#include <map>
 8using namespace std;
 9
10#define ONLINEJUDGE
11#define MAXN 11000
12#define MAXQ 110
13#define MAXP 110
14#define MIN(a, b) ((a) < (b) ? (a):(b))
15
16int P, Q;
17vector<int> v;
18vector<int> Ret, buf;
19int F[MAXP][MAXP];
20
21int Find(int l, int r)
22{
23    if(l + 1 == r)
24    {
25        F[l][r] = 0;
26        return F[l][r];
27    }

28    if(F[l][r] >= 0return F[l][r];
29
30    int i, j;
31    int iMin;
32    iMin = 999999999;
33    for(i = l + 1; i < r; i++)
34    {
35        iMin = MIN(iMin, Find(l, i) + Find(i, r));
36    }

37    F[l][r] = iMin + v[r] - v[l] - 2;
38    return F[l][r];
39}

40
41int main()
42{
43#ifdef ONLINEJUDGE
44    freopen("C-large.in""r", stdin);
45    freopen("C-large.out""w", stdout);
46#endif
47
48    int iCaseTimes, i, j;
49    int iBuf;
50    int iMax, iRet, iMin;
51
52    scanf("%d"&iCaseTimes);
53    for(int k = 0; k < iCaseTimes; k++)
54    {
55        printf("Case #%d: ", k + 1);
56        scanf("%d%d"&P, &Q);
57        v.clear();
58        v.push_back(0);
59        for(i = 0; i < Q; i++)
60        {
61            scanf("%d"&iBuf);
62            v.push_back(iBuf);
63        }

64        v.push_back(P + 1);
65
66        iMin = 0;
67        memset(F, -1sizeof(F));
68        sort(v.begin(), v.end());
69        
70        iMin = 999999999;
71        for(i = 1; i < v.size() - 1; i++)
72        {
73            iMin = MIN(iMin, Find(0, i) + Find(i, v.size() - 1));
74        }

75        F[0][v.size() - 1= iMin + v[v.size() - 1- v[0- 2;
76        printf("%d\n", F[0][v.size() - 1]);
77    }

78    return 0;
79}

posted on 2009-09-14 19:28 Philip85517 閱讀(1447) 評論(2)  編輯 收藏 引用 所屬分類: GoogleCodeJam

評論

# re: GoogleCodejam2009 Round 1C Bribe the Prisoners[未登錄] 2009-09-15 16:09 vincent

googlecodejam啊 ..貌似是9月2日?
當天有事情沒參加..orz  回復  更多評論   

# re: GoogleCodejam2009 Round 1C Bribe the Prisoners[未登錄] 2009-09-15 23:32 Philip85517

@vincent
9月2號開始的是資格賽,這個是上個星期天的比賽。
估計Round2 是死活進不去了,太弱了……
  回復  更多評論   

導航

<2025年9月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

統計

常用鏈接

留言簿

隨筆分類

隨筆檔案

文章分類

文章檔案

搜索

最新評論

閱讀排行榜

評論排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            亚洲一区二区三区免费在线观看| 免费视频最近日韩| 午夜在线成人av| 亚洲欧美国产va在线影院| 亚洲一区二区伦理| 亚洲男人的天堂在线观看| 欧美在线短视频| 久久这里只有精品视频首页| 欧美成人黄色小视频| 91久久精品一区二区别| 日韩视频在线播放| 性做久久久久久| 欧美电影在线观看完整版| 欧美日韩国产色综合一二三四 | 国产日产欧美精品| 在线免费一区三区| 午夜精品福利一区二区蜜股av| 久久精品最新地址| 亚洲日韩第九十九页| 午夜欧美电影在线观看| 欧美成人自拍| 国产亚洲美州欧州综合国| 亚洲每日在线| 久久手机免费观看| 一区二区黄色| 欧美1区2区3区| 国产一区二区三区久久悠悠色av | 欧美大片91| 亚洲综合丁香| 欧美日韩三区| 亚洲经典在线看| 久久偷窥视频| 亚洲永久网站| 欧美日韩在线免费视频| 亚洲国产精品999| 久久久久久国产精品mv| 一区二区不卡在线视频 午夜欧美不卡'| 久久福利影视| 国产精品亚洲第一区在线暖暖韩国| 亚洲福利一区| 老色鬼精品视频在线观看播放| 亚洲婷婷综合色高清在线 | 久久久久久9999| 国产一区二区精品久久| 午夜欧美理论片| 在线亚洲一区二区| 欧美日韩激情网| 91久久综合亚洲鲁鲁五月天| 欧美一区二区三区视频在线| 亚洲欧洲视频| 久久综合久久综合这里只有精品 | 国产精品亚洲综合一区在线观看 | 亚洲综合日本| 一区二区三区波多野结衣在线观看| 乱中年女人伦av一区二区| 国产一区二区视频在线观看| 亚洲一区二区视频在线观看| 亚洲欧洲日本mm| 女人色偷偷aa久久天堂| 亚洲国产美女| 亚洲国语精品自产拍在线观看| 久久亚洲一区二区| 亚洲国产欧美在线| 亚洲人成在线观看网站高清| 欧美xart系列高清| 亚洲精品国产品国语在线app| 农夫在线精品视频免费观看| 玖玖在线精品| 亚洲美女在线看| 一区二区三区**美女毛片| 国产精品电影观看| 久久国产手机看片| 久久久亚洲国产美女国产盗摄| 在线精品一区二区| 亚洲看片网站| 国产无一区二区| 欧美xx视频| 欧美日韩日本国产亚洲在线| 亚洲欧美在线高清| 久久精品亚洲国产奇米99| 亚洲茄子视频| 亚洲一区二区三区中文字幕在线| 国产午夜精品久久久| 欧美激情五月| 国产精品蜜臀在线观看| 快she精品国产999| 欧美日韩亚洲综合一区| 久久精品理论片| 免费av成人在线| 性欧美长视频| 欧美精品免费播放| 久久精品国产一区二区三| 久久午夜电影网| 亚洲永久免费精品| 狂野欧美一区| 亚洲欧美区自拍先锋| 久久久久国产一区二区三区四区| 99热这里只有精品8| 欧美中文字幕视频| 日韩午夜高潮| 久久久久在线观看| 性欧美大战久久久久久久久| 另类尿喷潮videofree| 欧美一级电影久久| 欧美日韩a区| 欧美激情亚洲| 永久91嫩草亚洲精品人人| 久久蜜桃资源一区二区老牛 | 亚洲激情视频在线播放| 中文av一区二区| 日韩视频免费观看| 欧美在线播放视频| 亚洲欧美伊人| 欧美午夜大胆人体| 最近看过的日韩成人| 精品999久久久| 欧美一级电影久久| 性做久久久久久| 欧美日韩一区成人| 亚洲激情欧美| 亚洲丰满在线| 久久天堂精品| 你懂的国产精品| 黑人操亚洲美女惩罚| 午夜欧美电影在线观看| 欧美一区二区三区久久精品| 欧美午夜视频网站| 99精品免费网| 亚洲中字在线| 国产精品高清在线观看| 亚洲视频导航| 午夜亚洲视频| 国产日韩视频| 久久精品五月| 欧美大片第1页| 亚洲精品在线一区二区| 欧美另类变人与禽xxxxx| 亚洲乱码国产乱码精品精可以看| 亚洲精品中文字幕在线| 欧美精品日本| 国产精品99久久久久久白浆小说| 亚洲视频在线观看网站| 国产精品高潮视频| 欧美尤物巨大精品爽| 久久久在线视频| 在线欧美日韩国产| 欧美丰满高潮xxxx喷水动漫| 亚洲国产精品一区二区www在线 | 好吊成人免视频| 久久综合狠狠综合久久激情| 欧美大片在线看免费观看| 91久久在线播放| 欧美日韩国产精品| 亚洲自拍偷拍色片视频| 噜噜爱69成人精品| 99国产精品国产精品久久| 国产精品ⅴa在线观看h| 欧美在线观看一区| 欧美激情在线观看| 亚洲欧美网站| 亚洲电影在线观看| 欧美精品在线一区| 欧美一区二区视频在线观看2020| 久久影视三级福利片| 亚洲精品影院| 国产色综合久久| 欧美国产视频一区二区| 亚洲一区三区视频在线观看| 久久综合亚州| 亚洲一级片在线看| 在线成人av| 国产精品视频一区二区三区| 久久婷婷久久一区二区三区| 亚洲精品久久视频| av成人老司机| 欧美激情久久久| 国产婷婷色一区二区三区四区| 伊人久久大香线| 亚洲视频大全| 久久只精品国产| 亚洲午夜精品| 亚洲国产精品成人久久综合一区| 欧美日韩高清在线观看| 欧美在线观看www| 日韩视频中文| 欧美成人国产一区二区| 亚洲欧美在线免费| 亚洲精品一区二区三区樱花 | 老司机精品福利视频| 亚洲午夜激情| 亚洲福利一区| 久久频这里精品99香蕉| 亚洲主播在线播放| 一本大道久久精品懂色aⅴ| 韩日精品中文字幕| 国产亚洲福利一区| 国产精品资源在线观看| 欧美日韩性视频在线| 美女精品一区| 另类图片综合电影|