Skip to content

Instantly share code, notes, and snippets.

@amoshyc
Created July 17, 2015 06:19
Show Gist options
  • Select an option

  • Save amoshyc/da2c1b92220a069f9a9c to your computer and use it in GitHub Desktop.

Select an option

Save amoshyc/da2c1b92220a069f9a9c to your computer and use it in GitHub Desktop.
Poj 1065: Wooden Sticks

Poj 1065: Wooden Sticks

分析

真想不到啊…竟然是 LIS

題解參

另外,這題是 Dilworth's theorem 的應用。

AC Code

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <functional>
#include <queue>

using namespace std;

struct Stick {
	int l;
	int w;
	
	bool operator < (const Stick& s) const {
		if (l != s.l)
			return l < s.l;
		return w < s.w;
	}
};

int N;
Stick sticks[5000];
int dp[5000];

int solve() {
	sort(sticks, sticks + N);
	
	// O(n^2)
	// fill(dp, dp + N, 1);
	// for (int i = 0; i < N; i++) {
	// 	for (int j = 0; j < i; j++) 
	// 		if (sticks[j].w > sticks[i].w)
	// 			dp[i] = max(dp[i], dp[j] + 1);
	// }
	
	// return *max_element(dp, dp + N);
	
	// O(nlgn)
	fill(dp, dp + N, -1);
	for (int i = 0; i < N; i++) {
		*lower_bound(dp, dp + N, sticks[i].w, greater<int>()) = sticks[i].w;
	}
	
	return lower_bound(dp, dp + N, -1, greater<int>()) - dp;
}

int main() {
	int T;
	scanf("%d", &T);
	
	while (T--) {
		scanf("%d", &N);
		for (int i = 0; i < N; i++)
			scanf("%d %d", &sticks[i].l, &sticks[i].w);
		
		printf("%d\n", solve());
	}
	
	return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment