feat: new dp problem formatted
This commit is contained in:
47
longest-increasing-subsequence-II/src/TLE.cpp
Normal file
47
longest-increasing-subsequence-II/src/TLE.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
int main(){
|
||||
int N; cin >> N;
|
||||
vector<int> a(N);
|
||||
for (int i = 0; i < N; i++) {
|
||||
cin >> a[i];
|
||||
}
|
||||
|
||||
vector<int> d(N, 1);
|
||||
vector<int> p(N, -1);
|
||||
for (int i = 0; i < N; i++) {
|
||||
for (int j = 0; j < i; j++) {
|
||||
if (a[j] < a[i] && d[j] + 1 > d[i]) {
|
||||
d[i] = d[j] + 1;
|
||||
p[i] = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int ans = d[0], pos = 0;
|
||||
for (int i = 1; i < N; i++) {
|
||||
if (d[i] > ans) {
|
||||
ans = d[i];
|
||||
pos = i;
|
||||
}
|
||||
}
|
||||
|
||||
vector<int> subseq;
|
||||
while (pos != -1) {
|
||||
subseq.push_back(a[pos]);
|
||||
pos = p[pos];
|
||||
}
|
||||
reverse(subseq.begin(), subseq.end());
|
||||
|
||||
cout << ans << endl;
|
||||
for (int i = 0; i < ans; i++) {
|
||||
if (i != 0) cout << " ";
|
||||
cout << subseq[i];
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
51
longest-increasing-subsequence-II/src/ac.cpp
Normal file
51
longest-increasing-subsequence-II/src/ac.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
const int INF = 1e9;
|
||||
|
||||
int main()
|
||||
{
|
||||
int N;
|
||||
cin >> N;
|
||||
|
||||
vector<int> a(N);
|
||||
for (int i = 0; i < N; i++)
|
||||
cin >> a[i];
|
||||
|
||||
vector<int> d(N + 1, INF);
|
||||
vector<int> pos(N + 1, -1);
|
||||
vector<int> p(N, -1);
|
||||
|
||||
d[0] = -INF;
|
||||
int ans = 0;
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
int l = upper_bound(d.begin(), d.end(), a[i]) - d.begin();
|
||||
if (d[l - 1] < a[i] && a[i] < d[l])
|
||||
{
|
||||
d[l] = a[i];
|
||||
pos[l] = i;
|
||||
p[i] = (l > 1 ? pos[l - 1] : -1);
|
||||
ans = max(ans, l);
|
||||
}
|
||||
}
|
||||
|
||||
vector<int> subseq;
|
||||
int cur = pos[ans];
|
||||
while (cur != -1)
|
||||
{
|
||||
subseq.push_back(a[cur]);
|
||||
cur = p[cur];
|
||||
}
|
||||
reverse(subseq.begin(), subseq.end());
|
||||
|
||||
cout << ans << endl;
|
||||
for (int i = 0; i < ans; i++)
|
||||
{
|
||||
if (i) cout << " ";
|
||||
cout << subseq[i];
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
58
longest-increasing-subsequence-II/src/checker.cpp
Normal file
58
longest-increasing-subsequence-II/src/checker.cpp
Normal file
@@ -0,0 +1,58 @@
|
||||
#include "testlib.h"
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int readAns(InStream& stream, vector<int>& nums) {
|
||||
|
||||
int size = stream.readInt(1, 1e5);
|
||||
vector<int> ans;
|
||||
while (!stream.seekEof())
|
||||
ans.push_back(stream.readInt());
|
||||
|
||||
if (size != ans.size())
|
||||
quitf(_wa, "Subsequence does not match size provided");
|
||||
|
||||
for (int i = 1; i < ans.size(); i++) {
|
||||
if (ans[i - 1] >= ans[i]) {
|
||||
quitf(_wa, "Subsequence is not strictly increasing");
|
||||
}
|
||||
}
|
||||
|
||||
// checking if numbers provided are indeed a subsequence of the original array
|
||||
int j = 0;
|
||||
for (int i = 0; i < nums.size() && j < ans.size(); i++) {
|
||||
if (nums[i] == ans[j]) {
|
||||
j++;
|
||||
}
|
||||
}
|
||||
|
||||
if (j != size) {
|
||||
quitf(_wa, "The provided subsequence is not present in the original array");
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
setName("Set the name of your checker here");
|
||||
registerTestlibCmd(argc, argv);
|
||||
|
||||
int n = inf.readInt();
|
||||
vector<int> nums(n);
|
||||
for (int i = 0; i < n; i++) nums[i] = inf.readInt();
|
||||
|
||||
int ja = readAns(ans, nums);
|
||||
int pa = readAns(ouf, nums);
|
||||
|
||||
if (ja > pa)
|
||||
quitf(_wa, "Expected (%i) found (%i)", ja, pa);
|
||||
|
||||
if (ja < pa)
|
||||
quitf(_fail, "Participant gave a better answer. PA - (%i), JA - (%i)", pa, ja);
|
||||
|
||||
quitf(_ok, "OK");
|
||||
|
||||
return 0;
|
||||
}
|
||||
106
longest-increasing-subsequence-II/src/generator.cpp
Normal file
106
longest-increasing-subsequence-II/src/generator.cpp
Normal file
@@ -0,0 +1,106 @@
|
||||
#include "testlib.h"
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
const int MIN_N = 1;
|
||||
const int MAX_N = 1e5;
|
||||
const int MIN_NI = -1e4;
|
||||
const int MAX_NI = 1e4;
|
||||
|
||||
const int rnd_test_n = 30;
|
||||
|
||||
template <typename T> void append(vector<T> &dest, const vector<T> &orig) {
|
||||
dest.insert(dest.end(), orig.begin(), orig.end());
|
||||
}
|
||||
|
||||
string output_tc(const vector<int> &nums) {
|
||||
ostringstream oss;
|
||||
oss << nums.size() << endl;
|
||||
for (int i = 0; i < nums.size(); i++) {
|
||||
if (i != 0) oss << " ";
|
||||
oss << nums[i];
|
||||
}
|
||||
oss << endl;
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
vector<string> generate_sample_tests() {
|
||||
vector<string> tests;
|
||||
tests.push_back(output_tc({1, 2, 3, 4, 5}));
|
||||
tests.push_back(output_tc({2, 3, -1, 4}));
|
||||
tests.push_back(output_tc({0}));
|
||||
return tests;
|
||||
}
|
||||
|
||||
vector<string> generate_manual_tests() {
|
||||
vector<string> tests;
|
||||
tests.push_back(output_tc({MIN_NI, MIN_NI, MIN_NI, MIN_NI}));
|
||||
tests.push_back(output_tc({0, 0, 0, 0, -1, -1, -1, -1, -1}));
|
||||
return tests;
|
||||
}
|
||||
|
||||
string rnd_test(int i){
|
||||
int min_n = MIN_N;
|
||||
int max_n = MAX_N;
|
||||
|
||||
if(i<rnd_test_n / 3){
|
||||
max_n = MAX_N/3;
|
||||
}
|
||||
else if(i<rnd_test_n / 2){
|
||||
max_n = MAX_N/2;
|
||||
}
|
||||
|
||||
int n = rnd.next(min_n, max_n);
|
||||
vector<int> nums(n);
|
||||
for (int i = 0; i < n; i++) {
|
||||
nums[i] = rnd.next(MIN_NI, MAX_NI);
|
||||
}
|
||||
return(output_tc(nums));
|
||||
}
|
||||
|
||||
vector<string> generate_random_tests() {
|
||||
vector<string> tests;
|
||||
for (int i = 0; i < rnd_test_n; i++){
|
||||
tests.push_back(rnd_test(i));
|
||||
}
|
||||
return tests;
|
||||
}
|
||||
|
||||
string extreme_test_1(){
|
||||
vector<int> nums(MAX_N);
|
||||
for (int i = 0; i < MAX_N; i++) {
|
||||
nums[i] = MIN_NI;
|
||||
}
|
||||
return(output_tc(nums));
|
||||
}
|
||||
|
||||
string extreme_test_2(){
|
||||
vector<int> nums(MAX_N);
|
||||
for (int i = 0; i < MAX_N; i++) {
|
||||
nums[i] = MAX_NI;
|
||||
}
|
||||
return(output_tc(nums));
|
||||
}
|
||||
|
||||
vector<string> generate_extreme_tests(){
|
||||
vector<string> tests;
|
||||
tests.push_back(extreme_test_1());
|
||||
tests.push_back(extreme_test_2());
|
||||
return tests;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
registerGen(argc, argv, 1);
|
||||
vector<string> tests;
|
||||
size_t test = 0;
|
||||
append(tests, generate_sample_tests());
|
||||
append(tests, generate_manual_tests());
|
||||
append(tests, generate_random_tests());
|
||||
append(tests, generate_extreme_tests());
|
||||
for (const auto &t : tests) {
|
||||
startTest(++test);
|
||||
cout << t;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
1
longest-increasing-subsequence-II/src/script.sh
Normal file
1
longest-increasing-subsequence-II/src/script.sh
Normal file
@@ -0,0 +1 @@
|
||||
generator
|
||||
5963
longest-increasing-subsequence-II/src/testlib.h
Normal file
5963
longest-increasing-subsequence-II/src/testlib.h
Normal file
File diff suppressed because it is too large
Load Diff
18
longest-increasing-subsequence-II/src/validator.cpp
Normal file
18
longest-increasing-subsequence-II/src/validator.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#include "testlib.h"
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
registerValidation(argc, argv);
|
||||
int n = inf.readInt(1, 1e5, "N");
|
||||
inf.readEoln();
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (i != 0) inf.readSpace();
|
||||
inf.readInt(-1e4, 1e4, "ai");
|
||||
}
|
||||
inf.readEoln();
|
||||
inf.readEof();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user