【Gym - 101775J】Straight Master(差分,思维)

题干:

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可以供选择这样的情况发生。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/439329.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【转】Azure DevOps —— Azure Board 之 长篇故事、特性、用户情景(故事)的用法应用场景

前提 我以前在之前的文章里大概介绍了 Azure Board 的基本使用&#xff0c;可以回看《Azure Board 的基本使用》。如果你想使用 Azure Board 来安排工作的话&#xff0c;请提前了解《敏捷开发》的相关知识。 作者将使用 “Agile” 作为项目的模板&#xff0c;不明白的先阅读《…

【VIJOS - P1037】搭建双塔(dp)

题干&#xff1a; 描述 2001年9月11日&#xff0c;一场突发的灾难将纽约世界贸易中心大厦夷为平地&#xff0c;Mr. F曾亲眼目睹了这次灾难。为了纪念“9?11”事件&#xff0c;Mr. F决定自己用水晶来搭建一座双塔。 Mr. F有N块水晶&#xff0c;每块水晶有一个高度&#xff0…

【转】10分钟精通SharePoint - VS开发模板

Visual Studio是SharePoint开发的最锋利的利器&#xff08;没有之一哦&#xff09;&#xff0c;是必不可少的开发工具。VS一直为SharePoint开发保驾护航。 提供了以下功能开发模板&#xff1a; WebPart &#xff1a;服务器端控件 SharePoint中最常用的展示控件之一&#xff…

【Tyvj - 1305】最大子序和(单调队列优化dp)

题干&#xff1a; 输入一个长度为n的整数序列&#xff0c;从中找出一段不超过M的连续子序列&#xff0c;使得整个序列的和最大。 例如 1&#xff0c;-3,5,1&#xff0c;-2,3 当m4时&#xff0c;S51-237 当m2或m3时&#xff0c;S516 输入格式 第一行两个数n,m 第二行有n个数&…

【转】SharePoint 2010 用户自定义编辑Meta标签的OOB方法

今天遇到一个小问题&#xff0c;客户想要动态定制页面的<Head>标签里Meta, 而且需要使用OOB. 考虑了一下&#xff0c;Meta 标签肯定是用来让Server Crawl的, 那JS的方法首先排除&#xff0c;那就只能用服务器控件了, 在做了一些测试后&#xff0c;Field Control 可以帮我…

【计蒜客 - 程序设计竞赛】商业信息共享(Tarjan缩点)

题干&#xff1a; 商业信息共享 有 N 个公司&#xff0c;从每个公司都能单向地向另外一个公司分享最新商业信息&#xff0c;因为他们之间有着某种合作&#xff0c;你需要解决两个问题&#xff1a; 现在有一个最新的商业信息&#xff0c;至少需要告诉多少个公司&#xff0c;使…

【转】SharePoint 编程指南

网站相关的对象模型 作为一个应用原型系统&#xff0c;SharePoint提供了很多强大的应用及管理功能&#xff0c;但是&#xff0c;在实际的应用中&#xff0c;这些原有的功能很难满足用户的需求。因此&#xff0c;SharePoint也提供了一套非常完整的对象模型作为底层接口&#xf…

Mr. Kitayuta‘s Technology CodeForces - 505D(并查集+拓扑排序或dfs找环) 题解

题目 Shuseki Kingdom is the world’s leading nation for innovation and technology. There are n cities in the kingdom, numbered from 1 to n. Thanks to Mr. Kitayuta’s research, it has finally become possible to construct teleportation pipes between two ci…

二进制枚举子集 CS Maxor 或运算,DP(SOS)

https://blog.csdn.net/noone0/article/details/78289517 目前没有题目链接。 题意:长度为n的序列a,选出两个元素,其或运算结果的最大值为多少,并求出a[i]|a[j]mx的方案数? n<1e5,0<a[i]<2^17,m<17. 假如最大值为mx,若x|ymx 则x和y肯定为mx的子集.否则或运算结果…

【转】SharePoint 中的编程模型

可以通过多种方式开发针对 SharePoint 平台的应用程序。可以基于以下各项将这些应用程序划分下列组中&#xff1a;用于创建应用程序的工具、用于开发应用程序的编程模型、打包和部署应用程序的方法、将应用程序投入市场的方式以及运行应用程序的设备。 SharePoint 框架 ShareP…

【转】SharePoint 术语表

术语表 术语定义访问控制条目 安全对象的自由访问控制列表 (DACL) 或对象的系统访问控制列表 (SACL) 中的一项。在 DACL 中&#xff0c;该项向用户或组授予权限或者拒绝向用户或组授予权限。在 SACL 中&#xff0c;该项指定审核特定用户或组的哪些安全事件或者控制对象的 Wind…

【HDU - 5890】Eighty seven(bitset优化背包)

题干&#xff1a; Mr. Fib is a mathematics teacher of a primary school. In the next lesson, he is planning to teach children how to add numbers up. Before the class, he will prepare NN cards with numbers. The number on the ii-th card is aiai. In class, eac…

【HDU - 1937 】Finding Seats(二维前缀和+尺取法)

题干&#xff1a; A group of K friends is going to see a movie. However, they are too late to get good tickets, so they are looking for a good way to sit all nearby. Since they are all science students, they decided to come up with an optimization problem …

【POJ - 1459】Power Network(网络流最大流,建图)

题干&#xff1a; A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied with an amount s(u) > 0 of power, may produce an amount 0 < p(u) < p max(u) of power, may …

【转】React Vue MVC MVVM MVP

首先&#xff0c;在谈这个话题之前&#xff0c; 我们有必要了解一下库和框架的区别。 我们先来看react官网以及vue官网对他们的定位&#xff1a; react: vue: react我们不说了&#xff0c;官网上明明白白说了&#xff0c;人家是一个library&#xff0c;用于构建用户界面。 v…

**【POJ - 3122】 Pie(二分寻值)

题干&#xff1a; My birthday is coming up and traditionally Im serving pie. Not just one pie, no, I have a number N of them, of various tastes and of various sizes. F of my friends are coming to my party and each of them gets a piece of pie. This should b…

【转】IsCallBack属性和IsPostBack属性有什么区别?

if (Page.IsCallback) return; 此句话在page的构造函数中使用&#xff0c;不让page反复生成。比如一个TEXTbox如果不组织页面刷新&#xff0c;其数据会丢失。 以postback方式进行客户端和服务器端的交互的&#xff0c; IsPostBack就是true。 以callback方式进行客户端和服务器…

【转】使用Feature导入WebPart

原文链接&#xff1a;http://www.cnblogs.com/glife/archive/2009/10/27/1590488.html 前些天在刚刚接触WebPart的时候&#xff0c;搜到了一篇《使用Feature导入WebPart》的文章&#xff0c;那个时候对Feature的了解还为零&#xff0c;所以看了是一知半解&#xff0c;等到今天…

【HDU - 5017】Ellipsoid(爬山算法,模拟退火,三分)

题干&#xff1a; Given a 3-dimension ellipsoid(椭球面) your task is to find the minimal distance between the original point (0,0,0) and points on the ellipsoid. The distance between two points (x 1,y 1,z 1) and (x 2,y 2,z 2) is defined as Input There a…

【转】[SharePoint 开发详解] 一个Feature中使用SPGridView的几个Tips

根据上面一篇随笔所介绍的PC购买流程的项目&#xff0c;在项目中&#xff0c;需要有一个生成订单的功能&#xff0c;能够使得Admin很方便的在获得批准的申请中选取一些来生成订单&#xff0c;要求界面操作简单明了&#xff0c;大概的效果图如下&#xff1a; 点击checkbox&#…