Posts

Showing posts from December, 2016

uva 10474 Solution - Where is the Marble?

#include<iostream> #include<set> #include<algorithm> using namespace std; int main(){ multiset<int> myset; multiset<int>::iterator msit; int n,q,x,i,kase=1; while(cin>>n>>q){ if(n==0 && q==0) break; myset.clear(); for(i=0;i<n;i++){ cin>>x; myset.insert(x); } cout<<"CASE# "<<kase++<<":"<<endl; for(i=0;i<q;i++){ cin>>x; msit=myset.find(x); if(msit==myset.end()) cout<<x<<" not found"<<endl; else cout<<x<<" found at "<<distance(myset.begin(),msit)+1<<endl; } } return 0; }

uva-10077 Solution --- The Stern-Brocot Number System

#include<iostream> using namespace std; int main(){ int input_num,input_den,left_num,left_den,right_num,right_den,cur_num,cur_den; while(cin>>input_num>>input_den){ if(input_num==1 && input_den==1) break; left_num=0; left_den=1; right_num=1; right_den=0; cur_num=cur_den=1; while(true){ if(cur_num== input_num && cur_den==input_den) break; if(cur_num*input_den < cur_den*input_num){ cout<<"R"; left_num=cur_num; left_den=cur_den; cur_num+=right_num; cur_den+=right_den; }else{ cout<<"L"; right_num=cur_num; right_den=cur_den; cur_num+=left_num; cur_den+=left_den; } } cout<<endl; } return 0; }

uva 679 - Dropping Balls Solution

uva 679 - Dropping Balls Solution Algorithm: 1. convert (position-1)  to binary and store it to a string. 2. make the binary string length equal to the (depth-1) by adding zero(0) at start of the string. 3. reverse the binary string. 4. convert binary string to integer + power(2,depth-1). Example: depth=7; position=4; covert 4-1= 3 to binary. binary string is 11. make the string length 7-1=6 by adding 0. Updated string 000011. reverse the binary string and string is now 110000 convert binary string(110000) to integer. value is 48. calculate power_value=pow(2,7-1)=pow(2,6)=64. Answer is = 48 + 64=112. Code in C++: #include<iostream> #include<string> #include<algorithm> using namespace std; string convertDecimalToBinary(int num){ string result; while(num!=0){ if(num%2==1) result.insert(0,1,'1'); else result.insert(0,1,'0'); num=num/2; } return result; } int convertBinaryToDecimal(string &n...

uva 481 - What Goes Up Solution

uva 481 - What Goes Up Solution #include<iostream> #include<vector> using namespace std; void binary_search(int *arr, int search_value, int start, int end, int &index){ if (start == end){ index = start; return; } int mid = (start + end) / 2; if (arr[mid] == search_value){ index = mid; return; } else if (arr[mid + 1] == search_value){ index = mid + 1; return; } else if (arr[mid]<search_value && arr[mid + 1]>search_value){ index = mid + 1; return; } else if (arr[mid] > search_value){ binary_search(arr, search_value, start, mid, index); } else{ binary_search(arr, search_value, mid + 1, end, index); } } void calculate_lis(vector<int>&arr,vector<int>&result){ int *temp, i, max = 0, j, index, n = arr.size(), *res; temp = new int[n]; res = new int[n]; temp[0] = 0; for (i = 1; i < n; i++){ if (arr[temp[max]] < arr[i]){ res[i] = temp[max]; max++; ...

uva 11790 Murcia’s Skyline Solution

uva 11790 Murcia’s Skyline Solution #include<algorithm> #include<iostream> #include<vector> using namespace std; int main() { vector<int> height, width, inc, dec; int i, j, test_case, n, x,max_i,max_d,cas=1; cin >> test_case; while (test_case--){ cin >> n; height.clear(); width.clear(); inc.clear(); dec.clear(); for (i = 0; i < n; i++){ cin >> x; height.push_back(x); } for (i = 0; i < n; i++){ cin >> x; width.push_back(x); } inc.push_back(width[0]); dec.push_back(width[0]); max_i = max_d = width[0]; for (i = 1; i < n; i++){ inc.push_back(width[i]); dec.push_back(width[i]); if (max_i<width[i]) max_i = width[i]; if (max_d<width[i]) max_d = width[i]; for (j = 0; j < i; j++){ if (height[i] > height[j] && inc[j] + width[i]>inc[i]){ inc[i] = inc[j] + width[i]; if (max_i < inc[i]) max_i...

uva 10534 - Wavio Sequence Solution

uva 10534 - Wavio Sequence Solution #include<algorithm> #include<iostream> #include<vector> using namespace std; void binary_search(int *arr, int search_value, int start, int end, int &index){ if (start == end){ index = start; return; } int mid = (start + end) / 2; if (arr[mid] == search_value){ index = mid; return; } else if (arr[mid + 1] == search_value){ index = mid + 1; return; } else if (arr[mid]<search_value && arr[mid + 1]>search_value){ index = mid + 1; return; } else if (arr[mid] > search_value){ binary_search(arr, search_value, start, mid, index); } else{ binary_search(arr, search_value, mid + 1, end, index); } } void calculate_lis(vector<int>&arr, int n, vector<int>&result){ int *temp, i, max = 0, j, index; temp = new int[n]; temp[0] = 0; result.push_back(1); for (i = 1; i < n; i++){ if (arr...