• FZU 2216 The Longest Straight 模拟


    题目链接: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 << "===
    ";
    
                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
    ", longest);
        }
        return 0;
    }
    

      

  • 相关阅读:
    终于学会用WinCVS来开源了
    从网上收集EMail(正则表达式,C#源码)
    元宝NewBar发布1.2.0测试版
    使用Gimp切图
    shell编程笔记
    poj2485
    poj 3630 字典树
    杭电oj题目和分类
    强连通分量Kosaraju算法
    随机数的产生
  • 原文地址:https://www.cnblogs.com/icode-girl/p/5356731.html
Copyright © 2020-2023  润新知