为什么手写大根堆过不了第七个点,STL就能AC?
  • 板块P2094 运输
  • 楼主lingyun_void
  • 当前回复6
  • 已保存回复6
  • 发布时间2021/9/17 16:54
  • 上次更新2023/11/4 06:33:32
查看原帖
为什么手写大根堆过不了第七个点,STL就能AC?
533206
lingyun_void楼主2021/9/17 16:54

如题。。

应该还没有人遇到我这种情况吧...

大根堆代码:

#include<iostream>
#include<cstdio>
#include<algorithm>

using namespace std;

int n,k,w,t1,t2;

template<typename type>//快读 
inline void read(type &x)
{
	x=0;bool flag(0);char ch=getchar();
	while(!isdigit(ch)) flag^=ch=='-',ch=getchar();
	while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
	flag?x=-x:0;
}

template<typename type>//快写 
inline void write(type x,bool mode)
{
	x<0?x=-x,putchar('-'):0;static short Stack[100],top(0);
	do Stack[++top]=x%10,x/=10;while(x);
	while(top) putchar(Stack[top--]|48);
	mode?puts(""):putchar(' ');
}

struct heap//大根堆 
{
	int n;
	int q[10010];
	
	heap()//初始化 
	{
		n=0;
	}
	
	void push(int x)//加入元素 
	{
		q[++n]=x;
		swap(q[1],q[n]);
		for(int i=n;i!=1&&q[i>>1]<q[i];i>>=1)
		{
			swap(q[i>>1],q[i]);
		}
	}
	
	void pop()//弹出堆顶 
	{
		swap(q[1],q[n]);
		n--;
		int t=1,tt;
		while((t<<1)<=n)
		{
			tt=t<<1;
			if(tt+1<=n&&q[tt+1]>q[tt]) tt++;
			if(q[t]<q[tt]) swap(q[t],q[tt]),t=tt;
			else break;
		}
	}
	
	int top()//返回堆顶元素(最大值) 
	{
		return q[1];
	}
	
	int back()//返回堆底元素(最小值) 
	{
		return q[n];
	}
	
	int size()//返回堆中元素个数 
	{
		return n;
	}
}q;

signed main()
{
	read(n),read(k);
	for(int i=1;i<=n;i++)
	{
		read(w);
		q.push(w);
	}
	while(q.size()>1)//与合并果子思路大体相同 
	{
		t1=q.top();
		q.pop();
		t2=q.top();
		q.pop();
		q.push((t1+t2)/k);
	}
	write(q.top(),1);
	return 0;
}

#7WA了...

这个用了priority_queue就能过?(迷惑)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>

using namespace std;

int n,k,w,t1,t2;
priority_queue<int>q;

template<typename type>//快读 
inline void read(type &x)
{
	x=0;bool flag(0);char ch=getchar();
	while(!isdigit(ch)) flag^=ch=='-',ch=getchar();
	while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
	flag?x=-x:0;
}

template<typename type>//快写 
inline void write(type x,bool mode)
{
	x<0?x=-x,putchar('-'):0;static short Stack[100],top(0);
	do Stack[++top]=x%10,x/=10;while(x);
	while(top) putchar(Stack[top--]|48);
	mode?puts(""):putchar(' ');
}

signed main()
{
	read(n),read(k);
	for(int i=1;i<=n;i++)
	{
		read(w);
		q.push(w);
	}
	while(q.size()>1)//与合并果子思路大体相同 
	{
		t1=q.top();
		q.pop();
		t2=q.top();
		q.pop();
		q.push((t1+t2)/k);
	}
	write(q.top(),1);
	return 0;
}

求助大佬awa

2021/9/17 16:54
加载中...