跳到正文

剪贴板

剪贴板 bpz1m8ho

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=25;
const int maxm=1.1e6+10;
int n,a[maxn],b[maxn];
struct node{
	int x,y;
	bool operator<(const node &o)const{
		if(x!=o.x)return x<o.x;
		return y<o.y;
	}
}t[maxm],tmp[maxm];
int tot,sa,sb,cnt;
stack<node> s;
signed main()
{
	scanf("%lld",&n);
	for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
	for(int i=1;i<=n;i++)scanf("%lld",&b[i]);
	for(int st=0;st<(1ll<<n);st++)
	{
		sa=sb=0;
		for(int i=1;i<=n;i++)
		{
			if(st&(1ll<<(i-1)))sa+=a[i];
			else sb+=b[i];
		}
		t[++tot]=(node){sa,sb};
	}
	sort(t+1,t+tot+1);
	for(int i=1;i<=tot;i++)
	{
		while(!s.empty() && s.top().y<=t[i].y)s.pop();
		s.push(t[i]);
	}
	while(!s.empty())tmp[++cnt]=s.top(),s.pop();
	reverse(tmp+1,tmp+cnt+1);
	int ans=1e18,tx=0,ty=0;
	for(int i=1;i<=cnt;i++)
	{
		if(tmp[i].x+tmp[i].y+2<ans)
		{
			ans=tmp[i].x+tmp[i].y+2;
			tx=tmp[i].x+1;ty=tmp[i].y+1;
		}
	}
	for(int i=1;i<=cnt-1;i++)
	{
		if(tmp[i].x+tmp[i+1].y+2<ans)
		{
			ans=tmp[i].x+tmp[i+1].y+2;
			tx=tmp[i].x+1;ty=tmp[i+1].y+1;
		}
	}
	printf("%lld\n%lld\n",tx,ty);
	return 0;
}