2020牛客寒假演算法基礎集訓營1

2021-10-02 13:25:05 字數 3994 閱讀 8315

找規律,推公式

三角形個數為2(m

+n)m

n(m−

1)(n

−1

)2(m+n)mn(m-1)(n-1)

2(m+n)

mn(m

−1)(

n−1)

#include

#include

#include

#include

using

namespace std;

typedef

long

long ll;

const

int m =

1e9+7;

intmain

(void

)

簽到

#include

#include

#include

#include

using

namespace std;

intmain

(void

)

簽到

#include

#include

#include

#include

using

namespace std;

const

int n =

1e5+5;

bool a[n]

;int

main

(void

)for

(int i =

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

(a[i]==0

)printf

("%d\n"

, i)

;return0;

}

之前求約數的個數時,將求解操作寫成了自定義函式,結果tle了

聽h哥的把自定義函式寫到了主函式裡面,就過了

#include

#include

#include

#include

using

namespace std;

typedef

long

long ll;

const

int n =

1e6+5;

intmain

(void

) n = cnt;

ans++;}

cout << ans << endl;

return0;

}

圖論,這道題本菜雞還不會qaq

#include

using

namespace std;

#define ll long long

int fa[

111111];

//父親

int kdm[

111111];

//孩子數量

string color;

intf

(int x)

void

uni(

int x,

int y)

else}}

ll t[

111111];

//統計連通塊白點數量

vector<

int>g[

111111];

ll gao

(vector<

int>temp)

,sum[n]

=,s=0;

sum[0]

=s=temp[0]

;for

(i=0

;i)for

(i=1

;i)for

(i=1

;i)return res+dp[n-1]

;}intmain()

ll sum=0;

for(i=

1;i<=n;i++

)t[i]

=kdm[

f(i)]+

1;for(i=

1;i<=n;i++

) sum+

=gao

(temp);}

} cout<}

尺取法

#include

#include

using

namespace std;

const

int n =

2e5+5;

const

int inf =

0x3f3f3f3f

;char str[n]

;int al[30]

;int

main

(void

)//結束遍歷

if(r >= n)

break

;//更新答案

ans =

min(r - l, ans)

;//左端點右移

al[str[l]

-'a']--

; num =0;

for(

int i =

0; i <

26; i++

) num =

max(num, al[i]);

l++;}

if(ans == inf)

//不存在滿足條件的區間

printf

("-1\n");

else

printf

("%d\n"

, ans)

;return0;

}

這道題的資料比較水,只計算了將0轉化為1的情況就通過了,按照題目的意思是既要考慮0變1也要考慮1變0

還是尺取法

每次右端點向右移,直至遍歷到0且k個轉化次數都用完了,此時更新答案

左端點左移,直至空出乙個轉化次數

#include

#include

#include

const

int n =

2e5+5;

using

namespace std;

char str[n]

;int

main

(void

)//更新答案

ans =

max(r - l, ans)

;//遍歷結束

if(r >= n)

break

;//左端點右移

while

(str[l]

!='0'

) cnt--

; l++;}

printf

("%d\n"

, ans)

;return0;

}

線性dp,太菜了,連這種最簡單的dp都沒有想上來

#include

#include

#include

#include

using

namespace std;

const

int n =

3e5+5;

typedef

long

long ll;

string s;

ll dp[n]

;int

main

(void)

cout << dp[n -1]

<< endl;

return0;

}

2020牛客寒假演算法基礎集訓營

長期更新,補完為止 2 g 判正誤 題意 t組資料。判斷a d b e c f是否等於g。1e9 a,b,c,g 1e9,0 d,e,f 1e9。保證不會出現指數和底數同為 0 的情況。思路 硬算會tle或mle。快速冪取模,為了增加過題概率,多取幾個模數判斷。includeusing namesp...

2020牛客寒假演算法基礎集訓營1

h題 突然發現h題可以用好多種方法做的。方法一 雙指標,維護乙個修改次數小於等於k的區間 include using namespace std typedef long long ll const int mod 1e9 7 const int n 1e5 5 const int inf 0x3f...

2020牛客寒假演算法基礎集訓營1

a honoka和格點三角形 honoka最近在研究三角形計數問題。她認為,滿足以下三個條件的三角形是 好三角形 1.三角形的三個頂點均為格點,即橫座標和縱座標均為整數。2.三角形的面積為1 3.三角形至少有一條邊和 x軸或y 軸平行。honoka想知道,在平面中選取乙個大小為 的矩形格點陣,可以找...