HEY this is one of the very good problem.......
here the logic just little bit of hash u can say or
dp u can say bcoz i m using previous store values
HERE IS MY AC C++ SOLUTION IS:::::
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 | #include<bits/stdc++.h>
using namespace std;
int arr[100009];
int main()
{
int t,j;
scanf("%d",&t);
for(j=1;j<=t;j++)
{
int n,q,i,y,x;
scanf("%d %d",&n,&q);
memset(arr,0,sizeof(arr));
for(i=0;i<n;i++)
{
scanf("%d",&x);
if(i==0)
arr[i]=1;
else
{
if(x!=y)
arr[i]=arr[i-1]+1;
else
arr[i]=arr[i-1];
}
y=x;
}
printf("Case %d:\n",j);
for(i=0;i<q;i++)
{
scanf("%d %d",&x,&y);
printf("%d\n",arr[y-1]-arr[x-1]+1);
}
}
}
|
2 comments:
i hope my logic is not very good bcz it is taking time 0.19
so plz if u have better solution let me know............
same logic optimise i/o.. gives 0.04
Post a Comment