Skip to content

Commit d5d651a

Browse files
committed
srm 581 div 1 250
1 parent 5d55db1 commit d5d651a

File tree

2 files changed

+151
-0
lines changed

2 files changed

+151
-0
lines changed

SurveillanceSystem.cpp

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
#include <cstdio>
11+
using namespace std;
12+
typedef pair<int,int> pi;
13+
typedef set<int> si;
14+
typedef vector<int> vi;
15+
typedef vector<vi> vvi;
16+
typedef vector<string> vs;
17+
typedef long long ll;
18+
#define sz size()
19+
#define mp(x, y) make_pair(x, y)
20+
#define ri(a, b) for(int i=((int)(a)); i < ((int)(b)); i++) // i -> [a, b)
21+
#define rie(a, b) for(int i=((int)(a)); i <= ((int)(b)); i++) // i -> [a, b]
22+
#define rj(a, b) for(int j=((int)(a)); j < ((int)(b)); j++) // j -> [a, b)
23+
#define rje(a, b) for(int j=((int)(a)); j <= ((int)(b)); j++) // j -> [a, b]
24+
#define rk(a, b) for(int k=((int)(a)); k < ((int)(b)); k++) // k -> [a, b)
25+
#define rke(a, b) for(int k=((int)(a)); k <= ((int)(b)); k++) // k -> [a, b]
26+
#define fi(b) for(int i=0; i < ((int)(b)); i++) // i -> [0, b)
27+
#define fie(b) for(int i=0; i <= ((int)(b)); i++) // i -> [0, b]
28+
#define fj(b) for(int j=0; j < ((int)(b)); j++) // j -> [0, b)
29+
#define fje(b) for(int j=0; j <= ((int)(b)); j++) // j -> [0, b]
30+
#define fk(b) for(int k=0; k < ((int)(b)); k++) // k -> [0, b)
31+
#define fke(b) for(int k=0; k < ((int)(b)); k++) // k -> [0, b]
32+
#define fle(b) for(int l=0; l <= ((int)(b)); l++) // l -> [0, b]
33+
34+
class SurveillanceSystem{
35+
public:
36+
void g(string s, int c[55], int L){
37+
if(s.empty()) return;
38+
int n = s.size();
39+
if(n < L) return;
40+
fie(n-L){
41+
string t = s.substr(i, L);
42+
int j = count(t.begin(), t.end(), 'X');
43+
c[i] = j;
44+
}
45+
}
46+
vi convert(int n, int L, int c[55]){
47+
vi m(55, 0);
48+
fie(n-L) m[c[i]]++;
49+
return m;
50+
}
51+
string getContainerInfo(string containers, vector <int> reports, int L){
52+
string s = containers;
53+
int n = s.size();
54+
int c[55] = {0};
55+
si S;
56+
for(int i : reports) S.insert(i);
57+
string ans(n, '?');
58+
g(s, c, L);
59+
vi m(55, 0);
60+
for(int i : reports) m[i]++;
61+
fi(n) cout<<c[i];
62+
cout<<endl;
63+
fi(n) cout<<m[i];
64+
cout<<endl;
65+
66+
fi(n){
67+
int flag = 0;
68+
for(int j = max(0, i+1-L); j <= i && j+L <= n; j++) if(S.count(c[j])) flag = 1;
69+
if(!flag) ans[i] = '-';
70+
}
71+
72+
fi(n){
73+
if(ans[i] == '-') continue;
74+
75+
string s1, s2;
76+
s1 = s.substr(0, i);
77+
s2 = s.substr(i+1, n-1-i);
78+
int c1[55] = {0}, c2[55] = {0};
79+
g(s1, c1, L);
80+
g(s2, c2, L);
81+
vi m1 = convert(s1.size(), L, c1), m2 = convert(s2.size(), L, c2);
82+
fje(n){
83+
int k = m1[j]+m2[j];
84+
if(k < m[j]) ans[i] = '+';
85+
}
86+
}
87+
return ans;
88+
}
89+
90+
91+
// BEGIN CUT HERE
92+
public:
93+
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(); }
94+
private:
95+
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(); }
96+
void verify_case(int Case, const string &Expected, const string &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
97+
void test_case_0() { string Arg0 = "-X--XX"; int Arr1[] = {1, 2}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 3; string Arg3 = "??++++"; verify_case(0, Arg3, getContainerInfo(Arg0, Arg1, Arg2)); }
98+
void test_case_1() { string Arg0 = "-XXXXX-"; int Arr1[] = {2}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 3; string Arg3 = "???-???"; verify_case(1, Arg3, getContainerInfo(Arg0, Arg1, Arg2)); }
99+
void test_case_2() { string Arg0 = "------X-XX-"; int Arr1[] = {3, 0, 2, 0}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 5; string Arg3 = "++++++++++?"; verify_case(2, Arg3, getContainerInfo(Arg0, Arg1, Arg2)); }
100+
void test_case_3() { string Arg0 = "-XXXXX---X--"; int Arr1[] = {2, 1, 0, 1}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 3; string Arg3 = "???-??++++??"; verify_case(3, Arg3, getContainerInfo(Arg0, Arg1, Arg2)); }
101+
void test_case_4() { string Arg0 = "-XX--X-XX-X-X--X---XX-X---XXXX-----X"; int Arr1[] = {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 7; string Arg3 = "???++++?++++++++++++++++++++??????--"; verify_case(4, Arg3, getContainerInfo(Arg0, Arg1, Arg2)); }
102+
103+
// END CUT HERE
104+
105+
};
106+
107+
// BEGIN CUT HERE
108+
int main(){
109+
110+
SurveillanceSystem ___test;
111+
___test.run_test(-1);
112+
}
113+
// END CUT HERE

SurveillanceSystem.html

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<html><body bgcolor="#000000" text="#ffffff"><table><tr><td colspan="2"><h3>Problem Statement</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td>There is a long narrow storehouse.
2+
The storehouse is divided into a sequence of N identical sectors, labeled 0 through N-1.
3+
Each sector is large enough to contain a single container.
4+
Currently, some sectors are empty and some sectors are filled by containers.
5+
The storehouse also contains a surveillance system that is described below.
6+
<br></br><br></br>
7+
We are going to break into the storehouse.
8+
As a part of preparation for the heist, we already found out some information about the warehouse.
9+
In particular, we know exactly how the containers are currently placed in the warehouse.
10+
You are given a string <b>containers</b> consisting of N characters.
11+
For each i, character i of <b>containers</b> is 'X' if sector i contains a container, and it is '-' if sector i is empty.
12+
<br></br><br></br>
13+
We also discovered some information about the surveillance system.
14+
The system consists of several hidden cameras.
15+
You are given a int <b>L</b> with the following meaning:
16+
Each of the cameras monitors exactly <b>L</b> consecutive sectors.
17+
The segments of sectors monitored by different cameras might overlap, but no two cameras watch exactly the same segment.
18+
(In other words, each sector may be monitored by multiple cameras, but each camera monitors a different set of consecutive sectors.)
19+
<br></br><br></br>
20+
Finally, we know something about what the cameras currently see.
21+
You are given a vector &lt;int&gt; <b>reports</b>.
22+
Each element of <b>reports</b> corresponds to one of the cameras (in no particular order).
23+
More precisely, <b>reports</b>[i] is the number of containers stored in the sectors monitored by the corresponding camera.
24+
<br></br><br></br>
25+
It is guaranteed that all our information is correct and consistent.
26+
<br></br><br></br>
27+
Your task is to use the provided information to deduce which sectors are monitored by at least one surveillance camera.
28+
Return a string containing N characters.
29+
For each i, character i of the return value should be one of '+', '?', and '-'.
30+
Character '+' represents that sector i is certainly monitored by at least one camera.
31+
Character '-' represents that sector i is certainly not monitored by any of the cameras.
32+
Character '?' represents the remaining case: given the information we have, it is possible that sector i is monitored, but it is also possible that it is not monitored.</td></tr><tr><td colspan="2"><h3>Definition</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td>Class:</td><td>SurveillanceSystem</td></tr><tr><td>Method:</td><td>getContainerInfo</td></tr><tr><td>Parameters:</td><td>string, vector &lt;int&gt;, int</td></tr><tr><td>Returns:</td><td>string</td></tr><tr><td>Method signature:</td><td>string getContainerInfo(string containers, vector &lt;int&gt; reports, int L)</td></tr><tr><td colspan="2">(be sure your method is public)</td></tr></table></td></tr><tr><td colspan="2"><h3>Limits</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td>Time limit (s):</td><td>2.000</td></tr><tr><td>Memory limit (MB):</td><td>64</td></tr></table></td></tr><tr><td colspan="2"><h3>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td><b>containers</b> will contain N elements, where N is between 1 and 50, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each character in <b>containers</b> will be either 'X' or '-'.</td></tr><tr><td align="center" valign="top">-</td><td><b>L</b> will be between 1 and N, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td><b>reports</b> will contain between 1 and N-<b>L</b>+1 elements, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each element of <b>reports</b> will be between 0 and <b>L</b>, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>The given information will be consistent.</td></tr><tr><td colspan="2"><h3>Examples</h3></td></tr><tr><td align="center" nowrap="true">0)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>&quot;-X--XX&quot;</pre></td></tr><tr><td><pre>{1, 2}</pre></td></tr><tr><td><pre>3</pre></td></tr></table></td></tr><tr><td><pre>Returns: &quot;??++++&quot;</pre></td></tr><tr><td><table><tr><td colspan="2">This storehouse has 6 sectors.
33+
There are containers in sectors 1, 4, and 5.
34+
There are two cameras: camera #0 monitors 1 container, and camera #1 monitors 2 containers.
35+
Clearly, camera #1 must be watching sectors 3, 4, and 5.
36+
Camera #0 may be watching sectors (0, 1, 2), (1, 2, 3), or (2, 3, 4).
37+
Thus, camera #0 is surely monitoring sector 2.
38+
Sectors 0 and 1 may or may not be monitored.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">1)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>&quot;-XXXXX-&quot;</pre></td></tr><tr><td><pre>{2}</pre></td></tr><tr><td><pre>3</pre></td></tr></table></td></tr><tr><td><pre>Returns: &quot;???-???&quot;</pre></td></tr><tr><td><table><tr><td colspan="2">The camera is monitoring either the leftmost or the rightmost segment, thus the middle sector is surely not under surveillance.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">2)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>&quot;------X-XX-&quot;</pre></td></tr><tr><td><pre>{3, 0, 2, 0}</pre></td></tr><tr><td><pre>5</pre></td></tr></table></td></tr><tr><td><pre>Returns: &quot;++++++++++?&quot;</pre></td></tr><tr><td><table><tr><td colspan="2">We can deduce that cameras #1 and #3 are watching segments (0, 1, 2, 3, 4) and (1, 2, 3, 4, 5). Camera #2 is monitoring the segment (4, 5, 6, 7, 8), since this is the only segment with two occupied sectors. Camera #0 is either watching (5, 6, 7, 8, 9) or (6, 7, 8, 9, 10), thus the rightmost sector might have slipped from the surveillance.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">3)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>&quot;-XXXXX---X--&quot;</pre></td></tr><tr><td><pre>{2, 1, 0, 1}</pre></td></tr><tr><td><pre>3</pre></td></tr></table></td></tr><tr><td><pre>Returns: &quot;???-??++++??&quot;</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">4)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>&quot;-XX--X-XX-X-X--X---XX-X---XXXX-----X&quot;</pre></td></tr><tr><td><pre>{3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3}</pre></td></tr><tr><td><pre>7</pre></td></tr></table></td></tr><tr><td><pre>Returns: &quot;???++++?++++++++++++++++++++??????--&quot;</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr></table><p>This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved. </p></body></html>

0 commit comments

Comments
 (0)