线段树,堆,multiset:Holedox Eating

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;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值