再探uvaoj10474

来源:互联网 发布:瑜伽自己在家练 知乎 编辑:程序博客网 时间:2024/06/07 11:58

这回我用快排先把输入的N个数字先排序,然后用折半查找去找Q个数字 在 N个排序完的数中的位置

不料,WA了,为什么呢。

想了想,原来这题不能用折半,差点被这题意蒙了,直接教条般的搬折半直接WA无悬念

附上折半的代码如下:(折半之后能通过题目给的2个测试案例,但是换一些就不行了)

最后是用了快排之后不用折半的AC解法,奇怪的是我用快排为什么省的时间还不如不用呢,我的代码到底哪些地方花费太多时间呢,1.575,有点难接受。

附上AC图

Problem  VerdictLangTimeBestRankSubmit Timediscuss10474 -Where is the Marble? AcceptedC++1.5750.02424891 mins ago

#include<iostream>using std::cin;using std::cout;int mN[10050];int partition(int a[], int low, int high){    int pivot = a[low];    while(low < high)    {        while(low < high && a[high] >= pivot)                --high;        a[low] = a[high];        while(low < high && a[low] <= pivot)                ++low;        a[high] = a[low];    }    a[low] = pivot;    return low;}void sort(int a[], int low, int high){    if(low < high)    {        int pivotpos = partition(a,low,high);        sort(a, low, pivotpos-1);        sort(a, pivotpos+1, high);    }}int Binary_Search(int a[], int key,int l,int h){    int mid,low = l,high = h;    while(low <= high)    {        mid = (low+high)/2;        if(a[mid] == key)        return mid;        else if(a[mid] > key)        high = mid-1;        else low = mid+1;    }    return -1;}int main(){    int N,Q,RES;    int i,j,_CASE = 0;    int mQ;    cin >> N,cin >> Q;    while(N != 0 || Q != 0)    {        _CASE++;       for(i = 0; i < N; i++)       {           cin >> mN[i];       }        sort(mN,0,N-1);       cout << "CASE# " << _CASE << ":\n";       for(j = 0; j < Q; j++)          {              cin >> mQ;               RES = Binary_Search(mN,mQ,0,N-1);            if(RES != -1)                cout << mQ << " found at "<< (RES+1) << "\n";            else                cout << mQ << " not found\n";          }        cin >> N,cin >> Q;    }        return 0;}


#include<iostream>using std::cin;using std::cout;int marbleNumbers[10010];int partition(int a[], int low, int high){    int pivot = a[low];    while(low < high)    {        while(low < high &&a[high] >= pivot)                --high;        a[low] = a[high];        while(low < high && a[low] <= pivot)                ++low;        a[high] = a[low];    }    a[low] = pivot;    return low;}void sort(int a[], int low, int high){    if(low < high)    {        int pivotpos = partition(a,low,high);        sort(a, low, pivotpos-1);        sort(a, pivotpos+1, high);    }}int main(){    int N,Q,find;    int i,j,_CASE = 0;    int marbleQueries;    cin >> N,cin >> Q;    while(N != 0 || Q != 0)    {        _CASE++;       for(i = 0; i < N; i++)       {           cin >> marbleNumbers[i];       }        sort(marbleNumbers,0,N-1);       cout << "CASE# " << _CASE << ":\n";       for(j = 0; j < Q; j++)          {              cin >> marbleQueries;                find = 0;                for(i = 0; i <= N-1; i++)                {                    if(marbleQueries == marbleNumbers[i])                    {                        find = 1;                        cout << marbleQueries << " found at " << i+1 << "\n";                        break;                    }                }                if(find == 0)                cout << marbleQueries << " not found"<< "\n";          }        cin >> N,cin >> Q;    }    return 0;}