-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlongaltseq.cc
85 lines (78 loc) · 1.74 KB
/
longaltseq.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <vector>
#include <cstdlib>
#include <string>
#include <cstring>
using namespace std;
typedef vector<int> vi_t;
void a_show(const vi_t&a) {
const char *sep = "";
for (int x: a) {
cout << sep << x; sep = " ";
}
cout << '\n';
}
bool is_alter(const vi_t&a) {
bool alt = true;
if (a.size() > 1) {
bool up = (a[0] < a[1]);
alt = (a[0] != a[1]);
for (unsigned i = 2; alt && (i < a.size()); ++i) {
up = !up;
alt = (a[i - 1] != a[i]) && (up == (a[i - 1] < a[i]));
}
}
return alt;
}
void longest_alternate_subseq(vi_t& res, const vi_t& a) {
}
void naive_longest_alternate_subseq(vi_t& res, const vi_t& a) {
unsigned mask_max = 1u << a.size();
for (unsigned mask = 1; mask < mask_max; ++mask) {
vi_t cand;
for (unsigned i = 0; i < a.size(); ++i) {
if (mask & (1u << i)) {
cand.push_back(a[i]);
}
}
if ((res.size() < cand.size()) && is_alter(cand) ) {
swap(res, cand);
}
}
}
static int comp_test(const vi_t& a) {
int rc = 0;
vi_t res, naive_res;
naive_longest_alternate_subseq(naive_res, a);
longest_alternate_subseq(res, a);
if (res.size() != naive_res.size()) {
rc = 1;
a_show(a);
}
return rc;
}
static int rand_test(int nt, unsigned sz) {
int rc = 0;
for (int t = 0; (rc == 0) && (t < nt); ++t) {
vi_t a;
while (a.size() < sz) {
a.push_back(rand() % (2*sz));
}
rc = comp_test(a);
}
return rc;
}
int main(int argc, char **argv) {
int rc = 1;
const char *cmd = argv[1];
if (strcmp(cmd, "rand") == 0) {
rc = rand_test(stoi(argv[2]), stoi(argv[3]));
} else {
vi_t a;
for (int ai = 1; ai < argc; ++ai) {
a.push_back(stoi(argv[ai]));
}
rc = comp_test(a);
}
return rc;
}