时间限制:1 秒
内存限制:32 兆
特殊判题:否
提交:919
解决:230
每个测试案例包括两行:
第一行有1个整数n,表示数组的大小。1<=n <= 10^6。
第二行有n个整数,表示数组元素,每个元素均为int。
第三行有1个整数m,表示接下来有m次查询。1<=m<=10^3。
下面有m行,每行有一个整数k,表示要查询的数。
81 2 3 3 3 3 4 513
4
利用二分查找,分别查找某个数字在数组中第一次出现的位置和最后一次出现的位置。
//============================================================================ // Name : judo1349.cpp // Author : wdy // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ //bisearch #include <iostream> #include <stdio.h> using namespace std; int num[1000001] = {0}; void init(int n){ for(int i = 0;i<n;i++) scanf("%d",num+i);; } int findL(int b,int e,int value){ if(b==e && value == num[0]) return 1; if(b==e && value != num[0]) return -1; int bid = b; int eid = e; int mid = bid + ((eid-bid)>>1); while(bid<eid-1){ mid = bid + ((eid-bid)>>1); if(num[mid]>= value) eid = mid; else bid = mid; } if(num[bid]==value) return bid; else if(num[eid]==value) return eid; else return -1;//not found } int findR(int b,int e,int value){ if(b==e && value == num[0]) return 1; if(b==e && value != num[0]) return -1; int bid = b; int eid = e; int mid = bid + ((eid-bid)>>1); while(bid<eid-1){ mid = bid + ((eid-bid)>>1); if(num[mid]<= value) bid = mid; else eid = mid; } if(num[eid]==value) return eid; else if(num[bid]==value) return bid; else return -1;// not found } void test(){ num[0] = 1; num[1] = 1; num[2] = 1; num[3] = 3; num[4] = 3; std::cout<<"L"<<findL(3,4,4)<<std::endl; std::cout<<"R"<<findR(3,4,4)<<std::endl; } void findValue(int b,int e,int value){ std::cout<<findR(b,e,value)-findL(b,e,value)+1<<std::endl; } void judo(){ int n; int m; int value; while(scanf("%d",&n) != EOF){ init(n); scanf("%d",&m); while(m>0){ scanf("%d",&value); findValue(0,n-1,value); m--; } } } int main() { //cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!! judo(); return 0; } /************************************************************** Problem: 1349 User: KES Language: C++ Result: Wrong Answer ****************************************************************/
//============================================================================ // Name : judo1349.cpp // Author : wdy // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ //bisearch #include <iostream> #include <stdio.h> using namespace std; int num[1000001] = {0}; void init(int n){ for(int i = 0;i<n;i++) scanf("%d",num+i);; } int findL(int b,int e,int value){ if(b==e && value == num[0]) return 1; if(b==e && value != num[0]) return -1; int bid = b; int eid = e; int mid = bid + ((eid-bid)>>1); while(bid<eid-1){ mid = bid + ((eid-bid)>>1); if(num[mid]>= value) eid = mid; else bid = mid; } if(num[bid]==value) return bid; else if(num[eid]==value) return eid; else return -1;//not found } int findR(int b,int e,int value){ if(b==e && value == num[0]) return 1; if(b==e && value != num[0]) return -1; int bid = b; int eid = e; int mid = bid + ((eid-bid)>>1); while(bid<eid-1){ mid = bid + ((eid-bid)>>1); if(num[mid]<= value) bid = mid; else eid = mid; } if(num[eid]==value) return eid; else if(num[bid]==value) return bid; else return -1;// not found } void test(){ num[0] = 1; num[1] = 1; num[2] = 1; num[3] = 3; num[4] = 3; std::cout<<"L"<<findL(3,4,4)<<std::endl; std::cout<<"R"<<findR(3,4,4)<<std::endl; } void findValue(int b,int e,int value){ int R = findR(b,e,value); int L = findL(b,e,value); if( R>0) std::cout<<R-L+1<<std::endl; else std::cout<<0<<std::endl; } void judo(){ int n; int m; int value; while(scanf("%d",&n) != EOF){ init(n); scanf("%d",&m); while(m>0){ scanf("%d",&value); findValue(0,n-1,value); m--; } } } int main() { //cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!! judo(); return 0; } /************************************************************** Problem: 1349 User: KES Language: C++ Result: Wrong Answer ****************************************************************/