Skip to content

Commit e330f47

Browse files
committed
9/13
1 parent 51d7400 commit e330f47

File tree

6 files changed

+327
-1
lines changed

6 files changed

+327
-1
lines changed

FENCE.cpp

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <iostream>
2+
#include <algorithm>
3+
4+
using namespace std;
5+
6+
int N;
7+
int hs[20000] = {0, };
8+
9+
int prob(int from, int to);
10+
11+
int main(void){
12+
int C;
13+
cin >> C;
14+
for(;C--;){
15+
cin >> N;
16+
for(int idx = 0; idx < N; idx++){
17+
cin >> hs[idx];
18+
}
19+
cout << prob(0, N) << endl;
20+
}
21+
return 0;
22+
}
23+
24+
25+
int prob(int from, int to){
26+
//base cases
27+
if (from + 1 == to){
28+
return hs[from];
29+
}
30+
if (from + 2 == to){
31+
return max(max(hs[from], hs[to - 1]), 2 * min(hs[from], hs[to - 1]));
32+
}
33+
int mid = (to + from) / 2;
34+
int prevA = prob(from, mid);
35+
int nextA = prob(mid, to);
36+
37+
//Start setting.
38+
int midFrom = mid - 1;
39+
int midTo = mid + 1;
40+
int curNophi = min(hs[midFrom], hs[midTo - 1]);
41+
int midA = curNophi * 2;
42+
int addedIdx;
43+
while(midFrom > from || midTo < to){
44+
if (midFrom == from){
45+
midTo++;
46+
addedIdx = midTo - 1;
47+
}
48+
else if (midTo == to){
49+
midFrom--;
50+
addedIdx = midFrom;
51+
}
52+
else if (hs[midFrom - 1] > hs[midTo]){
53+
midFrom--;
54+
addedIdx = midFrom;
55+
}
56+
else {
57+
midTo++;
58+
addedIdx = midTo - 1;
59+
}
60+
curNophi = min(curNophi, hs[addedIdx]);
61+
midA = max(midA, curNophi * (midTo - midFrom));
62+
}
63+
return max(prevA, max(nextA, midA));
64+
}

JLIS.cpp

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
using namespace std;
5+
6+
vector<int> A;
7+
vector<int> B;
8+
vector< vector<int> > cache;
9+
int Alen;
10+
int Blen;
11+
12+
int prob(int Astart, int Bstart);
13+
14+
int main(void){
15+
int C;
16+
cin >> C;
17+
for(;C--;){
18+
cin >> Alen >> Blen;
19+
A.clear();
20+
B.clear();
21+
A.resize(Alen);
22+
B.resize(Blen);
23+
cache.clear();
24+
cache.resize(Alen + 1);
25+
for(int idx = 0; idx < Alen; idx++){
26+
cin >> A[idx];
27+
cache[idx].clear();
28+
cache[idx].resize(Blen + 1, -1);
29+
}
30+
cache[Alen].resize(Blen + 1, -1);
31+
for(int idx = 0; idx < Blen; idx++){
32+
cin >> B[idx];
33+
}
34+
35+
int output = -1;
36+
for(int Adx = 0; Adx <= Alen; Adx++){
37+
for(int Bdx = 0; Bdx <= Blen; Bdx++){
38+
output = max(output, prob(Adx, Bdx));
39+
}
40+
}
41+
cout << output << endl;
42+
}
43+
}
44+
45+
int prob(int Astart, int Bstart){
46+
47+
if (Astart == Alen && Bstart == Blen){
48+
return 0;
49+
}
50+
51+
52+
int& ret = cache[Astart][Bstart];
53+
if (ret != -1){
54+
return ret;
55+
}
56+
57+
ret = 1;
58+
59+
int newBdx = Bstart;
60+
for(; newBdx < Blen; newBdx++){
61+
if (B[newBdx] > A[Astart]){
62+
break;
63+
}
64+
}
65+
int newAdx = Astart;
66+
for(; newAdx < Alen; newAdx++){
67+
if (A[newAdx] > B[Bstart]){
68+
break;
69+
}
70+
}
71+
for(int Adx = Astart + 1; Adx <= Alen; Adx++){
72+
if (Adx == Alen || A[Adx] > A[Astart]){
73+
74+
ret = max(ret, 1 + prob(Adx, newBdx));
75+
}
76+
}
77+
for(int Bdx = Bstart + 1; Bdx <= Blen; Bdx++){
78+
if (Bdx == Blen || B[Bdx] > B[Bstart]){
79+
80+
ret = max(ret, 1 + prob(newAdx, Bdx));
81+
}
82+
}
83+
return ret;
84+
}

