UVA11572 唯一的雪花 题解(map有天坑)

唯一的雪花 Unique Snowflakes

题目描述

Emily the entrepreneur has a cool business idea: packaging and selling snowfakes. She has devised amachine that captures snowfakes as they fall, and serializes them into a stream of snowfakes that fowone 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, everysnowfake in a package must be different from the others. Unfortuately, this is easier said than done.because in reality, many of the snowflakes flowing through the machine are identical. Emily would liketo know the size of the largest possible package of uique snowflakes that can be created. The machinecan start filing the package at any time, but once it starts, all snowfakes fowing from the machinemust go into the package until the package is completed and sealed. The package can be completedand sealed before all of the snowfakes have fowed out of the machine.

企业家 Emily 有一个很酷的主意:把雪花包起来卖。她发明了一台机器,这台机器可以捕捉飘落的雪花,并把它们一片一片打包进一个包裹里。一旦这个包裹满了,它就会被封上送去发售。

Emily 的公司的口号是“把独特打包起来”,为了实现这一诺言,一个包裹里不能有两片一样的雪花。不幸的是,这并不容易做到,因为实际上通过机器的雪花中有很多是相同的。Emily 想知道这样一个不包含两片一样的雪花的包裹最大能有多大,她可以在任何时候启动机器,但是一旦机器启动了,直到包裹被封上为止,所有通过机器的雪花都必须被打包进这个包裹里,当然,包裹可以在任何时候被封上。

输入格式

The first line of input contains one integer specifying the number of test cases to follow. Each testcase begins with a line containing an integer n, the number of snowfakes processed by the machineThe following n lines each contain an integer (in the range 0 to 10°, inclusive) uniquely identifying asnowfake. 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 snowfakes.

第一行是测试数据组数 \(T\),对于每一组数据,第一行是通过机器的雪花总数 \(n\)\(n \le {10}^6\)),下面 \(n\) 行每行一个在 \([0, {10}^9]\) 内的整数,标记了这片雪花,当两片雪花标记相同时,这两片雪花是一样的。

输出格式

对于每一组数据,输出最大包裹的大小。

样例 #1

样例输入 #1

1
5
1
2
3
2
1

样例输出 #1

3

解析

双指针维护滑块即可,由于雪花标记比较大,采用键值对存储是否已经出现过
需要注意的天坑是:

map 容器不支持像 std::unordered_map 或 std::vector 那样通过下标直接创建元素。如果你尝试访问一个在 map 中不存在的键,将会引发一个异常,而不是创建一个新元素并初始化为0。
如果你使用下标运算符 mp[x] 来访问一个在 map 中不存在的键 x,将会触发 map 的行为,它会插入一个新的键值对,其中键是 x,值会被默认构造。
对于 int 类型,默认构造的值是0。这是因为在C++中,int 的默认构造函数将其初始化为0。
但是,这种行为通常被认为是危险的,因为它可能会导致意外地插入新元素。
因此,更安全的方法是使用 map 的 find() 方法来检查元素是否存在,或者使用 map 的 at() 方法来访问元素,如果键不存在,at() 方法会抛出一个 std::out_of_range 异常。

unordered_map 的行为与 map 在使用下标运算符时有所不同。
如果你尝试通过下标运算符 mp[x] 访问一个在 unordered_map 中不存在的键 x,unordered_map 不会抛出异常,而是会自动创建一个新的键值对,其中键是 x,值会被默认构造。
对于 int 类型,默认构造的值是0。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e6+10;

int T;
int n;
map<int,int>mp;
int num[maxn];

inline long long read(){
    ll readtmp = 0, readflag = 1;
    char readch = getchar();
    while(readch < '0' || readch > '9') {if(readch == '-') readflag=-1; readch=getchar();}
    while('0' <= readch && readch <= '9') {readtmp = readtmp*10 + readch - '0'; readch = getchar();}
    return readtmp*readflag ;
}

int main(){
    
    //freopen("2.in", "r", stdin);

    T=read();

    while(T--){
        mp.clear();
        n = read();
        int l = 1;
        int ans = 0;
        
        for(int i=1;i<=n;++i){
		    int x = read();
		    if(mp.find(x) != mp.end() && mp[x])
			    l=max(l,mp[x]+1);
		    mp[x]=i;
		ans=max(ans,i-l+1);
	}

        printf("%d\n", ans);
    }



    return 0;
}
posted @ 2024-11-04 20:53  [丘李]Chilllee  阅读(109)  评论(0)    收藏  举报