Skip to content

Commit c43928f

Browse files
committed
srm 626 div 2 250
1 parent b5c4471 commit c43928f

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

SumOfPower.cpp

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <iostream>
2+
#include <sstream>
3+
#include <vector>
4+
#include <algorithm>
5+
#include <set>
6+
#include <map>
7+
#include <numeric>
8+
using namespace std;
9+
10+
class SumOfPower {
11+
public:
12+
int findSum(vector <int> a)
13+
{
14+
int sum = 0;
15+
int n = a.size();
16+
for(int i = 1; i <= n; i++){
17+
int s = 0;
18+
for(int j = 0; j+i <= n; j++)
19+
s += accumulate(a.begin()+j, a.begin()+j+i, 0);
20+
sum += s;
21+
}
22+
return sum;
23+
}
24+
25+
// BEGIN CUT HERE
26+
public:
27+
void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); }
28+
private:
29+
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
30+
void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
31+
void test_case_0() { int Arr0[] = {1,2}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 6; verify_case(0, Arg1, findSum(Arg0)); }
32+
void test_case_1() { int Arr0[] = {1,1,1}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 10; verify_case(1, Arg1, findSum(Arg0)); }
33+
void test_case_2() { int Arr0[] = {3,14,15,92,65}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 1323; verify_case(2, Arg1, findSum(Arg0)); }
34+
void test_case_3() { int Arr0[] = {1,2,3,4,5,6,7,8,9,10}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 1210; verify_case(3, Arg1, findSum(Arg0)); }
35+
36+
// END CUT HERE
37+
38+
};
39+
40+
// BEGIN CUT HERE
41+
int main()
42+
{
43+
SumOfPower ___test;
44+
___test.run_test(-1);
45+
}
46+
// END CUT HERE

SumOfPower.txt

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
PROBLEM STATEMENT
2+
3+
You are given a vector <int> array.
4+
At any moment, you may choose a nonempty contiguous subsequence of array.
5+
Whenever you do so, you will gain power equal to the sum of all elements in the chosen subsequence.
6+
7+
8+
9+
You chose each possible contiguous subsequence exactly once, each time gaining some power.
10+
Compute and return the total amount of power you gained.
11+
12+
13+
DEFINITION
14+
Class:SumOfPower
15+
Method:findSum
16+
Parameters:vector <int>
17+
Returns:int
18+
Method signature:int findSum(vector <int> array)
19+
20+
21+
CONSTRAINTS
22+
-array will contain between 1 and 50 elements, inclusive.
23+
-Each element in array will be between 1 and 100, inclusive.
24+
25+
26+
EXAMPLES
27+
28+
0)
29+
{1,2}
30+
31+
Returns: 6
32+
33+
We have the following three contiguous subsequences:
34+
35+
{1} => 1
36+
{2} => 2
37+
{1,2} => 3
38+
39+
Thus, the sum of all possible powers is 1+2+3=6.
40+
41+
1)
42+
{1,1,1}
43+
44+
Returns: 10
45+
46+
A 3-element sequence has 6 possible nonempty contiguous subsequences.
47+
For the sequence {1,1,1} these are the subsequences: {1}, {1}, {1}, {1,1}, {1,1}, and {1,1,1}.
48+
Their sums are 1, 1, 1, 2, 2, and 3.
49+
If you choose each of them once, the total power you'll gain is 1+1+1+2+2+3 = 10.
50+
51+
2)
52+
{3,14,15,92,65}
53+
54+
Returns: 1323
55+
56+
57+
58+
3)
59+
{1,2,3,4,5,6,7,8,9,10}
60+
61+
Returns: 1210
62+
63+

0 commit comments

Comments
 (0)