• <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>

            oyjpArt ACM/ICPC算法程序設(shè)計(jì)空間

            // I am new in programming, welcome to my blog
            I am oyjpart(alpc12, 四城)
            posts - 224, comments - 694, trackbacks - 0, articles - 6

            Supermarket
            Time Limit:2000MS  Memory Limit:65536K
            Total Submit:987 Accepted:369

            Description
            A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold by a deadline dx that is measured as an integral number of time units starting from the moment the sale begins. Each product takes precisely one unit of time for being sold. A selling schedule is an ordered subset of products Sell ≤ Prod such that the selling of each product x∈Sell, according to the ordering of Sell, completes before the deadline dx or just when dx expires. The profit of the selling schedule is Profit(Sell)=Σx∈Sellpx. An optimal selling schedule is a schedule with a maximum profit.
            For example, consider the products Prod={a,b,c,d} with (pa,da)=(50,2), (pb,db)=(10,1), (pc,dc)=(20,2), and (pd,dd)=(30,1). The possible selling schedules are listed in table 1. For instance, the schedule Sell={d,a} shows that the selling of product d starts at time 0 and ends at time 1, while the selling of product a starts at time 1 and ends at time 2. Each of these products is sold by its deadline. Sell is the optimal schedule and its profit is 80.


            Write a program that reads sets of products from an input text file and computes the profit of an optimal selling schedule for each set of products.

             

             

             

             

             

            Input
            A set of products starts with an integer 0 <= n <= 10000, which is the number of products in the set, and continues with n pairs pi di of integers, 1 <= pi <= 10000 and 1 <= di <= 10000, that designate the profit and the selling deadline of the i-th product. White spaces can occur freely in input. Input data terminate with an end of file and are guaranteed correct.

            Output
            For each set of products, the program prints on the standard output the profit of an optimal selling schedule for the set. Each result is printed from the beginning of a separate line.

            Sample Input

            4  50 2  10 1   20 2   30 1
            7  20 1   2 1   10 3  100 2   8 2
            5 20  50 10
            

             

             

             

             

             

            Sample Output

            80
            185

             

             

             

             

             

            Hint
            The sample input contains two product sets. The first set encodes the products from table 1. The second set is for 7 products. The profit of an optimal schedule for these products is 185.

            Source
            Southeastern Europe 2003


            貪心是很容易想到的 盡可能將利潤大的選中 然后盡可能放到DeadLine附近去

            這里就有一個(gè)問題 怎樣盡快拿到一個(gè)deadLine往上數(shù)第一個(gè)空閑日期?

            想法0:直接搜o(n^2)的查找總費(fèi)時(shí) 可能超時(shí)!

            想法1:線段樹!問題建模:一個(gè)數(shù)的前面第幾個(gè)是空閑?
            對(duì)10000的區(qū)間建立線段樹 可以將查詢復(fù)雜度降低到NlogN

            想法2:   并查集!對(duì)一個(gè)連續(xù)非空閑區(qū)間建立集合 其代表元選擇上面第一個(gè)空閑的日期!時(shí)間復(fù)雜度再降低到o(N)! 系數(shù)為ackman函數(shù) 很小

            Problem Id:1456  User Id:oyjpart
            Memory:172K  Time:15MS
            Language:G++  Result:Accepted

            //by oyjpArt
            #include <stdio.h>
            #include <string.h>
            #include <algorithm>
            using namespace std;
            const int N = 10010;
            int ng;
            struct good{int p, d;}g[N];
            int p[N];
            bool flag[N];

            bool operator < (const good& a, const good& b) {return a.p > b.p; }

            int find_set(int a) {
             int t = a;
             while(t != p[t]) t = p[t];
             int r = a;
             while(r != p[r]) { int k = p[r]; p[r] = t; r = k; }
             return t;
            }

            int main() {
             int i;
             while(scanf("%d", &ng) != EOF) {
              memset(flag, 0, sizeof(flag));
              int maxd = 0;
              for(i = 0; i<ng; i++) {
               scanf("%d%d", &(g[i].p), &(g[i].d));
               if(g[i].d > maxd) maxd = g[i].d;
              }
              for(i = 1; i<maxd; i++)
               p[i] = i;
              sort(g, g+ng);
              int cnt = 0;
              for(i = 0; i<ng; i++) {
               int d;
               if(!flag[g[i].d]) { d = g[i].d; p[d] = d-1; }
               else d = find_set(g[i].d);
               if(d > 0) cnt += g[i].p;
               flag[d] = 1;
               if(d > 0 && flag[d+1]) p[d] = p[d-1];
              }
              printf("%d\n", cnt);
             }
             return 0;
            }

            Feedback

            # re: PKU 1456 Supermarket 并查集加速實(shí)例  回復(fù)  更多評(píng)論   

            2007-03-11 18:39 by nick
            if(flag[d+1]) p[d] = p[d-1];

            加這一行的目的在哪呢?

            # re: PKU 1456 Supermarket 并查集加速實(shí)例  回復(fù)  更多評(píng)論   

            2007-03-11 20:07 by oyjpart
            p[d]代表選中在D之前第一個(gè)空閑的日子
            就是說d這個(gè)日期用了之后 那么第一個(gè)空閑的日子就一定時(shí)p[d-1];
            也可以這樣寫:
            for(i = 0; i<ng; i++) {
            int d = g[i].d;
            if(flag[g[i].d]) d = find_set(g[i].d);
            if(d > 0) cnt += g[i].p;
            flag[d] = 1;
            p[d] = p[d-1]; //如果p[d-1]沒用用過 那么p[d-1]就應(yīng)該=d-1
            }

            # re: PKU 1456 Supermarket 并查集加速實(shí)例  回復(fù)  更多評(píng)論   

            2007-03-12 03:00 by nick
            @oyjpart

            可以舉個(gè)例子嗎? 真的有些難懂
            什麼情況會(huì)需要用到這個(gè)
            抱歉, 謝謝你的回覆

            # re: PKU 1456 Supermarket 并查集加速實(shí)例  回復(fù)  更多評(píng)論   

            2007-03-12 17:20 by oyjpart
            抱歉 我說的不是很清楚

            首先 我這里的p[]數(shù)組代表的時(shí)parent 也就是在樹形的并查集中父結(jié)點(diǎn)的標(biāo)號(hào) 比如 如果1節(jié)點(diǎn)(根)的子節(jié)點(diǎn)是2,3 則p[2] = p[3] = 1; 而根節(jié)點(diǎn)的父結(jié)點(diǎn)將會(huì)被初始化為自己的標(biāo)號(hào) 如p[1] = 1;
            在樹形的并查集中 我們將根節(jié)點(diǎn)定義為這個(gè)集合的代表元
            而在這個(gè)題目中我們將代表元定義為這個(gè)從某一天往上數(shù)第一個(gè)空閑的日期 比如 日期
            1 2 3 4 5 6 7 8 9 10
            如果3是空閑(沒有被分配一個(gè)商品)而4,5都已經(jīng)分配 那么p[4]=p[5] = 3;
            如果4也是空閑 那么p[3] = 3; p[4] = 4;
            這樣 我們?cè)诜峙渖唐返臅r(shí)候 要得到從某一天往上數(shù)第一個(gè)空閑的日期 就是直接查找這一天所在集合里面的代表元就可以了
            從另外一個(gè)角度上來看 這個(gè)集合其實(shí)時(shí)一個(gè)空閑的天 后面跟著一連串的非空閑天
            比如
            1 2 3 4 5 6 7 8 9 10
            如果3是空閑(沒有被分配一個(gè)商品)而4,5都已經(jīng)分配 6沒有分配 那這個(gè)集合就是3,4,5 其中代表元為3
            以下面這段代碼來作說明吧
            for(i = 0; i<ng; i++) {
            int d = g[i].d;
            if(flag[g[i].d]) d = find_set(g[i].d);
            if(d > 0) cnt += g[i].p;
            flag[d] = 1;
            p[d] = p[d-1]; //如果p[d-1]沒用用過 那么p[d-1]就應(yīng)該=d-1
            }
            如果flag[g[i].d] is true 代表g[i].d這一天已經(jīng)被分配 于是空閑的天就是find_set(g[i].d);
            否則 d = g[i].d; 就是這一天 這個(gè)時(shí)候集合只有一個(gè)元素 就是自己作為代表元
            if(d > 0) cnt += g[i].p; 因?yàn)橛锌赡艽碓獮?(設(shè)置數(shù)據(jù)的時(shí)候p[d-1]為了不越界 故意留出0) 實(shí)際上代表沒有空閑 比如1,2,3,4都滿了 你要放在3的地方 就會(huì)得到0 這個(gè)位置 所以要>0才能cnt += g[i].p;
            flag[d] = 1; 接著把剛才找到的空閑處標(biāo)記
            p[d] = p[d-1]; 然后把剛才找到的空閑處的代表元設(shè)置一下
            這里的設(shè)置對(duì)于下面2中情況都是對(duì)的
            情況1: 1 2 3 4 5
            2空閑 3空閑 我在3處放 然后標(biāo)記p[3] = p[2] = 2;
            情況2: 1 2 3 4 5 6
            2滿 3滿 4空閑 我在4處放 然后標(biāo)記 p[4] = p[3] = 1
            基本上就是這樣了 沒有解釋清楚 真不好意思 :)

            # re: PKU 1456 Supermarket 并查集加速實(shí)例  回復(fù)  更多評(píng)論   

            2007-03-18 21:51 by nick
            呵呵, 真的很謝謝你回覆這麼說
            我懂
            for(i = 0; i<ng; i++) {
            int d = g[i].d;
            if(flag[g[i].d]) d = find_set(g[i].d);
            if(d > 0) cnt += g[i].p;
            flag[d] = 1;
            p[d] = p[d-1]; //如果p[d-1]沒用用過 那么p[d-1]就應(yīng)該=d-1
            }

            但就是不懂原本程序的 if(flag[d+1]) p[d] = p[d-1]; 這一行的意義在哪

            如果這個(gè)程序不加這一行是一定會(huì)錯(cuò)

            但什麼樣的 case 會(huì)導(dǎo)致錯(cuò)誤呢?

            # re: PKU 1456 Supermarket 并查集加速實(shí)例  回復(fù)  更多評(píng)論   

            2007-03-18 21:56 by nick
            喔喔, 我想到了.. 呵呵, 謝謝你

            原本那樣寫讓我比較不好理解

            不過後來的寫法
            p[d] = p[d-1]; //如果p[d-1]沒用用過 那么p[d-1]就應(yīng)該=d-1

            這邊要 if(d>0) 不然 p[d-1] 會(huì) runtime error ^^

            # re: PKU 1456 Supermarket 并查集加速實(shí)例  回復(fù)  更多評(píng)論   

            2007-03-19 17:37 by oyjpart
            o 對(duì) 呵呵~~ 我改一下
            :)
            久久狠狠爱亚洲综合影院| 久久99精品国产麻豆蜜芽| 久久久久久久久久久久中文字幕 | 久久96国产精品久久久| 国产成人精品综合久久久| 久久精品国产免费观看| 蜜桃麻豆www久久| 亚洲中文字幕久久精品无码APP| 国产精品18久久久久久vr| 久久久高清免费视频| 国产精品久久久久久影院| 久久天天躁狠狠躁夜夜躁2014| 7国产欧美日韩综合天堂中文久久久久 | 狠狠色丁香久久婷婷综合蜜芽五月| 精品久久8x国产免费观看| 中文字幕精品久久久久人妻| 狠狠色丁香婷婷综合久久来| 久久无码人妻一区二区三区| 一本色道久久综合狠狠躁篇 | 久久国产亚洲精品| 国内精品伊人久久久久影院对白 | 天天影视色香欲综合久久| 久久777国产线看观看精品| 久久人人爽爽爽人久久久| 亚洲精品乱码久久久久66| 一本久久综合亚洲鲁鲁五月天| 精品久久久久久久中文字幕| 久久精品国产亚洲欧美| 精品久久无码中文字幕| 久久精品99久久香蕉国产色戒 | 国产成人精品久久| 一级女性全黄久久生活片免费 | 久久久久久久女国产乱让韩| 亚洲午夜精品久久久久久app| 亚洲国产成人久久一区WWW| 色婷婷噜噜久久国产精品12p| 久久99精品久久久久久9蜜桃| 久久久久久国产精品免费免费 | 久久精品免费一区二区| 久久精品国产久精国产思思| 国产婷婷成人久久Av免费高清|