HDU 1051 并查集+贪心

Wooden Sticks

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

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

题目大意:

给你n个木棒,每个木棒长度为l、重量为w,然后用机器加工木棒,机器加工第一个木棒时间为1,若加工完第i个木棒后,再加工第j个木棒,若l[j]>=l[i]&&w[j]>=w[i]则加工第j个木棒不需要消耗时间。求加工完n条木棒所需要的最短时间。

思路:
想做动归题目,看了一下HDU题目分类,但是没想出动归转移方程,用并查集和贪心水了一下就过了。。。= = 。。

这道题很明显使得木棒构成的链最长,一条链就是前面木棒i的l和w都小于等于后面木棒j的l和w, 当链最长的时候,那么链的个数最少,于是花费的时间也最短。怎么使得链最长呢,就需要贪心了,先对l从小到大排序,再按w从小到大排序,然后当第j个木棒的l和w都大于等于第i个木棒的l和w的话就把它们并到一个集合里。最后算一下几个集合即为时间。

代码:

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <vector>
 5 #include <iostream>
 6 using namespace std;
 7 #define N 5005
 8
 9 struct node{
10     int id;
11     int l, w;
12     int flag;
13 }a[N];
14
15 int father[N];
16 int findroot(int p){
17     int r=p;
18     while(r!=father[r]) r=father[r];
19     return r;
20 }
21
22 bool cmp(node a,node b){
23     if(a.l==b.l) return a.w<b.w;
24     return a.l<b.l;
25 }
26
27 main()
28 {
29     int t;
30     int n, i, j, ans, k;
31     cin>>t;
32     while(t--){
33         scanf("%d",&n);
34         for(i=0;i<=n;i++) father[i]=i;
35         k=0;
36         for(i=0;i<n;i++) {
37             scanf("%d %d",&a[i].l,&a[i].w);
38             a[i].id=k++;
39             a[i].flag=0;
40         }
41         sort(a,a+n,cmp);
42         for(i=0;i<n;i++){
43             if(!a[i].flag){
44                 k=i;
45                 for(j=i+1;j<n;j++){
46                 if(!a[j].flag&&a[j].l>=a[k].l&&a[j].w>=a[k].w){
47                     father[a[j].id]=a[k].id;
48                     a[j].flag=1;
49                     k=j;
50                 }
51               }
52             }
53
54         }
55         ans=0;
56         for(i=0;i<n;i++) {
57             if(findroot(a[i].id)==a[i].id)
58             ans++;
59         }
60         printf("%d\n",ans);
61     }
62 }

HDU 1051 并查集+贪心,布布扣,bubuko.com

时间: 2024-12-25 01:50:29

HDU 1051 并查集+贪心的相关文章

HDU 1512 并查集+左偏树

Monkey King Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3105    Accepted Submission(s): 1330 Problem Description Once in a forest, there lived N aggressive monkeys. At the beginning, they e

hdu 1829 并查集(食物链的弱化版)

http://acm.hdu.edu.cn/showproblem.php?pid=1829 Problem Description Background  Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of

hdu 4514 并查集+树形dp

湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 4539    Accepted Submission(s): 816 Problem Description 随着杭州西湖的知名度的进一步提升,园林规划专家湫湫希望设计出一条新的经典观光线路,根据老板马小腾的指示,新的风景线最好能建成环形,如果没有条件建成环形,

hdu 1856 并查集

http://acm.hdu.edu.cn/showproblem.php?pid=1856 More is better Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others)Total Submission(s): 13672    Accepted Submission(s): 5008 Problem Description Mr Wang wants some boys

HDU 1181 并查集 or SPFA

变形课 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 12773    Accepted Submission(s): 4733 Problem Description 呃......变形课上Harry碰到了一点小麻烦,因为他并不像Hermione那样能够记住所有的咒语而随意的将一个棒球变成刺猬什么的,但是他发现了变形咒语的一个统一规

hdu 1213 并查集入门

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12538    Accepted Submission(s): 6145 Problem Description Today is Ignatius' b

zoj 3659 Conquer a New Region 并查集+贪心

点击打开链接题目链接 Conquer a New Region Time Limit: 5 Seconds      Memory Limit: 32768 KB The wheel of the history rolling forward, our king conquered a new region in a distant continent. There are N towns (numbered from 1 to N) in this region connected by s

HDU 3938 并查集

求小于L的路径点对数(路上的最大值),按权值排序,从小到大并查集建图,有点kruskal的意思. /** @Date : 2017-09-22 17:30:11 * @FileName: HDU 3938 并查集 离线.cpp * @Platform: Windows * @Author : Lweleth ([email protected]) * @Link : https://github.com/ * @Version : $Id$ */ #include <bits/stdc++.h>

HDU 1829 并查集

A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8224    Accepted Submission(s): 2631 Problem Description Background Professor Hopper is researching the sexual behavior of a rare sp