#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <queue>
using namespace std;

class C {
public:
  bool operator() (int a, int b) {
    return a>b;
  }
};

struct rec {
  int x,y;
};

bool operator< (const rec& a, const rec& b) {
  return a.x<b.x || a.x==b.x && a.y<b.y;
}

bool cmp(const rec& a, const rec& b) {
  return a.x<b.x || a.x==b.x && a.y<b.y;
}

bool cmp2(const rec& a, const rec& b) {
  return a.x>b.x || a.x==b.x && a.y>b.y;
}

int main() {
  rec x[100];
  vector<rec> y;

  sort(x,x+100,cmp2);
  reverse(x,x+100);
  
  string s;
  reverse(s.begin(),s.end());


  /*
    cytse => 0
    cmliu => 7
    mhchan => 2
   */

  map<string,int> mm;
  mm["cytse"]=1;
  mm["cmliu"]=7;
  mm["mhchan"]=2;

  cout << mm["cytse"] << endl;
  cout << mm["cytse2"] << endl;

  cout << mm.size() << endl;

  map<string,int>::iterator itr;
  
  itr=mm.find("kwchong");
  if (itr!=mm.end()) {
    cout << "found kwchong" << endl;
  } else {
    cout << "not found" << endl;
  }

  for (itr=mm.begin(); itr!=mm.end(); itr++) {
    cout << itr->first << " => " << itr->second << endl;
  }

  cout << "***" << endl;

  for (map<string,int>::reverse_iterator ritr=mm.rbegin(); ritr!=mm.rend(); ritr++) {
    cout << ritr->first << " => " << ritr->second << endl;
  }

  mm.erase("cytse2");

  set<string> ss;
  ss.insert("cytse");
  ss.erase("cytse");
  
  set<string>::iterator itr2=ss.find("cytse");
  if (itr2==ss.end())
    cout << "not found" << endl;
  else
    cout << *itr2 << endl;

  vector<string> vv;
  vv.push_back("cytse");
  vv.push_back("cmliu");

  /*
    Input: 2 1 4 7 4 8 3 6 4 7
    Problem: Find the largest number
   */

  /*
    Solution 1:
    Sort the list
   */

  int a[10]={2,1,4,7,4,8,3,6,4,7};
  sort(a,a+10);
  cout << a[9] << endl;

  /*
    Problem: What if... query not necessarily follows all input
   */

  cout << "Prioirty queue"<< endl;

  priority_queue<int,vector<int>,greater<int> > pq;
  /*
    alternatively, you may use:
    priority_queue<int,vector<int>,C> pq;
   */
  pq.push(2);
  pq.push(1);
  pq.push(4);
  cout << pq.top() << endl;

  pq.push(7);
  pq.push(4);
  cout << pq.top() << endl;

  pq.pop();
  cout << pq.top() << endl;

  pq.pop();
  cout << pq.top() << endl;

  pq.pop();
  cout << pq.top() << endl;

  /*
    next permutation
   */

  int b[4]={1,2,3,4};
  
  cout << "Permutation!!!"<< endl;

  do {
    for (int i=0; i<4; i++)
      cout << b[i] << ' ';
    cout << endl;
  } while (next_permutation(b,b+4));

  /* binary search */

  int c[10]={1,2,4,8,16,32,64,128,256,512};
  
  cout << "Binary search "<< endl;

  cout << binary_search(c,c+10,16) << endl; /* 1 */
  cout << binary_search(c,c+10,17) << endl; /* 0 */

  /*
    lower bound: return the itr/ptr of smallest element that is not less
                 than the search key
		 e.g. searching 17 in c will return the iterator/pointer of 32
		         "      32  "                                       32
   */
  int *p=lower_bound(c,c+10,17);
  cout << *p << endl;

  p = lower_bound(c,c+10,32);
  cout << *p << endl;

  /* the following code finds the location of 32 in c */
  int k=lower_bound(c,c+10,32)-c;
  cout << k << endl;

  /* moliu STL */

  int c1=101,c2=102,c3=103,c4=104;
  swap(c1,c2);

  cout << max(c1,c2) << endl;
  cout << min(c1,c2) << endl;

  /*
    (c1>c2? c1:c2)
   */

  vector<int> v1;
  vector<int> v2;
  
  if (v1<v2) {
  }

  pair<int,int> pa;
  /*  itr->first, itr->second */

  pa.first=10;
  pa.second=20;

  return 0;
}
