HDU 3607 Traversal (线段树)

本文介绍了一道关于寻找最优路径以获取最多金币的算法题目。玩家需从湖的一侧通过不同高度的盒子跳跃到另一侧,目标是在遵守游戏规则的同时收集最多的金币。文章详细解析了使用线段树来优化搜索过程的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


Traversal

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 899    Accepted Submission(s): 320


Problem Description
Xiao Ming is travelling, and today he came to the West Lake, to see people playing a game, this game is like this, lake placed n-box, from 1 to n label. These boxes are floating in the water, there are some gold inside each box. Then participants from coast to jump the other side, each box have its’s height, you can only jump from lower height to higher height, and from a small label to a big label, you can skip some of the middle of the box. Suppose the minimum height is this side, the other side has the maximum height. Xiao Ming would like to jump how to get the most gold? He now needs your help.
 

Input
There are multiple test cases. Each test case contains one integer N , representing the number of boxes . The following N lines each line contains two integers hi and gi , indicate the height and the number of gold of the ith box.
1 < = N < 100 001
0 < hi < 100 000 001
0 < gi < = 10000
 

Output
For each test case you should output a single line, containing the number of maximum gold XiaoMing can get.

 

Sample Input

  
4 1 1 2 2 2 3 5 1 1 1 10000
 

Sample Output

  
5 10000
 

Source
 

Recommend
lcy
 

解题思路:类似我博客的HDU2227,每进来一个数,算出它的前面的数的最大的值(用线段树),然后加上它本身,就是它的最大值,插入线段树,然后循环

 

 

#include <iostream>
#include <cstdio>
#include <map>
#include <algorithm>
using namespace std;

const int maxn=100010;
struct point{
	int h,w;
}data[maxn];

struct node{
	int l,r,c,maxc;	
}a[maxn*4];

int n;
map <int,int> mp;

void build(int l,int r,int k){
	a[k].l=l;a[k].r=r;a[k].c=0,a[k].maxc=0;
	if(l<r){
		int mid=(l+r)/2;
		build(l,mid,2*k);
		build(mid+1,r,2*k+1);
	}
}

void insert(int l,int r,int c,int k){
	if(l<=a[k].l && a[k].r<=r){
		a[k].c=max(c,a[k].c);
		a[k].maxc=a[k].c;
	}
	else{
		int mid=(a[k].l+a[k].r)/2;
		if(l>=mid+1) insert(l,r,c,2*k+1);
		else if(r<=mid) insert(l,r,c,2*k);
		else{
			insert(l,mid,c,2*k);
			insert(mid+1,r,c,2*k+1);
		}
		a[k].maxc=max(a[2*k].maxc,a[2*k+1].maxc);
	}
}

//query maxc in l,r
int query(int l,int r,int k){
	if(l<=a[k].l && a[k].r<=r){
		return a[k].maxc;
	}else{
		int mid=(a[k].l+a[k].r)/2;
		if(l>=mid+1) return query(l,r,2*k+1);
		else if(r<=mid) return query(l,r,2*k);
		else{
			 return max(query(l,mid,2*k),query(mid+1,r,2*k+1));
		}
	}
}

void initial(){
	mp.clear();
	build(0,n,1);
}

void input(){
	int cnt=1;
	map <int,int>::iterator it;
	for(int i=1;i<=n;i++){
		scanf("%d%d",&data[i].h,&data[i].w);
		mp[data[i].h]=0;
	}
	for(it=mp.begin();it!=mp.end();it++) it->second=cnt++;
}

void computing(){
	int ans=0,tmp,pos;
	for(int i=1;i<=n;i++){
		pos=mp[data[i].h];
		tmp=query(0,pos-1,1)+data[i].w;
		if(ans<tmp) ans=tmp;
		insert(pos,pos,tmp,1);
	}
	cout<<ans<<endl;
}

int main(){
	while(scanf("%d",&n)!=EOF){
		initial();
		input();
		computing();
	}
	return 0;
}


 

转载于:https://www.cnblogs.com/toyking/p/3797419.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值