![:o](./images/smilies/icon_eek.gif)
Could somone tell me please
Moderator: Board moderators
Code: Select all
#include <iostream>
using namespace std;
int main()
{
int t;
cin >> t;
bool * a = new bool[1000000001];
long n;
for(int tt = 0; tt < t; tt++)
{
cin >> n;
long count = 0;
long snow;
memset(a,0,sizeof(a));
int temp = 0;
for(long i = 0; i < n; i++)
{
cin >> snow;
if(a[snow])
{
memset(a,0,sizeof(a));
if(temp > count)
count = temp;
temp = 1;
a[snow] = 1;
continue;
}
else
{
a[snow] = 1;
temp ++;
}
}
if(temp > count)
count = temp;
cout << count << endl;
}
delete a;
return 0;
}
Code: Select all
bool * a = new bool[1000000001];
Thanks.Jan wrote:1) You are declaring a huge memory locally.Code: Select all
bool * a = new bool[1000000001];
2) 1000000000 byte = 954 MB (almost), the memory limit is 32 MB.
You can use a map instead of the array. Now it takes O(1) to access, but in a map it will take O(log(n)), but still its feasible. Hope these help.
Code: Select all
#include <iostream>
#include <set>
using namespace std;
int main()
{
int t;
cin >> t;
set<long> a;
long n;
for(int tt = 0; tt < t; tt++)
{
cin >> n;
long count = 0;
long snow;
a.clear();
int temp = 0;
pair<set<long>::iterator,bool> insert_OK;
for(long i = 0; i < n; i++)
{
cin >> snow;
insert_OK = a.insert(snow);
if(insert_OK.second)
temp++;
else
{
a.clear();
if(temp > count)
count = temp;
temp = 1;
a.insert(snow);
continue;
}
}
if(temp > count)
count = temp;
cout << count << endl;
}
return 0;
}
I solved it in O(n log n) too. It runs in ~0.7 seconds, but Rio solved it in 0.09 seconds, which makes me believe an O(n) solution does exist.f74956227 wrote:Hey! i got AC use a O(nlogn) algorithm.. i want to know if there is a O(N) algorithm for solving this problem?
Could somone tell me please
Code: Select all
1
12
7
4
0
9
4
8
8
2
4
5
5
1
Code: Select all
4
PLZ HELP ME!!!!!!!!!!!!
i m getting WA..................![]()
![]()
![]()
![]()
![]()
Code: Select all
code remove.........
Code: Select all
1
5
1
2
1
1
3
Code: Select all
2
saiful_sust wrote:PLZ HELP ME!!!!!!!!!!!!
i try some input all are ok..
BUT
i m getting WA..................![]()
![]()
![]()
![]()
![]()
Code: Select all
i got it..............
Code: Select all
#include<iostream>
#include<map>
using namespace std;
int num,t,i,j,n,tem,ar[100010];
int main(){
#ifndef ONLINE_JUDGE
freopen("1.txt","r",stdin);
#endif
scanf("%d",&t);
while(t--){
num=tem=0;
scanf("%d",&n);
for(i=0;i<n;i++)scanf("%d",&ar[i]);
for(i=0;i<n;i++){
j=i;
map<int,int>mp;
while(j<n){
if(mp[ar[j]]++)break;
tem++;
j++;
}
if(num<tem)num=tem;
tem=0;
}
printf("%d\n",num);
}
return 0;
}
Code: Select all
1
6
1 2 3 2 4 5