查看文章
 
noip 1999 拦截导弹
2009年11月14日 星期六 下午 10:25
先求最长上升子序列,输出长度,标记已拦截的。
再循环做最长上升子序列,直到导弹都被拦截,统计循环的次数即需要的系统数。

#include<stdio.h>
#include<string.h>
const int SIZE=10005;
int main()
{
//freopen("dat.in","r",stdin);
//freopen("dat.out","w",stdout);
bool used[SIZE];
int n=0,i,dat[SIZE],ans,cnt=1,dp[SIZE],j,root[SIZE];
while (scanf ("%d",&dat[n])!=EOF)
n++;
memset(used,0,sizeof(used));
memset(dp,0,sizeof(dp));
for (i=0;i<n;i++)
root[i]=-1;
dp[0]=1;
for (i=1;i<n;i++){
int tmp=0;
for (j=0;j<i;j++)
if (dat[j]>dat[i] && dp[j]>tmp){
tmp=dp[j];
root[i]=j;
}
dp[i]=tmp+1;
}
int max=0,pos,size=n;
for (i=n-1;i>=0;i--)
if (dp[i]>max){
max=dp[i];
pos=i;
}
size-=max;
printf("%d\n",max);
while (size>0){
while (root[pos]!=-1){
used[pos]=1;
pos=root[pos];
}
used[pos]=1;
memset(dp,0,sizeof(dp));
for (i=0;i<n;i++) root[i]=-1;
for (i=0;i<n;i++)
if (!used[i]){
int tmp=0;
for (j=0;j<i;j++)
if (!used[i] && dat[j]>dat[i] && dp[j]>tmp){
tmp=dp[j];
root[i]=j;
}
dp[i]=tmp+1;
}
max=0;
for (i=n-1;i>=0;i--)
if (dp[i]>max){
max=dp[i];
pos=i;
}
size-=max;
cnt++;
}
printf("%d\n",cnt);
return 0;
}

syntax highlighted by Code2HTML, v. 0.9.1

类别:Noip||添加到搜藏 |分享到i贴吧|浏览(486)|评论 (0)
 
最近读者:
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
     

   
帮助中心 | 空间客服 | 投诉中心 | 空间协议
©2012 Baidu