PROBLEM LINKS:DIFFICULTYCakewalk PREREQUISITESBasic string reading, using array PROBLEMThe problem essentially is to maintain binary (open(1) or close(0)) state of N tweets and simulate K actions on them. The two types of actions are (a) CLICK X , which toggles the state of X^{th} tweet and (b) CLOSEALL , which sets the state of all the N tweets to close(0). After each of the K actions given, we need to find the total number tweets in open(1) state. EXPLANATIONIf you are beginner in programming contests, note that its common to assume that 10^{8} basic arithmetic operations are performed in 1 second on modern computers. It may actually be faster than this (roughly 10^{9} operations per second) on your computer. This is not always guaranteed as it may depend on several other factors. Some of them are handling heavy input/output, using modulo operator a lot, using long or double operands etc. So its always good to try a few practice problems in a site, for better estimation of actual run times. For this problem, first lets look at a very naive approach, which is fast enough for the given constraints N ≤ 1000 and K ≤ 1000. We need to maintain the current state (isOpen) of N tweets, for which we can simply use an array of booleans (true/false) or integers (1/0). CLICK X toggles the state of X^{th} tweet, so if isOpen[X] == 1 then set isOpen[X] = 0, else if isOpen[X] == 0 then set isOpen[X] = 1. This can be done using a single statement isOpen[X] = 1  isOpen[X] or using xor(^) operator, isOpen[X]^=1. CLOSEALL sets the state isOpen[X] = 0, for all the tweets X = 1 to N. To find the number of opened tweets at any time we can simply traverse the state array and count the number of opened tweets. This method requires O(N) memory, O(1) time for toggle, O(N) time for each CLOSEALL and O(N) time to find number of opened tweets. Overall for K operations it has worst case time of O(NK), which is fast enough for the given constraints. SETTER'S SOLUTIONCan be found here APPROACHThe problem setter used the above approach to solve the problem. For comparing two strings, the inbuilt function strcmp in string library is used. TESTER'S SOLUTIONCan be found here APPROACHNote that we don't need to run a O(N) loop each time we want the count of opened tweets. The number of opened tweets changes by only either +1 or 1 with each CLICK X. So if the state of X^{th} tweet changes from close to open, we add 1, else if it changes from open to close, we subtract 1. So we can have this count in O(1) time per CLICK. Note that CLOSEALL still requires to reset all the states to 0 and is O(N). The overall solution if still O(NK) worst case time. Refer tester's solution for this approach. EXERCISE
This question is marked "community wiki".
asked 18 Jun '12, 00:21

Here I present some way how to do all in O(K) time and O(N) memory. Note however that complexity of dealing with each particular CLOSEALL query can be not O(1). Additionally to approach described in the editorial you just need to save in some stack all tweets that were clicked. Once we have CLOSEALL query we pop each tweet from the stack and set isOpen[X] to 0, where X is the current tweet. Note that for some X we can have already isOpen[X] == 0 if it was clicked several times but we don't care about that. After this loop we will have empty stack and can continue to handle queries one by one. This approach has complexity O(K) since we analyze each click at most twice: one time when we deal with actual query of this click and second time for the first CLOSEALL query after this click. This is a socalled amortized analyses  very useful logic that anyone should learn. answered 18 Jun '12, 02:54

And now let's discuss actual solution to the exercise where each query performed in O(1) time. At first we numerate all queries starting from 1. Next let's modify a bit our array Initially Now suppose we have query If we have It is easy to see after each query You can check this solution as a reference for this approach. answered 18 Jun '12, 03:24

Link to my solution : answered 26 Jan '16, 20:49

Here's an alternate solution in O(Q) which is easier to understand: Q= no of queries each time we have a click we add the element clicked to our click list, if an element is even no of times in our clicklist it means it is closed. now when we have a close all query, we 1)decrease the opencount to 0 2) for all clicked elements, unclick them(we find the clicked elements by traversing the clicklist) 3) decrease the listCtr to 0 since each click is used only once while clicking and second while unclicking (during CLOSEALL) the net solution is O(no of clicks)= O(Q) link= solution answered 25 Aug '12, 17:50

I M NOT ABLE TO FIND OUT WAT IS WRONG IN MY SOLUTION..PLZ HELP. answered 05 Oct '12, 19:22
here is counter example  http://ideone.com/A6o21
(05 Oct '12, 19:31)
hey thnx..my mistake i didn't observe dat..thnx a lot..
(05 Oct '12, 19:49)

What's wrong???? answered 27 Oct '12, 18:48

Why am i getting NZEC in this solution ? Can anyone help me out ? answered 20 Mar '14, 12:24
Your class cannot be
smells  http://ideone.com/J3TYWf
(20 Mar '14, 14:58)

Im getting NZEC with the following code.. although sample input works fine.. smone please help me out.. i'm new import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] input = br.readLine().split("\s"); int N = Integer.parseInt(input[0]); int k = Integer.parseInt(input[1]); String str; int open=0; int arr[]=new int[1000]; while(k!=0){ String[] input1 = br.readLine().split("\s");
}}} answered 30 Aug '14, 00:58

