Skip to content

Instantly share code, notes, and snippets.

@wolfsyntax
Created January 26, 2018 04:32
Show Gist options
  • Save wolfsyntax/bfc8d9871a53d1644dd68a565d4fd72c to your computer and use it in GitHub Desktop.
Save wolfsyntax/bfc8d9871a53d1644dd68a565d4fd72c to your computer and use it in GitHub Desktop.
HackerEarth Solution+ Editorial Snippet

Some part of this codes contains codesnippet/algorithm from HackerEarth Editorial

#include <iostream>
#include <stack>
#include <cmath>
#include <cstdio>
#include <vector>
#include <algorithm>
/**
Problem :
Link :
Verdict : Accepted
Author :
**/
using namespace std;
int main(){
int t, n, cnt = 0, mval = 0; scanf("%d",&t);
stack<int> pile;
pile.push(0);
int arr[t+1];
for(int i = 1; i <= t; i++){
scanf("%d",&n);
arr[i] = n;
if(n > 0) pile.push(i);
else{
int index = pile.top();
pile.pop();
if(-arr[i] == arr[index]){
if(!pile.empty()){
mval = max(mval,i-pile.top());
}else pile.push(i);
} else pile.push(i);
//pile.pop();
}
}
printf("%d\n",mval);
return 0;
}
//#include <bits/stdc++.h>
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <cstdio>
#include <iostream>
/**
Problem :
Link :
Verdict : Accepted
Author : Wolf Syntax
**/
using namespace std;
int dy[] = {-1,0,1,0};
int dx[] = {0,1,0,-1};
int main(void){
int t, n; scanf("%d",&t);
while(t--){
scanf("%d",&n);
int arr[n][n];
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
scanf("%d",&arr[i][j]);
}
}
int cnt = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
int v = arr[i][j];
for(int p = 0; p <= i; p++){
for(int q = 0; q <= j; q++){
if(v < arr[p][q]) cnt++;
}
}
}
}
printf("%d\n",cnt);
}
return 0;
}
#include <iostream>
#include <cstdio>
#include <vector>
#include <cassert>
#include <cmath>
//accepted-HE:editorial
using namespace std;
#define MAX 5123
#define abs(x) (x < 0 ? -x : x)
vector< int >G[MAX];
int absv(int x){
if(x < 0) x *= (-1);
return x;
}
int main(){
int t, n, k; scanf("%d",&t);
while(t--){
scanf("%d %d",&n,&k);
//string grid[n];
char c;
int arrested = 0;
vector<int> police;
vector<int> thief;
for(int i = 0; i < n; i++) {
police.clear(); thief.clear();
for(int j = 0; j < n; j++){
cin >> c;
assert(c == 'P' || c == 'T');
if (c == 'P') police.push_back(j);
else thief.push_back(j);
}
int l = 0, r = 0;//, m = (police.size() > thief.size() ? thief.size(): police.size());
while (l < thief.size() && r < police.size()) {
if (absv(thief[l] - police[r]) <= k) {
++arrested; ++l; ++r;
} else if (thief[l] < police[r]) ++l;
else ++r;
}
}
printf("%d\n",arrested);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment