有点绕的水题

今天遇到一个大水题,但是有点绕,想了一会才想明白又想到好久没在博客园更新了,营业一下。
Today Alice wants to make a present for her sister by herself. She plans to cut a pretty paper house and needs different sizes of paper.

Alice now has N+1 different sizes of paper. The size of \(A_{i-1}\) is twice as large as the size of \(A_i\) (1<=i<=N).

You have \(a_0\) ​ pieces of paper of size A0, \(a_1\) ​ of size \(A1\)…, \(a_{N}\) ​ pieces of size \(AN\). You want to obtain at least \(b_0\) pieces of size \(A0, b_1\) ​ of size A1…, \(b_{N}\) ​ pieces of size \(AN\). At any point you can fold and cut a paper in half, obtaining two pieces of smaller size (\(e.g. A4 \rightarrow A5 \times 2A4→A5×2\)). What is the minimum number of cuts needed to obtain the required pieces?

Input

The first line contains a single integer N (1≤N≤2⋅105).

The second line contains N+1 integers \(a_0\),\(a_1\),…,\(a_N\) \((0≤a_i≤10^9).\)

The third line contains N+1 integers \(b_0\),\(b_1\),…,\(b_N\) \((0≤bi≤10^9).\)

Output

Output a single integer — the minimum number of cuts needed to obtain the required pieces, or −1, if it's not possible to obtain them.

思路:发现可以按\(a_i+b_i\)的和排序,贪心前后两两配对。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=200010;
int n,p[maxn];
ll a[maxn],b[maxn];
bool cmp(int x,int y)
{
	return a[x]+b[x]<a[y]+b[y];
}
int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n<<1;i++)
	{
		scanf("%lld%lld",&a[i],&b[i]);
		p[i]=i;
		if (a[i]>b[i])
		{
			swap(a[i],b[i]);
		}
	}
	sort(p+1,p+(n<<1)+1,cmp);
	ll ans=0;
	for (int i=1;i<=n;i++)
	{
		ans+=max(b[p[(n<<1|1)-i]],a[p[(n<<1|1)-i]])-min(a[p[i]],b[p[i]]);
	}
	cout<<ans<<endl;
	return 0;
}
posted @ 2022-03-25 22:30  摸鱼自动机  阅读(36)  评论(0)    收藏  举报