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\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

时间: 2024-10-22 21:51:24

Straight Master (贪心)的相关文章

2017 ECL-FINAL J.Straight Master

题目链接:http://codeforces.com/gym/101775/problem/J 思路:序列差分一下,然后用得到的查分序列乱搞就可以了 注意差分序列第一项等于a[i],之后n-1项为cha[i]=a[i]-a[i-1],第n+1项为0-a[n] 1 #include<bits/stdc++.h> 2 using namespace std; 3 4 typedef long long ll; 5 const int mod=1e9+7; 6 const int maxn=2e5+

2017-2018 ACM-ICPC Asia East Continent League Final (ECL-Final 2017) 个人题解

题面:https://codeforces.com/gym/101775 A. Chat Group #include <iostream> #include <cstring> using namespace std; typedef long long ll; const int maxn = 1e6+10; const ll mod = 1000000007; int T; ll N,K; ll ksm(ll a,ll b){ ll res = 1; while(b){ if

poj1064 cable master(最大值问题:二分+贪心)

题意: 有n条电缆,他们的长度分别为l[i].如果从n条电缆中切割出K条长度相同的电缆的话,这k条电缆每条最长能多长?答案小数点后保留两位有效数字. 输入: n, k n行:l[i] Sample Input 4 11 8.02 7.43 4.57 5.39 Sample Output 2.00 数据范围: 1<=N<=10000; 1<=k<=10000; 1<=l[i]<=100000. 分析: 设命题:can(x)=能切割出k条长度为x的电缆. 问题转化:求can

Fishing Master HDU - 6709 (贪心)acm

Heard that eomeom is a fishing MASTER, you want to acknowledge him as your mentor. As everybody knows, if you want to be a MASTER's apprentice, you should pass the trial. So when you find fishing MASTER eomeom, the trial is as follow: There are nn fi

【Fishing Master HDU - 6709 】【贪心】

题意分析 题意:题目给出n条鱼,以及捕一条鱼所用的时间k,并给出煮每一条鱼的时间,问抓完并煮完所有鱼的最短时间. 附题目链接 思路: 1.捕第一条鱼的时间是不可避免的,煮每条鱼的时间也是不可避免的,这些都要算上. 2.可以优化的是煮鱼的时间,在时间允许的范围内可进行捕其他鱼.当然煮鱼的时间也许不够捕其他鱼,这就需要增加额外的时间. 3.设在煮每条鱼煮的时间内抓的最多的鱼数为cnt,捕鱼的时间为cost,将每次额外增加的时间存储在一个数组中,记为fre[ ]. 4.更多详细信息在代码中给出. AC

Fishing Master (思维+贪心)

题目网站:http://acm.hdu.edu.cn/showproblem.php?pid=6709 Problem Description Heard that eom is a fishing MASTER, you want to acknowledge him as your mentor. As everybody knows, if you want to be a MASTER’s apprentice, you should pass the trial. So when yo

codeforces Gym 100187F F - Doomsday 区间覆盖贪心

F. Doomsday Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/F Description Doomsday comes in t units of time. In anticipation of such a significant event n people prepared m vaults in which, as they think, it will

Saruman&#39;s Army(贪心)

 Saruman's Army Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3069 Description Saruman the White must lead his army along a straight path from Isengard to Helm's Deep. To keep track of his f

HDU 4004 The Frog&#39;s Games(基本算法-贪心,搜索-二分)

The Frog's Games Problem Description The annual Games in frogs' kingdom started again. The most famous game is the Ironfrog Triathlon. One test in the Ironfrog Triathlon is jumping. This project requires the frog athletes to jump over the river. The