码迷,mamicode.com
首页 > 编程语言 > 详细

#1128 : 二分·二分查找 ( 两种方法 先排序在二分O(nlogN) + 直接二分+快排思想O(2N) )

时间:2015-04-13 09:37:29      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:


#1128 : 二分·二分查找
时间限制: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

//先排序在二分  48ms

#include<cstdio>
#include<algorithm>
//#include<bits/stdc++.h>
using namespace std;
template<class T>inline T read(T&x)
{
    char c;
    while((c=getchar())<=32)if(c==EOF)return 0;
    bool ok=false;
    if(c=='-')ok=true,c=getchar();
    for(x=0; c>32; c=getchar())
        x=x*10+c-'0';
    if(ok)x=-x;
    return 1;
}
template<class T> inline T read_(T&x,T&y)
{
    return read(x)&&read(y);
}
template<class T> inline T read__(T&x,T&y,T&z)
{
    return read(x)&&read(y)&&read(z);
}
template<class T> inline void write(T x)
{
    if(x<0)putchar('-'),x=-x;
    if(x<10)putchar(x+'0');
    else write(x/10),putchar(x%10+'0');
}
template<class T>inline void writeln(T x)
{
    write(x);
    putchar('\n');
}
//-------ZCC IO template------
const int maxn=1000011;
const double inf=999999999;
#define lson (rt<<1),L,M
#define rson (rt<<1|1),M+1,R
#define M ((L+R)>>1)
#define For(i,t,n) for(int i=(t);i<(n);i++)
typedef long long  LL;
typedef double DB;
typedef pair<int,int> P;
#define bug printf("---\n");
#define mod 10007

LL a[maxn];
int bs(int left,int right,int key)
{
    while(left<=right)
    {
        int mid=(left+right)>>1;
        if(key==a[mid])return mid;
        else if(a[mid]>key)
            right=mid-1;
        else
            left=mid+1;
    }
    return -1;
}

int main()
{
    //#ifndef ONLINE_JUDGE
   // freopen("in.txt","r",stdin);
    //#endif // ONLINE_JUDGE
    int n,m,i,j,k,t;
    while(read_(n,k))
    {
        For(i,1,n+1)
        {
            read(a[i]);
        }
        sort(a+1,a+n+1);
        writeln(bs(1,n+1,k));
    }
    return 0;
}


//用二分的思想, 28ms

#include<cstdio>
#include<algorithm>
//#include<bits/stdc++.h>
using namespace std;
template<class T>inline T read(T&x)
{
    char c;
    while((c=getchar())<=32)if(c==EOF)return 0;
    bool ok=false;
    if(c=='-')ok=true,c=getchar();
    for(x=0; c>32; c=getchar())
        x=x*10+c-'0';
    if(ok)x=-x;
    return 1;
}
template<class T> inline T read_(T&x,T&y)
{
    return read(x)&&read(y);
}
template<class T> inline T read__(T&x,T&y,T&z)
{
    return read(x)&&read(y)&&read(z);
}
template<class T> inline void write(T x)
{
    if(x<0)putchar('-'),x=-x;
    if(x<10)putchar(x+'0');
    else write(x/10),putchar(x%10+'0');
}
template<class T>inline void writeln(T x)
{
    write(x);
    putchar('\n');
}
//-------ZCC IO template------
const int maxn=1000011;
const double inf=999999999;
#define lson (rt<<1),L,M
#define rson (rt<<1|1),M+1,R
#define M ((L+R)>>1)
#define For(i,t,n) for(int i=(t);i<(n);i++)
typedef long long  LL;
typedef double DB;
typedef pair<int,int> P;
#define bug printf("---\n");
#define mod 10007
LL a[maxn];
int bs(int left,int right,int key)
{
    if(left>right)
    {
        if(a[left]==key)return left-1;
        else return -1;
    }
    int mid=a[left];
    int low=left;
    int high=right;
    while(low<high)
    {
        while(low<high&&a[high]>=mid)
            high--;
        if(low<high)swap(a[high],a[low]);
        while(low<high&&a[low]<=mid)
            low++;
        if(low<high)swap(a[high],a[low]);
    }
    if(mid<key)
        return bs(low+1,right,key);
    else
        return bs(left,low-1,key);
}



int main()
{
    //#ifndef ONLINE_JUDGE
   // freopen("in.txt","r",stdin);
    //#endif // ONLINE_JUDGE
    int n,m,i,j,k,t;
    while(read_(n,k))
    {
        For(i,1,n+1)
        {
            read(a[i]);
        }
        writeln(bs(1,n+1,k));
    }
    return 0;
}
















#1128 : 二分·二分查找 ( 两种方法 先排序在二分O(nlogN) + 直接二分+快排思想O(2N) )

标签:

原文地址:http://blog.csdn.net/u013167299/article/details/45014951

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!