Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

QHEAP1

In c++
#include <cmath>

#include <cstdio>

#include <vector>

#include <iostream>

#include <algorithm>

using namespace std;

#include <queue>

int main() {

vector<int> vec;

int q;

cin >> q;

int minval=1000000007;

bool flag=false;

for (int i=0; i<q; i++)

int a,v;

cin >> a;

if (a==1)

cin >> v;

vec.push_back(v);

minval=min(minval,v);

}
if (a==2)

cin >> v;

if (v==minval)

flag=true;

for (int j=0; j<vec.size(); j++)

if (vec[j]==v)

vec.erase(vec.begin()+j);

if (a==3)

if (flag)

minval=1000000007;

for (int j=0; j<vec.size();j++)

minval=min(minval,vec[j]);

flag = false;

cout << minval << endl;

}
}

/*

priority_queue<int> pq;

int q;

cin >> q;

for (int i=0; i<q; i++)

int a,v;

cin >> a;

if (a==1)

cin >> v;

pq.push(-1*v);

if (a==2)

cin >> v;

pq.pop();

if (a==3)

cout << pq.top()*-1 << endl;

//pq.pop();

*/
return 0;

Castle on the Grid


#include <cmath>

#include <cstdio>

#include <vector>

#include <iostream>

#include <algorithm>

using namespace std;

int main() {

struct Point {

int x;

int y;

Point(int _x, int _y) {

x = _x;

y = _y;

};

};

int n;

cin >> n;

char z[100][100];

for (int x = 0; x < 100; x++) {


for (int y = 0; y < 100; y++) {

z[x][y] = 0;

};

};

for (int x = 0; x < n; x++) {

for (int y = 0; y < n; y++) {

cin >> z[x][y];

};

};

int a, b, c, d;

cin >> a; cin >> b; cin >> c; cin >> d;

if (a == c && b == d) {

printf("0\n"); return 0;

//

z[a][b] = 'A';

z[c][d] = 'B';

vector<Point> q[2];

char s = -1;

q[(-s) % 2].push_back(Point(a, b));

while (1) {

for (vector<Point>::iterator i = q[(-s) % 2].begin(); i

!= q[(-s) % 2].end(); i++) {

// go left

for (int left = i->x - 1; left >= 0; left--)

{
if (z[left][i->y] == 'B') {

printf("%d\n", -s );

//Print(z,n);

return 0;

};

if (z[left][i->y] == '.') {

z[left][i->y] = s;

//Print(z, n);

q[(-s + 1) %

2].push_back(Point(left, i->y));

else if (z[left][i->y] != s) {

break;

};

// go right

for (int right = i->x + 1; right < n; right++)

if (z[right][i->y] == 'B') {

printf("%d\n", -s );

//Print(z, n);

return 0;

};

if (z[right][i->y] == '.') {

z[right][i->y] = s;

//Print(z, n);
q[(-s + 1) %

2].push_back(Point(right, i->y));

else if (z[right][i->y] !=s ) {

break;

};

// go up

for (int up = i->y - 1; up >= 0; up--)

if (z[i->x][up] == 'B') {

printf("%d\n", -s);

//Print(z, n);

return 0;

};

if (z[i->x][up] == '.') {

z[i->x][up] = s;

//Print(z, n);

q[(-s + 1) % 2].push_back(Point(i-

>x, up));

else if (z[i->x][up] != s) {

break;

};

// go down
for (int down = i->y + 1; down < n; down++)

if (z[i->x][down] == 'B') {

printf("%d\n", -s);

//Print(z, n);

return 0;

};

if (z[i->x][down] == '.') {

z[i->x][down] = s;

//Print(z, n);

q[(-s + 1) % 2].push_back(Point(i-

>x, down));

else if (z[i->x][down] != s) {

break;

};

};

s--;

};

};

Balanced Brackets
#include <cmath>

#include <cstdio>

#include <vector>
#include <iostream>

#include <algorithm>

#include<stack>

using namespace std;

#include<map>

int main() {

map<char,char>lol;

lol[')']='(';

lol[']']='[';

lol['}']='{';

int t;

cin >> t;

while(t--)

string s;

stack<char>st;

cin >> s;

int flag=0;

for(int i=0;s[i]!='\0';i++)

if(s[i]=='(' || s[i]=='{' || s[i]=='[')

st.push(s[i]);

else{

if(!st.empty() and lol[s[i]]==st.top())


st.pop();

else

flag=1;

break;

if(!flag and st.empty())

cout << "YES" << endl;

else

cout << "NO " << endl;

return 0;

Median Updates
#include <cstdio>

#include <set>

#include <cstring>

#include <string>

using namespace std;

multiset<int> s1,s2; //0<=|s1|-|s2|<=1


char s[55];

void gao() {

int a,b;

bool f1,f2;

if (s1.empty()) {

puts("Wrong!");

return;

if (s1.size()==s2.size()) {

a=*s1.rbegin();

b=*s2.begin();

f1=a%2;

f2=b%2;

if (f1==f2) {

printf("%.0lf\n",(a*1.+b)/2.);

else {

printf("%.1lf\n",(a*1.+b)/2.);

else {

printf("%d\n",*s1.rbegin());

}
void gao1(int x) { //add x

if (s1.empty()) {

s1.insert(x);

else if (s1.size()==s2.size()) {

s2.insert(x);

s1.insert(*s2.begin());

s2.erase(s2.begin());

else {

s1.insert(x);

s2.insert(*s1.rbegin());

s1.erase(s1.find(*s1.rbegin()));

gao();

void gao2(int x) {

multiset<int>::iterator t1=s1.find(x),t2=s2.find(x);

if ((t1==s1.end()) && (t2==s2.end())) {

puts("Wrong!");

return;

if (s1.size()==s2.size()) {

if (t2!=s2.end()) {
s2.erase(t2);

else {

s1.erase(t1);

s1.insert(*s2.begin());

s2.erase(s2.begin());

else if (t1!=s1.end()) {

s1.erase(t1);

else {

s2.erase(t2);

s2.insert(*s1.rbegin());

s1.erase(s1.find(*s1.rbegin()));

gao();

int main() {

int i,x;

/*s1.clear();

s2.clear();*/

for (scanf("%d",&i);i;--i) {

scanf("%s%d",s,&x);
if (s[0]=='a') {

gao1(x);

else {

gao2(x);

return 0;

Merging Communities
#include <cmath>

#include <cstdio>

#include <vector>

#include <iostream>

#include <algorithm>

using namespace std;

int f[100005], s[100005];

int find(int x) {

if (x == f[x]) return x;

return f[x] = find(f[x]);

void merge(int x, int y) {

int fx = find(x), fy = find(y);

if (fx == fy) return;

if (s[fx] > s[fy]) {


f[fy] = fx;

s[fx] += s[fy];

} else {

f[fx] = fy;

s[fy] += s[fx];

int Q, N;

char c[5];

int main() {

scanf("%d%d", &N, &Q);

for (int i = 1; i <= N; ++i) f[i] = i, s[i] = 1;

for (int i = 0; i < Q; ++i) {

scanf("%s", c);

if (c[0] == 'M') {

int x, y; scanf("%d%d", &x, &y);

merge(x, y);

} else {

int x; scanf("%d", &x);

printf("%d\n", s[find(x)]);

return 0;

You might also like