CF 1130B Two Cakes

B. Two Cakes
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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
inputCopy
3
1 1 2 2 3 3
outputCopy
9
inputCopy
2
2 1 1 2
outputCopy
5
inputCopy
4
4 1 3 2 2 3 1 4
outputCopy
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层蛋糕,已知一条街上的商店每家只能提供一个x层蛋糕,输出两个人组装成n层蛋糕总共需要步行的最少步数。
题目思路:用vector记录每一层蛋糕的店家地址,形成一个二维数组。对于第一层和第二层蛋糕,就有两种组合方式,用点dp的思路往后推。另外要注意数组的初始化,要插入两个0

#include<iostream>
#include<cmath>
#include<vector>
using namespace std;
long long n,d[100005];
vector<long long> a[100005];
int main()
{
	long long i,b,d1,d2;
	cin >>n;
	for(i = 0;i<2*n;i++)
	{
		cin >> b;
		a[b].push_back(i);
	}
	a[0].push_back(0);
	a[0].push_back(0);
	for(i = 1;i<=n;i++)
	{
		d1 = abs(a[i][0] - a[i-1][0]) + abs(a[i][1] - a[i-1][1]);
		d2 = abs(a[i][0] - a[i-1][1]) + abs(a[i][1] - a[i-1][0]);
		d[i] = d[i-1] + min(d1,d2);
	 } 
	 cout<<d[n]<<endl;
	return 0;
}
38.He took his umbrella ______ it should rain.(2分) A in case of B in case C for fear D in time 39.Not until this term ( ) to realize how important this subject is to his future career as a diplomat.(2分) A he began B did he begin C he has begun D that he has begun 40.Great changes ______ in Shanghai since the beginning of the reform and opening-up policy.(2分) A took place B has taken place C has been taken place D have taken place 41.The larger the house is, ______.(2分) A the higher rent it is B the higher rent it would have C the higher the rent is D the higher rent it would be 42.Every time Jane has trouble ______ her car started, Sean will show up to lend a hand.(2分) A get B getting C to getting D to get44.Some bookshelves have been moved out of this office to make ______ for more computers.(2分) A space B place C room D position46. Wendy: Have you been to the new bakery on the corner? Arthur: NO, how is it? Wendy: It is heaven!________! (2分) A Their cakes are to strive for B Their cakes are to pay for C Their cakes are to struggle for D Their cakes are to die for 47.Dr. Jones, many students want to see you.( ) they wait here or outside?(2分) A Do B Will C Shall D Would 48.( ) who would like to go on the trip should put their names on the list. (2分) A Those B These C Somebody D The ones 49. Kate: Do you mind opening the door for me? Bob: _________. (2分) A It’s nothing B That’s all right C Yes, I’ll do it D Not at all 50.They first stop at a board _____ the menu is displayed.(2分) A when B while C where D which
最新发布
06-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值