(搜索) poj 3700

Missile Defence System
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 4284   Accepted: 1012

Description

To play against the threats of malicious countries nearby, Country R has updated their missile defence system. The new type system can bring down a series of missiles as long as they are coming in ascending order by altitude or descending order by altitude.

Given the heights of a sequence of coming missiles, the general wants to know how many sets of the new type systems are needed to bring down all of them.

Input

The input consists of several test cases. The first line of each test case contains an integer n(1 ≤ n ≤ 50). The next line contains n different integers indicating the heights.

Output

For each test case output a single line containing the number of systems needed.

Sample Input

5
3 5 2 4 1
0 

Sample Output

2

Hint

Two sets of systems are needed for the sample. One brings down 3, 4 and the other brings down 5, 2, 1.

Source

 
题意:
 
就是求 最少有几个递增序列和递减序列
 
解析。。。
数据这么少。。直接搜索啊。。。。
 
dfs(x,y,t)x个递增序列,y个递减序列
 
然后胡乱搞就好了。。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<map>
#define INF 100000000
using namespace std;
int up[55],down[55];
int n,a[55],ans;
void dfs(int x,int y,int t)
{
    if(x+y>=ans)
        return ;
    if(t>n)
    {
        if(x+y<ans)
            ans=x+y;
        return ;
    }
    int i,temp;
    for(i=1;i<=x;i++)
    {
        if(up[i]<a[t])
            break;
    }
    temp=up[i];
    up[i]=a[t];
    dfs(max(i,x),y,t+1);
    up[i]=temp;
    for(i=1;i<=y;i++)
    {
        if(down[i]>a[t])
            break;
    }
    temp=down[i];
    down[i]=a[t];
    dfs(x,max(i,y),t+1);
    down[i]=temp;
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
            break;
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]),up[i]=0,down[i]=0;
        ans=INF;
        dfs(0,0,1);
        printf("%d\n",ans);
    }
    return 0;
}

  

posted @ 2015-05-15 14:56  waterfull  阅读(282)  评论(0编辑  收藏  举报