zoukankan      html  css  js  c++  java
  • Codeforces 600B Queries about less or equal elements(二分查找)

    Description

    You are given two arrays of integers a and b. For each element of the second array bj you should find the number of elements in array athat are less than or equal to the value bj.

    Input

    The first line contains two integers n, m (1 ≤ n, m ≤ 2·105) — the sizes of arrays a and b.

    The second line contains n integers — the elements of array a ( - 109 ≤ ai ≤ 109).

    The third line contains m integers — the elements of array b ( - 109 ≤ bj ≤ 109).

    Output

    Print m integers, separated by spaces: the j-th of which is equal to the number of such elements in array a that are less than or equal to the value bj.

    Sample Input

    Input
    5 4
    1 3 5 7 9
    6 4 2 8
    Output
    3 2 1 4
    Input
    5 5
    1 2 1 2 5
    3 1 4 1 5
    Output
    4 2 4 2 5
     题意:找出a数组中比b数组中每个数小或者相等的数的个数
    题解:c++中的upper_bound(a,a+n,k)返回的就是第一个大于k的位置指针(a按照升序排列)如果a按照降序排列则是第一个小于k的位置指针
           lower_bound(a,a+n,k)返回的就是第一个大于等于k的位置指针(a按照升序排列)如果a按照降序排列则是第一个小于等于k的位置指针
     
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<algorithm>
    #define MAX 200100
    using namespace std;
    int a[MAX],b[MAX];
    int c[MAX],d[MAX];
    int main()
    {
    	int n,m,j,i;
    	int s;
    	while(scanf("%d%d",&n,&m)!=EOF)
    	{
    		for(i=0;i<n;i++)
    			scanf("%d",&a[i]);
    		for(i=0;i<m;i++)
    			scanf("%d",&b[i]);
    		sort(a,a+n);
    		for(i=0;i<m;i++)
    		{
    			if(i==m-1)
    			    printf("%d
    ",upper_bound(a,a+n,b[i])-a);
    			else
    			    printf("%d ",upper_bound(a,a+n,b[i])-a);
    		}
    //		__int64 Max=-1;	
    //		memset(a,0,sizeof(0));
    //		memset(b,0,sizeof(b));
    //		memset(d,0,sizeof(d));
    //		for(i=1;i<=n;i++)
    //		{
    //			scanf("%I64d",&s);
    //			if(s>0)
    //			d[s]++;
    //			a[s]=s;
    //			Max=max(Max,s);
    //		}
    //		for(i=1;i<=m;i++)
    //		{
    //			scanf("%I64d",&s);
    //			b[i]=s;
    //		}
    //		for(i=1;i<=Max;i++)
    //		{
    //			if(a[i]==0)
    //			    a[i]=i+1;
    //		}
    //		sort(a,a+n);
    //		memset(c,0,sizeof(c));
    //		for(i=1;i<=Max;i++)
    //		{
    //			if(a[i]<=i)
    //			    c[i]+=(c[i-1]+d[i]);
    //			else
    //			    c[i]=c[i-1];
    //		}		
    //		for(i=1;i<m;i++)
    //			printf("%I64d ",c[b[i]]);
    //		printf("%I64d
    ",c[b[m]]);	
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    ReSharper 10.0.0.2 Ultimate 破解
    Image 和byte[]之间的转换
    IntelliJ IDEA 2016.1.3(64) license server 与汉化
    冒泡排序
    选择排序
    希尔排序
    插入排序
    redis学习笔记——Redis过期键的删除策略
    redis学习笔记——应用场景
    perl学习笔记——字符串和排序
  • 原文地址:https://www.cnblogs.com/tonghao/p/5116513.html
Copyright © 2011-2022 走看看