Language: Java

#include<iostream>
#include<cstdio>
#include<queue>
#include<stack>
#include<cassert>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n)   REP(i,0,n)

const int N = 20000;//max element
queue<int> B;
stack<int> F;
deque<int> minback,maxback;
int minfront[N],minp=0;
int maxfront[N],maxp=0;

//operation front
inline int fPop(){
  int ret=F.top();
  F.pop();
  if (minp>0&&minfront[minp-1] == ret)minp--;
  if (maxp>0&&maxfront[maxp-1] == ret)maxp--;
  return ret;
}

inline void fPush(int v){
  F.push(v);
  if (minp==0 || minfront[minp-1] > v)minfront[minp++]=v;
  if (maxp==0 || maxfront[maxp-1] < v)maxfront[maxp++]=v;
}

inline int getIthMinFront(int i){
  assert(minp-i >= 0);
  return minfront[minp-i];
}

inline int getIthMaxFront(int i){
  assert(maxp-i >= 0);
  return maxfront[maxp-i];
}

//operation back
inline int bPop(){
  int ret=B.front();
  B.pop();
  if (!minback.empty() && minback.front() == ret)minback.pop_front();
  if (!maxback.empty() && maxback.front() == ret)maxback.pop_front();
  return ret;
}

inline void bPush(int v){
  B.push(v);
  while(!minback.empty()){
    if (minback.back() > v)minback.pop_back();
    else break;
  }
  minback.push_back(v);

  while(!maxback.empty()){
    if (maxback.back() < v)maxback.pop_back();
    else break;
  }
  maxback.push_back(v);
}

inline int getIthMinBack(int i){
  assert(i <= (int)minback.size());
  return minback[i-1];
}

inline int getIthMaxBack(int i){
  assert(i <= (int)maxback.size());
  return maxback[i-1];
}

void push(int v){
  if (F.size() == 0){
    fPush(v);
  }else if (F.size() == B.size()){
    int tmp=bPop();
    fPush(tmp);
    bPush(v);
  }else if (F.size() > B.size()){
    bPush(v);
  }else assert(false);
}

int pop(){
  assert(F.size() != 0);
  int ret;
  if (F.size() == B.size()){
    int tmp=bPop();
    ret=fPop();
    fPush(tmp);
  }else if (F.size() > B.size()){
    ret=fPop();
  }else assert(false);
  return ret;
}

int main(){
  int q;
  while(scanf("%d",&q) && q){
    minback.clear();
    maxback.clear();
    minp=0;
    maxp=0;
    while(!B.empty())B.pop();
    while(!F.empty())F.pop();

    rep(k,q){
      int val,ret,in;
      scanf("%d",&in);
      switch(in){
      case 0:
	scanf("%d",&val);
	push(val);
	break;
      case 1:
	ret=pop();
	//cout <<"pop " <<  ret << endl;
	printf("%d\n",ret);
	break;
      case 2:
	//cout << "minfront " << getIthMinFront(1)<<endl;
	printf("%d\n",getIthMinFront(1));
	break;
      case 3:
	//cout <<"minback " << getIthMinBack(1) << endl;
	printf("%d\n",getIthMinBack(1));
	break;
      case 4:
	scanf("%d",&val);
	//cout <<val <<" th min front " << getIthMinFront(val) << endl;
	printf("%d\n",getIthMinFront(val));
	break;
      case 5:
	scanf("%d",&val);
	//cout <<val <<" th min back " << getIthMinBack(val) << endl;
	printf("%d\n",getIthMinBack(val));
	break;
      case 6:
	//cout << "maxfront " << getIthMaxFront(1)<<endl;
	printf("%d\n",getIthMaxFront(1));
	break;
      case 7:
	//cout << "maxback " << getIthMaxBack(1) << endl;
	printf("%d\n",getIthMaxBack(1));
	break;
      case 8:
	scanf("%d",&val);
	//cout <<val <<" th max front " << getIthMaxFront(val) << endl;
	printf("%d\n",getIthMaxFront(val));
	break;
      case 9:
	scanf("%d",&val);
	//cout <<val <<" th max back " << getIthMaxBack(val) << endl;
	printf("%d\n",getIthMaxBack(val));
	break;
      }
    }
    cout <<"end"<<endl;
  }
  return false;
}

