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

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 閱讀(1451) 評論(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 是死活進不去了,太弱了……
  回復  更多評論   


只有注冊用戶登錄后才能發表評論。
網站導航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


導航

<2009年4月>
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

統計

常用鏈接

留言簿

隨筆分類

隨筆檔案

文章分類

文章檔案

搜索

最新評論

閱讀排行榜

評論排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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久久久久电影| 亚洲免费视频观看| 亚洲黄色在线观看| 欧美国产亚洲精品久久久8v| 美女视频网站黄色亚洲| 欧美日韩一区二| 日韩小视频在线观看| 国产精品拍天天在线| 国内成人精品一区| 欧美aa国产视频| 欧美日本免费| 欧美日韩精品是欧美日韩精品| 日韩亚洲精品电影| 久久久国产精品一区二区三区| 91久久夜色精品国产网站| 激情另类综合| 亚洲精品国产欧美| 亚洲精品女av网站| 亚洲精品一区二| 日韩视频在线一区二区三区| 一区二区三区视频在线播放| 永久域名在线精品| 伊人久久大香线| 国产午夜亚洲精品羞羞网站| 欧美日韩一区在线| 国产伦理一区| 亚洲精品黄网在线观看| 一本久道久久久| 翔田千里一区二区| 欧美va亚洲va日韩∨a综合色| 亚洲福利视频一区二区| 亚洲国产导航| 中文欧美日韩| 欧美成人亚洲成人日韩成人| 欧美日韩理论| 在线不卡视频| 欧美一级电影久久| 亚洲韩日在线| 亚洲在线中文字幕| 国产精品大片| 一区二区三区高清| 亚洲人成毛片在线播放女女| 国产精品自拍小视频| 亚洲毛片一区二区| 久久综合中文| 性欧美18~19sex高清播放| 欧美日本不卡视频| 亚洲人久久久| 久久综合伊人77777麻豆| 亚洲一区二区三区四区在线观看 | 日韩午夜av电影| 美女国内精品自产拍在线播放| 国产精品免费看久久久香蕉| 亚洲网友自拍| 999在线观看精品免费不卡网站| 欧美区高清在线| 亚久久调教视频| 午夜精品久久久久久久99樱桃 | 久久久久久亚洲精品不卡4k岛国| 亚洲精品一二三区| 国产精品一级在线| 另类亚洲自拍| 欧美日韩在线播放一区二区| 性xx色xx综合久久久xx| 欧美在线网址| 99国产精品久久久久久久久久 | 久久亚洲春色中文字幕| 亚洲东热激情| 一本色道久久加勒比88综合| 欧美日本成人| 久久都是精品| 欧美日韩一级黄| 国产精品美女久久久免费| 欧美刺激性大交免费视频 | 国产精品萝li| 麻豆国产va免费精品高清在线| 美女视频网站黄色亚洲| 欧美亚洲综合另类| 欧美激情一区二区三区蜜桃视频| 亚洲欧美综合v| 欧美日韩国产影片| 欧美福利一区二区三区| 狠狠色2019综合网| 欧美一区视频| 欧美自拍偷拍午夜视频| 国产乱肥老妇国产一区二| 一区二区三区欧美成人| 亚洲欧洲综合| 欧美激情精品久久久久久久变态| 男女视频一区二区| 国产日韩欧美夫妻视频在线观看| 亚洲色图自拍| 久久国产色av| 亚洲成人在线免费| 久久噜噜噜精品国产亚洲综合 | 久久综合国产精品| 美女视频黄免费的久久| 91久久国产综合久久蜜月精品 | 欧美1区2区3区| 欧美成在线观看| 国产精品99久久久久久久久久久久| 欧美成人久久| 亚洲另类在线视频| 美女脱光内衣内裤视频久久网站| 亚洲综合999| 午夜欧美精品| 久久综合精品一区| 欧美xxx在线观看| 欧美天天视频| 国产亚洲精品久久久久婷婷瑜伽| 国产精品日本精品| 国产日韩精品一区二区浪潮av| 欧美日韩国产成人在线观看| 久久久精品国产一区二区三区 | 欧美在线不卡| 欧美一区二区三区视频免费| 久久精品一区二区三区中文字幕| 亚洲——在线| 亚洲永久免费av| 欧美一区二区三区在线观看视频 | 毛片av中文字幕一区二区| 亚洲图片欧美日产| 亚洲精品在线视频观看| 亚洲另类视频| 这里只有精品视频在线| 日韩一级黄色大片| 欧美激情免费在线| 久久综合色一综合色88| 久久一区二区三区av| 久久九九久精品国产免费直播| 亚洲在线播放电影| 亚洲男人的天堂在线| 欧美一区二区国产| 久久久久女教师免费一区| 久久人人超碰| 最新中文字幕一区二区三区| 狼人社综合社区| 久久久久国产精品午夜一区| 在线视频欧美一区| 亚洲在线视频网站| 亚洲综合导航| 欧美在线观看视频一区二区| 欧美专区中文字幕| 老**午夜毛片一区二区三区| 欧美性开放视频| 韩国一区电影| 欧美中文日韩| 久久精品久久综合| 欧美精选在线| 在线观看亚洲视频啊啊啊啊| 亚洲精品四区| 麻豆91精品| 亚洲一区精品电影| 欧美激情中文字幕乱码免费| 国产一级一区二区| 午夜亚洲精品| 一区二区国产日产| 欧美片第一页| 亚洲欧美福利一区二区| 亚洲伦理久久| 亚洲永久网站| 欧美成人三级在线| 韩国av一区二区三区在线观看| av不卡在线| 亚洲日韩欧美视频| 一区二区视频在线观看| 欧美丝袜一区二区| 亚洲美女一区| 欧美专区一区二区三区| 欧美日韩国产综合久久| 亚洲一区二区免费| 欧美在线观看一区| 亚洲综合第一页| 欧美极品在线视频| 午夜亚洲性色福利视频| 久久狠狠亚洲综合| 午夜欧美理论片| 亚洲天堂第二页| 亚洲人体偷拍| 久久综合伊人77777| 美脚丝袜一区二区三区在线观看 | 久久久久99精品国产片| 欧美精品日韩www.p站| 久久免费观看视频| 欧美午夜不卡视频| 99国产成+人+综合+亚洲欧美| 欧美午夜电影完整版| 亚洲激情一区二区| 狠狠色丁香婷婷综合| 久久精品国产久精国产思思| 先锋资源久久| 国产一区二区三区成人欧美日韩在线观看| 亚洲国产二区| 一本色道久久综合亚洲精品婷婷 | 久久视频在线免费观看| 久久久www成人免费无遮挡大片| 国产欧美欧美| 久久久精品2019中文字幕神马| 亚洲一区二区三区中文字幕在线| 久久男人资源视频|