Straight Master (贪心)

本文探讨了一种名为'StraightMaster'的扑克游戏策略,玩家需将手中的牌分成长度为3到5的连续序列。文章通过实例分析,提出了一种贪心算法来判断是否能成功分配所有牌,并详细解释了算法的逻辑和实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目如下:
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

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

可以先举例子

1233581056742

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

1233581056742
1111111     
 111111     
  11111     
    222     
     33333  
      22222 
        1111
         111




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

1233581056742
1111111     
 111111     
  11111     
    222     
     33333  
      22222 
        1111
         111
通过简单的观察和逻辑推理  就可以注意到  如果我们 第 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\n",++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\n",++cases);
else 
    printf("Case #%d: No\n",++cases);
}
return 0;
}
 
 

 

转载于:https://www.cnblogs.com/DWVictor/p/10283206.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值