zoukankan      html  css  js  c++  java
  • Unique Snowflakes UVA

    Emily the entrepreneur has a cool business idea: packaging and selling snowflakes. She has devised a machine that captures snowflakes as they fall, and serializes them into a stream of snowflakes that flow, one by one, into a package. Once the package is full, it is closed and shipped to be sold. The marketing motto for the company is “bags of uniqueness.” To live up to the motto, every snowflake in a package must be different from the others. Unfortunately, this is easier said than done, because in reality, many of the snowflakes flowing through the machine are identical. Emily would like to know the size of the largest possible package of unique snowflakes that can be created. The machine can start filling the package at any time, but once it starts, all snowflakes flowing from the machine must go into the package until the package is completed and sealed. The package can be completed and sealed before all of the snowflakes have flowed out of the machine. Input The first line of input contains one integer specifying the number of test cases to follow. Each test case begins with a line containing an integer n, the number of snowflakes processed by the machine. The following n lines each contain an integer (in the range 0 to 109 , inclusive) uniquely identifying a snowflake. Two snowflakes are identified by the same integer if and only if they are identical. The input will contain no more than one million total snowflakes. Output For each test case output a line containing single integer, the maximum number of unique snowflakes that can be in a package.

    Sample Input 1 5 1 2 3 2 1 Sample Output 3

    题意:求没有重复数字的最长区间.

    思路:由于数组中元素的范围太大,上限为1e9,但是最多只有1e5个数,所以我们需要离散化一下,也可以不用离散化,直接用map进行标记。

    然后快乐的尺取法。

    我们维护一个区间,[ l , r ] ,使这个区间里的元素总是没有重复的,

    当R向右移动后,如果有重复的就把L一直向右推,直到区间里没有重复元素,(因为[L,R] 不符合条件,那么[L,R+1]一定也不符合,所以要改变的是L,)

    每一次符合条件后就立即停止L的右推,更新答案,然后移动R,

    总体时间复杂度O(n*long n+ n

    细节见我的AC代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <vector>
    #define sz(a) int(a.size())
    #define all(a) a.begin(), a.end()
    #define rep(i,x,n) for(int i=x;i<n;i++)
    #define repd(i,x,n) for(int i=x;i<=n;i++)
    #define pii pair<int,int>
    #define pll pair<long long ,long long>
    #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
    #define MS0(X) memset((X), 0, sizeof((X)))
    #define MSC0(X) memset((X), '', sizeof((X)))
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define eps 1e-6
    #define gg(x) getInt(&x)
    #define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
    using namespace std;
    typedef long long ll;
    ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
    ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
    inline void getInt(int* p);
    const int maxn=1000010;
    const int inf=0x3f3f3f3f;
    /*** TEMPLATE CODE * * STARTS HERE ***/
    int t;
    int n;
    int a[maxn];
    int b[maxn];    
    // map<int,int> bj;
    int bj[maxn];
    int main()
    {
        gg(t);
        while(t--)
        {
            gg(n);
            repd(i,1,n)
            {
                gg(a[i]);
                b[i]=a[i];
            }
            sort(b+1,b+1+n);
            int cnt=unique(b+1,b+1+n)-b-1;
            repd(i,1,n)
            {
                a[i]=lower_bound(b+1,b+1+cnt,a[i])-b;
            }
            // repd(i,1,n)
            // {
            //     db(a[i]);
    
            int ans=1;
            int l=1;
            int r=2;
            MS0(bj);
            bj[a[1]]++;bj[a[2]]++;
            while(r<=n)
            {
                while(bj[a[r]]>1)
                    bj[a[l++]]--;
                ans=max(ans,r>n?r-l:r-l+1);
                bj[a[++r]]++;
            }
            printf("%d
    ",ans );    
        }
        return 0;
    }
    
    inline void getInt(int* p) {
        char ch;
        do {
            ch = getchar();
        } while (ch == ' ' || ch == '
    ');
        if (ch == '-') {
            *p = -(getchar() - '0');
            while ((ch = getchar()) >= '0' && ch <= '9') {
                *p = *p * 10 - ch + '0';
            }
        }
        else {
            *p = ch - '0';
            while ((ch = getchar()) >= '0' && ch <= '9') {
                *p = *p * 10 + ch - '0';
            }
        }
    }

    本博客为本人原创,如需转载,请必须声明博客的源地址。 本人博客地址为:www.cnblogs.com/qieqiemin/ 希望所写的文章对您有帮助。
  • 相关阅读:
    sudo配置临时取得root权限
    Linux 前台 和 后台进程 说明
    延迟加载
    事件代理
    字符串方法总结
    javascript
    HTML
    通用样式,如清除浮动
    html脱离文档流事件
    经典容易忽略的行内块
  • 原文地址:https://www.cnblogs.com/qieqiemin/p/10308903.html
Copyright © 2011-2022 走看看