正文
拦截导弹简单版(读入一串整数时getline(cin,s) stringstream is(s);)
小程序:扫一扫查出行
【扫一扫了解最新限行尾号】
复制小程序
【扫一扫了解最新限行尾号】
复制小程序
拦截导弹简单版
时间限制: 1 Sec 内存限制: 128 MB
提交: 40 解决: 16
[提交][状态][讨论版][命题人:外部导入]
题目描述
某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统。但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度。某天,雷达捕捉到敌国的导弹来袭。由于该系统还在试用阶段,所以只有一套系统,因此有可能不能拦截所有的导弹。
输入导弹依次飞来的高度(雷达给出的高度数据是不大于30000的正整数,导弹数不超过1000),计算这套系统最多能拦截多少导弹,如果要拦截所有导弹最少要配备多少套这种导弹拦截系统。
输入
输入只有一行,为若干个正整数,一次为导弹的高度。
输出
第一行为最多能拦截的导弹数;
第二行为要拦截所有导弹最少要配备的系统数
样例输入
389 207 155 300 299 170 158 65
样例输出
6
2
提示
#include<stdio.h>
#include<string.h>
#include<sstream>
#include<string>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
int dp[],a[],b[];
int main()
{
//freopen("a.txt","r",stdin);
int t;
string s;
getline(cin,s);
memset(dp,,sizeof(dp));
stringstream is(s);
int x,i=,j,k,n=;
while(is>>x) a[n++]=x;
b[]=; k=;
for(i=;i<n;i++)
{
for(j=;j<=k;j++)
{
if(a[i]<=b[j])
{
b[j]=a[i];
break;
}
else if(j==k)
{
b[++k]=a[i];
break;
}
}
}
int ans=;
for(i=n-;i>=;i--)
{
for(j=n-;j>i;j--)
if(a[i]>=a[j] && dp[i]<dp[j]+) dp[i]=dp[j]+;
if(dp[i]>ans) ans=dp[i];
}
printf("%d\n",ans+);
printf("%d\n",k);
return ;
}
某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统。但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度。某天,雷达捕捉到敌国的导弹来袭。由于该系统还在试用阶段,所以只有一套系统,因此有可能不能拦截所有的导弹。
输入导弹依次飞来的高度(雷达给出的高度数据是不大于30000的正整数,导弹数不超过1000),计算这套系统最多能拦截多少导弹,如果要拦截所有导弹最少要配备多少套这种导弹拦截系统。
输入只有一行,为若干个正整数,一次为导弹的高度。
输出
第一行为最多能拦截的导弹数;
第二行为要拦截所有导弹最少要配备的系统数
样例输入
389 207 155 300 299 170 158 65
样例输出
6
2
提示
#include<stdio.h>
#include<string.h>
#include<sstream>
#include<string>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
int dp[],a[],b[];
int main()
{
//freopen("a.txt","r",stdin);
int t;
string s;
getline(cin,s);
memset(dp,,sizeof(dp));
stringstream is(s);
int x,i=,j,k,n=;
while(is>>x) a[n++]=x;
b[]=; k=;
for(i=;i<n;i++)
{
for(j=;j<=k;j++)
{
if(a[i]<=b[j])
{
b[j]=a[i];
break;
}
else if(j==k)
{
b[++k]=a[i];
break;
}
}
}
int ans=;
for(i=n-;i>=;i--)
{
for(j=n-;j>i;j--)
if(a[i]>=a[j] && dp[i]<dp[j]+) dp[i]=dp[j]+;
if(dp[i]>ans) ans=dp[i];
}
printf("%d\n",ans+);
printf("%d\n",k);
return ;
}
第一行为最多能拦截的导弹数;
第二行为要拦截所有导弹最少要配备的系统数
389 207 155 300 299 170 158 65
样例输出
6
2
提示
#include<stdio.h>
#include<string.h>
#include<sstream>
#include<string>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
int dp[],a[],b[];
int main()
{
//freopen("a.txt","r",stdin);
int t;
string s;
getline(cin,s);
memset(dp,,sizeof(dp));
stringstream is(s);
int x,i=,j,k,n=;
while(is>>x) a[n++]=x;
b[]=; k=;
for(i=;i<n;i++)
{
for(j=;j<=k;j++)
{
if(a[i]<=b[j])
{
b[j]=a[i];
break;
}
else if(j==k)
{
b[++k]=a[i];
break;
}
}
}
int ans=;
for(i=n-;i>=;i--)
{
for(j=n-;j>i;j--)
if(a[i]>=a[j] && dp[i]<dp[j]+) dp[i]=dp[j]+;
if(dp[i]>ans) ans=dp[i];
}
printf("%d\n",ans+);
printf("%d\n",k);
return ;
}
2
#include<stdio.h>
#include<string.h>
#include<sstream>
#include<string>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std; int dp[],a[],b[]; int main()
{
//freopen("a.txt","r",stdin);
int t;
string s;
getline(cin,s);
memset(dp,,sizeof(dp));
stringstream is(s);
int x,i=,j,k,n=;
while(is>>x) a[n++]=x;
b[]=; k=;
for(i=;i<n;i++)
{
for(j=;j<=k;j++)
{
if(a[i]<=b[j])
{
b[j]=a[i];
break;
}
else if(j==k)
{
b[++k]=a[i];
break;
}
}
}
int ans=;
for(i=n-;i>=;i--)
{
for(j=n-;j>i;j--)
if(a[i]>=a[j] && dp[i]<dp[j]+) dp[i]=dp[j]+;
if(dp[i]>ans) ans=dp[i];
}
printf("%d\n",ans+);
printf("%d\n",k);
return ;
}