openjudge 拦截导弹 (DP 动态规划)
2018-03-15 17:24
267 查看
样例输入
当整个数组位递增序列时候输出1,所以用来记录最大值maxx=1,不然会输出0,卡在这里好久,dp写的没错,但是ac不了,后来对比别人的代码,发现问题,还有一个注意点,a[i]>=a[j](i<j) ,因为是不递增序列,等于情况符合#include <iostream>
#include "cstring"
#include <stdio.h>
#include "iomanip"
#include "vector"
#include "cmath"
#include "stack"
#include "algorithm"
#include <math.h>
#include "map"
#include "queue"
using namespace std;
int main()
{
freopen("a.txt","r",stdin );
int n,a[20]={0},dp[20]={0};
cin>>n;
int i,j;
for(i=0;i<n;i++)
{
cin>>a[i];
dp[i]=1;
}
int maxx=1;
for(i=0;i<n;i++)
{
for(j=0;j<i;j++)
{
if(a[i]<=a[j])
{
dp[i]=max(dp[i],dp[j]+1);
maxx=max(dp[i],maxx);
}
}
}
// for(i=0;i<n;i++)
// maxx=max(dp[i],maxx);
cout<<maxx;
return 0;
}
8 389 207 155 300 299 170 158 65样例输出
6
当整个数组位递增序列时候输出1,所以用来记录最大值maxx=1,不然会输出0,卡在这里好久,dp写的没错,但是ac不了,后来对比别人的代码,发现问题,还有一个注意点,a[i]>=a[j](i<j) ,因为是不递增序列,等于情况符合#include <iostream>
#include "cstring"
#include <stdio.h>
#include "iomanip"
#include "vector"
#include "cmath"
#include "stack"
#include "algorithm"
#include <math.h>
#include "map"
#include "queue"
using namespace std;
int main()
{
freopen("a.txt","r",stdin );
int n,a[20]={0},dp[20]={0};
cin>>n;
int i,j;
for(i=0;i<n;i++)
{
cin>>a[i];
dp[i]=1;
}
int maxx=1;
for(i=0;i<n;i++)
{
for(j=0;j<i;j++)
{
if(a[i]<=a[j])
{
dp[i]=max(dp[i],dp[j]+1);
maxx=max(dp[i],maxx);
}
}
}
// for(i=0;i<n;i++)
// maxx=max(dp[i],maxx);
cout<<maxx;
return 0;
}
相关文章推荐
- 【NOIP】CODE[VS] 1044 拦截导弹 动态规划 序列型DP
- 九度题目:导弹拦截(1112)最大不增子序列 dp问题
- nyoj 79 拦截导弹 (动态规划)
- NYOJ 79 拦截导弹 (经典dp) 最长上升子序列 LIS
- 最长递增、递减子序列(导弹拦截问题)动态规划练习
- nyoj 79 拦截导弹 连续递减最长子序列(动态规划)
- 导弹拦截问题系列(dp)
- 拦截导弹-最长逆序长度-动态规划
- 动态规划_拦截导弹
- 经典的导弹拦截问题——动态规划和一点形式化证明
- 蓝桥杯 拦截导弹 动态规划
- 动态规划_拦截导弹
- 蓝桥杯-导弹拦截-贪心-dp-java
- 拦截导弹 dp java
- [dp][uestc oj][最长上升子序列] LIS N - 导弹拦截
- Codves 1044 拦截导弹 ---1999年NOIP全国联赛提高组 dp(n2 || nlogn)
- 【日常学习】【线性DP】codevs1044 拦截导弹题解
- 洛谷Oj-导弹拦截-线性dp-最长不下降子序列
- 【DP】洛谷 P1020 导弹拦截
- 导弹拦截(dp-最长不上升子序列)