• <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>
            posts - 195,  comments - 30,  trackbacks - 0

            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.


            #include<iostream>
            #include<cstdlib>
            using namespace std;
            #define MAX 10001
            #define min(a,b) ((a)<(b) ? (a) : (b))
            int father[MAX];
            int p[MAX];
            int result[MAX];
            struct job{
                 int value;
                 int T;
              }JOB[MAX];
            bool operator <(job job1,job job2)
             {
              if(job1.value>job2.value)
              return true;
              else
              return false;
             } 
             int find(int x)  //·µ»ØµÚ£Ø½ÚµãËùÊô¼¯ºÏµÄ¸ù½áµã
              {
            int px=x;
            while(p[px]>=0)
               px=p[px];
            int tmp;
            while(p[x]>=0)//ӦΪ³õֵΪ¸º
            {
               tmp=p[x];
               p[x]=px;
               x=tmp;
            }
            return px;
            }

               void UNION(int x,int y)
            {
            x=find(x);
            y=find(y);
            if(x==y)
               return ;
            int tmp=p[x]+p[y];
            if(p[x]>p[y])
            {
               p[y]=tmp;
               p[x]=y;
            }
            else
            {
               p[x]=tmp;
               p[y]=x;
            }
            }
             
              int main()
              {
              freopen("s.txt","r",stdin);
              freopen("key.txt","w",stdout);
              int num,temp=0;
              while(cin>>num)
              {
              memset(result,0,num);
              int i,l,j,k=0;
              for( i=0;i<num;i++)
              {
               cin>>JOB[i].value>>JOB[i].T;
               father[i]=i;
               p[i]=-1;
              }
              sort(JOB,JOB+num);
              for(i=0;i<num;i++)
              {
               j=find(min(JOB[i].T,num-1));//
               if(father[j]!=0)
                   {
                 k++;
                 result[k]=i;
                    l=find(father[j]-1);
                    UNION(l,j);
                    father[j]=father[l];
                }
              }
              i=0;
              for(j=1;j<=k;j++)
              { 
               i+=JOB[result[j]].value;}
                cout<<i<<endl;
               }

              //system("PAUSE");
              return   0;
              }
            對著課本寫得,自己都看不怎么懂。

            posted on 2009-07-02 13:11 luis 閱讀(517) 評論(0)  編輯 收藏 引用 所屬分類: 貪心*二分
            <2025年7月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            常用鏈接

            留言簿(3)

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            友情鏈接

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            久久无码人妻一区二区三区 | 69久久夜色精品国产69| 精品国产99久久久久久麻豆| 久久精品国产亚洲αv忘忧草 | 久久精品国产2020| 中文精品久久久久人妻不卡| 精品午夜久久福利大片| 日韩va亚洲va欧美va久久| 少妇无套内谢久久久久| 国产精品久久久久影院嫩草| 亚洲国产成人久久精品99| AAA级久久久精品无码片| 精品久久久久久无码免费| 亚洲欧美日韩久久精品第一区| 亚洲国产成人久久综合碰碰动漫3d| 久久久WWW成人| 国产日产久久高清欧美一区| 伊人色综合久久天天网| 国产一区二区精品久久岳| 精品无码久久久久久尤物| 久久久精品久久久久影院| 青青国产成人久久91网| 亚洲婷婷国产精品电影人久久| 久久久久亚洲av无码专区| 久久婷婷人人澡人人爽人人爱| 日本道色综合久久影院| 97久久国产亚洲精品超碰热| 久久青青草原精品国产不卡| 久久无码人妻一区二区三区| 一本色道久久99一综合| 精品久久久久成人码免费动漫 | 国产农村妇女毛片精品久久| 成人久久精品一区二区三区| 伊人久久大香线蕉亚洲| 日韩欧美亚洲综合久久| 亚洲国产小视频精品久久久三级| 久久久噜噜噜久久中文字幕色伊伊| 日本一区精品久久久久影院| 秋霞久久国产精品电影院| 99国产欧美精品久久久蜜芽| 国产亚洲欧美成人久久片|