• hihocoder 第三十六周 二分·二分查找


    时间限制:10000ms
    单点时限:1000ms
    内存限制:256MB

    描述

    Nettle最近在玩《艦これ》,因此Nettle收集了很多很多的船(这里我们假设Nettle氪了很多金,开了无数个船位)。去除掉重复的船之后,还剩下N(1≤N≤1,000,000)种不同的船。每一艘船有一个稀有值,任意两艘船的稀有值都不相同,稀有值越小的船越稀有,价值也就越高。
    Nettle现在通过大建又造出了一艘船,他想知道这艘船是不是重复的。如果是重复的,那么这艘船在Nettle所有的船里面稀有值排多少位。

    问题一
    Nettle已经先把自己所有船按照稀有值从小到大排列好了(a[1..N]),我们要做的是看看新得到的船(假设稀有值为K)是否在这个序列中,且有对应的a[i]=K时,i为多少?

    提示一:有序数组的二分查找

    问题二
    因为Nettle的船太多了,他不愿意去给所有船按照稀有值排序,而是直接告诉了我们每一艘船的稀有值。在这种情况下我们该如何解决这个问题呢?

    提示二:非有序数组的二分查找

    输入

    第1行:2个整数N,K。N表示数组长度,K表示需要查找的数;
    第2行:N个整数,表示a[1..N],保证不会出现重复的数,1≤a[i]≤2,000,000,000。

    输出

    第1行:一个整数t,表示K在数组中是第t小的数,若K不在数组中,输出-1。

    样例输入
    10 5180
    2970 663 5480 4192 4949 1 1387 4428 5180 2761
    样例输出
    9
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstdlib>
     4 #include<cstring>
     5 #include<string>
     6 #include<queue>
     7 #include<algorithm>
     8 #include<map>
     9 #include<iomanip>
    10 #include<climits>
    11 #include<string.h>
    12 #include<cmath>
    13 #include<stdlib.h>
    14 #include<vector>
    15 #include<stack>
    16 #include<set>
    17 using namespace std;
    18 #define INF 1000000007
    19 #define MAXN 40010
    20 #define Mod 1000007
    21 #define N 10007
    22 #define NN 30
    23 #define sigma_size 3
    24 const int maxn = 6e5 + 10;
    25 using namespace std;
    26 typedef long long LL;
    27 const double pi = acos(-1);
    28 
    29 int partition(int *a,int left,int right)
    30 {
    31     a[0] = a[left];
    32     while (left < right) {
    33         while (left < right && a[right] >= a[0]) right--;
    34         a[left] = a[right];
    35         while (left < right && a[left] <= a[0]) left++;
    36         a[right] = a[left];
    37     }
    38     a[left] = a[0];
    39     return left;
    40 }
    41 
    42 void QuickSort(int *a, int left, int right)
    43 {
    44     if (left < right) {
    45         int mid = partition(a,left,right);
    46         QuickSort(a,left,mid-1);
    47         QuickSort(a, mid + 1, right);
    48     }
    49 }
    50 
    51 int bsearch(int *a, int left, int right,int tar)
    52 {
    53     if (tar < a[left] || tar > a[right] || left > right) return -1;
    54         int mid = (left + right) / 2;
    55         if (a[mid] < tar)
    56             bsearch(a, mid + 1, right, tar);
    57         else if (a[mid] > tar)
    58             bsearch(a, left, mid - 1, tar);
    59         else return mid;
    60 }
    61 
    62 int a[1000010];
    63 int main()
    64 {
    65     int n, t, x;
    66     scanf("%d%d",&n,&t);
    67     for (int i = 1; i <= n; ++i)
    68         scanf("%d", &a[i]);
    69     QuickSort(a, 1, n);
    70     int re = bsearch(a, 1, n, t);
    71     printf("%d
    ",re);
    72     //system("pause");
    73     return 0;
    74 }
  • 相关阅读:
    Ajax的个人总结
    JSON和计算机网络的个人总结
    Bootstrap内辅助类,响应式工具,组件的个人总结
    Bootstrap内栅格布局,表格,按钮,图片的个人总结
    [BUG] Linux font family error #153
    WPF在WindowStyle=None时去掉顶部白条
    WPF 之 左键弹出操作菜单,并禁用右键菜单
    WPF 气泡提示框的简单实现
    WPF TextBox 如何简单粗暴的实现水印效果?
    c#笔记--WPF文本框和密码框添加水印效果(背景文字提示)
  • 原文地址:https://www.cnblogs.com/usedrosee/p/4321772.html
Copyright © 2020-2023  润新知