博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 1836 Alignment
阅读量:6771 次
发布时间:2019-06-26

本文共 3278 字,大约阅读时间需要 10 分钟。

POJ  1836  Alignment(双向LIS)解题报告

题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730#problem/B

原题:

Description

In the army, a platoon is composed by n soldiers. During the morning inspection, the soldiers are aligned in a straight line in front of the captain. The captain is not satisfied with the way his soldiers are aligned; it is true that the soldiers are aligned in order by their code number: 1 , 2 , 3 , . . . , n , but they are not aligned by their height. The captain asks some soldiers to get out of the line, as the soldiers that remain in the line, without changing their places, but getting closer, to form a new line, where each soldier can see by looking lengthwise the line at least one of the line's extremity (left or right). A soldier see an extremity if there isn't any soldiers with a higher or equal height than his height between him and that extremity.        
Write a program that, knowing the height of each soldier, determines the minimum number of soldiers which have to get out of line.        

Input

On the first line of the input is written the number of the soldiers n. On the second line is written a series of n floating numbers with at most 5 digits precision and separated by a space character. The k-th number from this line represents the height of the soldier who has the code k (1 <= k <= n).        
There are some restrictions:         • 2 <= n <= 1000 • the height are floating numbers from the interval [0.5, 2.5]        

Output

The only line of output will contain the number of the soldiers who have to get out of the line.

Sample Input

81.86 1.86 1.30621 2 1.4 1 1.97 2.2

Sample Output

4 题目大意: 有N个身高不一样的士兵排成一排,现在要去掉一些人,使剩下的每一个士兵都能看到队首或队尾,即 前面或后面没有比他高或和他一样高的人,求,至少要去掉 多少人。 分析: 这一个双向LIS的题目,即最长上升子序列和最长下降子序列,要分别从前往后和从后往前求LIS,但不能直接套用LIS。去掉一些人,使队伍成一个三角形或一个 梯形(最高的两个值相等)。 从前往后:对第i个人求1~i的以i结尾的最长上升子序列的长度 从后往前:对第i个人求n~i的以i结尾的最长上升子序列的长度 总长度就是这两个值相加减1,再用N-最多剩余的长度,即为最少去除的。 代码:
1 #include
2 #include
3 #include
4 #include
5 using namespace std; 6 const int maxn=1005; 7 8 int dp1[maxn],dp2[maxn];//dp1[]从前往后LIS;dp2[]从后往前LIS 9 double a[maxn];//身高10 11 int max(int a,int b)12 {13 return a>b?a:b;14 }15 16 int main()17 {18 int n;//士兵人数19 while(scanf("%d",&n)!=EOF)20 {21 for(int i=1;i<=n;i++)22 {23 scanf("%lf",&a[i]);24 dp1[i]=1;//初始化dp1[]25 dp2[i]=1;//初始化dp2[]26 }27 int ans=0;//剩余士兵的最大长度28 for(int i=1;i<=n;i++)//求最长上升子序列dp1[]29 {30 for(int j=1;j<=i-1;j++)31 {32 if(a[j]
=1;i--)//求最长下降子序列dp2[]37 {38 for(int j=i+1;j<=n;j++)39 {40 if(a[j]
=1;j--)48 {49 if(a[j]==a[i])//找到i前面的离i最近的与a[i]相等的点,更新dp1[i]50 {51 dp1[i]=max(dp1[i],dp1[j]+1);52 flag=0;53 break;54 }55 }56 for(int j=i+1;j<=n;j++)57 {58 if(flag=1&&a[j]==a[i])//先判断前面有没有算过最高点(a==0),若没算过,找到i后面的离i最近的与a[i]相等的最高点,更新dp2[i]59 {60 dp2[i]=max(dp2[i],dp2[j]+1);61 break;62 }63 }64 ans=max(ans,dp1[i]+dp2[i]-1);//更新最大长度65 }66 printf("%d\n",n-ans);//最小删除的长度67 }68 return 0;69 }
 

 

 
 

转载于:https://www.cnblogs.com/ttmj865/p/4738776.html

你可能感兴趣的文章
AlwaysON同步性能监控的三板斧
查看>>
环球天下刘学辉:教育业的IT选型心经
查看>>
使用.net编写的 短连接(短网址)服务
查看>>
加速物联网业务发展 英特尔有意收购Altera
查看>>
“预知能力的超人” 细数威胁情报那些事儿
查看>>
物联网推动CRM创造新型服务
查看>>
你信隔空取物吗?你以为不插网线就安全吗?DiskFiltration可以突破物理隔绝窃取数据...
查看>>
单盘随机读IOPS超百万、容量11TB SSD诞生---PBlaze5 PCIe NVMe SSD
查看>>
中国工程院院士高文:智慧城市的管控思路
查看>>
鑫泰水处理用条码构建企业物联网的全连接
查看>>
伪标签:教你玩转无标签数据的半监督学习方法
查看>>
美政府报告:黑客或通过WiFi系统劫持客机
查看>>
王者荣耀爆勒索病毒,玩家手机可能被锁
查看>>
云适配推企业级浏览器Enterplorer “No APP”跨入移动时代
查看>>
IBM Watson AI:这些公司正在用认知计算打击网络犯罪
查看>>
指尖上的“无声服务”带来传统呼叫中心运营模式的变革
查看>>
十年的坚守与执着 CDN行业需要匠人精神
查看>>
国外主流公有云服务厂商优劣势一览
查看>>
滴滴出行在智慧交通领域为自己设定的三个短期目标
查看>>
微信VS支付宝,移动支付的战争久燃不息
查看>>