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;
    }
    

      

  • 相关阅读:
    SEO常用外链资源站整理分享
    不同的LINUX系统,跨服务器快速拷贝文件
    WPS表格、Excel常用技巧大全,99%人都不知道,但非常实用!
    php 5.4中php-fpm 的重启、终止操作命令
    帝国CMS伪静态
    Centos7访问Win7/Win10系统中的共享文件
    H3C S5500三层交换机划分Vlan与H3C路由组网
    H3C S5500V2交换机误格式化恢复
    linux_centos7_时间更新
    Centos7安装mysql数据库
  • 原文地址:https://www.cnblogs.com/tonghao/p/5116513.html
Copyright © 2011-2022 走看看