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
#include
#include
using namespace std;
long long n,d[100005];
vector 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<

你可能感兴趣的:(dp)