Compiler output:
Main.java:1: error: illegal character: '#'
#include<iostream>
^
Main.java:2: error: illegal character: '#'
#include<cstdio>
^
Main.java:3: error: illegal character: '#'
#include<queue>
^
Main.java:4: error: illegal character: '#'
#include<stack>
^
Main.java:5: error: illegal character: '#'
#include<cassert>
^
Main.java:7: error: illegal character: '#'
#define REP(i,b,n) for(int i=b;i<n;i++)
^
Main.java:7: error: class, interface, enum, or record expected
#define REP(i,b,n) for(int i=b;i<n;i++)
        ^
Main.java:7: error: class, interface, enum, or record expected
#define REP(i,b,n) for(int i=b;i<n;i++)
                               ^
Main.java:7: error: class, interface, enum, or record expected
#define REP(i,b,n) for(int i=b;i<n;i++)
                                   ^
Main.java:8: error: illegal character: '#'
#define rep(i,n)   REP(i,0,n)
^
Main.java:11: error: unnamed classes are a preview feature and are disabled by default.
queue<int> B;
^
  (use --enable-preview to enable unnamed classes)
Main.java:13: error: class, interface, enum, or record expected
deque<int> minback,maxback;
^
Main.java:14: error: class, interface, enum, or record expected
int minfront[N],minp=0;
^
Main.java:15: error: class, interface, enum, or record expected
int maxfront[N],maxp=0;
^
Main.java:18: error: class, interface, enum, or record expected
inline int fPop(){
^
Main.java:20: error: class, interface, enum, or record expected
  F.pop();
  ^
Main.java:21: error: class, interface, enum, or record expected
  if (minp>0&&minfront[minp-1] == ret)minp--;
  ^
Main.java:22: error: class, interface, enum, or record expected
  if (maxp>0&&maxfront[maxp-1] == ret)maxp--;
  ^
Main.java:23: error: class, interface, enum, or record expected
  return ret;
  ^
Main.java:24: error: class, interface, enum, or record expected
}
^
Main.java:28: error: class, interface, enum, or record expected
  if (minp==0 || minfront[minp-1] > v)minfront[minp++]=v;
  ^
Main.java:29: error: class, interface, enum, or record expected
  if (maxp==0 || maxfront[maxp-1] < v)maxfront[maxp++]=v;
  ^
Main.java:30: error: class, interface, enum, or record expected
}
^
Main.java:34: error: class, interface, enum, or record expected
  return minfront[minp-i];
  ^
Main.java:35: error: class, interface, enum, or record expected
}
^
Main.java:39: error: class, interface, enum, or record expected
  return maxfront[maxp-i];
  ^
Main.java:40: error: class, interface, enum, or record expected
}
^
Main.java:45: error: class, interface, enum, or record expected
  B.pop();
  ^
Main.java:46: error: class, interface, enum, or record expected
  if (!minback.empty() && minback.front() == ret)minback.pop_front();
  ^
Main.java:47: error: class, interface, enum, or record expected
  if (!maxback.empty() && maxback.front() == ret)maxback.pop_front();
  ^
Main.java:48: error: class, interface, enum, or record expected
  return ret;
  ^
Main.java:49: error: class, interface, enum, or record expected
}
^
Main.java:53: error: class, interface, enum, or record expected
  while(!minback.empty()){
  ^
Main.java:55: error: class, interface, enum, or record expected
    else break;
    ^
Main.java:56: error: class, interface, enum, or record expected
  }
  ^
Main.java:59: error: class, interface, enum, or record expected
  while(!maxback.empty()){
  ^
Main.java:61: error: class, interface, enum, or record expected
    else break;
    ^
Main.java:62: error: class, interface, enum, or record expected
  }
  ^
Main.java:64: error: class, interface, enum, or record expected
}
^
Main.java:68: error: class, interface, enum, or record expected
  return minback[i-1];
  ^
Main.java:69: error: class, interface, enum, or record expected
}
^
Main.java:73: error: class, interface, enum, or record expected
  return maxback[i-1];
  ^
