HDU-4272 LianLianKan

http://acm.hdu.edu.cn/showproblem.php?pid=4272

据说是状态压缩,+dfs什么什么的,可我这样也过了,什么算法都是浮云 ,暴力才是王道。我也归类为状态压缩,可以用状态压缩来做。

LianLianKan

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2482    Accepted Submission(s): 773

Problem Description

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

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 using namespace std;
 5 int main()
 6 {
 7     int n,j,i,k,a[1005],v[1005];
 8     while(~scanf("%d",&n))
 9     {
10         for(i=1;i<=n;i++)
11           scanf("%d",&a[i]);
12        memset(v,0,sizeof(v));
13        int cnt=0;
14        int flag=0;
15        while(cnt<n)
16        {
17            for(i=1;i<=n;i++)
18            {
19                if(v[i]==1)
20                   continue;
21                   k=0;
22                 for(j=i+1;j<=n&&k<=5;j++)
23                 {
24                     if(v[j]==1)
25                       continue;
26                     if(a[i]==a[j])
27                     {
28                        v[i]=v[j]=1;
29                         break;
30                     }
31                     k++;
32                 }
33            }
34            cnt++;
35        }
36        for(i=1;i<=n;i++)
37         {
38                if(v[i]==0)
39                {
40                    printf("0\n");
41                    break;
42                }
43         }
44         if(i>n)
45           printf("1\n");
46
47     }
48     return 0;
49 }

HDU-4272 LianLianKan

时间: 2024-09-30 10:37:41

HDU-4272 LianLianKan的相关文章

Hdu 4272 LianLianKan(贪心+链表)

LianLianKan Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3080    Accepted Submission(s): 956 Problem Description I like playing game with my friend, although sometimes looks pretty naive. To

HDU 4272 LianLianKan(模拟)

LianLianKan Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3292    Accepted Submission(s): 996 Problem Description I like playing game with my friend, although sometimes looks pretty naive. To

LianLianKan - HDU 4272(状态压缩)

题目大意:有一列数据,可以从最上面的开始连接下面相同的元素,然后消除,不过距离不能超过6,询问最后能不能消除完整个数列. 分析:首先讨论一点最远能消除的地方,比如点的位置是x,如若想要消除x+1位置处的值,那么至少也得在x-4处开始消除,所以x后面最多能有4个被消除,也就是最多能下落4个位置,能够消除的最远距离是x+9,不会超过10个状态,所以可以使用状态压缩做,不过有几点还是要注意的,首先输入的数据是从栈底到栈顶的,其次,判断状态的能否达到的时候要注意判断中间有多少个已经被消除的. 代码如下:

LianLianKan HDU - 4272(状压dp)

题意:就是连连看,有两个相同的就能消除,再加上两个特别的规定,一是只能从栈顶开始消除,而是两个相同的元素之间距离不能超过6,询问能否消除序列中所有元素. 思路:数据水,贪心就能过,但严谨的考虑,贪心显然不能解决所有问题.这题虽然序列很长,但是状态并不复杂,可以使用滚动的状压dp,然后考虑使用多少位表示状态,每一种状态表示第i位为栈首的序列状态,该位前最多能消除掉该位后四位,所以我们只需要表示后9位的状态即可,总计10位. 某位上1表示该位已被消除,0表示未被消除. dp[i][j]表示从i位开始

hdoj 4272 LianLianKan 数据太水

LianLianKan Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2884    Accepted Submission(s): 898 Problem Description I like playing game with my friend, although sometimes looks pretty naive. Tod

hdu 4272 2012长春赛区网络赛 dfs暴力 ***

总是T,以为要剪枝,后来发现加个map就行了 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<queue> 7 #include<map> 8 using namespace std; 9 #define MOD 1000000007 10 const

hdu 4274 Spy&amp;#39;s Work(水题)

Spy's Work Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1266    Accepted Submission(s): 388 Problem Description I'm a manager of a large trading company, called ACM, and responsible for the

hdu 4274 Spy&#39;s Work(水题)

Spy's Work Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1266    Accepted Submission(s): 388 Problem Description I'm a manager of a large trading company, called ACM, and responsible for the

HDU 6203 ping ping ping [LCA,贪心,DFS序,BIT(树状数组)]

题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=6203] 题意 :给出一棵树,如果(a,b)路径上有坏点,那么(a,b)之间不联通,给出一些不联通的点对,然后判断最少有多少个坏点. 题解 :求每个点对的LCA,然后根据LCA的深度排序.从LCA最深的点对开始,如果a或者b点已经有点被标记了,那么continue,否者标记(a,b)LCA的子树每个顶点加1. #include<Bits/stdc++.h> using namespace std;