1026 String of Colorful Beads
Statement
Metadata
- 作者: 陈越
- 单位: 浙江大学
- 代码长度限制: 16 KB
- 时间限制: 150 ms
- 内存限制: 64 MB
Eva would like to buy a string of beads with no repeated colors so she went to a small shop of which the owner had a very long string of beads. However the owner would only like to cut one piece at a time for his customer. With as many as ten thousand beads in the string, Eva needs your help to tell her how to obtain the longest piece of string that contains beads with all different colors. And more, each kind of these beads has a different value. If there are more than one way to get the longest piece, Eva would like to take the most valuable one. It is guaranteed that such a solution is unique.
Input Specification
Each input file contains one test case. Each case first gives in a line a positive integer N (
Output Specification
For each test case, print in a line the total value of the piece of string that Eva wants, together with the beginning and the ending indices of the piece (start from 0). All the numbers must be separated by a space and there must be no extra spaces at the beginning or the end of the line.
Sample Input
Sample Output
Solution
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 10;
int n, pre[N], a[N], b[N], f[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", a + i);
for (int i = 1; i <= n; ++i) scanf("%d", b + i), f[i] = a[b[i]], f[i] += f[i - 1];
memset(pre, 0, sizeof pre);
int Max = 0, l = -1, r = -1;
int P = 0;
for (int i = 1; i <= n; ++i) {
P = max(P, pre[b[i]]);
pre[b[i]] = i;
if (f[i] - f[P] > Max) {
Max = f[i] - f[P];
l = P;
r = i - 1;
}
}
printf("%d %d %d\n", Max, l, r);
return 0;
}