杂七杂八的知识

1.重载运算符

理解并不难,主要是格式值得注意。
另外要注意在队列中要注意排序的顺序,q.top()/q.front() 访问的是队首元素,如果queue是升序,q.front()就是最大的;

#include <iostream>
#include <queue>
using namespace std;

struct student {
  string name;
  int score;
};

struct cmp {
  bool operator()(const student& a, const student& b) const {
    return a.score < b.score || (a.score == b.score && a.name > b.name);
  }
};

priority_queue<student, vector<student>, cmp> pq;
int main()  {
  return 0;
}

2.sort

当用sort排序vector时,这样写:

sort(vector.begin() , vector.end() , cmp);

3.高精度计算器

模板好长啊qwq

  #include <cstdio>
  #include <cstring>

  static const int LEN = 1004;

  int a[LEN], b[LEN], c[LEN], d[LEN];

  void clear(int a[]) {
    for (int i = 0; i < LEN; ++i) a[i] = 0;
  }

  void read(int a[]) {
    static char s[LEN + 1];
    scanf("%s", s);

    clear(a);

    int len = strlen(s);
    for (int i = 0; i < len; ++i) a[len - i - 1] = s[i] - '0';
  }

  void print(int a[]) {
    int i;
    for (i = LEN - 1; i >= 1; --i)
      if (a[i] != 0) break;
    for (; i >= 0; --i) putchar(a[i] + '0');
    putchar('\n');
  }

  void add(int a[], int b[], int c[]) {
    clear(c);

    for (int i = 0; i < LEN - 1; ++i) {
      c[i] += a[i] + b[i];
      if (c[i] >= 10) {
        c[i + 1] += 1;
        c[i] -= 10;
      }
    }
  }

  void sub(int a[], int b[], int c[]) {
    clear(c);

    for (int i = 0; i < LEN - 1; ++i) {
      c[i] += a[i] - b[i];
      if (c[i] < 0) {
        c[i + 1] -= 1;
        c[i] += 10;
      }
    }
  }

  void mul(int a[], int b[], int c[]) {
    clear(c);

    for (int i = 0; i < LEN - 1; ++i) {
      for (int j = 0; j <= i; ++j) c[i] += a[j] * b[i - j];

      if (c[i] >= 10) {
        c[i + 1] += c[i] / 10;
        c[i] %= 10;
      }
    }
  }

  bool greater_eq(int a[], int b[], int last_dg, int len) {
    if (a[last_dg + len] != 0) return true;
    for (int i = len - 1; i >= 0; --i) {
      if (a[last_dg + i] > b[i]) return true;
      if (a[last_dg + i] < b[i]) return false;
    }
    return true;
  }

  void div(int a[], int b[], int c[], int d[]) {
    clear(c);
    clear(d);

    int la, lb;
    for (la = LEN - 1; la > 0; --la)
      if (a[la - 1] != 0) break;
    for (lb = LEN - 1; lb > 0; --lb)
      if (b[lb - 1] != 0) break;
    if (lb == 0) {
      puts("> <");
      return;
    }

    for (int i = 0; i < la; ++i) d[i] = a[i];
    for (int i = la - lb; i >= 0; --i) {
      while (greater_eq(d, b, i, lb)) {
        for (int j = 0; j < lb; ++j) {
          d[i + j] -= b[j];
          if (d[i + j] < 0) {
            d[i + j + 1] -= 1;
            d[i + j] += 10;
          }
        }
        c[i] += 1;
      }
    }
  }

  int main() {
    read(a);

    char op[4];
    scanf("%s", op);

    read(b);

    switch (op[0]) {
      case '+':
        add(a, b, c);
        print(c);
        break;
      case '-':
        sub(a, b, c);
        print(c);
        break;
      case '*':
        mul(a, b, c);
        print(c);
        break;
      case '/':
        div(a, b, c, d);
        print(c);
        print(d);
        break;
      default:
        puts("> <");
    }

    return 0;
  }

最长上升子序列 $ O(nlogn) $ 的做法

挺难想的 , 我觉得考场现写俺也调不出来

  #include <bits/stdc++.h>
  using namespace std;

  const int N = 1e5+10;
  int n,len;
  int a[N],stk[N];

  int main() {
  	ios::sync_with_stdio(false);
  	cin.tie(0);
  	cin >> n;
  	for(int i=1 ; i<=n ; i++)	cin >> a[i];
  	
  	stk[0] = -2e9 ;
  	for(int i=1 ; i<=n ; i++)	{
  		if(a[i] > stk[len])	stk[++len] = a[i];
  		else	stk[lower_bound(stk+1 , stk+len+1 , a[i]) - stk] = a[i];
  	}
  	
  	cout << len << endl;
  	return 0;
  }
posted @ 2023-08-10 22:32  Hu_taooo  阅读(37)  评论(1)    收藏  举报