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;
    }
  • 相关阅读:
    MySQL 主主同步配置和主从配置步骤
    Nginx 禁止访问某个目录或文件的设置方法
    LNMP笔记:更改网站文件和MySQL数据库的存放目录
    centos最小安装 setuptools安装
    mkdir命令
    linux的mount(挂载)命令详解
    Java中wait()和notify()方法的使用
    剑指 offer面试题22 栈的压入和弹出序列
    剑指 offer面试题20 顺时针打印矩阵
    Leetcode 23.Merge Two Sorted Lists Merge K Sorted Lists
  • 原文地址:https://www.cnblogs.com/KirinSB/p/10010010.html
Copyright © 2011-2022 走看看