0
点赞
收藏
分享

微信扫一扫

7-6 修理牧场

_LEON_ 2022-01-06 阅读 35
数据结构
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
long long n,que[10005],fr=-1,la,ans,tong[1005];
int wd[10005];
void read(int &x){ 
	int f=1;x=0;char s=getchar();
	while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
	while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
	x*=f;
}
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		read(wd[i]);
		tong[wd[i]]++;
	}
	int cnt=0;
	for(int i=1;i<=1000;i++)
	{
		for(int j=1;j<=tong[i];j++)
		wd[++cnt]=i;
	}
	int i=0;
	for(int j=1;j<n;j++)
	{
		if(la==0)
		que[la++]=wd[++i]+wd[++i];
		else{
			i++;fr++;
			if(wd[i]<que[fr]&&i<=n||fr==la)
			{
				que[la]+=wd[i];
				if(i==n) i=n+1;
				fr--;
			}
			else{
				que[la]+=que[fr];
				i--;				
			}
			i++;fr++;
			if(wd[i]<que[fr]&&i<=n||fr==la)
			{
				que[la]+=wd[i];
				if(i==n) i=n+1;
				fr--;
			}
			else{
				que[la]+=que[fr];
				i--;				
			}			
			la++;	//cout<<j<<' '<<que[la-1]<<endl;	
		}
		ans+=que[la-1];
		
	}
	cout<<ans;
	return 0;
}
举报

相关推荐

7-5 修理牧场 (25 分)

PTA 7-6 查找整数 题解

7-6 组合数的和

7-6 冒泡法排序 (20 分)

7-6 列出连通集 (25 分)

7-6 输出全排列 (20 分)

0 条评论