Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
69 lines (57 loc) · 1.54 KB

File metadata and controls

69 lines (57 loc) · 1.54 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#include <iostream>
#define MAXN 1000
using namespace std;
int dp[MAXN];
int q[MAXN];
int rec[MAXN];
void r_print(int data[], int pos){
if(rec[pos]==-1){
//cout<<"pos is "<<pos;
cout<<data[pos]<<" ";
return ;
}
r_print(data, rec[pos]);
//cout<<"pos is "<<pos;
cout<<data[pos]<<" ";
}
void work(int data[], int N, int max_sum){
int len = -1;
if(data[0] > max_sum){
dp[0] = 0;
q[0] = 0;
}else{
dp[0] = 1;
q[0] = data[0];
}
rec[0] = -1;
int end_pos = -1;
int max_ans = -1;
for(int i=1; i<N; i++){
//each step of dp we have a choice of including ith element in data or not
int pos = -1;
for(int j = 0; j<i; j++){
if(data[i]+q[j] <=max_sum && dp[j]+1 > dp[i]){
dp[i] = dp[j]+1;
q[i] = data[i]+q[j];
pos = j;
rec[i] = j;
if(dp[i]> max_ans){
max_ans = dp[i];
end_pos = i;
}
}
}
}
cout<<"end pos is "<<end_pos<<endl;
for(int i=0; i<N;i++){
cout<<rec[i]<<" ";
}cout<<endl;
r_print(data, end_pos);
}
int main(){
int data[] = {1, -2, 4, 5, -2, 6, 7};
int N = 7;
int max_sum = 7;
work(data, N, max_sum);
return 0;
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.