Please find the error in this solution : My Solution :) Thanks in advance! answered 02 Dec '14, 12:57
1
@ajinkya1p3
(02 Dec '14, 13:19)
Thanks a lot!! :D
(02 Dec '14, 13:55)
most welcome :)
(02 Dec '14, 14:14)

include<string.h>include<stdlib.h>void main() { int t,n,p,q,i,j,k,x; char a[8]; scanf("%d %d",&t,&n); p=(int)calloc(t,sizeof(int)); q=(int)calloc(n,sizeof(int)); for(i=0;i<n;i++) { k=0; scanf("%s",a); if(strcmp(a,"CLICK")==0) { scanf("%d",&x); if(p[x1]==1) p[x1]=0; else p[x1]=1; } else{ for(j=0;j<t;j++) p[j]=0;} for(j=0;j<t;j++) if(p[j]==1) k++; q[i]=k; } for(i=0;i<n;i++) printf("%d\n",q[i]); } Is there any error in my code??? It works fine when i run it in my system but it is giving a runtime error when i submit it!! :/ answered 22 Jun '15, 23:31

answered 03 Jul '15, 22:57

https://www.codechef.com/viewsolution/8138468 Please have a look at the above link. I need some help. What I did? I simply calculated the length of the choice"7 for CLICK X" otherwise "CLOSEALL". If len==7,then I just checked choice[6] what is it's numeric value.To calculate the numeric value I simply did this: choice[6]'0'. But it showed wrong answer. I will be thankful to anyone who will take some patience and will help me for this. I will learn something which I should have known till now. Thank You. :) answered 12 Sep '15, 17:15

i cant find out my mistake..can anyone help me..
} answered 15 Oct '15, 17:06

please help me out in this problem name "Closing the Tweets". I am getting wrong output error for my solution answered 29 Oct '15, 15:07

I have used a Set for inserting and deleting of elements which requires O(logN) for both operations and clear for closing all the tweets, which takes O(N) hence, overall it took O(NK) time. Even though my solution got AC in 0.0 seconds, still O(NK) is more for this problem right? answered 19 Jan '16, 13:43

On what test case this code fails? include<stdio.h>int main() { int n,k,i; scanf("%d %d",&n,&k); char s[10]; int a[n]; for(i=0;i<n;i++) a[i]="0;</p"> while(k){ int flag=0,y=0,m,num=0; scanf (" %[^\n]%c",s); for(i=6;s[i]!='\0';i++) { m=s[i]'0'; num=num10+m; } if(num>=1 && num<=n) { if(a[num1]==0) a[num1]=1; else a[num1]=0; } else{ y=0; flag=1; } if(flag==0){ for(i=0;i<n;i++) {="" if(a[i]="=1)" y++;="" }="" }="" printf("%d\n",y);="" }="" }<="" p=""> answered 26 Jan '16, 19:15

Why is this problem placed among medium difficulty problems? It looks trivial to me and should belong to the beginner problems. answered 20 Apr '16, 17:28

include<iostream>include<string>using namespace std; int change(int* arr, char c,int n)
{
} int main() { int n,k; string s1,s2; char c; cin>>n>>k; int arr[n]; for(int j=0;j<n;j++) arr[j]=0;
} Why am I getting wrong answer? answered 20 May '16, 15:24

https://www.codechef.com/viewsolution/10581225 Why is this getting WA? answered 23 Jun '16, 16:45

can anyone please point out my mistake in this solution, it gives wrong ans on submission. https://www.codechef.com/viewsolution/11548341 answered 18 Sep '16, 00:10

its showing wrong answer but works fine. can anyone please tell me whats wrong?
answered 07 Apr '17, 14:59

https://www.codechef.com/viewsolution/15965360 Can anyone tell me what's wrong in my solution? It's showing Runtime error. answered 27 Oct '17, 02:37

include<string>using namespace std; int main(){ int N,K; cin>>N>>K; string choice; int in,count = 0,arr[N]; for(int i = 0;i < N;i++) arr[i] = 0; while(K){ cin>>choice; if(choice.compare("CLICK")==0){ cin>>in; if(arr[in1]){ arr[in] = 0; count; } else{ arr[in1] = 1; count++; } } else if(choice.compare("CLOSEALL")==0){ for(int j = 0;j < K;j++) arr[j] = 0; count = 0; } cout<<count<<endl; } }` answered 07 Dec '17, 03:46

I applied the same approach while it gets wrong when i use getline https://www.codechef.com/viewsolution/16737490 correct this got correct when i didn't. https://www.codechef.com/viewsolution/16737259 is not correct can anyone help me finding out why. answered 02 Jan '18, 19:53

Can anyone help why I am getting a wrong answer? My solution is https://www.codechef.com/viewsolution/18952476 answered 20 Jun '18, 16:09

I am unable to understand what is wrong with this solution : https://www.codechef.com/viewsolution/19271123 When i run this program it shows : Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 7 at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:44) at java.base/java.lang.String.charAt(String.java:692) at numberOfOpenTweets.main(numberOfOpenTweets.java:16) So if you can spot the error please tell answered 19 Jul '18, 18:20

I used memset for filling 0s. My solution was showing wrong answer though I have implemented it same as mentioned in the tutorial. http://www.codechef.com/viewsolution/1132512 Can't find what is the difference :(
@vikram535 : Please check the 'memset' function in the library. The last parameter should be the number of bytes, and not number of elements in the array. If your array is int A[1001]; , you should use it as, memset(A,0,sizeof(A));
Thank you so much for pointing out my mistake. I was not able to find it. Thanks again :)