Uva 10474 sort以及lower_bound的用法

现有N个大理石,每个大理石上写了一个非负整数。首先把各数从小到大排序,然后回 答Q个问题。每个问题问是否有一个大理石写着某个整数x,如果是,还要回答哪个大理石上 写着x。排序后的大理石从左到右编号为1~N。(在样例中,为了节约篇幅,所有大理石上 的数合并到一行,所有问题也合并到一行。)
样例输入:
4 1          (N Q)
2 3 5 1    (石头)
5             (问题)
5 2
1 3 3 3 1
2 3
样例输出:
CASE #1:
5 found at 4
CASE #2:
2 not found
3 found at 3

#include<iostream>
#include<stdio.h>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
    int N, Q;
    while (scanf("%d%d", &N, &Q) == 2)
    {
        int *a = new int[N];
        int *b = new int[Q];
        for(int i=0;i<N;i++)
            scanf("%d", &a[i]);
        for(int j=0;j<Q;j++)
            scanf("%d", &b[j]);
        sort(a, a + N);
        bool flag=false;
        for (int k = 0; k < Q; k++)
        {
            for (int l = 0; l < N; l++)
                if (b[k] == a[l]) { flag = true; printf("%d found at %d\n", b[k], l + 1); break; }
            if (flag != true) { printf("%d not found\n", b[k]); }
        }
        delete[]a;
        delete[]b;
        a = NULL;
        b = NULL;
    }
}

很基础的排序和查找,然而自己却卡在while上一会,不心细果然什么都办不了

用stl会快很多

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 10000;
int main()
{
    int n, q, x, a[maxn], kase = 0;
    while (scanf("%d%d", &n, &q) == 2 && n)
    {
        printf("CASE# %d:\n", ++kase);
        for (int i = 0; i < n; i++) scanf("%d", &a[i]);
        sort(a, a + n); //排序
        while(q--)
        {
            scanf("%d", &x);
            int p = lower_bound(a, a+n, x) - a; //在已排序数组a中寻找x
            if(a[p] == x) printf("%d found at %d\n", x, p+1);
            else printf("%d not found\n", x);
        }
    }
    return 0;
}

sort

排序,并非只是普通的快速排序,除了对普通的快速排序进行优化,它还结合了插入排序堆排序。根据不同的数量级别以及不同情况,能自动选用合适的排序方法。当数据量较大时采用快速排序,分段递归。一旦分段后的数据量小于某个阀值,为避免递归调用带来过大的额外负荷,便会改用插入排序。而如果递归层次过深,有出现最坏情况的倾向,还会改用堆排序。

#include <algorithm>

template< class RandomIt >
void sort( RandomIt first, RandomIt last );

template< class RandomIt, class Compare >
void sort( RandomIt first, RandomIt last, Compare comp );

cmp的用法自行定义

例如:

#include<iostream>
#include<stdio.h>
#include<string>
#include<algorithm>
using namespace std;
struct Node {
    int x, y;
}p[1001];
int n;
int cmp(Node a, Node b) {
    if (a.x != b.x) return a.x < b.x;  //如果a.x不等于b.x,就按x从小到大排
    return a.y < b.y;  //如果x相等按y从小到大排
}
int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d%d", &p[i].x, &p[i].y);
    sort(p + 1, p + n + 1, cmp);
    for (int i = 1; i <= n; i++) printf("%d %d\n", p[i].x, p[i].y);
    return 0;
}
lower_bound查找一个比自己大或者和自己相等的数并返回它的位置注意:调用lower_bound之前必须确定序列为有序序列,否则调用出错。
template<class ForwardIterator, class Type>
   ForwardIterator lower_bound(
      ForwardIterator _First,
      ForwardIterator _Last,
      const Type& _Val
   );
template<class ForwardIterator, class Type, class BinaryPredicate>
   ForwardIterator lower_bound(
      ForwardIterator _First,
      ForwardIterator _Last,
      const Type& _Val,
      BinaryPredicate _Comp
   );
传入参数说明:

_First 要查找区间的起始位置

_Last 要查找区间的结束位置

_Val 给定用来查找的值

_Comp 自定义的表示小于关系的函数对象,根据某个元素是否满足小于关系而返回true或者false
 

lower_bound(first,last,val)表示找到第一个>=val的值的地址 
upper_bound(first,last,val)表示找到第一个>val的值的地址

在lower_bound(first,last,val,cmp)中cmp是比较函数

在lower_bound(first,last,val)中cmp默认为

bool cmp(mid,val){
    return mid<val;//表示如果mid<val 则继续lower_bound
}

所以当mid>=val时停止

原文地址:https://www.cnblogs.com/seamusopen/p/8438671.html

时间: 2024-11-20 11:20:39

Uva 10474 sort以及lower_bound的用法的相关文章

UVA 10474:Where is the Marble?(STL初步)

 Where is the Marble?  Raju and Meena love to play with Marbles. They have got a lot of marbles with numbers written on them. At the beginning, Raju would place the marbles one after another in ascending order of the numbers written on them. Then Mee

uva 10474 Where is the Marble?(排序)

uva 10474 Where is the Marble? Raju and Meena love to play with Marbles. They have got a lot of marbles with numbers written on them. At the beginning, Raju would place the marbles one after another in ascending order of the numbers written on them.

UVA 10474 - Where is the Marble?

Where is the Marble?  Where is the Marble?  Raju and Meena love to play with Marbles. They have got a lot of marbles with numbers written on them. At the beginning, Raju would place the marbles one after another in ascending order of the numbers writ

UVA 10474 STL

题目实在是水题,主要是学习sort以及 lower_bound x为待查找的元素 int p=lower_bound(a,a+n,x)-p;返回a中第一个大于或等于x的元素的位置,使用lower_bound前要将数组进行排序.函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置.如果所有元素都小于val,则返回last的位置,且last的位置是越界的! upper_bound #include<iostream> #includ

Python: sort,sorted,OrderedDict的用法

Python: sort,sorted,OrderedDict的用法 from http://stqdd.com/archives/427 by 莫亚菜 python对容器内数据的排序有两种,一种是容器自己的sort函数,一种是内建的sorted函数. sort函数和sorted函数唯一的不同是,sort是在容器内排序,sorted生成一个新的排好序的容器. 对于一个简单的数组 L=[5,2,3,1,4]. sort: L.sort() sorted(...)    sorted(iterabl

UVA - 10474 - Where is the Marble? (基数排序)

UVA - 10474 Where is the Marble? Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Raju and Meena love to play with Marbles. They have got a lot of marbles with numbers written on them. At the beginni

【UVA - 10474 】Where is the Marble?(排序)

Where is the Marble? Descriptions: Raju and Meena love to play with Marbles. They have got a lot of marbles with numbers written on them. At the beginning, Raju would place the marbles one after another in ascending order of the numbers written on th

UVA 10474 大理石在哪 lower_bound

题意:找输入的数在排完序之后的位置. 主要是lower_bound 函数的使用.它的作用是查找大于或者等于x的第一个位置. #include<cstdio> #include<algorithm> using namespace std; const int maxn = 10000; int main() { int n,q,x,a[maxn],kase = 0; while(scanf("%d%d",&n,&q) == 2 &&

大理石在哪里UVa 10474

我自己写的代码 #include<iostream>#include<algorithm>using namespace std;int main(){    int N,a[100],b[100],Q,flag;    int k=1;    while(cin>>N>>Q)    {        for(int i=0;i<N;i++)        {            cin>>a[i];        }        fo