Skip to content

Commit 13800b6

Browse files
committed
tccc 2003 round 2 500
1 parent a9ebba1 commit 13800b6

File tree

2 files changed

+151
-0
lines changed

2 files changed

+151
-0
lines changed

Marketing.cpp

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include <iostream>
2+
#include <sstream>
3+
#include <vector>
4+
#include <algorithm>
5+
#include <set>
6+
#include <map>
7+
#include <queue>
8+
#include <cstring>
9+
#include <climits>
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+
int M[35][35];
17+
int l[35];
18+
int n;
19+
20+
class Marketing {
21+
public:
22+
int dfs(int s){
23+
int x = l[s];
24+
for(int p = 0; p < n; p++){
25+
if(M[s][p]){
26+
if(l[p] == x) return -1;
27+
if(l[p] == -1){
28+
l[p] = (x^1);
29+
if(dfs(p) == -1) return -1;
30+
}
31+
}
32+
}
33+
return 1;
34+
}
35+
36+
long long howMany(vector <string> comp)
37+
{
38+
n = comp.size();
39+
fill(l, l+n+1, -1);
40+
for(int i = 0; i <= n; i++)
41+
for(int j = 0; j <= n; j++)
42+
M[i][j] = 0;
43+
44+
for(int i = 0; i < n; i++){
45+
string s = comp[i];
46+
stringstream ss(s);
47+
int j;
48+
while(ss>>j) M[i][j] = M[j][i] = 1;
49+
}
50+
int c = 1;
51+
for(int i = 0; i < n; i++)
52+
if(l[i] == -1){
53+
l[i] = 1;
54+
if(dfs(i) == -1) return -1;
55+
c *= 2;
56+
}
57+
return c;
58+
}
59+
60+
// BEGIN CUT HERE
61+
public:
62+
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(); if ((Case == -1) || (Case == 4)) test_case_4(); }
63+
private:
64+
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(); }
65+
void verify_case(int Case, const long long &Expected, const long long &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
66+
void test_case_0() { string Arr0[] = {"1 4","2","3","0",""}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); long long Arg1 = 2LL; verify_case(0, Arg1, howMany(Arg0)); }
67+
void test_case_1() { string Arr0[] = {"1","2","0"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); long long Arg1 = -1LL; verify_case(1, Arg1, howMany(Arg0)); }
68+
void test_case_2() { string Arr0[] = {"1","2","3","0","0 5","1"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); long long Arg1 = 2LL; verify_case(2, Arg1, howMany(Arg0)); }
69+
void test_case_3() { string Arr0[] = {"","","","","","","","","","",
70+
"","","","","","","","","","",
71+
"","","","","","","","","",""}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); long long Arg1 = 1073741824LL; verify_case(3, Arg1, howMany(Arg0)); }
72+
void test_case_4() { string Arr0[] = {"1","2","3","0","5","6","4"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); long long Arg1 = -1LL; verify_case(4, Arg1, howMany(Arg0)); }
73+
74+
// END CUT HERE
75+
76+
};
77+
78+
// BEGIN CUT HERE
79+
int main(){
80+
81+
Marketing ___test;
82+
___test.run_test(-1);
83+
84+
}
85+
// END CUT HERE

Marketing.txt

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
PROBLEM STATEMENT
2+
You work for a very large company that markets many different products. In some cases, one product you market competes with another. To help deal with this situation you have split the intended consumers into two groups, namely Adults and Teenagers. If your company markets 2 products that compete with each other, selling one to Adults and the other to Teenagers will help maximize profits. Given a list of the products that compete with each other, you are going to determine whether all can be marketed such that no pair of competing products are both sold to Teenagers or both sold to Adults. If such an arrangement is not feasible your method will return -1. Otherwise, it should return the number of possible ways of marketing all of the products.
3+
4+
The products will be given in a vector <string> compete whose kth element describes product k. The kth element will be a single-space delimited list of integers. These integers will refer to the products that the kth product competes with. For example:
5+
compete = {"1 4",
6+
"2",
7+
"3",
8+
"0",
9+
""}
10+
11+
The example above shows product 0 competes with 1 and 4, product 1 competes with 2, product 2 competes with 3, and product 3 competes with 0. Note, competition is symmetric so product 1 competing with product 2 means product 2 competes with product 1 as well.
12+
Ways to market:
13+
1) 0 to Teenagers, 1 to Adults, 2 to Teenagers, 3 to Adults, and 4 to Adults
14+
2) 0 to Adults, 1 to Teenagers, 2 to Adults, 3 to Teenagers, and 4 to Teenagers
15+
Your method would return 2.
16+
17+
18+
DEFINITION
19+
Class:Marketing
20+
Method:howMany
21+
Parameters:vector <string>
22+
Returns:long long
23+
Method signature:long long howMany(vector <string> compete)
24+
25+
26+
CONSTRAINTS
27+
-compete will contain between 1 and 30 elements, inclusive.
28+
-Each element of compete will have between 0 and 50 characters, inclusive.
29+
-Each element of compete will be a single space delimited sequence of integers such that: All of the integers are unique.Each integer contains no extra leading zeros.Each integer is between 0 and k-1 inclusive where k is the number of elements in compete.
30+
-No element of compete contains leading or trailing whitespace.
31+
-Element i of compete will not contain the value i.
32+
-If i occurs in the jth element of compete, j will not occur in the ith element of compete.
33+
34+
35+
EXAMPLES
36+
37+
0)
38+
{"1 4","2","3","0",""}
39+
40+
Returns: 2
41+
42+
The example from above.
43+
44+
1)
45+
{"1","2","0"}
46+
47+
Returns: -1
48+
49+
Product 0 cannot be marketed with product 1 or 2. Product 1 cannot be marketed with product 2. There is no way to achieve a viable marketing scheme.
50+
51+
2)
52+
{"1","2","3","0","0 5","1"}
53+
54+
Returns: 2
55+
56+
3)
57+
{"","","","","","","","","","",
58+
"","","","","","","","","","",
59+
"","","","","","","","","",""}
60+
61+
Returns: 1073741824
62+
63+
4)
64+
{"1","2","3","0","5","6","4"}
65+
66+
Returns: -1

0 commit comments

Comments
 (0)