线段树,堆,multiset:Holedox Eating

探讨Holedox在一个管道中寻找蛋糕并最小化移动距离的问题。通过三种方法——线段树、堆和multiset实现算法,每种方法各有特点。

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

Holedox Eating

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


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


这道题神奇得可以用三种方法,线段树容易想到但难敲,堆相对容易,multiset这种奇淫巧计竟然也可以过:


线段树:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<cmath>
#include<stack>
#include<algorithm>

using namespace std;

const int N=100005;
struct node
{
    int l,r;
    int sum;
}mem[N*3];

int num[N];//记录某个位置的食物数量
int place,suml,sumr;//小动物的位置 和它左边 右边的食物数量
int to;//表示方向 1 向右 0 向左
int ans;//保存答案
int L,R;//搜索左边和右边最靠近小动物的食物位置
int Search(int,int );
void insert(int ,int ,int );
void Right()//选择右边的食物 进行的一些必要更新
{
    to=1;
    sumr-=num[R];
    insert(1,R,-num[R]);
    --num[R];
    ans=ans+R-place;
    place=R;
}
void Left()//选择左边的食物 进行的一些必要更新
{
    to=0;
    suml-=num[L];
    insert(1,L,-num[L]);
    --num[L];
    ans=ans+place-L;
    place=L;
}
void build(int x,int i,int j)//建树
{
    mem[x].l=i;
    mem[x].r=j;
    mem[x].sum=0;
    if(i==j)
    return ;
    int mid=(i+j)>>1;
    build(x*2,i,mid);
    build(x*2+1,mid+1,j);
}
void insert(int x,int p,int k)//在p这个位置 插入k个食物 k可以为负 用来减少操作
{
    int mid=(mem[x].l+mem[x].r)>>1;
    if(mem[x].l==mem[x].r)
    {
        mem[x].sum+=k;
        return ;
    }
    if(p<=mid)
    insert(x*2,p,k);
    else
    insert(x*2+1,p,k);
    mem[x].sum=mem[x*2].sum+mem[x*2+1].sum;
}
int Search(int x,int d)//搜索第d个食物的位置
{
    if(mem[x].l==mem[x].r)
    return mem[x].r;
    if(mem[x*2].sum>=d)
    return Search(x*2,d);
    else
    return Search(x*2+1,d-mem[x*2].sum);
}
int main()
{
   int T;
   scanf("%d",&T);
   for(int w=1;w<=T;++w)
   {
       int n,m;
       place=0,suml=0,sumr=0;
       to=1;
       ans=0;
       scanf("%d %d",&n,&m);
       build(1,0,n);
       memset(num,0,sizeof(num));
       while(m--)
       {
           int k,x;
           scanf("%d",&k);
           if(k==0)
           {
               scanf("%d",&x);
               ++num[x];
               if(x!=place)//插入位置 不是在小动物位置才更新线段树
               insert(1,x,1);
               if(x<place)
               ++suml;
               else if(x>place)
               ++sumr;
           }else
           {
               if(num[place]>0)//在小动物位置 直接减少 不需其他操作
               {
                   --num[place];
                   continue;
               }
               if(suml==0&&sumr==0)//没有食物
               continue;
               if(sumr==0)//右边没食物 选左边的
               {
                   L=Search(1,suml);
                   Left();
               }else
               if(suml==0)//选右边的
               {
                   R=Search(1,1);
                   Right();
               }else
               if(suml>0&&sumr>0)
               {
                   L=Search(1,suml);//求的左边最近食物位置
                   R=Search(1,suml+1);//求的右边最近食物位置
                   if(place-L<R-place||(place-L==R-place&&to==0))
                   Left();
                   else
                   Right();
               }
           }
       }
       printf("Case %d: %d\n",w,ans);
   }
   return 0;
}

堆:

