题干:

A straight is a poker hand containing five cards of sequential rank, not necessarily to be the same suit. For example, a hand containing 7 club, 6 spade, 5 spade, 4 heart and 3 diamond forms a straight. In this problem, we extend the definition of a straight to allow 3 to 5 cards of sequential rank. Hence a hand containing K spade, Q club, and J heart is also a straight.

Mr. Panda is playing a poker game called Straight Master. The game uses a large deck of card that has N ranks from 1 to N. The rule of the game is simple: split the cards in Mr. Panda's hand into several straights of length from 3 to 5.

Now given a hand of cards, can you help Mr. Panda to determine if it is possible to split the cards into straights?

Input

The first line of the input gives the number of test cases, TT test cases follow.

Each test case contains two lines. The first line contains an integer N, indicating the number of ranks in the deck. The next line contains N integers a1, a2, ..., aNindicating the number of cards for each rank in Mr. Panda's hand.

  • 1 ≤ T ≤ 100.
  • 1 ≤ N ≤ 2 × 105.
  • 0 ≤ ai ≤ 109.
  • .

Output

For each test case, output one line containing "Case #x: y", where x is the test case number (starting from 1) and y is Yes if Mr. Panda can split all his cards into straights of length from 3 to 5, or No otherwise.

Example

Input

2
13
1 2 2 1 0 0 0 0 0 0 0 0 0
13
1 1 1 1 0 1 1 0 0 0 0 0 0

Output

Case #1: Yes
Case #2: No

Note

In the first test case, Mr. Panda can split his cards into two straights: [1, 2, 3]and [2, 3, 4]. In the second test case, there is no way to form a straight for card 6and 7.

题目大意:

给定一个长度为n的数列,初始时数列内数字都为0。

有一个操作,可以将数列中连续的长度为3、4或5的子数列中的数字全部+1。

问多次操作后能不能将数列变成输入的数列。
输入第一行包含一个正整数T(1<=T<=100),表示样例组数。

接下来2T行,其中第i行一个数字N(1<=N<=2*10^5)表示数列长度,第i+1行N个数字(0<=a<=10^9)表示最终要变成的数列;
所有N的和不超过4e6.

解题报告:

这题有一个巧妙的地方,也就是,如果你可以将连续长度为3,4,5的子数列的数字都+1,那么这就等价于你可以将任意长度>=3的所有子数列都+1。

