Posts

Showing posts from November, 2016

uva 11456 - Trainsorting Solution

uva 11456 - Trainsorting Solution #include<algorithm> #include<cstdio> using namespace std; int main() { int A[2000], Ma[2000], Mb[2000], N, T, sum, ans, i, j; scanf("%d", &T); while(T--) { scanf("%d", &N); for (i = 0; i < N; i++) scanf("%d", &A[i]); ans = 0; for (i = N - 1; i >= 0; i--) { Ma[i] = 1; Mb[i] = 1; for (j = i + 1; j < N; j++) { if (A[i] < A[j]) { Ma[i] = max(Ma[j] + 1, Ma[i]); } if (A[i] > A[j]) { Mb[i] = max(Mb[j] + 1, Mb[i]); } } ans = max(ans, Ma[i] + Mb[i] - 1); } printf("%d\n", ans); } return 0; }

uva 10131 - Is Bigger Smarter? Solution

uva 10131 - Is Bigger Smarter? Solution #include<iostream> #include<algorithm> #include<vector> using namespace std; typedef pair<int, int> pii; typedef pair<pii, int> piii; vector<piii> vec; vector<int> res; bool piii_sort(piii p1, piii p2){  if (p1.first.first<p2.first.first)   return true;  else   return false; } void calculate_lis(int len){ int *order = new int[len], *count = new int[len], i, j, last_index = 0, max = 1; fill(order, order + len, -1); fill(count, count + len, 1); for (i = 1; i < len; i++){ for (j = 0; j < i; j++){ if (vec[i].first.second<vec[j].first.second && vec[i].first.first>vec[j].first.first){ if (count[j] + 1>count[i]){ if (max < count[j] + 1){ max = count[j] + 1; last_index = i; } count[i] = count[j] + 1; order[i] = j; } } } } res.pus...

uva 497 - Strategic Defense Initiative Solution

uva 497 - Strategic Defense Initiative Solution #include<iostream> #include<algorithm> #include<vector> #include<string> #include<stdio.h> #include<string.h> using namespace std; vector<int> list,order; int convertStringToInt(string str){ int i, len=str.length(), sum = 0,mul=1; for (i = len - 1; i >= 0; i--){ sum = sum + (str[i] - 48)*mul; mul = mul * 10; } return sum; } int getMaxTargets(){ int len = list.size(),i,j,max=1,last_index=0; int *count = new int[len],*index=new int[len]; fill(count, count + len, 1); fill(index, index + len, -1); order.clear(); for (i = 1; i < len; i++){ for (j = 0; j < i; j++){ if (list[i] > list[j]){ if (count[j] + 1>count[i]){ count[i] = count[j] + 1; if (max < count[i]){ max = count[i]; last_index = i; } index[i] = j; } } } } order.push_back(last_index); for (i = 1; i < max; i++){ ...

uva 231 - Testing the CATCHER Solution

uva 231 - Testing the CATCHER Solution #include<iostream> #include<algorithm> #include<vector> using namespace std; vector<int> list; int getMaxMissiles(){ long long len = list.size(),i,j,max=1; long long *count = new long long[len]; fill(count, count + len, 1); for (i = 1; i < len; i++){ for (j = 0; j < i; j++){ if (list[i] < list[j]){ if (count[j] + 1>count[i]){ count[i] = count[j] + 1; if (max < count[i]) max = count[i]; } } } } return max; } int main(){ int x; long long kk = 1; while (cin >> x){ if (x == -1) break; list.clear(); list.push_back(x); while (cin >> x){ if (x == -1) break; list.push_back(x); } if (kk != 1) cout << endl; cout << "Test #" << kk++ << ":" << endl << "  maximum possible interceptions: " << getMaxMissiles() << endl;...

uva 111 - History Grading Solution

uva 111 - History Grading Solution  #include<iostream> #include<algorithm> using namespace std; int calculate_lis(int *arr1,int *arr2,int n){ int i, j, k; int lis[25][25]; for (i = 0; i <= n; i++){ lis[i][0] = 0; lis[0][i] = 0; } for (i = 0; i < n; i++){ for (j = 0; j < n; j++){ if (arr1[i] == arr2[j]){ lis[i + 1][j + 1] = lis[i][j] + 1; } else{ lis[i + 1][j + 1] = max(lis[i][j + 1], lis[i + 1][j]); } } } return lis[n][n]; } int main(){ int correct_order[25], rank[20],i,j,n,x; cin >> n; for (i = 0; i <n; i++){ cin >> x; x--; correct_order[x] = i; } while (cin >> x){ x--; rank[x] = 0; for (i = 1; i < n; i++){ cin >> x; x--; rank[x] = i; } cout << calculate_lis(correct_order, rank, n) << endl; } return 0; }

437 - The Tower of Babylon Solution

437 - The Tower of Babylon Solution #include<iostream> #include<algorithm> #include<vector> using namespace std; class dimension{ public: int x; int y; int z; }; bool area_sort(dimension d1, dimension d2){ if ((d1.x * d1.y) > (d2.x * d2.y)) return true; return false; } int calculate_max_height(vector<dimension>&vec){ int max = 0, *height, len=vec.size(), i,j,sum; dimension d1,d2; height = new int[len]; for (i = 0; i < len; i++){ height[i] = vec[i].z; } max = height[0]; for (i = 1;i < len; i++){ d1 = vec[i]; for (j = 0; j < i; j++){ d2 = vec[j]; if (d1.x < d2.x && d1.y < d2.y){ sum = d1.z + height[j]; if (sum > height[i]){ height[i] = sum; if (sum > max) max = sum; } } } } return max; } int main(){ vector<dimension>vec; dimension d[90]; int n,i,k,x,y,z,temp,test_case=1; while (cin >> n){ if (n == ...

uva 11951 - Area Solution O(n^3)

uva 11951 - Area #include<iostream> #include<algorithm> #include<string> #include<string.h> #include<stdio.h> using namespace std; void maxSum(const int *const arr,int len,int max,int &count,int &lowest_cost){ int i,j,sum=0; count=0; lowest_cost=0; for(i=0;i<len;i++){ sum=arr[i]; if(sum<=max){ for(j=i+1;j<len;j++){ sum=sum+arr[j]; if(sum>max){ sum=sum-arr[j]; break; } } if((j-i)>count){ count=j-i; lowest_cost=sum; }else if((j-i)==count && lowest_cost>sum){ lowest_cost=sum; } } } } int main() { int arr[105][105]; int lowest,count,temp_count,temp[105],i,j,n,m,k,final_lowest,test_case,max,test; cin>>test_case; test=1; while(test_case--){ cin>>n>>m>>max; final_lowest=0; count=0; for(i=0;i<n;i++) for(j=0;j<m;j++) cin>>arr[i][j]; for(i=0;i<n;i++){ fill(temp,t...