[HDU] [1051] Wooden Sticks

Problem Description

Loading...

Problem Analysis

Still a very simple problem
If the problem statement is unclear, you can check the Chinese version of this problem on NYOJ

The idea is to sort the sticks
Then, for each stick, find a later one whose weight and length are both greater than or equal to it
Record it, then search again
A “series” found this way can all be processed in one go

So we only need to record the number of times this search is started
Here a flag array is used to record sticks that have already been processed

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<stdio.h>
#include<algorithm>
using namespace std;
bool flag[10200];
struct wood {
	int l;
	int w;
} a[10200];
bool cmp(wood A,wood B) {
	if(A.l==B.l)
		return A.w<B.w;
	else
		return A.l<B.l;
}
int main() {
	int T;
	scanf("%d",&T);
	while(T--) {
		int n;
		scanf("%d",&n);
		for(int i=0; i<n; i++) {
			flag[i]=true;
			scanf("%d %d",&a[i].l,&a[i].w);
		}
		sort(a,a+n,cmp);
		int cnt=0;
		for(int i=0; i<n; i++) {
			if(flag[i]) {
				cnt++;
				int tl=a[i].l,tw=a[i].w;
				for(int j=i; j<n; j++) {
					if(flag[j]&&a[j].l>=tl&&a[j].w>=tw) {
						tl=a[j].l;
						tw=a[j].w;
						flag[j]=false;
					}
				}
			}
		}
		printf("%d\n",cnt);
	}
	return 0;
}

Problem links:
HDU Wooden Sticks
POJ Wooden Sticks
NYOJ Impatient C Xiaojia

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...