牛客小白月賽 30

2021-10-11 13:01:56 字數 4795 閱讀 4772

並查集或者最小生成樹

//並查集

#include

#include

#include

#include

using

namespace std;

typedef long

long ll;

const

int maxn=

2e+5

;int f[maxn]

;struct node

e[maxn]

;bool

cmp(node a,node b)

void

init

(int n)

}int

find

(int x)

intmain()

sort

(e+1

,e+m+

1,cmp)

;int cnt1=

0,cnt2=0;

for(

int i=

1;i<=m;i++)if

(cnt1==n-1)

}}if(cnt1==n-1)

cout<

else

cout<

<-1

;}

線段樹

#include

#include

#include

#include

using

namespace std;

typedef long

long ll;

const

int maxn=

2e5+5;

struct node

t[maxn<

<2]

;int w[maxn]

;void

pushup

(int rt)

else

else}}

void

build

(int u,

int l,

int r)

int mid=

(l+r)

>

>1;

build

(u<

<

1,l,mid)

;build

(u<

<1|

1,mid+

1,r)

;pushup

(u);

}void

updata

(int x,

int y,

int z)

int mid=

(t[x]

.l+t[x]

.r)>

>1;

if(z<=mid)

updata

(x<

<

1,y,z)

;else

updata

(x<

<1|

1,y,z)

;pushup

(x);

}node query

(int x,

int l,

int r);}

node res=

;int mid=

(t[x]

.l+t[x]

.r)>

>1;

if(l<=mid)

if(r>mid)

}return res;

}int n,m;

char s[10]

;int

main()

else

}}

模擬水題

#include

#include

using

namespace std;

typedef long

long ll;

intmain()

ll res=m+sum;

cout<

}

素數篩

題目的大意就是找最大公約數,素數之間沒有最大公約數

所以找到最多可能的素數然後再加上乙個非素數就有了最大公約數

所以最後答案就是素數個數+1

//尤拉素數篩

#include

#include

#include

#include

#include

using

namespace std;

const

int maxn=

1e5+5;

int pri[maxn]

;int pd[maxn]

;void

pri()}

}int

main()

}

字串模擬相加的弱化版,沒有進製,注意一下為0時候特殊判斷一下就行

#include

#include

using

namespace std;

string

addstrings

(string num1,

string num2)

if(a2 >=0)

if(sum >=10)

//對應位相加 再加上進製 如果大於》=10說明要進製 更新step為1

else

ret.

insert(0

,1, sum +

'0')

; a1--

; a2--;}

return ret;

}int

main()

else

if(flag==1)

}if(flag==0)

cout<

<0;

return0;

}

貪心,一直選最大的那個就行,剩下最後乙個不拿;

所以答案就是,全體的和+最大的數*(n-2);

//貪心

#include

#include

#include

#include

using

namespace std;

typedef long

long ll;

const

int maxn=

2e5+5;

intmain()

res=sum+maxx*

(n-2);

cout<

}

模擬

先排序然後比較

沒有找到比他大的就往下找

找到的話就兩個一起向下

#include

#include

#include

#include

using

namespace std;

const

int maxn=

2e5+5;

intmain()

for(

int i=

1;i<=m;i++

)sort

(a+1

,a+n+1)

;sort

(b+1

,b+1

+m);

int s1=

1,s2=1;

int cnt=0;

while

(s1<=n&&s2<=m)

else

} cout<

}

優先佇列的用法,預設大根堆,降序排列

//優先佇列

#include

#include

#include

#include

#include

using

namespace std;

const

int maxn=

2e5+5;

int a[maxn]

;priority_queue<

int> q;

intmain()

sort

(a+1

,a+1

+n);

for(

int i=

1;i<=

min(k,n)

;i++

)int c,s;

for(

int i=

1;i<=m;i++

) s=q.

top();

cout<

}else

if(q.

top(

)>s)}}

}

dp,從第乙個開始向右遍歷 dp[i][1]是選第i個,dp[i][0]是不選

#include

#include

#include

using

namespace std;

typedef long

long ll;

const

int maxn=

2e5+5;

ll a[maxn]

,cnt[maxn]

,dp[maxn][2

];intmain()

dp[1]

[1]=cnt[1]

; ll maxx=dp[1]

[1];

for(

int i=

2;i)//用桶排序

cout<

}

牛客小白月賽3

a 我們規定母音字母有a e i o u,並且規定半母音字母y也是母音字母。cwbc在學習英語,xhrlyb為了讓cwbc的記憶更加深刻,於是她讓cwbc把每個字串的所有字母都變成乙個恰好不大於它本身的小寫母音字母。可是cwbc比較貪玩,並且他想讓你幫他完成這個任務。聰明的你在仔細閱讀題目後,一定可...

牛客小白月賽6

c 挑花 dfs或bfs 桃花一簇開無主,可愛深紅映淺紅。題百葉桃花 桃花長在桃樹上,樹的每個節點有乙個桃花,調皮的htbest想摘盡可能多的桃花。htbest有乙個魔法棒,摘到樹上任意一條鏈上的所有桃花,由於htbest法力有限,只能使用一次魔法棒,請求出htbest最多可以摘到多少個桃花。第一行...

牛客小白月賽16

很容易得到n 1時,因為小石先手,所以小石一定輸 而n!1時,假設n 5 小石先取1 小陽取2 4 小石去3 小陽輸,無論怎樣小石都有贏的機會 includeusing namespace std int main 打表求出1 1e3之間的所有三角形每層之和 include define ll lo...