2020大一寒假ACM培訓 (優先佇列篇)

2021-10-02 19:31:27 字數 3063 閱讀 9168

優先佇列是一種具有受限訪問操作的儲存結構,元素可以以任意順序進入優先佇列,一旦元素在優先佇列容器中,出隊操作將出佇列中優先順序最高的元素。(可以理解為元素進入優先佇列自動排序)

構建:

priority_queue<

int,vector<

int>

,greater<

int>

> s;

//公升序排列

priority_queue

,less

> s;

//降序排列

//這裡注意變數名稱前面的兩個》要隔開,否則系統會誤認為是位運算子而報錯。

操作:

操作**實現

入隊vis.push(x)

判斷佇列是否為空

vis.empty()

判斷佇列中元素的數量

vis.size()

得到隊首元素

vis.top()

出隊vis.pop()

其他關於優先佇列的詳細知識,請移步大佬部落格

題目情況:

普通優先佇列

結構體優先佇列

b d e f

a c g

但是實現方法其實是一致的。

#include

int n,x,cut;

using

namespace std;

priority_queue<

int,vector<

int>

,greater<

int>

> s;

intmain()

cut=0;

while

(s.size()

>1)

while

(!s.

empty()

)printf

("%d\n"

,cut);}

return0;

}

注意平均等待時間的演算法;

#include

int n,x,cut,a,ti;

double ans;

using

namespace std;

struct sa

;bool

operator

<

(const sa &a,

const sa &b)

priority_queue

,less

> s;

intmain()

);}while

(!s.

empty()

) ans=

(double

)a/n*

1.0;

printf

("%\.2lf"

,ans);}

return0;

}

#include

using

namespace std;

//typedef long long ll;

priority_queue<

int, vector<

int>

,greater<

int>

>s;

int n,x,t;

intmain()

if(x==2)

if(x==3)

}return0;

}

#include

using

namespace std;

int n,x;

long

long cut,sum;

priority_queue<

int,vector<

int>

,greater<

int>

>s;

intmain()

while

(s.size()

>1)

printf

("%lld"

,sum)

;return0;

}

這個其實算是運用了矩陣的思維,看洛谷題解可能理解起來更清晰。

#include

typedef

long

long ll;

using

namespace std;

ll n,x,a[

400005

],b[

400005

],t;

struct sa

;bool

operator

<

(const sa &a,

const sa &b)

priority_queue

>s;

intmain()

sort

(a+1

,a+n+1)

;for

(int i=

1;i<=n;i++

)sort

(b+1

,b+1

+n);

for(

int i=

1;i<=n;i++))

;}for(

int i=

1;i<=n;i++))

;}return0;

}

#include

using

namespace std;

int n,k,j,t,l;

string x;

struct sa

;bool

operator

<

(const sa &a,

const sa &b)

intmain()

);} cin>>t;

while

(t--))

;}return0;

}

2020大一寒假ACM培訓 (二進位制列舉篇)

二進位制?列舉?二進位制列舉?聽都沒聽過。然而在我還沒搞懂的時候,大佬們已經ak了。otr otr otr acm培訓 大佬鑑賞大賽 真快樂!讓我這個小菜雞先來搞懂什麼是二進位制列舉吧。一 二進位制操作 算數字運算 1 與 對於指定的兩個數a 60 0011 1100 b 13 0000 1101 ...

大一寒假集訓(9) 優先佇列

定義乙個priority queue的方法為 priority queue name type通常為結構體 其中,type可以是任何基本型別或者容器,name為優先隊 列的名字。和queue不一樣的是,priority queue沒有front 和back 而只能通過top 或pop 訪問隊首元素 ...

2023年大一第一次寒假培訓二維陣列和結構體

最好用c 用c 打c的 也可以。最先打萬能標頭檔案 include。一,二維陣列 一維陣列 a n 相當於乙個1n的長方形,那麼二維陣列b n m 就相當於乙個nm的長方形。定義時,可以省略第一維的大小,但第二維的大小不可省略。向a 2 3 中輸入1 2 3 4 5 6,儲存的狀態就如 所示。45 ...