01065: Wooden Sticks
greedy, binary search http://cs101.openjudge.cn/practice/01065/
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 ( 9 , 4 ) , ( 2 , 5 ) , ( 1 , 2 ) , ( 5 , 3 ) , and ( 4 , 1 ) , then the minimum setup time should be 2 minutes since there is a sequence of pairs ( 4 , 1 ) , ( 5 , 3 ) , ( 9 , 4 ) , ( 1 , 2 ) , ( 2 , 5 ) .
输入
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 2n 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.
输出
The output should contain the minimum setup time in minutes, one per line.
样例输入
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样例输出
2
1
3来源: Taejon 2001
def min_setup_time(sticks):
n = len(sticks)
check = [0]*n
setup_time = 0
while (0 in check):
#print(check)
#print(sticks)
i = 0
for j in range(n):
if check[j] == 0:
i = j
break
current = sticks[i]
check[i] = 1
setup_time += 1
i += 1
while i < n:
if check[i]==0 and current[0]<=sticks[i][0] and current[1]<= sticks[i][1]:
check[i] = 1
current = sticks[i]
i +=1
return setup_time
T = int(input())
for _ in range(T):
n = int(input())
data = list(map(int, input().split()))
sticks = [(data[i], data[i + 1]) for i in range(0, 2 * n, 2)]
sticks.sort()
print(min_setup_time(sticks))#dilworth和最长单调子序列
# 答案就是对l排序后求w的最长严格递减子序列(用Dilworth's theorem不难证明),
# 最长严格递减子序列有经典的nlogn的算法
# (https://en.wikipedia.org/wiki/Longest_increasing_subsequence)。
#一般有一样的都不是大问题,因为可以把(3,5) (3,6) 直接看作(3.1, 5) (3.2, 6)
import bisect
def doit():
n = int(input())
data = list(map(int, input().split()))
sticks = [(data[i], data[i + 1]) for i in range(0, 2 * n, 2)]
sticks.sort()
f = [sticks[i][1] for i in range(n)]
f.reverse()
stk = []
for i in range(n):
t = bisect.bisect_left(stk, f[i])
if t == len(stk):
stk.append(f[i])
else:
stk[t] = f[i]
print(len(stk))
T = int(input())
for _ in range(T):
doit()【陈子良 25物理学院】这题让我想到了T25353排队,都是每次从列表中选出满足一定条件的最多的元素。首先将木棍按l排序加入队列中,每次遍历一遍队列元素,如果某一元素的w大于当前值,就能在此次加工,否则重新加入队列中。
from collections import deque
for _ in range(int(input())):
n=int(input())
iter1=iter(map(int,input().split()))
sticks=[]
for _ in range(n):
sticks.append((next(iter1),next(iter1)))
sticks.sort()
queue=deque(sticks)
s=0
while queue:
m=-float('inf')
for _ in range(len(queue)):
a,b=queue.popleft()
if b>=m:
m=b
else:
queue.append((a,b))
s+=1
print(s)