国产激情自拍_国产9色视频_丁香花在线电影小说观看 _久久久久国产精品嫩草影院

首頁 > 編程 > C > 正文

貪心算法 WOODEN STICKS 實例代碼

2020-01-26 16:08:13
字體:
來源:轉載
供稿:網友

Problem Description
There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The sticks are to be processed by a woodworking machine in one by one fashion. It needs some time, called setup time, for the machine to prepare processing a stick. The setup times are associated with cleaning operations and changing tools and shapes in the machine. The setup times of the woodworking machine are given as follows:
(a) The setup time for the first wooden stick is 1 minute. (b) Right after processing a stick of length l and weight w , the machine will need no setup time for a stick of length l' and weight w' if l<=l' and w<=w'. Otherwise, it will need 1 minute for setup.
You are to find the minimum setup time to process a given pile of n wooden sticks. For example, if you have five sticks whose pairs of length and weight are (4,9), (5,2), (2,1), (3,5), and (1,4), then the minimum setup time should be 2 minutes since there is a sequence of pairs (1,4), (3,5), (4,9), (2,1), (5,2).

Input
The input consists of T test cases. The number of test cases (T) is given in the first line of the input file. Each test case consists of two lines: The first line has an integer n , 1<=n<=5000, that represents the number of wooden sticks in the test case, and the second line contains n 2 positive integers l1, w1, l2, w2, ..., ln, wn, each of magnitude at most 10000 , where li and wi are the length and weight of the i th wooden stick, respectively. The 2n integers are delimited by one or more spaces.

Output
The output should contain the minimum setup time in minutes, one per line.

Sample Input
3 5 4 9 5 2 2 1 3 5 1 4 3 2 2 1 1 2 2 3 1 3 2 2 3 1

Sample Output
2 1 3

復制代碼 代碼如下:

#include<stdio.h>
 #include<stdlib.h>
 #include<string.h>
 #define N 5000;

 struct node
 {
     int l;
     int w;
     int flag;
 }sticks[5000];
 int cmp(const void *p,const void *q)
 {
     struct node *a = (struct node *)p;
     struct node *b = (struct node *)q;
     if(a->l > b->l) return 1;
     else if(a->l < b->l) return -1;
     else return a->w > b->w ? 1 : -1;
 }
 int main()
 {
     int t,n,cnt,cl,cw;
     int i,j;
     scanf("%d",&t);
     while(t--)
     {
         memset(sticks,0,sizeof(sticks));
         scanf("%d",&n);
         for( i = 0; i < n; i++)
             scanf("%d %d",&sticks[i].l,&sticks[i].w);
         qsort(sticks,n,sizeof(sticks[0]),cmp);
         sticks[0].flag = 1;
         cl = sticks[0].l;
         cw = sticks[0].w;
         cnt = 1;
         for( j = 1; j < n; j++)
         {
             for( i = j; i < n; i++)
             {
                 if(!sticks[i].flag&&sticks[i].l>=cl&&sticks[i].w>=cw)
                 {
                     cl = sticks[i].l;
                     cw = sticks[i].w;
                     sticks[i].flag = 1;
                 }
             }
             i = 1;
             while(sticks[i].flag)
                i++;
             j = i;
             if(j == n) break;
             cl = sticks[j].l;
             cw = sticks[j].w;
             sticks[j].flag = 1;
             cnt++;
         }
         printf("%d/n",cnt);

     }
     return 0;
 }

題意:

我們要處理一些木棍,第一根的時間是1分鐘,另外的,在長度為l,重為w的木棍后面的那根的長度為l', 重量w',只要l <=l' 并且w <= w',就不需要時間,否則需要1分鐘,求如何安排處理木棍的順序,才能使花的時間最少。

思路:

貪心算法。先把這些木棍按長度和重量都從小到大的順序排列。cl和cw是第一根的長度和重量,依次比較后面的是不是比當前的cl,cw大,是的話把標志flag設為1,并跟新cl,cw。比較完后,再從前往后掃描,找到第一個標志位為0的,作為是第二批的最小的一根,計數器加一。把它的長度和重量作為當前的cl,cw,再循環往后比較。直到所有的都處理了。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

