【細小碎的oi小知識點總結貼】不定時更新(顯然也沒人看qwq)

鎮樓圖:html

1.memcpy:node

從a數組中複製k個元素到b數組:ios

memcpy(b,a,sizeof(int)*k);git

#include<cstring>
#include<iostream>
#include<cstdio>
using namespace std;
int a[10],b[20];
int main(){
    for(int i=0;i<10;i++)
        cin>>a[i];
    for(int i=0;i<10;i++)
    cin>>b[i];
    memcpy(b,a,sizeof(int)*5);
    for(int i=0;i<20;i++)
    cout<<b[i]<<" ";
}

【輸入】數組

1 2 3 4 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2 1less

【輸出】ide

1 2 3 4 5 5 4 3 2 1 0 0 0 0 0 0 0 0 0 0 函數

(b數組的值被更新了,上面的話b數組的前k個值就被賦值變成了a數組的前k個值【從0開始qwq】b數組其餘值不變)spa

將a所有賦值給b:.net

memcpy(b,a,sizeof(a));

#include<cstring>
#include<iostream>
#include<cstdio>
using namespace std;
int a[10],b[20];
int main(){
    for(int i=0;i<10;i++)
        cin>>a[i];
    for(int i=0;i<10;i++)
    cin>>b[i];
    memcpy(b,a,sizeof(a));
    for(int i=0;i<20;i++)
    cout<<b[i]<<" ";
}

【輸入】

1 2 3 4 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2 1

【輸出】

1 2 3 4 5 6 7 8 9 10 0 0 0 0 0 0 0 0 0 0 

爲何忽然寫這個,由於用到了啊qwq(我是不會告訴你人家是題解上用的qwq)

 2.數據類的東西吧qwq:

①int範圍的無窮大:INT_MAX,無窮小INT_MIN。

②memset賦值:能夠正常賦值的:-1,0;神奇的賦值:(一個很大的數)0xfff(大概幾個f是沒問題的qwq)

 2.優先隊列(priority queue):

首先,你須要:

#include<queue>

優先隊列,顧名思義,是比對列更增強大的隊列。它的功能強大在它能夠自動排序。

自動排序是從大到小qwq

那麼如何從小到大排呢?

重載來幫忙:(做爲大括號不換行的異教徒)

struct node{
    int x,y;
    bool operator < (const node & a) const{
        return x<a.x;
    }
};

聲明:priority_queue<結構類型> 隊列名;

經常使用聲明格式:

priority_queue <node> q;
//node是一個結構體
//結構體裏重載了‘<’小於符號
priority_queue <int,vector<int>,greater<int> > q;
//注意後面兩個「>」不要寫在一塊兒,「>>」是右移運算符
//從小到大排序
priority_queue <int,vector<int>,less<int> >q;
//從大到小排序

基本操做:

 3.set

頭文件#include<set>

特色:

一、set中的元素都是排好序的

二、set集合中沒有重複的元素

基本操做

前向星存圖(一個神奇的超越鄰接矩陣的存在)

首先講一下須要定義的一些東西??

1.head數組:head[點數]:head[i]表示以當前點i爲起點的最後一條邊(這裏的最後指的是編號【咱們按輸入順序給邊編一個號】)。

這個圖即爲head[1]=4,表示以1爲起點的邊的最後一條是點1—>點5編號爲4的邊;

2.num_edge,表示邊的總個數;

3.結構體:

struct Edge{
    int next,to,dis;
}edge[邊數];

這裏,edge[i].next表示上一條以i爲起點的邊:

仍是上面那個圖,這裏edge[4].next=3;

edge[i].to表示這條邊i的終點;

edge[i].dis表示這條邊的權值;

複製代碼
void addedge(int from,int to,int dis){
    num_edge++;//由於存入一條邊,總邊數+1;
    edge[num_edge].next=head[from];//新存入一條以from爲起點的邊,以前的以from爲起點的最後一條邊變成了新邊的上一條邊
    edge[num_edge].to=to;//存終點
    edge[num_edge].dis=dis;//存權值
    head[from]=num_edge;//存入一條以from爲起點的邊,那麼如今以from爲起點的最後一條邊就是新存入的邊
}
複製代碼

提醒:若是要存的是無向圖,進行程序時應該:addedge(from,to,dis),addedge(to,from,dis)各跑一遍,所開空間也要*2;

memset的用法

