杭電acm Wooden Sticks

Wooden Sticks

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23191 Accepted Submission(s): 9411

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

Solution

//貪心問(wèn)題济锄,先處理最長(zhǎng)最重的木頭以便一臺(tái)機(jī)器可以處理盡可能多的木頭,

Code

/**
 * date:2017.11.15
 * author:孟小德
 * function:杭電acm1051
 *
 */
 
 
 
import java.util.*;
 
public class acm1051
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
 
        int num = input.nextInt();
        int[] result = new int[num];
        for (int j=0;j<num;j++)
        {
            int wood_num = input.nextInt();
            wood[] array = new wood[wood_num];
            for (int i=0;i<wood_num;i++)
            {
                int len = input.nextInt();
                int weight = input.nextInt();
                array[i] = new wood(len,weight);
            }
 
            sort(array);
            int use_num = 0;
            result[j] = 0;
            wood temp = new wood();
            while (use_num < wood_num)
            {
                for (int i=0;i<wood_num;i++)
                {   //尋找未處理木頭中最長(zhǎng)最重的一根
                    if(array[i].use == false)
                    {
                        temp = array[i];
 
                        break;
                    }
                }
 
                for (int i=0;i<wood_num;i++)
                {// 尋找可以被同時(shí)處理的木頭
                    if (array[i].use == false && array[i].len <= temp.len &&
                    array[i].weight <= temp.weight)
                    {
                        array[i].use = true;
                        use_num++;
                        temp = array[i];
                    }
                }
 
                result[j]++;
            }
 
        }
 
        for (int i=0;i<result.length;i++)
        {
            System.out.println(result[i]);
 
        }
    }
 
    //按長(zhǎng)度排序揍堰,長(zhǎng)度相同則按重量排序
    public static void sort(wood[] array)
    {
        boolean flag = true;
        wood temp = new wood();
        for (int i = 0;i<array.length && flag;i++)
        {
            flag = false;
            for (int j = 0;j<array.length-i-1;j++)
            {
                if (array[j].len < array[j+1].len)
                {
                    temp.len = array[j].len;
                    temp.weight = array[j].weight;
 
                    array[j].len = array[j+1].len;
                    array[j].weight = array[j+1].weight;
 
                    array[j+1].len = temp.len;
                    array[j+1].weight = temp.weight;
                    flag = true;
                }
                else if (array[j].len == array[j+1].len)
                {
                    if (array[j].weight < array[j+1].weight)
                    {
                        temp.len = array[j].len;
                        temp.weight = array[j].weight;
 
                        array[j].len = array[j+1].len;
                        array[j].weight = array[j+1].weight;
 
                        array[j+1].len = temp.len;
                        array[j+1].weight = temp.weight;
                        flag = true;
                    }
                }
            }
        }
    }
 
}
 
class wood
{
    public int len;
    public int weight;
    public boolean use = false;
 
    public wood()
    {
 
    }
 
    public wood(int len,int weight)
    {
        this.len = len;
        this.weight = weight;
    }
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市六水,隨后出現(xiàn)的幾起案子空另,更是在濱河造成了極大的恐慌,老刑警劉巖智蝠,帶你破解...
    沈念sama閱讀 222,104評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件咕娄,死亡現(xiàn)場(chǎng)離奇詭異亥揖,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)谭胚,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門徐块,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人灾而,你說(shuō)我怎么就攤上這事胡控。” “怎么了旁趟?”我有些...
    開封第一講書人閱讀 168,697評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵昼激,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我锡搜,道長(zhǎng)橙困,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,836評(píng)論 1 298
  • 正文 為了忘掉前任耕餐,我火速辦了婚禮凡傅,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘肠缔。我一直安慰自己夏跷,他們只是感情好哼转,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,851評(píng)論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著槽华,像睡著了一般壹蔓。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上猫态,一...
    開封第一講書人閱讀 52,441評(píng)論 1 310
  • 那天佣蓉,我揣著相機(jī)與錄音,去河邊找鬼亲雪。 笑死勇凭,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的义辕。 我是一名探鬼主播套像,決...
    沈念sama閱讀 40,992評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼终息!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起贞让,我...
    開封第一講書人閱讀 39,899評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤周崭,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后喳张,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體续镇,經(jīng)...
    沈念sama閱讀 46,457評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,529評(píng)論 3 341
  • 正文 我和宋清朗相戀三年销部,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了摸航。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,664評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡舅桩,死狀恐怖酱虎,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情擂涛,我是刑警寧澤读串,帶...
    沈念sama閱讀 36,346評(píng)論 5 350
  • 正文 年R本政府宣布,位于F島的核電站撒妈,受9級(jí)特大地震影響恢暖,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜狰右,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,025評(píng)論 3 334
  • 文/蒙蒙 一杰捂、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧棋蚌,春花似錦嫁佳、人聲如沸挨队。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,511評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)瞒瘸。三九已至,卻和暖如春熄浓,著一層夾襖步出監(jiān)牢的瞬間情臭,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,611評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工赌蔑, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留俯在,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,081評(píng)論 3 377
  • 正文 我出身青樓娃惯,卻偏偏與公主長(zhǎng)得像跷乐,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子趾浅,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,675評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容