LIS.cpp

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
using namespace std;
5+
6+
vector<int> L;
7+
vector<int> cache;
8+
int len;
9+
10+
int prob(int start);
11+
12+
int main(void){
13+
int C;
14+
cin >> C;
15+
for(;C--;){
16+
cin >> len;
17+
L.clear();
18+
cache.clear();
19+
L.resize(len);
20+
cache.resize(len);
21+
22+
for(int idx = 0; idx < len; idx++){
23+
cin >> L[idx];
24+
cache[idx] = -1;
25+
}
26+
int output = -1;
27+
for(int idx = 0; idx < len; idx++){
28+
output = max(output, prob(idx));
29+
}
30+
cout << output << endl;
31+
}
32+
}
33+
34+
//N^2 algorithm...
35+
int prob(int start){
36+
if (start == len - 1){
37+
return 1;
38+
}
39+
40+
int& ret = cache[start];
41+
if (ret != -1){
42+
return ret;
43+
}
44+
45+
int cur = L[start];
46+
ret = 1;
47+
for(int idx = start + 1; idx < len; idx++){
48+
if (cur < L[idx]){
49+
ret = max(ret, 1 + prob(idx));
50+
}
51+
}
52+
return ret;
53+
}

QUADTREE.cpp

+17-1
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,33 @@ using namespace std;
55
string data;
66

77
string prob(string data);
8+
string prob_lin(string data, int& pos);
89

910
int main(void){
1011
int C;
1112
cin >> C;
1213
for(;C--;){
1314
cin >> data;
14-
cout << prob(data) << endl;
15+
//pos is -1, like 'head' in linkedlist.
16+
int pos = -1;
17+
cout << prob_lin(data, pos) << endl;
1518
}
1619
return 0;
1720
}
1821

22+
string prob_lin(string data, int& pos){
23+
pos++;
24+
if (data[pos] != 'x'){
25+
return data.substr(pos, 1);
26+
}
27+
string Q[4];
28+
Q[0] = prob_lin(data, pos);
29+
Q[1] = prob_lin(data, pos);
30+
Q[2] = prob_lin(data, pos);
31+
Q[3] = prob_lin(data, pos);
32+
return "x" + Q[2] + Q[3] + Q[0] + Q[1];
33+
}
34+
1935
string prob(string data){
2036
if (data.length() == 1){
2137
return data;

TRIANGLEPATH.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
5+
using namespace std;
6+
7+
vector< vector<int> > triData;
8+
vector< vector<int> > cache;
9+
int triSize;
10+
11+
int prob(int y, int x);
12+
13+
int main(void){
14+
int C;
15+
cin >> C;
16+
for(;C--;){
17+
//init.
18+
triData.clear();
19+
cache.clear();
20+
cin >> triSize;
21+
triData.resize(triSize);
22+
cache.resize(triSize);
23+
24+
for(int row = 0; row < triSize; row++){
25+
triData[row].resize(row + 1);
26+
cache[row].resize(row + 1);
27+
for(int col = 0; col <= row; col++){
28+
cin >> triData[row][col];
29+
cache[row][col] = -1;
30+
}
31+
}
32+
cout << prob(0, 0) << endl;
33+
}
34+
}
35+
36+
int prob(int y, int x){
37+
if (cache[y][x] != -1){
38+
return cache[y][x];
39+
}
40+
if (y == (triSize - 1)){
41+
cache[y][x] = triData[y][x];
42+
return triData[y][x];
43+
}
44+
int ret = triData[y][x] + max(prob(y + 1, x), prob(y + 1, x + 1));
45+
cache[y][x] = ret;
46+
return ret;
47+
}

WILDCARD.cpp

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include <iostream>
2+
#include <string.h>
3+
#include <string>
4+
#include <algorithm>
5+
#include <vector>
6+
7+
using namespace std;
8+
9+
char w[101];
10+
int wLen;
11+
char s[101];
12+
int sLen;
13+
14+
vector<string> output;
15+
16+
bool prob(int wStart, int sStart);
17+
18+
int main(void){
19+
int C;
20+
cin >> C;
21+
for(;C--;){
22+
cin >> w;
23+
wLen = strlen(w);
24+
int sc;
25+
cin >> sc;
26+
for(;sc--;){
27+
cin >> s;
28+
sLen = strlen(s);
29+
if (prob(0, 0)){
30+
output.push_back(string(s));
31+
}
32+
}
33+
sort(output.begin(), output.end());
34+
for(vector<string>::iterator it = output.begin(); it != output.end(); it++){
35+
cout << *it << endl;
36+
}
37+
output.clear();
38+
}
39+
}
40+
bool prob(int wStart, int sStart){
41+
if (wStart == wLen && sStart == sLen){
42+
return true;
43+
}
44+
int wPos = wStart;
45+
int sPos = sStart;
46+
while(w[wPos] != '*'){
47+
if (w[wPos] != '?' && w[wPos] != s[sPos]){
48+
return false;
49+
}
50+
wPos++;
51+
sPos++;
52+
// finish together.
53+
if (wPos == wLen && sPos == sLen){
54+
return true;
55+
}
56+
}
57+
bool ret = false;
58+
for(; sPos <= sLen; sPos++){
59+
ret = ret || prob(wPos + 1, sPos);
60+
}
61+
return ret;
62+
}

0 commit comments

Comments
 (0)