您的位置:首页 > 大数据 > 人工智能

FZU 2216 The Longest Straight 模拟

2016-04-05 20:42 591 查看
题目链接:The Longest Straight

就是一个模拟就是这样,T_T然而当时恶心的敲了好久,敲完就WA了,竟然有这么简单的方法,真是感动哭了.......xintengziji...zhishang...

模拟和暴力也都是有黑科技的..

附黑科技代码:

#include<stdio.h>
#include<queue>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;

const int maxn = 100005;
int vis[maxn];

int main()
{
int t;
scanf("%d", &t);
while (t--)
{
int n, m;
std::queue<int> que;
scanf("%d%d", &n, &m);
memset(vis, 0, sizeof(vis));
for (int i = 0; i < n; ++i)
{
int a;
scanf("%d", &a);
vis[a]++;
}
int joker = vis[0];
int longest = 0;
int nlenght = 0;

for (int i = 1; i <= m; ++i)
{
// cout << i << "===\n";

if (vis[i] == 0) // 当前数字没有出现过
{
if (que.size() < joker) // 还有0可用
{
que.push(i);
++nlenght;
}
else if (!que.empty()) // 除了第一次遍历 保证所有的0在开始计数以前就已经用完了。
{
int j = que.front();
que.pop();
que.push(i);
nlenght = i - j;
}
else if (joker == 0) { // 这种情况只有在joker==0时会出现
nlenght = 0;
}
}
else
{
++nlenght;
}
longest = std::max(longest, nlenght);
}

printf("%d\n", longest);
}
return 0;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: