我是靠谱客的博主 清脆银耳汤,最近开发中收集的这篇文章主要介绍hdoj 4272 LianLianKan 【树状数组】,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

题目链接:hdoj 4272 LianLianKan

LianLianKan

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

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

题意:有一个栈,每次新元素入栈,可以将与它距离小于6的相同元素消掉。问你最后能不能消去所有的元素。

思路:模拟,初始元素置1,每次消去的元素置0,查询距离统计区间和。用个树状数组维护就好了。

AC代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#define CLR(a, b) memset(a, (b), sizeof(a))
using namespace std;
typedef long long LL;
const int MAXN = 1e6 + 10;
const int MOD = 1e9 + 7;
int a[1010];
bool vis[1010];
int n;
int lowbit(int x) {
    return x & (-x);
}
int C[1010];
void Update(int x, int d) {
    while(x <= n) {
        C[x] += d;
        x += lowbit(x);
    }
}
int Sum(int x) {
    int s = 0;
    while(x > 0) {
        s += C[x];
        x -= lowbit(x);
    }
    return s;
}
int main()
{
    while(scanf("%d", &n) != EOF) {
        CLR(C, 0);
        for(int i = 1; i <= n; i++) {
            scanf("%d", &a[i]);
            vis[i] = false; Update(i, 1);
        }
        for(int i = 2; i <= n; i++) {
            for(int j = 1; j < i; j++) {
                if(!vis[j] && a[j] == a[i] && Sum(i) - Sum(j-1) < 6) {
                    vis[j] = vis[i] = true;
                    Update(j, -1); Update(i, -1);
                    break;
                }
            }
        }
        bool flag = true;
        for(int i = 1; i <= n; i++) {
            if(vis[i] == false) {
                flag = false; break;
            }
        }
        printf(flag ? "1n" : "0n");
    }
    return 0;
}

最后

以上就是清脆银耳汤为你收集整理的hdoj 4272 LianLianKan 【树状数组】的全部内容,希望文章能够帮你解决hdoj 4272 LianLianKan 【树状数组】所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(60)

评论列表共有 0 条评论

立即
投稿
返回
顶部