HDU - 4272 (greedy analog or compressed state dp)

Topic: the Click
meaning of the questions: a stack from top to bottom, the top element and he can contact the following five elements, if a value of the top element of the same then these two elements disappeared, asked whether empty stack.

1. see a range of n, consider a map simulate what can be AC.
2. The idea of dynamic programming to do with the state compression, you will find that we need to consider only the largest sequence number i following nine elements, because the following four elements can be on top of the index i elements of elimination. 2 can be represented by not erasing 10,0 ^, 1 is erased.
DP [i] [j] indicates whether the j state i when the stack is the following nine elements can occur.
Attach both tags:
1:

#include<cmath>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<istream>
#include<vector>
#include<stack>
#include<map>
#include<algorithm>
#include<queue>
#define MAX_len 50100*4
using namespace std;
typedef long long ll;
int a[200100];
int n;
int main()
{
    while(~scanf("%d",&n))
    {
        int i,j,k;
        map<int,int>book;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        for(i=n;i>=1;i--)
        {
            if(book[i])
                continue;
            int sum=0;
            j=i+1;
            while(sum<=5&&j<=n)
            {
                if(book[j])
                {
                    j++;
                    continue;
                }
                sum++;
                if(a[j]==a[i])
                {
                    book[i]=1;
                    book[j]=1;
                    break;
                }
                j++;
            }
            sum=0;
            j=i-1;
            while(sum<=5&&j>=1)
            {
                if(book[j])
                {
                    j--;
                    continue;
                }
                sum++;
                if(a[j]==a[i])
                {
                    book[i]=1;
                    book[j]=1;
                    break;
                }
                j--;
            }
        }
        for(i=1;i<=n;i++)
        {
            if(book[i])
                continue;
            else
                break;
        }
        if(i==n+1)
            printf("1\n");
        else
            printf("0\n");
    }
    return 0;
}`
 

2:

#include<cmath>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<istream>
#include<vector>
#include<stack>
#include<set>
#include<map>
#include<algorithm>
#include<queue>
#define MAX_len 50100*4
using namespace std;
typedef long long ll;
int a[1010];
int dp[1010][1030];// 栈顶为i 状态为j 的情况可以发生否
void dfs(int n,int vis)
{
    int i,j;
    int cnt=0;
    for(i=9;i>=0;i--)
    {
        if(!(vis&(1<<i)))//表示没有消去可能可以与栈顶n看看匹配
        {
            cnt++;
            if(a[n]==a[n+9-i+1])
            {
                dp[n][vis|(1<<i)]=1;
            }
        }
        if(cnt>=5)
        {
            break;
        }
    }
}
int main()
{
    int n,i,j,k;
    while(~scanf("%d",&n))
    {
        for(i=n;i>=1;i--)
            scanf("%d",&a[i]);
        memset(dp,0,sizeof(dp));
        dp[0][0]=1;
        for(i=1;i<=n;i++)
        {
            for(j=0;j<1024;j++)
            {
                if(!dp[i-1][j])
                    continue;
                if(j&(1<<9))// 表示i已经被消去
                {
                    dp[i][((j-(1<<9)))<<1]=1;//表示这个状态可以延续
                }
                else
                    dfs(i,j<<1);
            }
        }
        printf("%d\n",dp[n-1][(1<<9)]);
    }
	return 0;
}
Published 72 original articles · won praise 19 · views 7499

Guess you like

Origin blog.csdn.net/weixin_43958964/article/details/104639862