Unique Snowflakes UVA - 11572queue和sort尺取法,简单AC

Unique Snowflakes UVA - 11572

题目如下
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
题意
我们有一个长度不超过10的9次方的序列,题要求我们求出这个序列中连续不重复元素的最长子序列的长度。比如说列题就是 1 2 3;

  1. 这里面如果你直接定义一个10的9次方,你的vs可能会报错,交的时候可能爆内存所以我这里建议使用queue或者deque,因为我们要后面添加数字,前面把不符合的排出去,这样可以不断释放内存。
  2. 我们这里使用sort函数,这个函数他会自己将重复的数字删去;而queue不会所以两个size不相等的时候,说明有数字重复了,记下那个重复的数字(假设为k)和sort的size,然后用queue函数,这里有一个queue便利的地方那就是queue是从前往后删的,而且可以查看第一个数字,然后用sort的find这个第一个数并且删去,一直删直到第一个数字等于k然后把queue里面的k也删了,就不用那么复杂的去记left,right;时不时的跟新max就行了。逻辑挺简单。

下面给出AC代码

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<set>
#include<cstdio>
#include<queue>
using namespace std;
int main()
{
	int t, i, m, sum, max, n;
	sum = 0;
	max = 0;
	set<int>di;
	queue<int>bi;
	cin >> t;
	while (t--)
	{
		cin >> m;
		for (i = 0; i < m; i++)
		{
			cin >> n;
			di.insert(n);
			bi.push(n);
			if (bi.size() != di.size())//在输入数字的时候就开始检查
			{
				sum = di.size();
				if (sum > max)
					max = sum;//记下目前最大值
				while (1)
				{
					if (bi.front() != n)//查看queue第一个数字
					{
						di.erase(bi.front());//删去sort里面的
						bi.pop();//删去queue里面的
					}
					else//sort里面的不要删,因为只有一个,函数已经帮你删
					{
						bi.pop();//而queue里面有两个,手动删
						break;
					}
				}
			}

		}
		sum = di.size();
		if (sum > max)
			max = sum;//最后的时候不会执行while,所以手动执行
		printf("%d\n", max);
		di.clear();//这边就是清空队列了
		while(1)
		{
			if (bi.size() != 0)
			{
				bi.pop();
			}
			else
				break;
		}
		sum = 0;
		max = 0;
	}
	return 0;
}
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现雪花飘落特效可以使用opencv-python库。下面是一个简单的实现方法: 1. 导入所需的库: ```python import cv2 import numpy as np ``` 2. 创建一个空白图像作为背景: ```python width, height = 800, 600 # 设置背景尺寸 background = np.zeros((height, width, 3), dtype=np.uint8) # 创建一个黑色背景图像 ``` 3. 创建一组雪花图像: ```python num_snowflakes = 100 # 雪花数量 snowflakes = [] for _ in range(num_snowflakes): center = (np.random.randint(0, width), np.random.randint(0, height)) # 随机设置雪花中心点 radius = np.random.randint(3, 8) # 随机设置雪花半径 snowflake = cv2.circle(np.zeros_like(background), center, radius, (255, 255, 255), -1) # 创建雪花图像 snowflakes.append(snowflake) ``` 4. 开始雪花飘落效果循环: ```python while True: for snowflake in snowflakes: # 随机设置雪花的飘落速度和方向 x_speed = np.random.randint(-5, 5) y_speed = np.random.randint(1, 5) # 更新雪花的位置 x, y = np.where(snowflake[:, :, 0] == 255) # 获得雪花的位置 snowflake[y, x] = [0, 0, 0] # 清空之前的位置 snowflake[y + y_speed, x + x_speed] = [255, 255, 255] # 更新位置 # 将雪花放置在背景上 background = cv2.bitwise_or(background, snowflake) # 显示背景图像 cv2.imshow("Snowfall Effect", background) if cv2.waitKey(30) == ord('q'): break cv2.destroyAllWindows() ``` 通过以上代码,我们可以实现一个简单的雪花飘落特效。需要注意的是,以上代码只是一种简单实现方法,你可以根据自己的需求进行修改和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值