POJ1769
来源:互联网 发布:淘宝排行榜如何提高 编辑:程序博客网 时间:2024/06/05 02:55
摘要:第一个线段树,^_^
注意别排序哦,按题目要求顺序已经固定了,所以不可以贪心。
#include <iostream>
#include <cassert>
#include <stdio.h>
using namespace std;
typedef struct Node{
int left;
int right;
int minstep;
Node *left_child;
Node *right_child;
void Construct(int, int);
void Insert(int, int);
int GetRank(int, int);
}Node;
const int size = 50000;
Node stree[size*2+2];
Node *root = &stree[0];
int len = 0;
void Node::Construct(int l, int r)
{
//cout << "Construct Node:" << len-1 << endl;
left = l;
right = r;
minstep = 999999;
if( l == r ){
left_child == NULL;
right_child == NULL;
return;
}
int mid = (l + r) >> 1;
//cout << l << "/t" << r << "/t" << mid << endl;
//assert( len < 200 );
left_child = &stree[len++];
right_child = &stree[len++];
assert(left_child && right_child);
left_child->Construct(l, mid);
right_child->Construct(mid+1, r);
}
void Node::Insert(int r, int s)
{
if(right < r || left > r){
return;
}
minstep = min(minstep, s);
if(left == right){
return;
}
int mid = (left + right) >> 1;
assert(left_child && right_child);
if( r <= mid ){
left_child->Insert(r, s);
}else{
right_child->Insert(r, s);
}
}
int Node::GetRank(int l, int r)
{
if( l==left && r==right){
return minstep;
}
int mid = (left + right) >> 1;
//cout << l << "/t" << left << endl;
//cout << r << "/t" << right << endl;
assert(left_child && right_child);
if( r <= mid ){
return left_child->GetRank(l, r);
}
if( l > mid ){
return right_child->GetRank(l, r);
}
int ret1, ret2;
ret1 = left_child->GetRank(l, mid);
ret2 = right_child->GetRank(mid+1, r);
return ret1<ret2 ? ret1 : ret2;
}
int main()
{
int n;
scanf("%d", &n);
len = 1;
root->Construct(1, n);
int m;
scanf("%d", &m);
root->Insert(1, 0);
for(int i=1; i<=m; i++){
int left, right;
scanf("%d", &left);
scanf("%d", &right);
if( left < right ){
int step = root->GetRank(left, right-1);
root->Insert(right, step + 1);
}
}
int min_step = root->GetRank(n, n);
cout << min_step << endl;
return 0;
}
- POJ1769
- poj1769(线段树+dp)
- poj1769 Minimizing maximizer线段树优化dp
- #Poj1769#Minimizing maximizer(Dp+线段树优化)
- 状压DP及线段树优化DP学习 POJ2686+POJ1769
- 嵌入式实时Hypervisor:XtratuM (11)
- 《WCF技术内幕》翻译3:第1部分_第1章_蓝月亮:普遍需求和普遍概念
- 算法分析与设计
- H.264 RTP payload 格式
- ARM920T MMU and Cache ——PART2 虚拟内存管理
- POJ1769
- 一个Aho-Corasick算法的C++实现
- 第一个 android 的界面程序
- 经典的农夫养牛题目,C语言,递归实现
- 如何获取主板的相关信息 包括主板编号、版本、制造商
- ARM920T MMU and Cache ——PART3 ARM920T的CP15协处理器
- POJ1298
- ARM920T MMU and Cache ——PART4 MMU
- POJ2528