Main.java:74: error: class, interface, enum, or record expected
}
^
Main.java:79: error: class, interface, enum, or record expected
  }else if (F.size() == B.size()){
  ^
Main.java:81: error: class, interface, enum, or record expected
    fPush(tmp);
    ^
Main.java:82: error: class, interface, enum, or record expected
    bPush(v);
    ^
Main.java:83: error: class, interface, enum, or record expected
  }else if (F.size() > B.size()){
  ^
Main.java:85: error: class, interface, enum, or record expected
  }else assert(false);
  ^
Main.java:86: error: class, interface, enum, or record expected
}
^
Main.java:91: error: class, interface, enum, or record expected
  if (F.size() == B.size()){
  ^
Main.java:93: error: class, interface, enum, or record expected
    ret=fPop();
    ^
Main.java:94: error: class, interface, enum, or record expected
    fPush(tmp);
    ^
Main.java:95: error: class, interface, enum, or record expected
  }else if (F.size() > B.size()){
  ^
Main.java:97: error: class, interface, enum, or record expected
  }else assert(false);
  ^
Main.java:98: error: class, interface, enum, or record expected
  return ret;
  ^
Main.java:99: error: class, interface, enum, or record expected
}
^
Main.java:103: error: class, interface, enum, or record expected
  while(scanf("%d",&q) && q){
  ^
Main.java:105: error: class, interface, enum, or record expected
    maxback.clear();
    ^
Main.java:106: error: class, interface, enum, or record expected
    minp=0;
    ^
Main.java:107: error: class, interface, enum, or record expected
    maxp=0;
    ^
Main.java:108: error: class, interface, enum, or record expected
    while(!B.empty())B.pop();
    ^
Main.java:109: error: class, interface, enum, or record expected
    while(!F.empty())F.pop();
    ^
Main.java:111: error: class, interface, enum, or record expected
    rep(k,q){
    ^
Main.java:113: error: class, interface, enum, or record expected
      scanf("%d",&in);
      ^
Main.java:114: error: class, interface, enum, or record expected
      switch(in){
      ^
Main.java:117: error: class, interface, enum, or record expected
	push(val);
	^
Main.java:118: error: class, interface, enum, or record expected
	break;
	^
Main.java:119: error: class, interface, enum, or record expected
      case 1:
      ^
Main.java:122: error: class, interface, enum, or record expected
	printf("%d\n",ret);
	^
Main.java:123: error: class, interface, enum, or record expected
	break;
	^
Main.java:124: error: class, interface, enum, or record expected
      case 2:
      ^
Main.java:127: error: class, interface, enum, or record expected
	break;
	^
Main.java:128: error: class, interface, enum, or record expected
      case 3:
      ^
Main.java:131: error: class, interface, enum, or record expected
	break;
	^
Main.java:132: error: class, interface, enum, or record expected
      case 4:
      ^
Main.java:135: error: class, interface, enum, or record expected
	printf("%d\n",getIthMinFront(val));
	^
Main.java:136: error: class, interface, enum, or record expected
	break;
	^
Main.java:137: error: class, interface, enum, or record expected
      case 5:
      ^
Main.java:140: error: class, interface, enum, or record expected
	printf("%d\n",getIthMinBack(val));
	^
Main.java:141: error: class, interface, enum, or record expected
	break;
	^
Main.java:142: error: class, interface, enum, or record expected
      case 6:
      ^
Main.java:145: error: class, interface, enum, or record expected
	break;
	^
Main.java:146: error: class, interface, enum, or record expected
      case 7:
      ^
Main.java:149: error: class, interface, enum, or record expected
	break;
	^
Main.java:150: error: class, interface, enum, or record expected
      case 8:
      ^
Main.java:153: error: class, interface, enum, or record expected
	printf("%d\n",getIthMaxFront(val));
	^
Main.java:154: error: class, interface, enum, or record expected
	break;
	^
Main.java:155: error: class, interface, enum, or record expected
      case 9:
      ^
Main.java:158: error: class, interface, enum, or record expected
	printf("%d\n",getIthMaxBack(val));
	^
Main.java:159: error: class, interface, enum, or record expected
	break;
	^
Main.java:160: error: class, interface, enum, or record expected
      }
      ^
Main.java:163: error: class, interface, enum, or record expected
  }
  ^
Main.java:165: error: class, interface, enum, or record expected
}
^
93 errors