memset按位賦值:一位等於8字節(一個int型是4位),(一字節能夠看作是二進制中的一位),對於memset,不管你這個數是什麼,它都會把每一位都變成第一位的數,若是咱們用memset賦值1的話,最後結果就是這個數:

(一個藍框爲1位)

而memset(a,63,sizeof(a));就是把a數組的初始值賦成了如下這個數:

floyd:

  1. floyd其實是一個DP(floyd:哈哈想不到吧qwq)
  2. floyd實際上是一個三維DP
  3. floyd其實就跟01揹包同樣把k這一維降掉了,所以k要放在最外層枚舉;
  4. 對於沒有降維的三維floyd disk,i,j來說,表示的是從i=>j只可能通過1——k這些點的最短路徑;

sort-cmp:

cmp函數的含義:若是返回值是 True,表示 要把 序列 (X,Y),X放Y前。
Tarjan:

關於tarjan:【here】

slow slow read瞭解一下:

首先是關於數據讀入的幾個結點:

  1. 1e4 能夠用cin,沒問題
  2. 1e5   scanf
  3. 1e6   getchar(也就是日常寫的快讀)
  4. 1e7   fread(要背板子比較好)
  5. 1e8及以上 emm,再見!

如今來寫一下1e6 getchar型slow slow read:

inline int read(){
    int ans=0;
    char last=' ',ch=getchar();
    while(ch<'0'||ch>'9') last = ch,ch=getchar();//過濾空格
    while(ch>='0'&&ch<='9') ans=ans*10+ch-'0',ch=getchar();
    if(last=='-') ans=-ans;
    return ans;
}

而後大體就是用getchar讀入(大概會快吧),而後轉化成數字;

而後(不知道爲何最近那麼喜歡用而後qwq),粘一粘各類不一樣大佬喜歡用的快讀板子qwq:

int read(){
    int x = 0; char c = gc();
    while(!isdigit(c)) c = gc();
    while(isdigit(c)){x = x * 10 + c - '0'; c = gc();}
    return x;
}
DDOSvoid Code
template <typename T>
inline void qr(T &x) {
  char ch;
  do { ch = getchar(); } while ((ch > '9') || (ch < '0'));
  do { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); } while ((ch >= '0') && (ch <= '9'));
}//(艱難的沒看懂)
zay Code
typedef int ll;
inline void read(ll &num)
{
    bool flag = 0;
    num = 0;
    char c = getchar();
    while ((c < '0' || c > '9') && c != '-')
        c = getchar();
    if (c == '-')
    {
        flag = 1;
        c = getchar();
    }
    num = c - '0';
    c = getchar();
    while (c >= '0' && c <= '9')
        num = (num << 3) + (num << 1) + c - '0', c = getchar();
    if (flag)
        num *= -1;
}
inline void read(char &c)
{
    c = getchar();
    while (c == '\n' || c == '\r' || c == '\0' || c == ' ' || c == '\t')
        c = getchar();
}
inline void output(ll num)
{
    if (num < 0)
    {
        putchar('-');
        num = -num;
    }
    if (num >= 10)
        output(num / 10);
    putchar(num % 10 + '0');
}
inline void outln(ll num)
{
    output(num);
    puts("");
}
inline void outsp(ll num)
{
    output(num);
    putchar(' ');
}
inline void outln(string str) { puts(str.c_str()); }
water_lift Code

而後對於zay大佬的fread,大概是隻能背個板子:

typedef long long int ll;

namespace IPT {
  const int L = 1000000;
  char buf[L], *front=buf, *end=buf;
  char GetChar() {
    if (front == end) {
      end = buf + fread(front = buf, 1, L, stdin);
      if (front == end) return -1;
    }
    return *(front++);
  }
}

template <typename T>
inline void qr(T &x) {
  char ch = IPT::GetChar(), lst = ' ';
  while ((ch > '9') || (ch < '0')) lst = ch, ch=IPT::GetChar();
  while ((ch >= '0') && (ch <= '9')) x = (x << 1) + (x << 3) + (ch ^ 48), ch = IPT::GetChar();
  if (lst == '-') x = -x;
}

 狀態壓縮中,如何較高效的求出共有多少個1:

(參考資料:求一個數的二進制序列中1的個數(3種方法)

 比較高效的方法是x=x&(x-1),當x=0時,進行此運算的次數也就是1的個數;

int count_one3(int m){
    int count = 0;
    while (m){
        m = m&(m - 1);
        count++;
    }
    return count;
}
相關文章
相關標籤/搜索