日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2023/12/10 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【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,?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。

有一個操作,可以將數列中連續(xù)的長度為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.

解題報告:

這題有一個巧妙的地方,也就是,如果你可以將連續(xù)長度為3,4,5的子數列的數字都+1,那么這就等價于你可以將任意長度>=3的所有子數列都+1。

所以我們可以求出差分數組,然后枚舉每一個>0的數字,然后將它變?yōu)?,代價就是將后面從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這樣操作最后得到的這樣一個序列,所以我們不難發(fā)現+1的數量和-1的數量應該是相同的,所以不會出現+1的數量過多,導致后面沒有-1可以供選擇這樣的情況發(fā)生。

總結

以上是生活随笔為你收集整理的【Gym - 101775J】Straight Master(差分,思维)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。