Coin flip

closed account (287LhbRD)
https://www.codechef.com/problems/FLIPCOIN

My solution is giving tle.
Any suggestions?

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<bits/stdc++.h>
using namespace std;
 
int heads(int *tails,int n,int a,int b){
	
	int res=0;
	
	for(int i=a;i<=b;i++)
	if(tails[i]==0)
     res++;
	 
	 return res;	
	
	
}
 
 
 
int main(){
int n,q;
 
cin>>n>>q;
 
     int c[q],a[q],b[q];
     
     for(int i=0;i<q;i++)
     cin>>c[i]>>a[i]>>b[i];
     
     int tails[n];
     
     for(int i=0;i<n;i++)
     tails[i]=1;
     
     
      for(int i=0;i<q;i++){
      	
      	if(c[i]==1)
      	cout<<heads(tails,n,a[i],b[i])<<endl;
      	
      	if(c[i]==0)
      	{
      		for(int j=a[i];j<=b[i];j++)
      		{
      			
      			if(tails[j]==0)
      			tails[j]=1;
      			
      			else
      			tails[j]=0;
      			
      			
      			
			  }
      		
     	
		  }
      	
      	
      	
      	
      	
	  }
 
 
return 0;
} 
Last edited on
In general a brute force solution to these problems will give you "time limit exceeded". It would be kind of pointless if brute force solutions worked since they take no thought or skill whatsoever. You need to think a little and come up with an intelligent solution.
closed account (287LhbRD)
Is there a need for a different data structure?
If yes,how am I supposed to know?
Can you please give a hint?
Last edited on
Topic archived. No new replies allowed.