首页 > 其他 > 详细

Hdu 4272 LianLianKan(贪心+链表)

时间:2015-08-13 06:31:42      阅读:261      评论:0      收藏:0      [点我收藏+]

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. 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
 

Source
 

题意:给你有n个数的栈,连续的6个数中有两个一样的数可以消除,一次只能消除两个,问最终这个栈的数能不能被消完。


题解:贪心。从头开始,每在6个里面找到两个一样的就消掉,再从头开始,用链表消除数比较方便。这样即使数据是1000个数,最多消500次,每次最多遍历的最后一个节点

          也就是1000,寻找相同数遍历6个数,复杂度为n*n/2*6,即O(3*n*n).

#include <stdio.h>
#include <stdlib.h>
typedef struct T {
    struct T * nxt;
    int a;
} node;
node * head;
int main() {
    int n,m,i,update,cnt;
    node *cur,*nxt,*pre,*spre,*it;
    head=(node *)malloc(sizeof(node));
    while(~scanf("%d",&n)) {
        m=n;
        cur=head;
        while(m--) {
            nxt=(node *)malloc(sizeof(node));
            scanf("%d",&nxt->a);
            cur->nxt=nxt;
            cur=nxt;
        }
        cur->nxt=NULL;
        if(n%2) {
            puts("0");
            continue;
        }
        n/=2;
        update=1;
        while(update) {
            update=0;
            cur=head->nxt;
            pre=head;
            while(cur!=NULL) {
                cnt=5;
                nxt=cur;
                while(cnt--) {
                    spre=nxt;
                    nxt=nxt->nxt;
                    if(nxt==NULL) break;
                    if(cur->a==nxt->a) {
                        update=1;
                        if(cnt==4) {
                            pre->nxt=nxt->nxt;
                        } else {
                            pre->nxt=cur->nxt;
                            spre->nxt=nxt->nxt;
                        }
                        free(cur);
                        free(nxt);
                        cur=pre;
                        break;
                    }
                }
                pre=cur;
                cur=cur->nxt;
            }
        }
        if(head->nxt==NULL)
            puts("1");
        else
            puts("0");
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

Hdu 4272 LianLianKan(贪心+链表)

原文:http://blog.csdn.net/acm_baihuzi/article/details/47477073

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!