Logic :
In each iteration keep count of groups and when a new person add in chain just see that if its left and right position are empty or not if they both are empty then groups will increase by 1 if both filled then decrease count of group by 1 otherwise nothing.
(Why this approach will work well try to simulate this in your mind or on paper ;) )
Below you can see code also :
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cctype>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <functional>
using namespace std;
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define FORD(i,a,b) for(int i=(a);i>=(b);i--)
inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
const int INF = 1<<29;
typedef long long ll;
inline int two(int n) { return 1 << n; }
inline int test(int n, int b) { return (n>>b)&1; }
inline void set_bit(int & n, int b) { n |= two(b); }
inline void unset_bit(int & n, int b) { n &= ~two(b); }
inline int last_bit(int n) { return n & (-n); }
inline int ones(int n) { int res = 0; while(n && ++res) n-=n&(-n); return res; }
template<class T> void chmax(T & a, const T & b) { a = max(a, b); }
template<class T> void chmin(T & a, const T & b) { a = min(a, b); }
/////////////////////////////////////////////////////////////////////
bool Line[1000000];
int main()
{
int Q;
scanf("%d", &Q);
int ans = 0;
while(Q--) {
int P;
scanf("%d", &P);
bool Left = false;
bool Right = false;
if(P - 1 > 0 && Line[P - 1]) Left = true;
if(P + 1 <= 20000 && Line[P + 1]) Right = true;
if(Left && Right) {
ans--;
} else if(Left || Right) {
ans += 0;
} else {
ans += 1;
}
Line[P] = true;
printf("%d\n", ans);
}
puts("Justice");
return 0;
}
Happy Coding :)
In each iteration keep count of groups and when a new person add in chain just see that if its left and right position are empty or not if they both are empty then groups will increase by 1 if both filled then decrease count of group by 1 otherwise nothing.
(Why this approach will work well try to simulate this in your mind or on paper ;) )
Below you can see code also :
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cctype>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <functional>
using namespace std;
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define FORD(i,a,b) for(int i=(a);i>=(b);i--)
inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
const int INF = 1<<29;
typedef long long ll;
inline int two(int n) { return 1 << n; }
inline int test(int n, int b) { return (n>>b)&1; }
inline void set_bit(int & n, int b) { n |= two(b); }
inline void unset_bit(int & n, int b) { n &= ~two(b); }
inline int last_bit(int n) { return n & (-n); }
inline int ones(int n) { int res = 0; while(n && ++res) n-=n&(-n); return res; }
template<class T> void chmax(T & a, const T & b) { a = max(a, b); }
template<class T> void chmin(T & a, const T & b) { a = min(a, b); }
/////////////////////////////////////////////////////////////////////
bool Line[1000000];
int main()
{
int Q;
scanf("%d", &Q);
int ans = 0;
while(Q--) {
int P;
scanf("%d", &P);
bool Left = false;
bool Right = false;
if(P - 1 > 0 && Line[P - 1]) Left = true;
if(P + 1 <= 20000 && Line[P + 1]) Right = true;
if(Left && Right) {
ans--;
} else if(Left || Right) {
ans += 0;
} else {
ans += 1;
}
Line[P] = true;
printf("%d\n", ans);
}
puts("Justice");
return 0;
}
Happy Coding :)