-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcountIncreasingSubsequences.cpp
More file actions
52 lines (47 loc) · 1.02 KB
/
countIncreasingSubsequences.cpp
File metadata and controls
52 lines (47 loc) · 1.02 KB
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
// Input : arr[] = {1, 2, 3, 4} Output : 15 There are total increasing subsequences
// {1}, {2}, {3}, {4}, {1, 2}, {1, 3}, {1, 4},
// {2, 3}, {2, 4}, {3, 4}, {1, 2, 3}, {1, 2, 4},
// {1, 3, 4}, {2, 3, 4}, {1, 2, 3, 4}
// okay but not accepted for 436 input
#include <iostream>
#include <vector>
using namespace std;
int countIncSub(vector<int>& arr)
{
if(arr.empty())
return 0;
vector<long long int> dp(arr.size(), 1);
for(int i = 1; i < arr.size(); i++)
{
for(int j = 0; j < i; j++)
{
if(arr[j] < arr[i])
{
dp[i] += dp[j];
}
}
}
long long int sum = 0;
for(auto x : dp)
{
sum += x;
}
return sum;
}
int main()
{
int cases;
cin >> cases;
while(cases--)
{
int n;
cin >> n;
vector<int> arr(n);
for(int i = 0; i < n; i++)
{
cin >> arr[i];
}
cout << countIncSub(arr) << endl;
}
return 0;
}