再探uvaoj10474
来源:互联网 发布:瑜伽自己在家练 知乎 编辑:程序博客网 时间:2024/06/07 11:58
这回我用快排先把输入的N个数字先排序,然后用折半查找去找Q个数字 在 N个排序完的数中的位置
不料,WA了,为什么呢。
想了想,原来这题不能用折半,差点被这题意蒙了,直接教条般的搬折半直接WA无悬念
附上折半的代码如下:(折半之后能通过题目给的2个测试案例,但是换一些就不行了)
最后是用了快排之后不用折半的AC解法,奇怪的是我用快排为什么省的时间还不如不用呢,我的代码到底哪些地方花费太多时间呢,1.575,有点难接受。
附上AC图
Problem Verdict Lang Time Best Rank Submit Time
#include<iostream>using std::cin;using std::cout;int mN[10050];int partition(int a[], int low, int high){ int pivot = a[low]; while(low < high) { while(low < high && a[high] >= pivot) --high; a[low] = a[high]; while(low < high && a[low] <= pivot) ++low; a[high] = a[low]; } a[low] = pivot; return low;}void sort(int a[], int low, int high){ if(low < high) { int pivotpos = partition(a,low,high); sort(a, low, pivotpos-1); sort(a, pivotpos+1, high); }}int Binary_Search(int a[], int key,int l,int h){ int mid,low = l,high = h; while(low <= high) { mid = (low+high)/2; if(a[mid] == key) return mid; else if(a[mid] > key) high = mid-1; else low = mid+1; } return -1;}int main(){ int N,Q,RES; int i,j,_CASE = 0; int mQ; cin >> N,cin >> Q; while(N != 0 || Q != 0) { _CASE++; for(i = 0; i < N; i++) { cin >> mN[i]; } sort(mN,0,N-1); cout << "CASE# " << _CASE << ":\n"; for(j = 0; j < Q; j++) { cin >> mQ; RES = Binary_Search(mN,mQ,0,N-1); if(RES != -1) cout << mQ << " found at "<< (RES+1) << "\n"; else cout << mQ << " not found\n"; } cin >> N,cin >> Q; } return 0;}
#include<iostream>using std::cin;using std::cout;int marbleNumbers[10010];int partition(int a[], int low, int high){ int pivot = a[low]; while(low < high) { while(low < high &&a[high] >= pivot) --high; a[low] = a[high]; while(low < high && a[low] <= pivot) ++low; a[high] = a[low]; } a[low] = pivot; return low;}void sort(int a[], int low, int high){ if(low < high) { int pivotpos = partition(a,low,high); sort(a, low, pivotpos-1); sort(a, pivotpos+1, high); }}int main(){ int N,Q,find; int i,j,_CASE = 0; int marbleQueries; cin >> N,cin >> Q; while(N != 0 || Q != 0) { _CASE++; for(i = 0; i < N; i++) { cin >> marbleNumbers[i]; } sort(marbleNumbers,0,N-1); cout << "CASE# " << _CASE << ":\n"; for(j = 0; j < Q; j++) { cin >> marbleQueries; find = 0; for(i = 0; i <= N-1; i++) { if(marbleQueries == marbleNumbers[i]) { find = 1; cout << marbleQueries << " found at " << i+1 << "\n"; break; } } if(find == 0) cout << marbleQueries << " not found"<< "\n"; } cin >> N,cin >> Q; } return 0;}
- 再探uvaoj10474
- uvaoj10474
- UVaOJ10474---Where is the Marble?
- uvaoj10474---详解细节(二分法)注意!
- 再探Widget
- 再探 makefile
- 再探MSCHART
- 再探 makefile
- 再探dxRTTIInspector
- 再探fragment
- javascript再探
- 再探AOP
- 再探AsyncTask
- 再探jQuery
- 再探KVO
- mysql再探
- 再探constrcut2
- 再探Struts框架
- 强化System.Convert.ChangeType, 使其能够转换枚举ENUM和可空类型Nullable
- 表单元素与普通类的对应属性
- 01-check the manual that corresponds to your MySQL server version for the right syntax
- latex解决目录文字重叠问题的方法
- ognl 怎么访问 set 集合中指定位置的元素
- 再探uvaoj10474
- First Missing Positive
- Code First Entity Framework动态组合Lambda表达式作为数据筛选条件,代替拼接SQL语句
- 黑马程序员_java修饰符学习总结
- Trapping Rain Water
- 今天遇到了这样一个问题 Exception in thread "main" org.hibernate.LazyInitializationException: could not initiali
- 捷克大排档
- Leetcode Mulitiply Strings
- skyeye ubuntu12.04