Monday, 14 November 2016

Closing Tweets


 Problem Statement:

All submissions for this problem are available.

Little kids, Jack and Evan like playing their favorite game Glass-and-Stone. Today they want to play something new and came across Twitter on their father's laptop.
They saw it for the first time but were already getting bored to see a bunch of sentences having at most 140 characters each. The only thing they liked to play with it is, closing and opening tweets.
There are N tweets on the page and each tweet can be opened by clicking on it, to see some statistics related to that tweet. Initially all the tweets are closed. Clicking on an open tweet closes it and clicking on a closed tweet opens it. There is also a button to close all the open tweets. Given a sequence of K clicks by Jack, Evan has to guess the total number of open tweets just after each click. Please help Evan in this game.

Input

First line contains two integers N K, the number of tweets (numbered 1 to N) and the number of clicks respectively (1 ≤ N, K ≤ 1000). Each of the following K lines has one of the following.
  • CLICK X , where X is the tweet number (1 ≤ X ≤ N)
  • CLOSEALL

Output

Output K lines, where the ith line should contain the number of open tweets just after the ith click.

Example

Input:
3 6
CLICK 1
CLICK 2
CLICK 3
CLICK 2
CLOSEALL
CLICK 1

Output:
1
2
3
2
0
1

Problem Link: tweets

The idea : 

Take a boolean array. 1. for ' CLOSEALL' set everything to false. 2. For click X toggle . 3. Count the numbers by simply iterating through it.

Problem Code:

  1. #include<iostream>
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. int main()
  5. {
  6. int N,K;
  7. scanf("%d %d",&N,&K);
  8. //cout<<N<<" "<<K<<endl;
  9. bool *status=new bool[N+1];
  10. while(K--)
  11. {
  12. char command[50];
  13. scanf("%s",&command);
  14. if(strcmp(command,"CLOSEALL")==0)
  15. {
  16. for(int i=1;i<=N;i++)
  17. {
  18. status[i]=false;
  19. }
  20. cout<<0<<endl;
  21. continue;
  22. }
  23. else
  24. {
  25. int x;
  26. scanf("%d",&x);
  27. status[x]=!(status[x]);
  28. int count=0;
  29. for(int i=1;i<=N;i++)
  30. {
  31. if(status[i])
  32. {
  33. count++;
  34. }
  35. }
  36. cout<<count<<endl;
  37. }
  38. }
  39. return 0;
  40. }


No comments:

Post a Comment