zoj3805 Machine DP
来源:互联网 发布:淘宝网儿童口罩有图案 编辑:程序博客网 时间:2024/05/14 04:11
In a typical assembly line, machines are connected one by one. The first machine's output product will be the second machine's raw material. To simplify the problem, we put all machines into a two-dimension shelf. Every machine occupied exactly one grid and has two input ports and only one output port. One input port can get material from only one machine.
Pipes will be used to connect between these machines. There are two kinds of pipes : 'I' kind and 'L' kind. We should notice that the 'I' kind pipe can be linked one by one. Each pipe will also occupied one grid.
In Bob's factory, each machine will get raw materials from zero, one or two other machines. Some machines don't need any input materials, but any machine must have an output. Machines are coded by numbers from 1 to n. The output of the machines with greater code can be the input of the machines with less code. The machine NO.1's output product will be the final product, and will not be any other machine's input. Bob's factory has a shelf with infinite height, but finite width. He will give you the dependency relationship of these machines, and want you to arrange these machines and pipes so that he can minimize the width of the shelf.
Here's an example for you to help understand :
Products will falling from higher machine to lower machine through the pipes. Here, machine 1 gets materials from machine 2 and machine 3. The whole width of this system is 2.
Input
For each case, the first line will be an integer n indicates the number of the machines (2≤ n≤ 10000). The following line will include n-1 numbers. The i-th number ai means that the output of machine i+1 will be the input of machine ai (ai≤ i). The same code will be appeared at most twice. Notice machine 1's output will be the final output, and won't be any machine's input.
Output
For each case, we need exactly one integer as output, which is the minimal width of the shelf.
Sample Input
31 171 1 2 2 3 3
Sample Output
23
Hint
Case 1 is the example.
Case 2:
This problem contains massive input and output, please use efficient IO methods.
简单dp,不说了
#include <iostream>#include <cstdio>#include <cstring>#define inf 0x3f3f3f3fusing namespace std;int n;int a[10010];int l[10010],r[10010];void init(){ memset(l,0x3f,sizeof l); memset(r,0x3f,sizeof r);}void read(){ for(int i=0;i<n-1;i++){ scanf("%d",&a[i]); if(l[a[i]]==inf) l[a[i]]=i+2; else r[a[i]]=i+2; }}int dfs(int x){ if(l[x]==inf&&r[x]==inf) return 1; int lans=0,rans=0; if(l[x]!=inf) lans=dfs(l[x]); if(r[x]!=inf) rans=dfs(r[x]); return max(min(lans,rans)+1,max(lans,rans));}int main(){ while(~scanf("%d",&n)){ init(); read(); cout<<dfs(1)<<endl; } return 0;}
- zoj3805 Machine DP
- ZOJ3805:Machine
- ZOJ3805 Machine I型和L型的管道最少多宽?(贪心)
- pku1276 Cash Machine (DP)
- sgu160 Magic Multiplying Machine DP
- POJ 1276 Cash Machine (DP)
- 【DP】poj 1276 Cash Machine
- sgu287:Amusing Qc Machine(DP)
- poj1276(dp) Cash Machine
- SGU 160 Magic Multiplying Machine(dp)
- 【DP|多重背包】POJ-1276 Cash Machine
- ZOJ1366 Cash Machine 优化的dp
- POJ 1276 Cash Machine【多重背包DP】
- ZOJ 3805 Machine(树形DP)
- poj 1276 Cash Machine(dp 水题)
- hdu 5067 Harry And Dig Machine(dp)
- ZOJ 3805 Machine(简单dp)
- poj - 1276 - Cash Machine(dp)
- Android手机号码获取问题
- block
- Linux中的设备文件与设备号
- CESHI
- Git 版本合并策略
- zoj3805 Machine DP
- Hibernate学习之get和load区别
- 你能说出SQL聚集索引和非聚集索引的区别吗(未知日期)
- 深入理解java虚拟机(十) Java 虚拟机运行时栈帧结构
- 探cocos2dx文本显示实现(label_nodes),源码版本(2.2.3)
- Codeforces 461B
- @protocol
- 解决apt-get install blktrace时,出现source 404 not found的问题
- Javascript之document对象详解