Skip to content

Commit caebad5

Browse files
committed
srm 195 div 1 250
1 parent 0dee039 commit caebad5

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

FanFailure.cpp

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <iostream>
2+
#include <sstream>
3+
#include <vector>
4+
#include <algorithm>
5+
#include <set>
6+
#include <map>
7+
#include <cstring>
8+
#include <climits>
9+
#include <numeric>
10+
using namespace std;
11+
typedef pair<int,int> pi;
12+
typedef vector<int> vi;
13+
typedef vector<vi> vvi;
14+
typedef vector<string> vs;
15+
typedef vector<vs> vvs;
16+
17+
class FanFailure {
18+
public:
19+
vector <int> getRange(vector <int> cap, int mincool)
20+
{
21+
int sum = accumulate(cap.begin(), cap.end(), 0);
22+
sort(cap.begin(), cap.end());
23+
int minc = 0, mins = 0;
24+
int n = cap.size();
25+
int ts = 0;
26+
for(int i = 0; i < n; i++){
27+
ts += cap[i];
28+
if(sum-ts >= mincool) mins++;
29+
}
30+
ts = 0;
31+
for(int i = n-1; i >= 0; i--){
32+
ts += cap[i];
33+
if(sum-ts >= mincool) minc++;
34+
}
35+
vi v = {mins, minc};
36+
return v;
37+
}
38+
39+
// BEGIN CUT HERE
40+
public:
41+
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(); }
42+
private:
43+
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(); }
44+
void verify_case(int Case, const vector <int> &Expected, const vector <int> &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(Received) << endl; } }
45+
void test_case_0() { int Arr0[] = {1,2,3}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 2; int Arr2[] = { 2, 1 }; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); verify_case(0, Arg2, getRange(Arg0, Arg1)); }
46+
void test_case_1() { int Arr0[] = {8,5,6,7}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 22; int Arr2[] = { 0, 0 }; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); verify_case(1, Arg2, getRange(Arg0, Arg1)); }
47+
void test_case_2() { int Arr0[] = {676, 11, 223, 413, 823, 122, 547, 187, 28}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 1000; int Arr2[] = { 7, 2 }; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); verify_case(2, Arg2, getRange(Arg0, Arg1)); }
48+
void test_case_3() { int Arr0[] = {955, 96, 161, 259, 642, 242, 772, 369, 311, 785,
49+
92, 991, 620, 394, 128, 774, 973, 94, 681, 771,
50+
916, 373, 523, 100, 220, 993, 472, 798, 132, 361,
51+
33, 362, 573, 624, 722, 520, 451, 231, 37, 921,
52+
408, 170, 303, 559, 866, 412, 339, 757, 822, 192}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 3619; int Arr2[] = { 46, 30 }; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); verify_case(3, Arg2, getRange(Arg0, Arg1)); }
53+
54+
// END CUT HERE
55+
56+
};
57+
58+
// BEGIN CUT HERE
59+
int main()
60+
{
61+
FanFailure ___test;
62+
___test.run_test(-1);
63+
}
64+
// END CUT HERE

FanFailure.txt

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
PROBLEM STATEMENT
2+
3+
In a robust computer system, one of the most important pieces is the cooling. Without proper cooling, processors can heat up to over 400 degrees C. The reliability of a system can be measured by determining how many fans can fail without risking the system processor. Each fan can be assigned a value indicating how much capacity it has to cool the system, and we can define a minimum cooling capacity, which the sum of the fan capacities must exceed or equal to properly cool the system. We define a Failure Set as a set of fans which are not necessary to cool the system. In other words, if the fans in a Failure Set break, the system can still be properly cooled by the remaining fans. The count of a Failure Set is the number of fans in the set.
4+
5+
6+
7+
To measure the reliability, we will define two values, the Maximum Failure Set (MFS) and the Maximum Failure Count (MFC). A MFS is a Failure Set of fans with the largest count possible. A set of fans may have more than one MFS (see below). A Failure Set is an MFS if and only if there are no Failure Sets with a higher count. The MFC is the largest value such that all fan sets with count <= MFC are Failure Sets. In other words, any set of fans of size MFC or less can fail, and the system will still be properly cooled by the remaining fans.
8+
9+
10+
11+
Consider the fan set with capacities 1, 2, 3, and a cooling requirement of 2. Two MFSs with a count of 2 exist: fans 1 and 3, or fans 1 and 2. However, the MFC is not 2 because fans 2 and 3 is not a Failure set (fan 1 could not cool the system properly by itself). Thus, the MFC is 1, because if any single fan fails, the system can still be cooled.
12+
13+
14+
15+
You will be given a vector <int> capacities, which designates how many units of cooling each fan provides, and an int minCooling, which designates the minimum units of cooling required to cool the system. Your method should return a vector <int>, where the first value should be the number of fans in the Maximum Failure Set (MFS), and the second value should be the Maximum Failure Count (MFC).
16+
17+
18+
DEFINITION
19+
Class:FanFailure
20+
Method:getRange
21+
Parameters:vector <int>, int
22+
Returns:vector <int>
23+
Method signature:vector <int> getRange(vector <int> capacities, int minCooling)
24+
25+
26+
CONSTRAINTS
27+
-capacities has between 1 and 50 elements, inclusive.
28+
-each element of capacities is between 1 and 1000, inclusive.
29+
-minCooling is between 1 and 49999, inclusive.
30+
-The sum of all elements in capacities will be greater than minCooling.
31+
-Due to a contradiction in the original problem statement, it was not clear whether having enough fans to equal exactly minCooling was enough to cool the system. For this reason, test cases where this anomaly changes the answer will be rejected.
32+
33+
34+
EXAMPLES
35+
36+
0)
37+
{1,2,3}
38+
2
39+
40+
Returns: { 2, 1 }
41+
42+
Example from the problem statement.
43+
44+
1)
45+
{8,5,6,7}
46+
22
47+
48+
Returns: { 0, 0 }
49+
50+
No fans can fail in this system.
51+
52+
2)
53+
{676, 11, 223, 413, 823, 122, 547, 187, 28}
54+
1000
55+
56+
Returns: { 7, 2 }
57+
58+
If you eliminate fans with 676, 11, 413, 122, 547, 187, and 28, you are left with 223 + 823 = 1046 units of cooling, which is sufficient, yielding an MFS size of 7. If you eliminate 676, 823, and 547, you are left with only 984 units of cooling. All combinations of 2 or less fans breaking leaves sufficient cooling, so the MFC is 2.
59+
60+
3)
61+
{955, 96, 161, 259, 642, 242, 772, 369, 311, 785,
62+
92, 991, 620, 394, 128, 774, 973, 94, 681, 771,
63+
916, 373, 523, 100, 220, 993, 472, 798, 132, 361,
64+
33, 362, 573, 624, 722, 520, 451, 231, 37, 921,
65+
408, 170, 303, 559, 866, 412, 339, 757, 822, 192}
66+
3619
67+
68+
Returns: { 46, 30 }

0 commit comments

Comments
 (0)