二分简单题

二分·二分查找

题目传送:hihoCoder - 1128 - 二分·二分查找

解法一:先排个序,然后在进行二分查找,复杂度为O(n*logn) 。

AC代码:

#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <cctype>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define INF 0x7fffffff
using namespace std;

int n, k;

int a[1000005];

int b_search(int x) {
    int L = 0, R = n - 1;
    while(L <= R) {
        int mid = (L + R) >> 1;
        if(a[mid] == x) return mid + 1;
        else if(a[mid] > x) R = mid - 1;
        else if(a[mid] < x) L = mid + 1;
    }
    return -1;
}

int main() {
    while(scanf("%d %d", &n, &k) != EOF) {
        for(int i = 0; i < n; i ++) {
            scanf("%d", &a[i]);
        }
        sort(a, a + n);
        int ans = b_search(k);
        printf("%d\n", ans);
    }
    return 0;
}

解法二:直接找是否存在k,并且有多少比k小的数,复杂度O(n)。

AC代码:

#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <cctype>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define INF 0x7fffffff
using namespace std;

int n, k, a;

int main() {
    scanf("%d %d", &n, &k);
    int ans = 0;
    int flag = 0;
    for(int i = 0; i < n; i ++) {
        scanf("%d", &a);
        if(a < k) ans ++;
        if(a == k) flag = 1;
    }
    if(flag == 1) {
        printf("%d\n", ans + 1);
    }
    else printf("-1\n");
    return 0;
}

二分·二分查找之k小数

题目传送:hihoCoder - 1133 - 二分·二分查找之k小数

解法一:直接先排个序,然后在找第k小数,复杂度为O(n*logn)。

AC代码:

#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <cctype>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define INF 0x7fffffff
using namespace std;

int n, k;
int a[1000005];

int main() {
    scanf("%d %d", &n, &k);
    for(int i = 1; i <= n; i ++) {
        scanf("%d", &a[i]);
    }
    sort(a + 1, a + n + 1);
    if(k > n || k < 1) printf("-1\n");
    else printf("%d\n", a[k]);
    return 0;
}

解法二:手写一个快排,其中排除掉不需要排序的区间,这样理论上可以达到O(n)的复杂度

AC代码:

#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <cctype>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define INF 0x7fffffff
using namespace std;

int a[1000005];
int n, k;

void Qsort(int *a, int low, int high) {
    if(low > k) return;
    if(high < k) return;
    if(low >= high) return;
    int first = low;
    int last = high;
    int key = a[first];
    while(first < last) {
        while(first < last && a[last] >= key) last --;
        a[first] = a[last];
        while(first < last && a[first] <= key) first ++;
        a[last] = a[first];
    }
    a[first] = key;
    Qsort(a, low, first - 1);
    Qsort(a, first + 1, high);
}

int main() {
    scanf("%d %d", &n, &k);
    for(int i = 1; i <= n; i ++) {
        scanf("%d", &a[i]);
    }

    Qsort(a, 1, n);

    printf("%d\n", a[k]);
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-17 15:39:39

二分简单题的相关文章

poj3041(二分匹配简单题)

题目链接:http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14022   Accepted: 7629 Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <

poj2105 IP Address(简单题)

题目链接:http://poj.org/problem?id=2105 Description Suppose you are reading byte streams from any device, representing IP addresses. Your task is to convert a 32 characters long sequence of '1s' and '0s' (bits) to a dotted decimal format. A dotted decima

poj 3270 Cow Sorting 置换群 简单题

假设初始状态为 a:2 3 1 5 4 6 则目标状态为 b:1 2 3 4 5 6且下标为初始状态中的3 1 2 4 5 6(a[3],a[1]...) 将置换群写成循环的形式 (2,3,1),(5,4),6就不用移动了. 移动方式2种 1:选循环内最小的数和其他len-1个数交换 2:选整个序列最小的数和循环内最小的数交换,转到1,再换回来. #include<cstdio> #include<queue> #include<algorithm> #include&

数论 --- 简单题

吃糖果 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 22376    Accepted Submission(s): 6396 Problem Description HOHO, 终于从Speakless手上赢走了所有的糖果,是Gardon吃糖果时有个特殊的癖好,就是不喜欢将一样的糖果放在一起吃,喜欢先吃一种,下一次吃另一 种,这样:

BZOJ 2683 简单题 ——CDQ分治

简单题 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define F(i,j,k) for (int i=j;i<=k;++i) #define D(i,j,k) for (int i=j;i>=k;--i) #define maxn 2000005 int sum[maxn]; void a

HNU 12868 Island (简单题)

题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12868&courseid=272 解题报告:输入n*m的地图,+表示土地,-表示水,要你求这个海岛的海岸线有多长,扫一遍就可以了. 1 #include<cstdio> 2 const int maxn = 2000; 3 char map[maxn][maxn]; 4 int _x[4] = {-1,0,1,0}; 5 int _y[4] = {0

poj 3112 Digital Biochemist Circuit(简单题)

题目链接:http://poj.org/problem?id=3112 Digital Biochemist Circuit Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 876   Accepted: 375 Description A digital biochemist circuit (DBC) is a device composed of a set of processing nodes. Each pro

hdu 1201 18岁生日 (简单题)

18岁生日 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 18281    Accepted Submission(s): 5776 Problem Description Gardon的18岁生日就要到了,他当然很开心,可是他突然想到一个问题,是不是每个人从出生开始,到达18岁生日时所经过的天数都是一样的呢?似乎并不全都是这样,所以他

bzoj3687简单题(dp+bitset优化)

3687: 简单题 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 861  Solved: 399[Submit][Status][Discuss] Description 小呆开始研究集合论了,他提出了关于一个数集四个问题:1.子集的异或和的算术和.2.子集的异或和的异或和.3.子集的算术和的算术和.4.子集的算术和的异或和.    目前为止,小呆已经解决了前三个问题,还剩下最后一个问题还没有解决,他决定把这个问题交给你,未来的集训队队员来实现