贪心 F - Two Cakes

本题是一道贪心算法问题,题目要求两个人分别购买两块蛋糕的n层,每层大小从1到n。街上共有2n家店铺,每家只能买到特定大小的一层。两人需按从小到大的顺序购买,目标是找到总步行距离最短的方案。输入包含蛋糕层数n和每家店铺可买的蛋糕层,输出为最小总距离。示例给出了不同n值的情况及对应的最小总距离。
摘要由CSDN通过智能技术生成

题目
Sasha and Dima want to buy two n-tier cakes. Each cake should consist of n different tiers: from the size of 1 to the size of n. Tiers should go in order from the smallest to the biggest (from top to bottom).

They live on the same street, there are 2⋅n houses in a row from left to right. Each house has a pastry shop where you can buy a cake tier. Unfortunately, in each pastry shop you can buy only one tier of only one specific size: in the i-th house you can buy a tier of the size ai (1≤ai≤n).

Since the guys carry already purchased tiers, and it is impossible to insert a new tier in the middle of the cake, they agreed to buy tiers from the smallest to the biggest. That is, each of them buys tiers in order: 1, then 2, then 3 and so on up to n.

Initially, Sasha and Dima are located near the first (leftmost) house. Output the minimum distance that they will have to walk in total to buy both cakes. The distance between any two neighboring houses is exactly 1.

Input
The first line of the input contains an integer number n — the number of tiers in each cake (1≤n≤105).

The second line contains 2⋅n integers a1,a2,…,a2n (1≤ai≤n), where ai is equal to the size of the tier, which can be bought in the i-th house. Remember that in each house you can buy only one tier. It is guaranteed that every number from 1 to n occurs in a exactly two times.

Output
Print one number — the minimum distance that the guys have to walk in total to buy both cakes. Guys can be near same house at the same time. They begin near the first (leftmost) house. Each of the guys should buy n tiers in ascending order of their sizes.

Examples
Input
3
1 1 2 2 3 3
Output
9
Input
2
2 1 1 2
Output
5
Input
4
4 1 3 2 2 3 1 4
Output
17
Note
In the first example, the possible optimal sequence of actions is:

Sasha buys a tier of size 1 near the 1-st house (a1=1);
Dima goes to the house 2;
Dima buys a tier of size 1 near the 2-nd house (a2=1);
Sasha goes to the house 4;
Sasha buys a tier of size 2 near the 4-th house (a4=2);
Sasha goes to the house 5;
Sasha buys a tier of size 3 near the 5-th house (a5=3);
Dima goes to the house 3;
Dima buys a tier of size 2 near the 3-rd house (a3=2);
Dima goes to the house 6;
Dima buys a tier of size 3 near the 6-th house (a6=3).
So, Sasha goes the distance 3+1=4, and Dima goes the distance 1+1+3=5. In total, they cover a distance of 4+5=9. You can make sure that with any other sequence of actions they will walk no less distance.
题意
有两个人分别要去做两块蛋糕,蛋糕有n层,并且从上到下每层宽度为1-n,现在在商业街有2n个店铺,每个店铺只有1块。两个人买蛋糕的规矩是从宽度1到n依次购买,即先买1,再买2,。。。从i店铺走到j店铺的距离为abs(i-j),求两个人都做出蛋糕的路程之和的最小值。

#include <stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn=2e5+10;
vector<int>a[maxn]; 
int main()
{

	int x,n;
	scanf("%lld",&n);
	for(int i=1;i<=2*n;i++){
		scanf("%d",&x);
		a[x].push_back(i);
	}			
	long long ans=0;
	int head=1,tail=1;
	for(int i=1;i<=n;i++)
	{
		ans+=abs(a[i][0]-head)+abs(a[i][1]-tail);
		head=a[i][0];
		tail=a[i][1];
	}
	printf("%lld",ans);
	return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值