所以我们可以求出差分数组,然后枚举每一个>0的数字,然后将它变为0,代价就是将后面从3个字符开始,找到最近的负数,都+上对应的一个数使之变成0.这样操作一遍然后check数组中是否有负数,如果有的话那就GG,否则就是Yes。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
int a[MAX],d[MAX],n;
int main()
{int t,iCase=0;cin>>t;while(t--) {scanf("%d",&n);for(int i = 1; i<=n; i++) scanf("%d",a+i);a[n+1]=0;for(int i = 1; i<=n+1; i++) d[i] = a[i] - a[i-1];int cur = 4,flag = 1;for(int i = 1; i<=n-2; i++) {if(d[i] <= 0) continue; cur = max(cur,i+3);while(d[cur] >= 0 && cur <= n+1) cur++;while(cur <= n+1 && d[i] > 0) {while(d[cur] >= 0 && cur <= n+1) cur++;if(cur > n+1) break;int dc = min(d[i],-d[cur]);//absd[i] -= dc;d[cur] += dc;}if(d[i] > 0) {flag = 0;break;}//这句加不加都可以 想想为什么}for(int i = 1; i<=n+1; i++) {if(d[i] < 0) flag = 0;}if(flag == 1) printf("Case #%d: Yes\n",++iCase);else printf("Case #%d: No\n",++iCase);}return 0 ;
}

对于上面代码中的那个疑惑,其实不难想出,因为我们已经假设,这个序列是左边+1右边-1这样操作最后得到的这样一个序列,所以我们不难发现+1的数量和-1的数量应该是相同的,所以不会出现+1的数量过多,导致后面没有-1可以供选择这样的情况发生。

【Gym - 101775J】Straight Master(差分,思维)相关推荐

  1. Gym - 101775J Straight Master(差分数组)

    题目链接:点击查看 题目大意:给出n种数,每种数有a[i]个,每3-5种连续的数都可以被消去,现在问给出的所有数字最后能否全部消去 题目分析:正难则反,题目问能否将数字全部消去,我们不妨设一开始的起点 ...

  2. Gym 101775J Straight Master(差分数组)题解

    题意:给你n个高度,再给你1~n每种高度的数量,已知高度连续的3~5个能消去,问你所给的情况能否全部消去:例:n = 4,给出序列1 2 2 1表示高度1的1个,高度2的2个,高度3的2个,高度4的1 ...

  3. 2017-2018 ACM-ICPC Asia East Continent League Final J. Straight Master(差分+思维)

    LINK] 首先能每次选择长度为3,4,53,4,53,4,5的区间长度加一 相当于可以让长度大于等于333任意的区间整体加一,因为3,4,53,4,53,4,5可以凑成任意数 但这样还是不好写,考虑 ...

  4. CH - 0304 IncDec Sequence(差分+思维)

    题目链接:点击查看 题目大意:给定一个长度为 n(n≤10^5 ) 的数列 {a_1,a_2,-,a_n},每次可以选择一个区间 [l,r],使下标在这个区间内的数都加一或者都减一.求至少需要多少次操 ...

  5. 100. 增减序列【差分 / 思维】

    本题考察的是差分的思想. 设 b[i]=a[i]-a[i-1] 因为我们要想让其所有的数字都相等.故即b[2],b[3].......b[n]都得为0才行. 此时数组所有的数的值都是b[1]. 因为我 ...

  6. CodeForces - 1000C Covered Points Count(差分+思维)

    题目链接:点击查看 题目大意:给出n个区间,现在要求输出覆盖次数为1,2,3....n-1,n的点分别有多少个 题目分析:一开始看到区间问题想用线段树去做,但想了想又可以直接用差分去做,不过因为数比较 ...

  7. Codeforces Round #590 (Div. 3) E. Special Permutations 差分 + 思维

    传送门 文章目录 题意: 思路: 题意: 思路: 直接考虑比较难想,这种公式题基本都是将部分答案看成一个整体,考虑xi,xi+1x_i,x_{i+1}xi​,xi+1​的贡献的. 假设当前的xi=x, ...

  8. AT2005-[AGC003E]Sequential operations on Sequence【差分,思维】

    正题 题目链接:https://www.luogu.com.cn/problem/AT2005 题目大意 开始有一个1∼n1\sim n1∼n依次排列的序列,然后QQQ次,第iii次把序列长度变为ai ...

  9. 101. 最高的牛【差分 思维】

    分析后发现,不会重叠,故都是互相嵌套的这种方式. 我们可以初始化,所有的身高都是最大的身高,对于每个区间.区间内的数减1. 需要注意的是需要去重,避免重复减. #include<bits/std ...

最新文章

  1. Spring入门详细教程(一)
  2. Xcode7查看iphone真机的应用程序文件
  3. python哪些类型可以作为迭代器_python0.8-----set类型与迭代器
  4. idea启动tomcat时蓝屏
  5. oracle的env函数用法,env命令_Linux env 命令用法详解:显示系统中已存在的环境变量...
  6. mysql 数据库安装命令_数据库mysql安装及最基本命令
  7. python源文件改写编写一个程序读取一个python源程序文件_学PYTHON第三节:编译和解释...
  8. iOS开发遇到的坑之五--解决工程已存在plist表,数据却不能存入的问题
  9. 2020-2-6 蓝桥杯阶段模拟总结
  10. 分形理论的Hausdorff维数
  11. CST软件基本操作—1
  12. Centos7如何编译安装vim8
  13. 【AWS云从业者基础知识笔记】——模块1:AWS服务简介
  14. Unirech:阿里云国际版怎么获得免费试用的机会以及注册流程
  15. 互动直播之WebRTC服务器Kurento实战
  16. js 和php 互操cookie 作用域
  17. 通俗易懂的LHS和RHS
  18. gitlab编译安装史----虽败犹荣
  19. 取消Outlook脱机工作
  20. archlm检验python,Python sublime.arch方法代码示例

热门文章

  1. 腾讯或联姻优酷,微信嫁女模式引发互联网通婚潮流
  2. sql 替换text字段中的指定字符
  3. 2008秋季-计算机软件基础-未交实验报告名单
  4. [密码学基础][每个信息安全博士生应该知道的52件事][Bristol Cryptography][第50篇]什么是BLS基于Weil对的签名方案?
  5. [剑指offer][JAVA][第62题][约瑟夫环][LinkedList vs ArrayList]
  6. web前端开发——HTML学习
  7. ext4fs error mysql_请教一个 Linux 挂在磁盘阵列报错 EXT4-fs error 问题
  8. python内存模型_内存篇3:CPython的内存管理架构-L2-块
  9. VS集成Qt开发入门(简易时间显示)
  10. python选取tensor某一维_Pytorch的Tensor操作(1)