UVa11572 - Unique Snowflakes(最长的不重复的子数组)
2014-08-07 22:34
441 查看
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 integern, 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
= f[n - 1];
return true;
}
public void solve()
{
int start = 0;
int ans = 1;
int[] pos = new int
;
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();
}
}
}
相关文章推荐
- UVA 11572 Unique Snowflakes(最长无重复元素问题) 用dp思路
- uva 11572 unique snowflakes——yhx
- uva 11572 Unique Snowflakes
- 【例题 8-7 UVA - 11572】Unique Snowflakes
- UVA11572_Unique Snowflakes
- UVA 11572 Unique Snowflakes
- UVA-11572-Unique snowflakes
- UVa 11572 - Unique Snowflakes
- uva 11572 unique snowflakes——yhx
- BNU19990 UVA11572 Unique Snowflakes
- uva 11572 - Unique Snowflakes(和书略有不同)
- UVA 11572 Unique Snowflakes (滑动窗口)
- uva 11572 Unique Snowflakes
- (白书训练计划)UVa 11572 Unique Snowflakes(窗口滑动法)
- UVA 11572 Unique Snowflakes(滑窗|双指针)
- UVa 11572 - Unique Snowflakes (滑动窗口STL_map||set) 好题!!!
- UVa 11572 Unique Snowflakes——思路题
- Uva11572 Unique Snowflakes【滑动窗口】【例题8-7】
- 【高效算法设计——滑动窗口】UVa 11572 Unique Snowflakes
- uva 11572 unique snowflakes