Za dati niz celih brojeva ispitati na koliko načina ga je moguće podeliti na segmente tako da svaki segment počinje i završava se brojem iste parnosti.
Sa standardnog ulaza se učitava veličina niza \(n\) (\(1 \leq n \leq 100\)), a zatim i \(n\) elemenata niza u opsegu od 0 do 100.
Na standardni izlaz ispisati traženi broj načina da se podeli niz.
5 1 2 3 4 5
5
Moguće podele su:
U ovom bloku se opisuje glavno rešenje zadatka.
#include <iostream>
#include <vector>
using namespace std;
/* i : 0 1 2 3 4 5
* a[i]: 1 2 3 4 5
* f(i): 5 3 2 1 1 1
*
* i:[0..4] i:[1..4] i:[2..4] i:[3..4] i:[4..4] i:[5..4]
* |1|2|3|4|5| |2|3|4|5| |3|4|5| |4|5| |5|
* |1 2 3|4|5| |2 3 4|5| |3 4 5|
* |1|2 3 4|5| |2|3 4 5|
* |1|2|3 4 5|
* |1 2 3 4 5|
*/
int count_segments(vector<int> &a)
{
const int N = a.size();
<int> dp(N + 1);
vector[N] = 1;
dp
int sum_even = 0, sum_odd = 0;
for (int n = N - 1; n >= 0; n--) {
if (a[n] % 2 == 1) { // neparni
+= dp[n + 1];
sum_odd [n] = sum_odd;
dp} else { // a[n] % 2 == 0, parni
+= dp[n + 1];
sum_even [n] = sum_even;
dp}
}
return dp[0];
}
int main()
{
int n; cin >> n;
<int> a(n);
vectorfor(int i = 0; i < n; i++) {
>> a[i];
cin }
<< count_segments(a) << endl;
cout
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
// count segments of a[i..n-1]
int count_segments(vector<int> &a, const int i)
{
const int n = a.size();
if (i == n) {
return 1; // a[n..n-1] = {}
}
int count = 0;
for (int j = i; j < n; j++) {
if (a[i] % 2 == a[j] % 2) {
// mark a[i..j] and continue counting from a[j+1..n-1]
+= count_segments(a, j + 1);
count }
}
return count;
}
int main()
{
int n; cin >> n;
<int> a(n);
vectorfor(int i = 0; i < n; i++) {
>> a[i];
cin }
<< count_segments(a, 0) << endl;
cout
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
// count segments of a[i..n-1]
int count_segments(vector<int> &a, const int i, vector<int> &memo)
{
const int n = a.size();
if (i == n) {
return memo[i] = 1; // a[n..n-1] = {}
}
if (memo[i]) {
return memo[i];
}
int count = 0;
for (int j = i; j < n; j++) {
if (a[i] % 2 == a[j] % 2) {
// mark a[i..j] and continue counting from a[j+1..n-1]
+= count_segments(a, j + 1, memo);
count }
}
return memo[i] = count;
}
int count_segments(vector<int> &a)
{
const int n = a.size();
<int> memo(n + 1);
vectorreturn count_segments(a, 0, memo);
}
int main()
{
int n; cin >> n;
<int> a(n);
vectorfor(int i = 0; i < n; i++) {
>> a[i];
cin }
<< count_segments(a) << endl;
cout
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
/* i : 0 1 2 3 4 5
* f(i): 5 3 2 1 1 1
*
* i:[0..4] i:[1..4] i:[2..4] i:[3..4] i:[4..4] i:[5..4]
* |1|2|3|4|5| |2|3|4|5| |3|4|5| |4|5| |5|
* |1 2 3|4|5| |2 3 4|5| |3 4 5|
* |1|2 3 4|5| |2|3 4 5|
* |1|2|3 4 5|
* |1 2 3 4 5|
*/
int count_segments(vector<int> &a)
{
const int N = a.size();
<int> dp(N + 1);
vector[N] = 1;
dp
for (int n = N - 1; n >= 0; n--) {
int count = 0;
for (int k = n; k < N; k++) {
if (a[n] % 2 == a[k] % 2) {
+= dp[k + 1];
count }
}
[n] = count;
dp}
return dp[0];
}
int main()
{
int n; cin >> n;
<int> a(n);
vectorfor(int i = 0; i < n; i++) {
>> a[i];
cin }
<< count_segments(a) << endl;
cout
return 0;
}