Demo entry 6349679

cpp

   

Submitted by anonymous on Feb 26, 2017 at 08:32
Language: C++. Code size: 917 Bytes.

#include<iostream>
using namespace std;

class Solution {

public:
	Solution() {
	};
	void readData() {
		cin >> length; 
		a = new int[length];
		for (int i = 0; i < length; i++) {
			cin >> a[i];
		}
	};
	int maxSubSequence(bool ascend) {   
	    // "ascend" indicates if  ascending or not.
		int* opt = new int[length];
		int max = 0;
		for (int i = 0; i < length; i++) {
			opt[i] = 1;
			for (int j = i - 1; j >= 0; j--) {
				if ((!((a[j] <= a[i]) ^ ascend)) && opt[i] < opt[j] + 1) {
					opt[i] = opt[j] + 1;
				}
			}
			if (max < opt[i]) {
				max = opt[i];
			}
		}
		delete[] opt;
		return max;
	};
	void solve() {
		readData();
		int anslen = maxSubSequence(false);
		int anstime = maxSubSequence(true);
		cout << anslen << endl;
		cout << anstime << endl;
	}
	~Solution() {
		delete[] a;
	};
private:
	int* a;
	int length;
	int anslen;
	int anstime;
};

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).