cf983C(恶心状压DP)

虽然很讨厌这种题啦。。不过还是很常见的题目。。还得多练练才行。。。

本来只想着只记录电梯内4人的目的地就行,因为搭载了第i个人那么位置肯定在a[i]。。

这种思路上说不上错。。然而实现上需要分类讨论太多情况了。。所以弃了。。

参考了ctl的做法。。将位置也考虑进状态中。。。状态变成电梯位置和4人的目标。。

上面的思路主要出现在多人出去时次序的问题。。那么咱们就让人一个一个出去。。

考虑到最多只能出去4个人。。那么就来4次出电梯的操作。。就是代码中的k循环。。

然后发现会做很多多余的操作。。不过要优化这个真的太麻烦了。。然后因为这个跑了2.8s。。(时限3s)

一个个出电梯就容易啦。。有人就出。。

第i个人进电梯也容易。。没人就进。。。

然后根据状态的定义随便转移。。。

总之思路简单。。编程技巧性却比较强。。。



/**
 *        ┏┓    ┏┓
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃  
 *        ┃   ━    ┃
 *        ┃ >   < ┃
 *        ┃       ┃
 *        ┃... ⌒ ...  ┃
 *        ┃       ┃
 *        ┗━┓   ┏━┛
 *          ┃   ┃ Code is far away from bug with the animal protecting          
 *          ┃   ┃   神兽保佑,代码无bug
 *          ┃   ┃           
 *          ┃   ┃        
 *          ┃   ┃
 *          ┃   ┃           
 *          ┃   ┗━━━┓
 *          ┃       ┣┓
 *          ┃       ┏┛
 *          ┗┓┓┏━┳┓┏┛
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛
 */ 
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-12
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 2005
#define nm 1000005
#define pi 3.1415926535897931
using namespace std;
const int inf=1000000007;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}


int d[10][10][10][10][10],f[10][10][10][10][10];
int a[NM],b[NM],n,ans;
void up(int&x,int y){if(x>y)x=y;}


int main(){
	n=read();
	inc(i,1,n)a[i]=read(),b[i]=read();
	inc(j,1,9)inc(t1,0,9)inc(t2,0,9)inc(t3,0,9)inc(t4,0,9)d[j][t1][t2][t3][t4]=inf;
	d[1][0][0][0][0]=0;
	inc(i,1,n+1){
		inc(k,1,4)
		inc(j,1,9)inc(t1,0,9)inc(t2,0,9)inc(t3,0,9)inc(t4,0,9)if(d[j][t1][t2][t3][t4]<inf){
			if(t1)up(d[t1][0][t2][t3][t4],d[j][t1][t2][t3][t4]+abs(j-t1)+1);
			if(t2)up(d[t2][t1][0][t3][t4],d[j][t1][t2][t3][t4]+abs(j-t2)+1);
			if(t3)up(d[t3][t1][t2][0][t4],d[j][t1][t2][t3][t4]+abs(j-t3)+1);
			if(t4)up(d[t4][t1][t2][t3][0],d[j][t1][t2][t3][t4]+abs(j-t4)+1);
		}
		//inc(j,1,9)printf("%d ",d[j][0][0][0][0]);putchar('\n');
		if(i>n)break;
		inc(j,1,9)inc(t1,0,9)inc(t2,0,9)inc(t3,0,9)inc(t4,0,9)f[j][t1][t2][t3][t4]=inf;
		inc(j,1,9)inc(t1,0,9)inc(t2,0,9)inc(t3,0,9)inc(t4,0,9){
			if(!t1)up(f[a[i]][b[i]][t2][t3][t4],d[j][t1][t2][t3][t4]+abs(j-a[i])+1);
			if(!t2)up(f[a[i]][t1][b[i]][t3][t4],d[j][t1][t2][t3][t4]+abs(j-a[i])+1);
			if(!t3)up(f[a[i]][t1][t2][b[i]][t4],d[j][t1][t2][t3][t4]+abs(j-a[i])+1);
			if(!t4)up(f[a[i]][t1][t2][t3][b[i]],d[j][t1][t2][t3][t4]+abs(j-a[i])+1);
		}
		inc(j,1,9)inc(t1,0,9)inc(t2,0,9)inc(t3,0,9)inc(t4,0,9)d[j][t1][t2][t3][t4]=f[j][t1][t2][t3][t4];
	}
	ans=inf;
	inc(i,1,9)up(ans,d[i][0][0][0][0]);
	return 0*printf("%d\n",ans);
}



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 sample t = 0

t = 2

t = 3

t = 5

t = 6

t = 7

t = 9

t = 10


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值