UVa 10474 - Where is the Marble?

来源:互联网 发布:ubuntu eclipse jdk 编辑:程序博客网 时间:2024/05/29 12:15

题目:给你一些数字和一些查询,问排序后,这些查询的数字最早出现的位置。

分析:简单题。排序后,直接二分查找即可。

说明:排序后直接从前向后扫描也可以,不影响整体效率。

#include <algorithm>#include <iostream>#include <cstdlib>#include <cstdio>using namespace std;int D[10001];int bs( int r, int a ){int mid = r/2,l = 0;while ( l < r ) {mid = l+(r-l)/2;if ( D[mid] < a )l = mid+1;else r = mid;}return r;} int main(){int n,m,T = 1,temp,space;while ( ~scanf("%d%d",&n,&m) && m+n ) {for ( int i = 0 ; i < n ; ++ i )scanf("%d",&D[i]);sort( D, D+n );printf("CASE# %d:\n",T ++);for ( int i = 0 ; i < m ; ++ i ) {scanf("%d",&temp);space = bs( n, temp );if ( D[space] == temp )printf("%d found at %d\n",temp,space+1);else printf("%d not found\n",temp);}}return 0;}

0 0
原创粉丝点击