zoukankan      html  css  js  c++  java
  • Gym 101775J Straight Master(差分数组)题解

    题意:给你n个高度,再给你1~n每种高度的数量,已知高度连续的3~5个能消去,问你所给的情况能否全部消去;例:n = 4,给出序列1 2 2 1表示高度1的1个,高度2的2个,高度3的2个,高度4的1个。那么我打出1 1 1(高度1 2 3),1 1 1(高度2 3 4)刚好打完。

    思路:对于差分数组我们知道,如果我们对区间L,R加1,那么d[L] += 1, d[R + 1] -= 1,我们可以想象这个序列是由0序列不断进行区间+1操作得到。我们打出差分数组,从左到右遍历,如果遇到正数,那么我们就往右找负数把他消掉,如果这个负数和正数距离小于3那么显然无法构成,其他都可以(大于5时我们可以证出这个距离可以用3 4 5表示);如果遇到负数显然没有正数和他匹配,也不行。

    代码:

    #include<set>
    #include<map>
    #include<stack>
    #include<cmath>
    #include<queue>
    #include<vector>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    typedef long long ll;
    using namespace std;
    const int maxn = 200000 + 10;
    const int seed = 131;
    const ll MOD = 1e9 + 7;
    const int INF = 0x3f3f3f3f;
    ll a[maxn], d[maxn];
    int main(){
        int T, n, Case = 1;
        scanf("%d", &T);
        while(T--){
            scanf("%d", &n);
            a[0] = 0;
            for(int i = 1; i <= n; i++){
                scanf("%lld", &a[i]);
                d[i] = a[i] - a[i - 1];
            }
            d[n + 1] = -a[n];
            int flag = 0;
            int j = 2;
            for(int i = 1; i <= n; i++){
                if(d[i] < 0) flag = 1;
                while(j <= n + 1 && d[i] > 0){
                    if(d[j] < 0){
                        if(j - i < 3) flag = 1;
                        if(d[j] + d[i] < 0){
                            d[j] += d[i];
                            d[i] = 0;
                        }
                        else{
                            d[i] += d[j];
                            d[j] = 0;
                            j++;
                        }
                    }
                    else j++;
                }
                if(d[i] > 0) flag = 1;
                if(flag) break;
            }
            printf("Case #%d: ", Case++);
            if(flag) printf("No
    ");
            else printf("Yes
    ");
        }
        return 0;
    }
  • 相关阅读:
    HDU 1010 Tempter of the Bone
    HDU 4421 Bit Magic(奇葩式解法)
    HDU 2614 Beat 深搜DFS
    HDU 1495 非常可乐 BFS 搜索
    Road to Cinema
    Sea Battle
    Interview with Oleg
    Spotlights
    Substring
    Dominating Patterns
  • 原文地址:https://www.cnblogs.com/KirinSB/p/10010010.html
Copyright © 2011-2022 走看看