uva 10474 - Where is the Marble?(排序,二分搜索)

點擊打開鏈接


題意:給出一個亂序序列,求key排在第幾。

貌似用counting sort比較快

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAX_N = 10000;
int N, Q;
int a[MAX_N+10];
int main()
{
	//freopen("in.txt", "r", stdin);
	int t = 1;
	while(scanf("%d%d", &N, &Q), N+Q)
	{
		printf("CASE# %d:\n", t++);
		for(int i = 0; i < N; i++)
			scanf("%d", a+i);
		sort(a, a+N);
		for(int i = 0; i < Q; i++)
		{
			int key;
			scanf("%d", &key);
			int *x = lower_bound(a, a+N, key);
			if(*x == key)
				printf("%d found at %d\n", key, x-a+1);
			else
				printf("%d not found\n", key);
		}

	}
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章