Skip to content

Commit 24b07de

Browse files
committed
srm 641 div 2 500
1 parent 7704454 commit 24b07de

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

Diff for: TrianglesContainOriginEasy.cpp

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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 vector<int> vi;
14+
typedef vector<vi> vvi;
15+
typedef vector<string> vs;
16+
typedef vector<vs> vvs;
17+
struct fPoint
18+
{
19+
float x;
20+
float y;
21+
fPoint(float x, float y) : x(x) , y(y) {
22+
23+
}
24+
};
25+
26+
float sign (fPoint p1, fPoint p2, fPoint p3)
27+
{
28+
return (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y);
29+
}
30+
31+
bool PointInTriangle (fPoint pt, fPoint v1, fPoint v2, fPoint v3)
32+
{
33+
bool b1, b2, b3;
34+
35+
b1 = sign(pt, v1, v2) < 0.0f;
36+
b2 = sign(pt, v2, v3) < 0.0f;
37+
b3 = sign(pt, v3, v1) < 0.0f;
38+
39+
return ((b1 == b2) && (b2 == b3));
40+
}
41+
42+
class TrianglesContainOriginEasy{
43+
public:
44+
int count(vector <int> x, vector <int> y) {
45+
int r = 0;
46+
int n = x.size();
47+
for(int i = 0; i < n; i++)
48+
for(int j = i+1; j < n; j++)
49+
for(int k = j+1; k < n; k++){
50+
fPoint p1(x[i], y[i]), p2(x[j], y[j]), p3(x[k], y[k]), p(0.0, 0.0);
51+
if(PointInTriangle(p, p1, p2, p3)) r++;
52+
}
53+
54+
return r;
55+
}
56+
57+
// BEGIN CUT HERE
58+
public:
59+
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(); }
60+
private:
61+
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(); }
62+
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; } }
63+
void test_case_0() { int Arr0[] = {-1,-1,1}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1,-1,0}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 1; verify_case(0, Arg2, count(Arg0, Arg1)); }
64+
void test_case_1() { int Arr0[] = {-1,-1,1,2}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1,-1,2,-1}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 2; verify_case(1, Arg2, count(Arg0, Arg1)); }
65+
void test_case_2() { int Arr0[] = {-1,-2,3,3,2,1}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {-2,-1,1,2,3,3}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 8; verify_case(2, Arg2, count(Arg0, Arg1)); }
66+
void test_case_3() { int Arr0[] = {1,5,10,5,-5,7,-9,-6,-3,0,8,8,1,-4,7,-3,10,9,-6}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {5,-6,-3,4,-2,-8,-7,2,7,4,2,0,-4,-8,7,5,-5,-2,-9}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 256; verify_case(3, Arg2, count(Arg0, Arg1)); }
67+
68+
// END CUT HERE
69+
70+
};
71+
72+
// BEGIN CUT HERE
73+
int main(){
74+
75+
TrianglesContainOriginEasy ___test;
76+
___test.run_test(-1);
77+
}
78+
// END CUT HERE

Diff for: TrianglesContainOriginEasy.html

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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>We have chosen a finite set of points in the plane.
2+
You are given their coordinates in the vector &lt;int&gt;s <b>x</b> and <b>y</b>:
3+
for each valid i, there is a point with coordinates (<b>x</b>[i],<b>y</b>[i]).
4+
5+
We are interested in triangles with the following properties:
6+
<ul>
7+
<li>Each vertex of the triangle is one of our chosen points.</li>
8+
<li>The point (0,0) lies inside the triangle.</li>
9+
</ul>
10+
Return the number of such triangles.
11+
12+
Note that the constraints guarantee that there are no degenerate triangles and that the point (0,0) never lies on the boundary of a triangle.
13+
</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>TrianglesContainOriginEasy</td></tr><tr><td>Method:</td><td>count</td></tr><tr><td>Parameters:</td><td>vector &lt;int&gt;, vector &lt;int&gt;</td></tr><tr><td>Returns:</td><td>int</td></tr><tr><td>Method signature:</td><td>int count(vector &lt;int&gt; x, vector &lt;int&gt; y)</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>256</td></tr><tr><td>Stack limit (MB):</td><td>256</td></tr></table></td></tr><tr><td colspan="2"><h3>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td><b>x</b> and <b>y</b> will contain between 3 and 50 elements, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td><b>x</b> and <b>y</b> will contain the same number of elements.</td></tr><tr><td align="center" valign="top">-</td><td>Each element of <b>x</b> and <b>y</b> will be between -1,000 and 1,000, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>No two points will be the same.</td></tr><tr><td align="center" valign="top">-</td><td>No three points will be collinear.</td></tr><tr><td align="center" valign="top">-</td><td>No point will be on the origin.</td></tr><tr><td align="center" valign="top">-</td><td>There will be no two points P and Q such that P, Q, and the origin are collinear.</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>{-1,-1,1}</pre></td></tr><tr><td><pre>{1,-1,0}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 1</pre></td></tr><tr><td><table><tr><td colspan="2">There is exactly one possible triangle. It does contain the origin.</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>{-1,-1,1,2}</pre></td></tr><tr><td><pre>{1,-1,2,-1}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 2</pre></td></tr><tr><td><table><tr><td colspan="2">There are four possible triangles. Two of them contain the origin. One is the triangle with vertices in (-1,1), (-1,-1), and (2,-1). The other is the triangle with vertices in (-1,-1), (1,2), and (2,-1).</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>{-1,-2,3,3,2,1}</pre></td></tr><tr><td><pre>{-2,-1,1,2,3,3}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 8</pre></td></tr><tr><td><table><tr><td colspan="2"></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>{1,5,10,5,-5,7,-9,-6,-3,0,8,8,1,-4,7,-3,10,9,-6}</pre></td></tr><tr><td><pre>{5,-6,-3,4,-2,-8,-7,2,7,4,2,0,-4,-8,7,5,-5,-2,-9}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 256</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)