poj 3636 Nested Dolls 动态更新表的二分查找

题意:

给n个玩具,每个有属性w,h。如果w1<w2且h1<h2那么玩具1可放在玩具2里,问最后最少能有几个玩具。

分析:

w升序,w相同时h降序排序后是可以贪心的,这里使用了动态维护表的二分算法,表里动态维护了每堆玩具中h的最大值(所以w相同时h要降序)。这题我一开始一看是个拓扑图还想着用什么图算法。。没想到直接可以贪心,不可以有思维定式啊~~

代码:

//poj 3636
//sep9
#include <iostream>
#include <algorithm>
using namespace std;
const int maxN=20012;
struct P{
	int x,y;
}p[maxN];
int H[maxN];
int cmp(P a,P b)
{
	return a.x==b.x?a.y>b.y:a.x<b.x;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--){
		int i,n,sum=0;
		scanf("%d",&n);
		for(i=0;i<n;++i)
			scanf("%d%d",&p[i].x,&p[i].y);
		sort(p,p+n,cmp);
		int len=0;
		for(i=0;i<n;++i){
			int cur=p[i].y;
			int left=0;
			int right=len;
			while(left<right){
				int mid=(left+right)/2;
				if(H[mid]>=cur)
					left=mid+1;
				else
					right=mid;
			}
			if(len==left)
				++len;
			H[left]=cur;
		}
		printf("%d\n",len);
	}
	return 0;
} 
时间: 2025-01-16 19:28:44

poj 3636 Nested Dolls 动态更新表的二分查找的相关文章

跳表与二分查找

跳表对数据结构中的数据常见的操作有:查找.插入.删除.有序数组的二分查找操作速度很快,但是插入.删除操作很耗时,并且对内存要求很苛刻.那么有什么数据结构能够做到查找.插入.删除操作速度都很快而且对内存要求不高呢?——答案是:跳表. 跳表是什么?即:把有序链表改造位支持“二分查找”算法,这种链表叫做跳表. 跳表的二分查找算法其实是一种“类似二分查找算法”. 跳表是一个各方面性能都比较优秀的“动态数据结构”.可以进行快速的插入.删除.查找操作.代码实现也不复杂.甚至能替代“红黑树”.时间复杂度:O(

Nested Dolls (单调递增子序列 + 二分)

Description Dilworth is the world's most prominent collector of Russian nested dolls: he literally has thousands of them! You know, the wooden hollow dolls of different sizes of which the smallest doll is contained in the second smallest, and this do

POJ 2455-Secret Milking Machine(网络流_最大流+二分查找)

Secret Milking Machine Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10119   Accepted: 2973 Description Farmer John is constructing a new milking machine and wishes to keep it secret as long as possible. He has hidden in it deep within

LintCode-排序列表转换为二分查找树

给出一个所有元素以升序排序的单链表,将它转换成一棵高度平衡的二分查找树 您在真实的面试中是否遇到过这个题? Yes 样例 标签 Expand 相关题目 Expand 分析:就是一个简单的递归,只是需要有些链表的操作而已 代码: /** * Definition of ListNode * class ListNode { * public: * int val; * ListNode *next; * ListNode(int val) { * this->val = val; * this->

POJ 2112-Optimal Milking(网络流_最大流+floyd+二分查找)

Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 12810   Accepted: 4632 Case Time Limit: 1000MS Description FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures among the C (1 <= C <= 200) co

I Count Two Three(打表+排序+二分查找)

I Count Two Three 二分查找用lower_bound 这道题用cin,cout会超时... AC代码: 1 /* */ 2 # include <iostream> 3 # include <cstring> 4 # include <string> 5 # include <cstdio> 6 # include <cmath> 7 # include <algorithm> 8 using namespace st

postgresql 存储过程动态更新数据

-- 目标:动态更新表中数据 -- 老规矩上代码-----------------------------tablename 表名--feildname 字段名数组--feildvalue 字段值数组--returnvalue 返回值 create or replace function f_update ( tablename text, condition text, feildname text[], feildvalue text[], out returnvalue text ) as

静态查找表:顺序查找、折半查找、分块查找

引言: 除去各种线性和非线性的数据结构外.另一种在实际应用中大量使用的数据结构--查找表.查找表是由同一类型的数据元素构成的集合. 对查找表常常进行的操作有:1.查找某个"特定的"数据元素是否在查找表中:2.检索某个"特定的"数据元素的各种属性:3.在查找表中插入一个数据元素:4.从查找表中删去某个数据元素.对查找表仅仅作前两种统称为"查找"的操作,则称此类查找表为静态查找表. 若在查找过程中同一时候插入查找表中不存在的数据元素,或者从查找表中删

UVA 11368 &amp; POJ 3636 &amp; HDU 1677 Nested Dolls(贪心 + 二分LIS)

A - Nested Dolls Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Dilworth is the world's most prominent collector of Russian nested dolls: he literally has thousands of them! You know, the wooden hollow dolls