LianLianKan(HDU-4272)(状压DP)

I like playing game with my friend, although sometimes looks pretty naive. Today I invent a new game called LianLianKan. The game is about playing on a number stack.
Now we have a number stack, and we should link and pop the same element pairs from top to bottom. Each time, you can just link the top element with one same-value element. After pop them from stack, all left elements will fall down. Although the game seems to be interesting, it's really naive indeed.


To prove I am a wisdom among my friend, I add an additional rule to the game: for each top element, it can just link with the same-value element whose distance is less than 6 with it.
Before the game, I want to check whether I have a solution to pop all elements in the stack.

Input

There are multiple test cases.
The first line is an integer N indicating the number of elements in the stack initially. (1 <= N <= 1000)
The next line contains N integer ai indicating the elements from bottom to top. (0 <= ai <= 2,000,000,000)

Output

For each test case, output “1” if I can pop all elements; otherwise output “0”.

Sample Input

2
1 1
3
1 1 1
2
1000000 1

Sample Output

1
0
0

Sponsor

 

题意:现在我们有了一个数字堆栈,我们应该从上到下链接并弹出相同的元素对。每次,您都可以将顶部元素与一个相同值的元素链接起来。将它们从堆栈中取出后,所有剩下的元素都会掉落。而且,每次可以选择和栈顶一样的数字,并且和栈顶距离小于6,然后同时消去他们,问能不能把所有的数消去。

思路:这道题的话,一个数字最远能消去和他相距9的数,因为中间4个可以被他上面的消去。因为还要判断栈顶有没有被消去,所以10位dp。dp[i][j]表示第i个栈顶状态为j能否存在,用1表示某位被消去。那么直接状压DP,假如被消去了则dp[i + 1][j >> 1] = 1。

AC代码:

#include <bits/stdc++.h>
typedef long long ll;
const int maxx=1010;
const int inf=0x3f3f3f3f;
using namespace std;
int dp[maxx][1<<10];
int a[maxx];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=n; i>=1; i--)
            scanf("%d",&a[i]);
        if(n&1)
            printf("0\n");
        else
        {
            int b;
            memset(dp,0,sizeof(dp));
            dp[1][0]=1;
            for(int i=1; i<=n; i++)
            {
                b=min(10,n-i+1);
                for(int j=0; j<(1<<b); j++)
                {
                    if(dp[i][j]==0)
                        continue;
                    if(j&1)
                        dp[i+1][j>>1]=1;
                    else
                    {
                        int ans=0;
                        for(int k=1; k<b; k++)
                        {
                            if(!((1<<k)&j))
                            {
                                ans++;
                                if(ans<6 && a[i]==a[i+k])
                                {
                                    int cnt=j|(1<<k);
                                    dp[i+1][cnt>>1]=1;
                                }
                            }
                        }
                    }
                }
            }
            printf("%d\n",dp[n][1]);
        }
    }
    return 0;
}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值