UVa11572 - Unique Snowflakes(最长的不重复的子数组)

Problem A: Unique Snowflakes

Emily the entrepreneur has a cool business idea: packaging and sellingsnowflakes. She has devised a machine that captures snowflakes as theyfall, and serializes them into a stream of snowflakes that flow, one byone, into a package. Once the package is full, it is closed andshipped to be sold.

The marketing motto for the company is "bags of uniqueness."To live up to the motto, every snowflake in a packagemust be different from the others. Unfortunately, this iseasier said than done, because in reality, many of the snowflakesflowing through the machine are identical. Emily would like toknow the size of the largest possible package of unique snowflakesthat can be created. The machine can start filling the packageat any time, but once it starts, all snowflakes flowing fromthe machine must go into the package until the package is completedand sealed. The package can be completed and sealed before all ofthe snowflakes have flowed out of the machine.

Input Specification

The first line of input contains one integer specifying the number oftest cases to follow. Each test case begins with a line containingan integer n, the number of snowflakes processed by the machine.The following n lines each contain an integer (in the range0 to 10^9, inclusive) uniquely identifyinga snowflake. Two snowflakes are identified by the same integer ifand only if they are identical.The input will contain no more than one million total snowflakes.

Sample Input

1
5
1
2
3
2
1

Output Specification

For each test case output a line containing single integer, the maximumnumber of unique snowflakes that can be in a package.

Output for Sample Input

3
f[]记录输入的数组,用pos[]记录数最近出现的位置,start表示子数组的起始位置,pos[f[i]]>=start,表示[start, i - 1]之间出现过f[i]这个数,更新start = pos[f[i]] + 1

import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Arrays;

public class Main 
{
	private static final boolean DEBUG = false;
	private static final int N = 1000010;
	private BufferedReader cin;
	private PrintWriter cout;
	private StreamTokenizer tokenizer;
	private int n;
	private int[] f;
	
	public void init() 
	{
		try {
			if (DEBUG) {
				cin = new BufferedReader(new InputStreamReader(
						new FileInputStream("d:\\OJ\\uva_in.txt")));
			} else {
				cin = new BufferedReader(new InputStreamReader(System.in));
			}
			cout = new PrintWriter(new OutputStreamWriter(System.out));
			tokenizer = new StreamTokenizer(cin);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String next()
	{ 
		try {
			tokenizer.nextToken();
			if (tokenizer.ttype == StreamTokenizer.TT_EOF) return null;
			else if (tokenizer.ttype == StreamTokenizer.TT_NUMBER) return String.valueOf((int)tokenizer.nval);
			else return tokenizer.sval;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public boolean input() 
	{
		n = Integer.parseInt(next());
		f = new int[n + 1];
		for (int i = 0; i < n; i++) {
			f[i] = Integer.parseInt(next());
		}
		f[n] = f[n - 1];
		return true;
	}

	
	
	public void solve() 
	{
		int start = 0;
		int ans = 1;
		
		int[] pos = new int[N];
		Arrays.fill(pos, -1);
		
		for (int i = 0; i <= n; i++) {
			if (pos[f[i]] >= start) {
				int tmp = i - start;
				ans = Math.max(ans, tmp);
				start = pos[f[i]] + 1;
				pos[f[i]] = i;
			} else {
				pos[f[i]] = i;
			}
		}
		
		cout.println(ans);
		cout.flush();
	}

	public static void main(String[] args) 
	{
		Main solver = new Main();
		solver.init();
		
		int t = Integer.parseInt(solver.next());
		while (t-- > 0) {
			solver.input();
			solver.solve();
		}
	}
}



  • 0
    点赞
  • 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
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值