CodeForces - 983C -状压dp

C. Elevator

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You work in a big office. It is a 9 floor building with an elevator that can accommodate up to 4 people. It is your responsibility to manage this elevator.

Today you are late, so there are queues on some floors already. For each person you know the floor where he currently is and the floor he wants to reach. Also, you know the order in which people came to the elevator.

According to the company's rules, if an employee comes to the elevator earlier than another one, he has to enter the elevator earlier too (even if these employees stay on different floors). Note that the employees are allowed to leave the elevator in arbitrary order.

The elevator has two commands:

  • Go up or down one floor. The movement takes 1 second.
  • Open the doors on the current floor. During this operation all the employees who have reached their destination get out of the elevator. Then all the employees on the floor get in the elevator in the order they are queued up while it doesn't contradict the company's rules and there is enough space in the elevator. Each employee spends 1 second to get inside and outside the elevator.

Initially the elevator is empty and is located on the floor 1.

You are interested what is the minimum possible time you need to spend to deliver all the employees to their destination. It is not necessary to return the elevator to the floor 1.

Input

The first line contains an integer n (1 ≤ n ≤ 2000) — the number of employees.

The i-th of the next n lines contains two integers ai and bi (1 ≤ ai, bi ≤ 9, ai ≠ bi) — the floor on which an employee initially is, and the floor he wants to reach.

The employees are given in the order they came to the elevator.

Output

Print a single integer — the minimal possible time in seconds.

Examples

input

Copy

2
3 5
5 3

output

Copy

10

input

Copy

2
5 3
3 5

output

Copy

12

Note

Explaination for the first samplet = 0

t = 2

t = 3

t = 5

t = 6

t = 7

t = 9

t = 10

 

不会写,看了杜教的提交,学到了。如果空间允许,存2000*10*10*10*10*10表示已经上过电梯的人,当前所在位置,第一个人目的地,第二个人目的地,第三个人目的地,第四个人目的地当然很好,但是会爆内存。可以发现如果此时电梯人满了(已经有四个人了),那么这四个人之中一定存在上一个刚上电梯的人,就可以通过第一个参数找到该人的目的地来省一些空间。

#include <cstdio>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
using namespace std;
const int max_n=4*(1e7)+220000;
unsigned int qq[max_n];
unsigned short int ti[max_n];
int sta,n,ret;
int p[2010],q[2010];
//已经上过电梯的人,当前所在位置,第一个人目的地,第二个人目的地,第三个人目的地,第四个人目的地存在否,上一状态时间
void update(int cur,int el,int p1,int p2,int p3,int p4,int tt)
{
	vector <int> vc;
	if(p1&&p1!=el) vc.push_back(p1);
	if(p2&&p2!=el) vc.push_back(p2);
	if(p3&&p3!=el) vc.push_back(p3);
	if(p4&&p4!=el) vc.push_back(p4);
	for(int i=cur;i<n&&vc.size()<4;i++)
	{
		if(p[i]==el) vc.push_back(q[i]),cur=i+1;
		else break;
	}
	p1=p2=p3=p4=0;
	if(!vc.empty()&&vc.back()==q[cur-1]) p4=1,vc.pop_back();
	if(!vc.empty()) p3=vc.back(),vc.pop_back();
	if(!vc.empty()) p2=vc.back(),vc.pop_back();
	if(!vc.empty()) p1=vc.back(),vc.pop_back();
	int ans=cur*20000+el*2000+p1*200+p2*20+p3*2+p4;
	if(tt+1<ti[ans]) qq[sta++]=ans,ti[ans]=tt+1;//printf("%d %d %d %d %d %d %d\n",cur,el,p1,p2,p3,p4,tt);
	if(cur==n&&p1==0&&p2==0&&p3==0&&p4==0) ret=min(ret,tt+1);
}
int main()
{
	while(scanf("%d",&n)!=EOF)
	{
		for(int i=0;i<n;i++) scanf("%d%d",&p[i],&q[i]);
		sta=0;ret=0x3f3f3f3f;
		memset(ti,0x3f,sizeof(ti));
		update(0,1,0,0,0,0,-1);
		for(int j=0;j<sta;j++)
		{
			int i=qq[j]/20000,el=qq[j]/2000%10,p1=qq[j]/200%10,p2=qq[j]/20%10,p3=qq[j]/2%10,p4=qq[j]%2;
			if(p4) p4=q[i-1];
			if(el+1<=9) update(i,el+1,p1,p2,p3,p4,ti[qq[j]]);
			if(el-1>=1) update(i,el-1,p1,p2,p3,p4,ti[qq[j]]);
		}
		printf("%d\n",ret+2*n);
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值