国产激情自拍_国产9色视频_丁香花在线电影小说观看 _久久久久国产精品嫩草影院
亚洲第一区视频| 综合激情亚洲| 九九热视频在线| www.99色.com| 青青草原国产在线| 天天爱天天做色综合| 精品视频vs精品视频| 亚洲精品一区中文字幕电影| 免费一区二区在线观看| 国产精品一二三区视频| 国产无遮挡又黄又爽免费网站| 国产精品四虎| 欧美日韩视频精品一区二区| 国产精品美女一区二区三区四区 | 午夜在线观看91| 欧美日韩国产亚洲沙发| 国产特级毛片| 人人干人人插| av在线播放国产| 国产福利av网站| 国产精品一区在线看| 天天操夜夜添| 国产激情在线视频| 久久国产情侣| 在线久久视频| 精品无人乱码| 国产黄大片在线观看画质优化| 丁香视频免费观看| a视频在线看| 91激情在线| 国产精品自拍亚洲| 青青草观看免费视频在线| 在线免费观看你懂的| 国产精品18久久久久久久久久| 成人超碰在线| 精品资源在线看| 久青青在线观看视频国产| yjizz视频网站在线播放| 亚洲图区综合| av片在线观看永久免费| 国产h在线观看| 国产一二三四| 国产黄色高清在线| 国产经典av| 在线中文字幕av| 精品欧美不卡一区二区在线观看| 国产裸舞福利在线视频合集| 国产伦精品一区二区三区高清版禁| 欧美亚洲系列| 黄网站app在线观看下载视频大全官网| 一区免费观看| 欧美a免费在线| 国产中文字幕在线视频| 国产理论电影在线| 国产在线中文字幕| 午夜国产视频| 国产videos| 久久久久久国产视频| 国产69精品久久久久孕妇国产69久久 | 99福利在线| 在线伊人免费视频| 欧美成人久久电影香蕉| 国产乱视频在线观看| 国产第一页在线视频 | 国产成人综合亚洲欧美在| 国产国产人免费人成免费视频| 亚洲最新永久在线观看| 国产在线超碰| 亚洲sss视频| 中文字幕在线免费| 99reav| 欧美精品日韩少妇| 国产高清在线观看| 久久国产综合视频| 97国产在线| 午夜av电影| 九九久久久2| xxx国产精品| 在线一区二区三区精品| 最近中文字幕mv免费高清在线| 在线成人综合色一区| 国产精品你懂的在线观看| 玖玖在线视频| 在线色视频观看| 国产娇喘精品一区二区三区图片| 日本欧美在线视频免费观看| 国产精选在线观看| 九七电影韩国女主播在线观看| 91涩漫在线观看c| 国产黄色免费| 国产一级免费黄色片| 久久一本精品| 久久综合精品视频| 永久免费网站在线| 99综合精品久久| 在线午夜影院| 精品免费视频一卡2卡三卡4卡不卡 | 国产日本韩国在线播放| 激情综合网五月激情| jizz在线视频| 人人干人人插| 国内自拍视频在线看免费观看| 欧美性受xxxx免费视频| 久久五月精品| 尤物视频网站在线观看| 国产美女视频一区二区三区| 国产麻豆精品视频一区二区 | 超碰免费在线| www.操操操.com| 亚洲一本大道| 超碰在线国产| 国产日韩精品在线看| 国产超碰97| 午夜影院免费看| 好男人免费精品视频| 国产三级av在线| 亚洲免费国产| 国产免费电影网站入口| 国产精品18久久久久网站| av在线电影观看| 国产免费视频| 精品国产二区三区| 天堂网中文在线| 五月婷婷丁香激情| 91涩漫在线观看c| 狠狠综合久久久综合| 国产午夜电影| 欧美成人久久电影香蕉| 国产精品乱码一区二区三区视频| av人人综合网| 国产丝袜在线| 非洲黑人最猛性xxxx交| av大片在线播放| 国产亚洲精品久久久网站好莱| 在线免费观看污| 中文字幕第一页在线| 日本一级理论片在线大全| 国产黄色免费在线观看| 天堂在线免费视频| 国产福利在线观看| 国产卡一卡二卡三| 国产在线观看18| 毛片网站在线观看| 在线播放一区二区精品产| 国产福利在线视频| 国产精品欧美韩国日本久久| 日本中文字幕视频| 国产在线高潮| 午夜在线小视频| 国产在线高清| 久久五月精品| 二人午夜免费观看在线视频| 超碰免费在线观看| 超碰免费97在线观看| 亚洲视频在线观看不卡| 99在线欧洲视频| 四虎久久影院| 啪啪免费视频一区| 国产综合视频一区二区三区免费| 九九热在线播放| 国产免费电影网站入口| 天天操天天艹| 国产视频福利在线| 2019中文字幕视频| 国产xxxxx| 天天噜天天色| 狠狠干天天爱| 国产高清视频在线播放| 亚洲免费国产| 日韩亚洲一区中文字幕| 国产中文字幕在线看| 国产精品久久久久久精| 亚洲综合激情六月婷婷在线观看| 亚洲第一区视频| 大香伊人久久| 精品推荐国产麻豆剧传媒| 天堂中文在线观看| 中文字幕视频在线| 在线视频观看你懂的| 久久精品最新免费国产成人| 国产一级在线观看www色| 国产精品合集一区二区| 四虎成人免费| 国产午夜电影| 天天操中文字幕视频| 福利资源在线久| 91国内精品久久久久| 免费中文字幕| 中文资源在线网| 国产一级免费看| 免费国产阿v视频在线观看| 国产精彩视频在线观看免费蜜芽| 天天插天天色| 在线中文资源天堂| 午夜伦全在线观看| 激情综合网五月激情| 亚洲字幕成人中文在线观看| 国产视频中文字幕在线观看| 欧美日韩视频精品二区| 国产99re|