冰楓論壇

 找回密碼
 立即註冊
ads_sugarbook
搜索
查看: 994|回覆: 0
打印 上一主題 下一主題

[心得] [演算法心得] Codefoces. Binary Search

[複製鏈接]

7

主題

0

好友

11

積分

新手上路

Rank: 1

UID
320966
帖子
146
主題
7
精華
0
積分
11
楓幣
756
威望
11
存款
0
贊助金額
0
推廣
0
GP
31
閱讀權限
10
性別
保密
在線時間
9 小時
註冊時間
2021-9-30
最後登入
2022-7-6
跳轉到指定樓層
1
發表於 2022-5-13 18:00:06 |只看該作者 |倒序瀏覽
本帖最後由 jimmyhealer 於 2022-5-13 18:01 編輯

嗨大家好~ 我是新加入論壇的萌新,之後可能會每日更新一下演算法心得,那目前會在 c/c++ 版,如果大家很熱絡的話再申請 演算法/競賽程式 版吧!

--------------------------------------------------- 正文開始 ---------------------------------------------------

題目名稱: Binary Search
題目網站: Codefoces
題目網址: https://codeforces.com/problemset/problem/1436/C
題目內容:

Andrey thinks he is truly a successful developer, but in reality he didn't know about the binary search algorithm until recently. After reading some literature Andrey understood that this algorithm allows to quickly find a certain number x in an array. For an array a indexed from zero, and an integer x the pseudocode of the algorithm is as follows:
180007dbhoplllhcwvhipo.png

Note that the elements of the array are indexed from zero, and the division is done in integers (rounding down).

Andrey read that the algorithm only works if the array is sorted. However, he found this statement untrue, because there certainly exist unsorted arrays for which the algorithm find x!

Andrey wants to write a letter to the book authors, but before doing that he must consider the permutations of size n such that the algorithm finds x in them. A permutation of size n is an array consisting of n distinct integers between 1 and n in arbitrary order.

Help Andrey and find the number of permutations of size n which contain x at position pos and for which the given implementation of the binary search algorithm finds x (returns true). As the result may be extremely large, print the remainder of its division by 10^9+7.

題目大意:

給你三個數字 n, x, pos,分別代表陣列長度、要尋找的數、以及那個數位置。
問你陣列有多少種排列,可以使 binary_search return true。

範例輸入 #1:
4 1 2

範例輸出 #1:
6

範例輸入 #2:
123 42 24

範例輸出 #2:
824071958

--------------------------------------------------- 題解心得 ---------------------------------------------------


可以先觀察 binary_search 的演算法,每次可以 mid 與下一個值的大小關係,最後確定目標位置左右大小數量。
陣列可以設為 [左邊空格] [目標] [右邊空格]。
所以就會總結出公式 C(左邊空格, 左邊小於目標的數字) * C(右邊空格, 右邊大於目標的數字) 。
  1. #include<bits/stdc++.h>
  2. #define ll long long
  3. #define INF 0x3f3f3f3f
  4. #define MOD 1000000007
  5. #define io iOS::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  6. using namespace std;
  7. const int MXN = 10;

  8. void sol(){
  9.   ll ans = 1, n = 0, m = 0, pos = 0;
  10.   ll x;
  11.   cin >> n >> x >> pos;
  12.   ll xl = x - 1, xr = n - x;
  13.   int l = 0, r = n;
  14.   while(l < r){
  15.     int mid = (l + r) >> 1;
  16.     if(mid <= pos){
  17.       l = mid + 1;
  18.       if(mid < pos){
  19.         ans = (ans * xl) % MOD;
  20.         xl--;
  21.       }
  22.     }
  23.     else{
  24.       ans = (ans * xr) % MOD;
  25.       xr--;
  26.       r = mid;
  27.     }
  28.   }
  29.   for(int i = 1; i <= xl + xr; ++i) ans = (ans * i) % MOD;
  30.   cout << ans << '\n';
  31. }

  32. int main(){
  33.   //io
  34.   int t = 1;
  35.   while(t--){
  36.     sol();
  37.   }
  38. }

複製代碼
收藏收藏0 推0 噓0


把本文推薦給朋友或其他網站上,每次被點擊增加您在本站積分: 1骰子
複製連結並發給好友,以賺取推廣點數
簡單兩步驟,註冊、分享網址,即可獲得獎勵! 一起推廣文章換商品、賺$$
高級模式
B Color Image Link Quote Code Smilies |上傳

廣告刊登意見回饋關於我們職位招聘本站規範DMCA隱私權政策

Copyright © 2011-2024 冰楓論壇, All rights reserved

免責聲明:本網站是以即時上載留言的方式運作,本站對所有留言的真實性、完整性及立場等,不負任何法律責任。

而一切留言之言論只代表留言者個人意見,並非本網站之立場,用戶不應信賴內容,並應自行判斷內容之真實性。

小黑屋|手機版|冰楓論壇

GMT+8, 2024-4-24 12:44

回頂部