寒假集训作业(7)——排序查找

来源:互联网 发布:centos下关闭selinux 编辑:程序博客网 时间:2024/05/21 10:43

利用STL库进行的查找:

#include <string>#include <list>#include <algorithm>#include <iostream>using namespace std;int a[100010],b[100010];int main(){    list <int> search;    list <int>::iterator searcher;    int n,m,temp1,temp2;    cin>>n>>m;    for(int i=0;i<=n-1;i++)    {        temp1=0;        cin>>temp1;        search.push_back(temp1);    }    for(int i=0;i<=m-1;i++)    {        temp2=0;        cin>>temp2;        searcher=find(search.begin(),search.end(),temp2);        if (searcher == search.end())        {            cout << "NO" << endl;        }        else        {            cout << "YES" << endl;        }    }}


以上的代码又可以当作模板。大概测试了一下,100000组数据,查找100000个,只需十秒。但仍然超时= =

以下是hash查找,即把需要查找的数字作为新数组hash的下标

#include <stdio.h>  const int maxn = 1000001;  int hash[maxn];  int main()  {      int t,m;      scanf("%d %d", &t, &m);      for(int i = 0; i < t; i++)      {          int n;          scanf("%d", &n);          hash[n]++;      }      for(int i = 0; i < m; i++)      {          int n;          scanf("%d", &n);          if(hash[n])          {              puts("YES");          }          else puts("NO");      }      return 0;  }


#include<string.h>#include<iostream>using namespace std;int main(){    int i,j,p,k,length;    char s[110];    while(cin>>s)    {        k=0;        length=strlen(s);        for(i=k; i<length; i++)        {            if(s[k]<s[i])k=i;        }        for(j=0; j<=k; j++)            cout<<s[j];        cout<<"(max)";        for(i=k+1; i<length; i++)        {            if(s[i]==s[k])            {               cout<<s[i];               cout<<"(max)";            }            else cout<<s[i];        }        cout<<endl;    }}

求最大元素(ASCII)


排序问题 猥琐龌龊淫荡的浪川把时间限定到了15ms,用cin竟然总是25ms,以后还是改用scanf吧= =

模板是MoreWindows写的,链接如下:

http://blog.csdn.net/morewindows/article/details/6678165

#include <iostream>#include <algorithm>#include <functional>#include <cstdio>using namespace std;int a[1000010];//将有二个有序数列a[first...mid]和a[mid...last]合并。void mergearray(int a[], int first, int mid, int last, int temp[]){int i = first, j = mid + 1;int m = mid,   n = last;int k = 0;while (i <= m && j <= n){if (a[i] >= a[j])temp[k++] = a[i++];elsetemp[k++] = a[j++];}while (i <= m)temp[k++] = a[i++];while (j <= n)temp[k++] = a[j++];for (i = 0; i < k; i++)a[first + i] = temp[i];}void mergesort(int a[], int first, int last, int temp[]){if (first < last){int mid = (first + last) / 2;mergesort(a, first, mid, temp);    //左边有序mergesort(a, mid + 1, last, temp); //右边有序mergearray(a, first, mid, last, temp); //再将二个有序数列合并}}/*bool MergeSort(int a[], int n){int *p = new int[n];if (p == NULL)return false;mergesort(a, 0, n - 1, p);delete[] p;return true;}*/int temp[1000010];int main(){    //freopen("D:\\aaa.txt","r",stdin);    //freopen("D:\\ans2.txt","w",stdout);    int n;    while(~scanf("%d",&n))    {        for(int i=0;i<=n-1;i++)        {           scanf("%d",&a[i]);        }        mergesort(a,0,n+1,temp);        //sort(a,a+n+1,greater<int>());        for(int i=0;i<=n-1;i++)        {            if(i!=n-1) printf("%d ",a[i]);            else printf("%d\n",a[i]);        }    }}

经测试,sort 跟mergesort在这组数据中是差不多的。