Decription
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, T. T 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。
问多次操作后能不能将数列变成输入的数列
解题思路:
题意等效于: 给定一个数列,每次可以将数列中连续的长度为3、4或5的子数列中的数字全部-1。问多次操作后能否将数列中的数字全部变为0.
用到 差分数组
先来介绍一下差分数组: 给定一个长度为n的数组a,下标从1到n(a[1]->a[n]),则数组a的差分数组为长度为n+1的数组b,其中b[i] = a[i]-a[i-1] , b[1] = a[1]-0 = a[1]. 可以发现一个长度为n的数组的差分数组的第一个数为该数组的第一个数,差分数组的第2->n个数为该数组第i个数与其前一个数的差。 我们如果把数组a的下标区间[l, r]内的所有数都+x,那么其差分数组b的变化就是第l个数+x,第r+1个数 - x。由于差分数组和原数组是一一对应的,因此我们可以改变区间内数的值可以用操作差分数组来代替操作原数组,时间复杂度由O(n)降到 2。
了解了差分数组后,我们再回来看这个题,我们要把给的数组里的值全部变为0,即将其对应的差分数组的值全部变为0.(第一个数为零且相邻两个数的差都为零,那么所有的数不就都为0了)。
ps: 3 4 5可以组成任意一个>=3的数
一重for从1到n遍历差分数组b,只要发现大于0的数b[l],就从后面找小于0的b[r+1],如果r+1-l > 3,那么就利用这个负数将正数b[l]尽量变为0,如果+b[r+1]后这个正数仍不为0,则继续往后找负数。 一旦发现小于0的数,则无论怎么操作,都必然会导致<=这个数的下标的数组b里的数有<0的。即不可能将给的数列里的数全部变为0。 需要注意的是,b[n+1]可以取-∞,因为b[n+1]并不影响数组a。
Code
#include <iostream>
#include <cmath>
using namespace std;
const int maxn = 2e5+5;
int a[maxn],b[maxn];
int main()
{
int T,n,Case=1;
cin >> T;
a[0] = 0;
bool flag;
while( T--) {
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
b[i] = a[i]-a[i-1];
b[n+1] = -0x3f3f3f3f;
flag = true;
int p=4;
for(int i=1;i<=n;i++) {
while(b[i] > 0) {
while(p<=n+1 && b[p]>=0) p++;
if(p > n+1 || p-i < 3) {
flag = false;
break;
}
int t = min(b[i],abs(b[p]));
b[i] -= t;
b[p] += t;
}
if(b[i] != 0 || !flag) {
flag = false;
break;
}
}
if(flag)
cout << "Case #" << Case++ << ": Yes\n";
else
cout << "Case #" << Case++ << ": No\n";
}
}