#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
priority_queue<int,vector<int>,greater<int> > minQue;
priority_queue<int> maxQue;
int main(){
	long long res;
	int k,l,n,left,targ,val,right,curr,dir;
	while(scanf("%d",&k)!=EOF){
		for(int i=1;i<=k;i++){
			while(!minQue.empty()) minQue.pop();
			while(!maxQue.empty()) maxQue.pop();
			res=0;
			curr=0;
			dir=1;
			scanf("%d%d",&l,&n);
			while(n--){
				scanf("%d",&targ);
				if(targ){
					if(minQue.empty()){
						if(!maxQue.empty()){
							left=maxQue.top();
							maxQue.pop();
							res+=curr-left;
							if(curr!=left) dir=0;
							curr=left;
						}
					}
					else{
						if(maxQue.empty()){
							right=minQue.top();
							minQue.pop();
							res+=right-curr;
							curr=right;
							dir=1;
						}
						else{
							left=maxQue.top();
							right=minQue.top();
							if(curr-left<right-curr){
								res+=curr-left;
								if(curr!=left) dir=0;
								curr=left;
								maxQue.pop();
							}
							else if(curr-left>right-curr){
								res+=right-curr;
								dir=1;
								curr=right;
								minQue.pop();
							}
							else{
								if(dir){
									res+=right-curr;
									dir=1;
									curr=right;
									minQue.pop();
								}
								else{
									res+=curr-left;
									if(curr!=left) dir=0;
									curr=left;
									maxQue.pop();
								}
							}
						}
					}
				}
				else{
					scanf("%d",&val);
					if(val<=curr) maxQue.push(val);
					else minQue.push(val);
				}
			}
			printf("Case %d: ",i);
			cout<<res<<endl;
		}
	}
	return 0;
}

multiset:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<cmath>
using namespace std;
int main(){
	int t;
	long long sum;
	int L,n;
	int a,b;
	long long Min,p;
	scanf("%d",&t);
	for(int Case=1;Case<=t;Case++){
		scanf("%d%d",&L,&n);
		sum=0;
		multiset<int>s;                       //multiset用红黑树来组织元素数据,题目中需要允许重复插入元素键值,故不用set
		multiset<int>::iterator It,del;
		int cur=0,pre=0;
		while(n--){
			 scanf("%d",&a);
			 if(a==0){
				 scanf("%d",&b);
				 s.insert(b);
			 }
			 else{
				  Min=0x3fffffff;
				  if(!s.size())
					  continue;
				  for(It=s.begin();It!=s.end();It++){
					  if(abs(*It-cur)<Min){   
						  Min=abs((*It)-cur);
						  p=(*It);           //得到距离最近的那个蛋糕的位置
						  del=It;            //标记要吃的蛋糕,以便于删除
					  }
					  else if(abs(*It-cur)==Min){   //当距离一样时取同方向的
						  if((*It-cur)*(cur-pre)>0){
							  Min=abs((*It)-cur);
							  p=(*It);
							  del=It;
						  }
					  }
				  }
				  s.erase(del);
				  sum+=Min;
				  pre=cur;                    //之前所在的位置
				  cur=p;                      //目前的位置
			 }
		}
		printf("Case %d: %I64d\n",Case,sum);
	}
//	system("pause");
	return 0;
}





资源下载链接为: https://pan.quark.cn/s/9648a1f24758 这个HTML文件是一个专门设计的网页,适合在告白或纪念日这样的特殊时刻送给女朋友,给她带来惊喜。它通过HTML技术,将普通文字转化为富有情感和创意的表达方式,让数字媒体也能传递深情。HTML(HyperText Markup Language)是构建网页的基础语言,通过标签描述网页结构和内容,让浏览器正确展示页面。在这个特效网页中,开发者可能使用了HTML5的新特性,比如音频、视频、Canvas画布或WebGL图形,来提升视觉效果和交互体验。 原本这个文件可能是基于ASP.NET技术构建的,其扩展名是“.aspx”。ASP.NET是微软开发的一个服务器端Web应用程序框架,支持多种编程语言(如C#或VB.NET)来编写动态网页。但为了在本地直接运行,不依赖服务器,开发者将其转换为纯静态的HTML格式,只需浏览器即可打开查看。 在使用这个HTML特效页时,建议使用Internet Explorer(IE)浏览器,因为一些老的或特定的网页特效可能只在IE上表现正常,尤其是那些依赖ActiveX控件或IE特有功能的页面。不过,由于IE逐渐被淘汰,现代网页可能不再对其进行优化,因此在其他现代浏览器上运行可能会出现问题。 压缩包内的文件“yangyisen0713-7561403-biaobai(html版本)_1598430618”是经过压缩的HTML文件,可能包含图片、CSS样式表和JavaScript脚本等资源。用户需要先解压,然后在浏览器中打开HTML文件,就能看到预设的告白或纪念日特效。 这个项目展示了HTML作为动态和互动内容载体的强大能力,也提醒我们,尽管技术在进步,但有时复古的方式(如使用IE浏览器)仍能唤起怀旧之情。在准备类似的个性化礼物时,掌握基本的HTML和网页制作技巧非常
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值