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?

he 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.
  • .

 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.




题目大意就是  给好多堆纸牌  我们一次可以在 相邻的三堆各取一张纸牌   或者相邻的四堆中各取一张纸牌  或者五堆中各取一张纸牌  问能不能 全部取完

数据量也比较大  贪心简单做


在做这个题目前  要先搞懂一件事情  就是3 4 5可以拼成任何大于三的数字 比如 7=4+3 9=3+3+3

贪心策略赶紧并不是很好想

可以先举例子

1 2 3 3 5 8 10 5 6 7 4 2

我在这里放了11堆的例子  这11堆安照题目中的规则是可以取完的 
  
为了证明他是可以取完的 我做了下面这串表 在下面中的每一行中 都有数目不少于3个的相同数字
刚才也说过 3 4 5可以组合成任何 不小于3的数字  也就是下面的每一行的个数我都可以用3 4 5获得

1 2 3 3 5 8 10 5 6 7 4 2
1 1 1 1 1 1 1          
  1 1 1 1 1 1          
    1 1 1 1 1          
        2 2 2          
          3 3 3 3 3    
            2 2 2 2 2  
                1 1 1 1
                  1 1 1




我将上面堆 数字做下处理 如下  相邻的相同颜色的几堆代表这 被取走的几堆 数值代表取多少次

1 2 3 3 5 8 10 5 6 7 4 2
1 1 1 1 1 1 1          
  1 1 1 1 1 1          
    1 1 1 1 1          
        2 2 2          
          3 3 3 3 3    
            2 2 2 2 2  
                1 1 1 1
                  1 1 1
通过简单的观察和逻辑推理  就可以注意到  如果我们 第 i 堆 比 第 i-1 堆 要大的话 那么大出来的那部分一定是 某次取牌的最左端的位置
如果 第 i 堆 比 第 i-1 堆小   就不能确定了  但是呢  第 i  堆 要满足一个条件  就是不能断了 前两堆  产生的新最左端位置  意思就是 第i堆的数目一定要 不小于  前三堆正差的和  否则会产生 一个断层就 无解了  在这一堆的结束位置也要满足一个条件  就是 倒数第二堆不能产生新的左端点  如果产生 就要n+1给补上  显然是不可以的  还有就是 倒数第三堆 产生的 左端点 一定要 由末尾补上 否则 也是无解
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn = 2e5+7;
int a[maxn],b[maxn],flag;
int main()
{
int T,n,cases=0;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
n++;
flag=0;
for(int i=2;i<=n;i++){
scanf("%d",a+i);
 
b[i]=max(a[i]-a[i-1],0);
}
if(n<=3){
printf("Case #%d: No
",++cases);
continue;
}
   for(int i=2;i<=n;i++){
   
if(a[i]<b[i-1]+b[i-2])
flag=1;
}
if(!(b[n]==0&&b[n-1]==0&&a[n]>=b[n-2]))
{
   flag=1;
}
if(!flag)
    printf("Case #%d: Yes
",++cases);
else 
    printf("Case #%d: No
",++cases);
}
return 0;
}
 
 
原文地址:https://www.cnblogs.com/DWVictor/p/10283206.html