啥玩意这是。。。最大珠子种类数和串最大长度咋都用N表示,整的我一直段错误,最后干脆直接全开最大了
#include <iostream>
#include <queue>
using namespace std;
const int maxn = 1e5+ 5;
const int maxType = 1e5+ 5;
int value[maxType];
int str[maxn];
int sum[maxn]; // sum[i] : 0...i value和
int last[maxn];
int main(){
int n;
scanf("%d",&n);
char c = ' ';
int tmp = 1;
while(c != '\n'){
scanf("%d%c",&value[tmp],&c);
tmp++;
}
c = ' ';
int len = 0;
int lastPosi[maxType];
fill(lastPosi,lastPosi + maxType,-1);
while(c != '\n'){
int t;
scanf("%d%c",&t,&c);
str[len] = t;
last[len] = lastPosi[t];
lastPosi[t] = len;
sum[len] = value[t] + (len == 0 ? 0 : sum[len - 1]);
len++;
}
int j = 0;
int begin = 0;
int curValue = 0;
int maxLength = 0;
int bestBegin,bestEnd,maxValue = 0;
for(;j < len;j++){
if(last[j] >= begin){ // 在i发现重复的了
curValue = sum[j - 1] - sum[begin] + value[str[begin]];
if(curValue > maxValue || (curValue == maxValue && j - begin > maxLength)){
maxValue = curValue;
bestBegin = begin;
bestEnd = j - 1;
maxLength = j - begin;
}
begin = last[j] + 1;
}
}
curValue = sum[j - 1] - sum[begin] + value[str[begin]];
if(curValue > maxValue || (curValue == maxValue && j - begin > maxLength)){
maxValue = curValue;
bestBegin = begin;
bestEnd = j - 1;
maxLength = j - begin;
}
printf("%d %d %d",maxValue,bestBegin,bestEnd);
system("pause");
return 0;
}