hdu4302(优先级队列)

Holedox Eating

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2704    Accepted Submission(s): 898


Problem Description
Holedox is a small animal which can be considered as one point. It lives in a straight pipe whose length is L. Holedox can only move along the pipe. Cakes may appear anywhere in the pipe, from time to time. When Holedox wants to eat cakes, it always goes to the nearest one and eats it. If there are many pieces of cake in different directions Holedox can choose, Holedox will choose one in the direction which is the direction of its last movement. If there are no cakes present, Holedox just stays where it is.
 

Input
The input consists of several test cases. The first line of the input contains a single integer T (1 <= T <= 10), the number of test cases, followed by the input data for each test case.The first line of each case contains two integers L,n(1<=L,n<=100000), representing the length of the pipe, and the number of events.
The next n lines, each line describes an event. 0 x(0<=x<=L, x is a integer) represents a piece of cake appears in the x position; 1 represent Holedox wants to eat a cake.
In each case, Holedox always starts off at the position 0.
 

Output
Output the total distance Holedox will move. Holedox don’t need to return to the position 0.
 

Sample Input
  
  
3 10 8 0 1 0 5 1 0 2 0 0 1 1 1 10 7 0 1 0 5 1 0 2 0 0 1 1 10 8 0 1 0 1 0 5 1 0 2 0 0 1 1
 

Sample Output
  
  
Case 1: 9 Case 2: 4 Case 3: 2
 
每次保证到达距离当前最近的位置,若两边相同则选择方向不变的那个方向
由于数据较多,不可以直接选择,那样时间复杂度较高O(N*N),可以用优先级队列降低时间复杂度,当前位置左边的用从大到小排列,右边的从小到大排列,每次移动前比较两个方向的移动距离,左边的肯定与最大的比较,左边的肯定与最小的就比较,于是有了优先级队列的思想,左边大顶堆,右边的小顶堆,总的时间复杂度为O(n*log),应该可以接受。
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstdio>
#include<cmath>
using namespace std;

struct cmp
{
	bool operator()(int a,int b)
	{
		return a>b;
	}
};

int main()
{
	int i,cas,len,n,cmd,pos,dis,cur,der,tag=1;
	int tmp1;
	int tmp2;
	cin>>cas;
	while(cas--)
	{
		dis=0;
		cur=0;
		der=1;
		priority_queue<int>bef;
		priority_queue<int,vector<int>,cmp>aft;
		scanf("%d%d",&len,&n);
		for(i=0;i<n;i++)
		{
			scanf("%d",&cmd);
			if(!cmd)
			{
				scanf("%d",&pos);
				if(pos<cur)
					bef.push(pos);
				else 
				{
					aft.push(pos);
				}
			}
			else 
			{
				if(!aft.empty()&&!bef.empty())
				{
					tmp1=bef.top();
			    	tmp2=aft.top();
					if(abs(cur-tmp1)<abs(tmp2-cur))
					{
						der=-1;
						dis+=abs(cur-tmp1);
						cur=tmp1;
						bef.pop();
					}
					else if(abs(cur-tmp1)>abs(tmp2-cur))
					{
						der=1;
						dis+=abs(tmp2-cur);
						cur=tmp2;
						aft.pop();
					}
					else 
					{
						dis+=abs(cur-tmp1);
						if(der==-1)
						{
							cur=tmp1;
						    bef.pop();
						}
						else
						{
							cur=tmp2;
					    	aft.pop();
						}
					}
				}
				else
				{
					if(!bef.empty())
					{
						tmp1=bef.top();
						der=-1;
						dis+=abs(cur-tmp1);
						cur=tmp1;
						bef.pop();
					}
					else if(!aft.empty())
					{
						tmp1=aft.top();
						der=1;
						dis+=abs(tmp1-cur);
						cur=tmp1;
						aft.pop();
					}
					else
					{
						continue;
					}
				}
			}
		}
		printf("Case %d: %d\n",tag++,dis);
	}
	return 0;
}

 
对于HDU4546问题,还可以使用优先队列(Priority Queue)来解决。以下是使用优先队列的解法思路: 1. 首先,将数组a进行排序,以便后续处理。 2. 创建一个优先队列(最小堆),用于存储组合之和的候选值。 3. 初始化优先队列,将初始情况(即前0个数的组合之和)加入队列。 4. 开始从1到n遍历数组a的元素,对于每个元素a[i],将当前队列中的所有候选值取出,分别加上a[i],然后再将加和的结果作为新的候选值加入队列。 5. 重复步骤4直到遍历完所有元素。 6. 当队列的大小超过k时,将队列中的最小值弹出。 7. 最后,队列中的所有候选值之和即为前k小的组合之和。 以下是使用优先队列解决HDU4546问题的代码示例: ```cpp #include <iostream> #include <vector> #include <queue> #include <functional> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a.begin(), a.end()); // 对数组a进行排序 priority_queue<long long, vector<long long>, greater<long long>> pq; // 最小堆 pq.push(0); // 初始情况,前0个数的组合之和为0 for (int i = 0; i < n; i++) { long long num = pq.top(); // 取出当前队列中的最小值 pq.pop(); for (int j = i + 1; j <= n; j++) { pq.push(num + a[i]); // 将所有加和结果作为新的候选值加入队列 num += a[i]; } if (pq.size() > k) { pq.pop(); // 当队列大小超过k时,弹出最小值 } } long long sum = 0; while (!pq.empty()) { sum += pq.top(); // 求队列中所有候选值之和 pq.pop(); } cout << sum << endl; return 0; } ``` 使用优先队列的方法可以有效地找到前k小的组合之和,时间复杂度为O(nklog(k))。希望这个解法对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值