sapnil wrote:Output is:4Code: Select all
1 6 1 2 3 2 4 5
i dont have any idea to pass the TL.
can u pliz share ur method.
Moderator: Board moderators
sapnil wrote:Output is:4Code: Select all
1 6 1 2 3 2 4 5
Code: Select all
map<int,int> mp;
mp[1]=1;
mp[2]=2;
mp[3]=3;
mp[2]=4;<
mp[4]=5;
mp[5]=6;
Code: Select all
import java.util.Scanner;
public class Main {
int[] nums;
public Main() {
Scanner in = new Scanner(System.in);
int numCases = in.nextInt();
for (int i = 0; i < numCases; i++){
int numNums = in.nextInt();
nums=new int[numNums];
for (int j=0; j < numNums; j++){
nums[j]=in.nextInt();
}
System.out.println(longest(0,numNums-1));
}
}
public int longest(int start,int end){
if (start-end==0)
return 1;
else if (start-end==1){
return nums[start]==nums[end]?1:2;
}
else{
int center = (start+end)/2;
int left = longest(start,center);
int right = longest(center+1,end);
int max = Math.max(left, right);
int b = center;
int e = center;
//Go forward across center until uniqueness is broken
forward:
while(e<end){
for (int i = b; i<=e; i++){
if (nums[i]==nums[e+1]){
break forward;
}
}
e++;
}
max = Math.max(max, e-b+1);
//Go backwards across center until the longest unique string goes doesn't span the center
while (e>center && b>start){
for (int i = b; i<=e; i++){
if (nums[i]==nums[b-1]){
e=i-1;
break;
}
}
b--;
max = Math.max(max, e-b+1);
}
return max;
}
}
public static void main(String[] args) {
new Main();
}
}
Code: Select all
#include <iostream>
#include <map>
int main()
{
int tc,tsc,s,ps,mxsize,cursize;
std::cin>>tc;
while(tc--)
{
mxsize = cursize = 0;
std::map<int,int> snf;
std::map<int,int>::iterator it;
std::cin>>tsc;
while(tsc>0)
{
std::cin>>s;
it = snf.find(s);
if(it == snf.end())
snf[s] = 0;
else
{
cursize = 1;
snf.clear();
snf[ps] = 0;
it = snf.find(s);
if(it != snf.end())
{
snf.clear();
cursize = 0;
}
snf[s] = 0;
}
cursize++;
if(mxsize<cursize) mxsize = cursize;
ps = s;
}
std::cout<<mxsize<<std::endl;
}
return 0;
}
Code: Select all
1
1
0
Code: Select all
/*
* UniqueSnowflakes.cpp
*
* Created on: Sep 7, 2013
* Author: mohamed
*/
#include <map>
#include <stdio.h>
using namespace std;
map<int, int> mac;
int main(int argc, char **argv) {
int t, n, m;
scanf("%d", &t);
while (t--) {
mac.clear();
scanf("%d", &n);
int max = 0, cur = 0;
for (int i = 1; i <= n; ++i) {
scanf("%d", &m);
if (mac[m] == 0) {
cur++;
mac[m] = i;
if (cur > max)
max = cur;
} else {
cur = i - mac[m];
mac.erase(mac.begin(), mac.find(m));
mac[m] = i;
}
}
printf("%d\n", max);
}
}
Code: Select all
1
5
2
1
2
1
3