FZU-2216 The Longest Straight(尺取法)
Problem 2216 The Longest StraightAccept: 523 Submit: 1663
Time Limit: 1000 mSec Memory Limit : 32768 KB
Problem Description
ZB is playing a card game where the goal is to make straights. Each card in the deck has a number between 1 and M(including 1 and M). A straight is a sequence of cards with consecutive values. Values do not wrap around, so 1 does not come after M. In addition to regular cards, the deck also contains jokers. Each joker can be used as any valid number (between 1 and M, including 1 and M).
You will be given N integers card[1] .. card[n] referring to the cards in your hand. Jokers are represented by zeros, and other cards are represented by their values. ZB wants to know the number of cards in the longest straight that can be formed using one or more cards from his hand.
Input
The first line contains an integer T, meaning the number of the cases.
For each test case:
The first line there are two integers N and M in the first line (1 <= N, M <= 100000), and the second line contains N integers card[i] (0 <= card[i] <= M).
Output
For each test case, output a single integer in a line -- the longest straight ZB can get.
Sample Input
7 11
0 6 5 3 0 10 11
8 1000
100 100 100 101 100 99 97 103
Sample Output
3
Source
第六届福建省大学生程序设计竞赛-重现赛(感谢承办方华侨大学)
你有n张牌在1~~m之间取值,0可以表示成任意一张牌(也就是王牌),
现在问你你手里的牌最长有几张牌是连续的。
思路:
记录卡牌的值,和零的值,求出1至M之间缺少的个数是否小于等于零的个数,求出最大值,
队友竟然用了尺取法!
代码
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
int main()
{
std::ios::sync_with_stdio(false);
int n,m,t,i,j,l,r,a,num[100001],b[100001],ans;
cin>>t;
while(t--)
{
ans=0;
memset(num,0,sizeof(num));
memset(b,0,sizeof(b));
cin>>n>>m;
for(i=0;i<n;i++)
{
cin>>a;
num[a]++;
}
ans=0;
for(i=1;i<=m;i++)
if(num[i]==0)b[i]=b[i-1]+1;else b[i]=b[i-1];
int l=0,r=0;
while(r<=m)
{
if(b[r]-b[l]<=num[0])
{
ans=max(ans,r-l);
r++;
}
else l++;
}
cout<<ans<<endl;
} }
FZU-2216 The Longest Straight(尺取法)的更多相关文章
- FZU 2216——The Longest Straight——————【二分、枚举】
Problem 2216 The Longest Straight Accept: 17 Submit: 39Time Limit: 1000 mSec Memory Limit : 32 ...
- FZU 2216 The Longest Straight(最长直道)
Description 题目描述 ZB is playing a card game where the goal is to make straights. Each card in the dec ...
- FZU 2216 The Longest Straight 模拟
题目链接:The Longest Straight 就是一个模拟就是这样,T_T然而当时恶心的敲了好久,敲完就WA了,竟然有这么简单的方法,真是感动哭了.......xintengziji...zhi ...
- FZU 2216 The Longest Straight 二分
0可以表示任何1到m的数,求一个最长的连续上升序列长度 因为m的范围在10w,所以以每个节点为起点 进行二分,复杂度mlogm 思路:b[i]表示到 1 到 i 有几个数没有出现,二分的时候注意加等号 ...
- The Longest Straight(FZUoj2216)
Problem 2216 The Longest Straight Accept: 82 Submit: 203Time Limit: 1000 mSec Memory Limit : ...
- The Longest Straight(二分,离散化)
Problem 2216 The Longest Straight Accept: 7 Submit: 14 Time Limit: 1000 mSec Memory Limit : 3 ...
- Codeforces Educational Codeforces Round 5 D. Longest k-Good Segment 尺取法
D. Longest k-Good Segment 题目连接: http://www.codeforces.com/contest/616/problem/D Description The arra ...
- HDU 4123 (2011 Asia FZU contest)(树形DP + 维护最长子序列)(bfs + 尺取法)
题意:告诉一张带权图,不存在环,存下每个点能够到的最大的距离,就是一个长度为n的序列,然后求出最大值-最小值不大于Q的最长子序列的长度. 做法1:两步,第一步是根据图计算出这个序列,大姐头用了树形DP ...
- A - Longest k-Good Segment (尺取法)
题目链接: https://cn.vjudge.net/contest/249801#problem/A 解题思路:尺取法,每次让尺子中包含k种不同的数,然后求最大. 代码: #include< ...
随机推荐
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目4
2014-04-23 18:17 题目:设计一个停车位的类. 解法:停车位,就要有停车.取车的功能了.另外我还加了一个工作线程用于计费,每秒给那些有车的车位加1块钱费用. 代码: // 8.4 Des ...
- Flash文件在asp页面无法播放,网页上面的Flash文件在火狐浏览器不播放
第一个问题:Flash文件放到asp页面以后无法播放. 解决方法:用浏览器打开页面->F12,选择Network,如下图: 然后刷新页面,如下图: 点击左侧状态是404的文件,如图: 可以发现F ...
- USACO Section1.5 Prime Palindromes 解题报告
pprime解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...
- appium+python的APP自动化(1)
写这个东西也是自己喜欢研究些自动化的东西,以下全是自己的经验所得,由于开源的软件对于各版本以及操作系统要求很高,会经常碰到一些不兼容的问题,这个都属于正常的,换版本就对了. 本人的环境搭建都是在win ...
- 【Python-遇到的Error】AttributeError: 'str' object has no attribute 'input_text'
学习类的实例化的时候遇到了AttributeError: 'str' object has no attribute 'input_text', 以下是报错的代码及修改正确的代码. class shu ...
- virt-install command
安装 virt-install --connect qemu:///system \ --virt-type=kvm \ --name windows2008 --ram --vcpus --arch ...
- Spring 笔记(四)AOP
前言 横切关注点 使用 @AspectJ 定义切面. 同时还需要在配置类上应用 @EnableAspectJAutoProxy 注解,启用 AOP 自动代理.(不添加它的话,@AspectJ 注解的类 ...
- 新浪微博 page应用 自适应高度设定 终于找到解决方法
我做的是PAGE应用,无法自适应高度.找了好久解决方法. 用js 设置父窗口 iframe 也不好用,有的浏览器不兼容. 官方上说发是这样的: 应用动态高度自适应 Iframe高度:开发者可以使Ifr ...
- Python中的多线程编程,线程安全与锁(二)
在我的上篇博文Python中的多线程编程,线程安全与锁(一)中,我们熟悉了多线程编程与线程安全相关重要概念, Threading.Lock实现互斥锁的简单示例,两种死锁(迭代死锁和互相等待死锁)情况及 ...
- P4160 [SCOI2009]生日快乐
题目描述 windy的生日到了,为了庆祝生日,他的朋友们帮他买了一个边长分别为 X 和 Y 的矩形蛋糕. 现在包括windy,一共有 N 个人来分这块大蛋糕,要求每个人必须获得相同面积的蛋糕. win ...