Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'master' of git@github.com:brianchenming/algorithms

  • Loading branch information...
commit aaba8b6d6ede1b090b3ff3b37f92ae79885d576e 2 parents dede3f2 + 3d05319
@brianchenming authored
View
160 1009/edge.cpp
@@ -0,0 +1,160 @@
+/*
+ * =====================================================================================
+ *
+ * Filename: edge.cpp
+ *
+ * Description: POJ 1009
+ *
+ * Version: 1.0
+ * Created: 09/18/2010 03:54:14 PM
+ * Revision: none
+ * Compiler: gcc
+ *
+ * Author: Ming Chen (chenming), chenming@sohu-rd.com
+ * Company: imeresearch@sogou
+ *
+ * =====================================================================================
+ */
+
+#include <iostream>
+#include <algorithm>
+#include <stdlib.h>
+
+using namespace std;
+
+const int MAX_RLE = 1000;
+const int MAX_VAL = 256;
+const int MAX_OFFSET = 1000000000;
+
+int rles[MAX_RLE][2], nrle = 0;
+int height, width, total;
+int checkpoints[9*MAX_RLE], numpoint = 0;
+int neighbors[9];
+
+/*
+ * === FUNCTION ======================================================================
+ * Name: find_neighbors
+ * Description:
+ * =====================================================================================
+ */
+ void
+find_neighbors ( int pos )
+{
+ static int offsets[9][2] = {
+ {-1, -1}, {0, -1}, {1, -1},
+ {-1, 0}, {0, 0}, {1, 0},
+ {-1, 1}, {0, 1}, {1, 1}
+ };
+ int yi = pos / width;
+ int xi = pos % width;
+ for (int i = 0; i < 9; ++i) {
+ int nxi = xi + offsets[i][0];
+ int nyi = yi + offsets[i][1];
+ if ( nxi < 0 || nxi >= width || nyi < 0 || nyi >= height ) {
+ neighbors[i] = -1;
+ } else {
+ neighbors[i] = nxi + nyi*width;
+ }
+ }
+} /* ----- end of function find_neighbors ----- */
+
+/*
+ * === FUNCTION ======================================================================
+ * Name: add_checkpoints
+ * Description:
+ * =====================================================================================
+ */
+ void
+add_checkpoints ( int pos )
+{
+ find_neighbors(pos);
+ for ( int i = 0; i < 9; ++i ) {
+ if ( neighbors[i] == -1 ) continue;
+ checkpoints[numpoint++] = neighbors[i];
+ }
+} /* ----- end of function add_checkpoints ----- */
+
+/*
+ * === FUNCTION ======================================================================
+ * Name: get_edge
+ * Description:
+ * =====================================================================================
+ */
+ int
+get_edge ( int pos, int* pfrom_rle )
+{
+ int values[9], irle = *pfrom_rle;
+ int new_from_rle = -1; // find a new start from pfrom_rle
+ find_neighbors(pos);
+ for ( int i = 0; i < 9; ++i ) {
+ if ( neighbors[i] == -1 ) {
+ values[i] = MAX_VAL;
+ continue;
+ }
+
+ while ( rles[irle][1] <= neighbors[i] ) ++irle; // locate rle containing current neighbor
+ values[i] = rles[irle][0];
+
+ if ( new_from_rle == -1 ) { // set new from_rle if not set
+ new_from_rle = *pfrom_rle;
+ while ( neighbors[i] > (rles[new_from_rle][1] + width + 1) )
+ ++new_from_rle;
+ }
+ }
+ *pfrom_rle = new_from_rle;
+
+ int edge = 0;
+ for ( int i = 0; i < 9; ++i ) {
+ if ( values[i] == MAX_VAL ) continue;
+ edge = max(edge, abs(values[i] - values[4]));
+ }
+
+ return edge;
+} /* ----- end of function get_edge ----- */
+
+/*
+ * === FUNCTION ======================================================================
+ * Name: main
+ * Description:
+ * =====================================================================================
+ */
+ int
+main ( int argc, char *argv[] )
+{
+ while ( cin >> width && width != 0 ) {
+ total = 0;
+ int val, len;
+ for ( nrle = 0; cin >> val >> len && (len != 0 || val != 0); ++nrle ) {
+ total += len;
+ rles[nrle][0] = val;
+ rles[nrle][1] = total;
+ }
+ height = total / width;
+ // add check points
+ numpoint = 0;
+ add_checkpoints(0);
+ for ( int i = 0; i < nrle; ++i )
+ add_checkpoints(rles[i][1]);
+ sort(checkpoints, checkpoints+numpoint);
+ numpoint = unique(checkpoints, checkpoints+numpoint) - checkpoints;
+
+ cout << width << "\n";
+ int edge_val, begin_pos = 0, from_rle = 0;
+ edge_val = get_edge(begin_pos, &from_rle);
+ for ( int i = 0; i < numpoint; ++i ) {
+ int edge = get_edge(checkpoints[i], &from_rle);
+ if ( edge != edge_val ) {
+ cout << edge_val << " " << (checkpoints[i] - begin_pos) << "\n";
+ edge_val = edge;
+ begin_pos = checkpoints[i];
+ }
+ }
+ if ( begin_pos < total ) {
+ cout << edge_val << " " << (total - begin_pos) << "\n";
+ }
+ cout << "0 0\n";
+ }
+ cout << "0\n";
+
+ return EXIT_SUCCESS;
+} /* ---------- end of function main ---------- */
View
1,150 1009/in
@@ -0,0 +1,1150 @@
+3
+255 1
+10 1
+255 2
+10 1
+255 2
+10 1
+255 1
+0 0
+10
+35 500000000
+200 500000000
+0 0
+7
+15 4
+100 15
+25 2
+175 2
+25 5
+175 2
+25 5
+0 0
+8
+4 8
+1 1
+2 1
+4 1
+8 1
+6 1
+4 1
+2 1
+0 1
+0 0
+8
+1 1
+2 1
+4 1
+8 1
+6 1
+4 1
+2 1
+0 1
+1 1
+2 1
+4 1
+8 1
+6 1
+4 1
+2 1
+0 1
+0 0
+8
+1 1
+2 1
+4 1
+8 1
+6 1
+4 1
+2 1
+0 1
+0 0
+5
+15 12
+100 3
+15 1
+100 4
+15 2
+100 3
+0 0
+30
+10 41
+20 41
+15 41
+30 41
+25 41
+0 5
+0 0
+1
+255 2
+0 0
+10
+4 20
+6 20
+0 0
+1
+4 1000000000
+0 0
+1000000000
+4 1000000000
+0 0
+30
+10 41
+20 41
+15 41
+30 41
+25 41
+0 5
+0 0
+8
+1 1
+2 1
+4 1
+8 1
+6 1
+4 1
+2 1
+0 1
+1 1
+2 1
+4 1
+8 1
+6 1
+4 1
+2 1
+0 1
+1 1
+2 1
+4 1
+8 1
+6 1
+4 1
+2 1
+0 1
+0 0
+8
+1 1
+2 1
+4 1
+8 1
+6 1
+4 1
+2 1
+0 1
+4 8
+0 0
+8
+4 8
+1 1
+2 1
+4 1
+8 1
+6 1
+4 1
+2 1
+0 1
+5 8
+0 0
+5
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+30 11
+20 11
+10 11
+20 11
+0 0
+0
View
134 1009/out
@@ -0,0 +1,134 @@
+3
+245 9
+0 0
+10
+0 499999990
+165 20
+0 499999990
+0 0
+7
+85 5
+0 2
+85 5
+75 10
+150 2
+75 3
+0 2
+150 2
+0 4
+0 0
+8
+3 2
+4 3
+2 1
+4 2
+3 1
+2 1
+4 2
+2 3
+4 1
+0 0
+8
+1 1
+2 1
+4 2
+2 4
+1 1
+2 1
+4 2
+2 4
+0 0
+8
+1 1
+2 1
+4 2
+2 4
+0 0
+5
+0 6
+85 12
+0 2
+85 3
+0 2
+0 0
+30
+0 10
+10 62
+5 20
+15 62
+5 20
+25 6
+5 15
+0 9
+25 6
+0 0
+1
+0 2
+0 0
+10
+0 10
+2 20
+0 10
+0 0
+1
+0 1000000000
+0 0
+1000000000
+0 1000000000
+0 0
+30
+0 10
+10 62
+5 20
+15 62
+5 20
+25 6
+5 15
+0 9
+25 6
+0 0
+8
+1 1
+2 1
+4 2
+2 4
+1 1
+2 1
+4 2
+2 4
+1 1
+2 1
+4 2
+2 4
+0 0
+8
+3 1
+2 1
+4 2
+2 3
+4 1
+3 2
+4 3
+2 1
+4 2
+0 0
+8
+3 2
+4 3
+2 1
+4 3
+3 1
+4 2
+2 2
+3 1
+5 1
+4 2
+3 4
+5 2
+0 0
+5
+0 5
+10 10990
+0 5
+0 0
+0
View
193 2791/area.cpp
@@ -0,0 +1,193 @@
+/*
+ * =====================================================================================
+ *
+ * Filename: area.cpp
+ *
+ * Description: POJ 2791. ACM ICPC 2005–2006, Northeastern European Regional Contest
+ *
+ * Version: 1.0
+ * Created: 09/19/2010 09:56:21 PM
+ * Revision: none
+ * Compiler: gcc
+ *
+ * Author: Ming Chen (chenming), chenming@sohu-rd.com
+ * Company: imeresearch@sogou
+ *
+ * =====================================================================================
+ */
+
+
+#include <stdlib.h>
+#include <math.h>
+#include <cstdio>
+#include <assert.h>
+#include <algorithm>
+
+using namespace std;
+
+const int MAXM = 101;
+
+class Chimeny{
+public:
+ char shape;
+ int x, y;
+};
+
+Chimeny chimneys[MAXM];
+double viewpoint;
+double intersections[MAXM][MAXM];
+// further[i][j] is true when chimneys[i] is further away than chimneys[j] from their
+// intersections[i][j] with x-axis
+bool further[MAXM][MAXM];
+
+
+/*
+ * === FUNCTION ======================================================================
+ * Name: toleft
+ * Description: check if chimneys[i] is to the left of chimneys[j] from current viewpoint
+ * =====================================================================================
+ */
+ bool
+toleft ( int i, int j )
+{
+ if ( i == j ) return false;
+ if ( chimneys[i].y == chimneys[j].y ) return chimneys[i].x < chimneys[j].x;
+ return (viewpoint < intersections[i][j]) ? further[i][j] : further[j][i];
+} /* ----- end of function toleft ----- */
+
+
+/*
+ * === FUNCTION ======================================================================
+ * Name: intersect
+ * Description: work out the intersection of the line from chimneys[i] to chimneys[j]
+ * with the x-axis.
+ * =====================================================================================
+ */
+ double
+intersect ( int i, int j )
+{
+ assert ( chimneys[i].y != chimneys[j].y );
+ double xi = chimneys[i].x,
+ yi = chimneys[i].y,
+ xj = chimneys[j].x,
+ yj = chimneys[j].y;
+ return (yi*xj - yj*xi)/(yi - yj);
+} /* ----- end of function intersect ----- */
+
+
+/*
+ * === FUNCTION ======================================================================
+ * Name: distance
+ * Description:
+ * =====================================================================================
+ */
+ inline double
+distance ( double dx, double dy )
+{
+ return dx*dx + dy*dy;
+} /* ----- end of function distance ----- */
+
+
+/*
+ * === FUNCTION ======================================================================
+ * Name: output
+ * Description:
+ * =====================================================================================
+ */
+ void
+output ( double x )
+{
+ char result[128];
+ sprintf(result, "%.6f", x);
+ int len = 0;
+ while (result[len] != '\0') ++len;
+ while (len > 0 && (result[len-1] == '0' || result[len-1] == '.')) {
+ --len;
+ if ( result[len] == '.' ) break;
+ }
+ if ( len == 0 ) {
+ printf("0");
+ } else {
+ result[len] = '\0';
+ printf("%s", result);
+ }
+} /* ----- end of function output ----- */
+
+/*
+ * === FUNCTION ======================================================================
+ * Name: main
+ * Description:
+ * =====================================================================================
+ */
+ int
+main ( int argc, char *argv[] )
+{
+ int m; // number of chimneys
+ char target[MAXM]; // the order of chimneys acclaimed by Nick
+ int perspective[MAXM]; // order of chimneys from current viewpoint
+ double areapoint[MAXM*MAXM+1]; // segment points of areas
+ int numareapoint = 0; // number of segment points
+
+ scanf("%d\n%s\n", &m ,target);
+ for (int i = 0; i < m; ++i) {
+ scanf("\n%c %d %d", &(chimneys[i].shape), &(chimneys[i].x), &(chimneys[i].y));
+ perspective[i] = i;
+ }
+
+ areapoint[numareapoint++] = 0.0; // placeholder for left infinity
+ for (int i = 0; i < m; ++i) {
+ for (int j = i+1; j < m; ++j) {
+ if ( chimneys[i].y == chimneys[j].y ) continue;
+ double x0 = intersect(i, j);
+ double di = distance(chimneys[i].x - x0, chimneys[i].y);
+ double dj = distance(chimneys[j].x - x0, chimneys[j].y);
+ intersections[i][j] = intersections[j][i] = x0;
+ further[i][j] = (di > dj);
+ further[j][i] = (dj > di);
+ areapoint[numareapoint++] = x0;
+ }
+ }
+
+ sort( areapoint+1, areapoint + numareapoint );
+ numareapoint = unique( areapoint+1, areapoint + numareapoint ) - areapoint;
+
+ int validareas[MAXM*MAXM+1], numvalid = 0;
+ for (int i = 0; i < numareapoint; ++i) {
+ if ( i == numareapoint - 1 ) viewpoint = areapoint[numareapoint-1] + 1.0;
+ else if ( i == 0 ) viewpoint = areapoint[1] - 1.0;
+ else viewpoint = (areapoint[i] + areapoint[i+1]) / 2;
+
+ sort( perspective, perspective + m, toleft );
+
+ int j = 0;
+ for ( ; j < m; ++j) {
+ if ( chimneys[perspective[j]].shape != target[j] )
+ break;
+ }
+ if ( j == m ) {
+ validareas[numvalid++] = i;
+ }
+ }
+
+ printf("%d\n", numvalid);
+ bool isfirst = true;
+ for (int i = 0; i < numvalid; ++i) {
+ if ( !isfirst ) {
+ printf(" ");
+ } else {
+ isfirst = false;
+ }
+ if ( validareas[i] == 0 && validareas[i] == numareapoint - 1 ) {
+ printf("* *");
+ } else if ( validareas[i] == 0 ) {
+ printf("* "); output(areapoint[validareas[i]+1]);
+ } else if ( validareas[i] == numareapoint - 1 ) {
+ output(areapoint[validareas[i]]); printf(" *");
+ } else {
+ output(areapoint[validareas[i]]); printf(" "); output(areapoint[validareas[i]+1]);
+ }
+ }
+ if ( !isfirst ) printf("\n");
+
+ return EXIT_SUCCESS;
+} /* ---------- end of function main ---------- */
View
BIN  3342/party
Binary file not shown
View
154 3342/party.cpp
@@ -0,0 +1,154 @@
+/*
+ * =====================================================================================
+ *
+ * Filename: party.cpp
+ *
+ * Description: POJ 3342. Tree DP
+ *
+ * Version: 1.0
+ * Created: 10/09/2010 10:11:58 AM
+ * Revision: none
+ * Compiler: gcc
+ *
+ * Author: Ming Chen (chenming), chenming@sohu-rd.com
+ * Company: imeresearch@sogou
+ *
+ * =====================================================================================
+ */
+
+#include <vector>
+#include <string>
+#include <map>
+#include <iostream>
+
+using namespace std;
+
+class State {
+public:
+ int num;
+ bool unique;
+ State() : num(0), unique(true) {};
+};
+
+class Employee {
+public:
+ int boss;
+ vector<int> subordinates;
+};
+
+
+/*
+ * === FUNCTION ======================================================================
+ * Name: addname
+ * Description:
+ * =====================================================================================
+ */
+void addname ( string name, string boss, map<string, int> &names, vector<Employee> &employees, multimap<string, string> &buffer )
+{
+ int ci = names.size();
+ int bi = names[boss];
+ names[name] = ci;
+ employees[bi].subordinates.push_back(ci);
+ employees[ci].boss = bi;
+
+ multimap<string, string>::iterator beg = buffer.lower_bound(name);
+ multimap<string, string>::iterator end = buffer.upper_bound(name);
+ if ( beg == buffer.end() ) return;
+
+ vector<string> subs;
+ for (multimap<string, string>::iterator it = beg ; it != end; ++it ) {
+ subs.push_back(it->second);
+ }
+ buffer.erase(beg, end);
+
+ for (size_t i = 0; i < subs.size(); ++i) {
+ addname( subs[i], name, names, employees, buffer );
+ }
+} /* ----- end of function addname ----- */
+
+/*
+ * === FUNCTION ======================================================================
+ * Name: solve
+ * Description:
+ * =====================================================================================
+ */
+void solve ( const vector<Employee>& employees, int &num, bool &unique )
+{
+ int n = employees.size();
+ vector< vector<State> > states(n, vector<State>(2));
+
+ for (int i = n-1; i >= 0; --i) {
+ if ( employees[i].subordinates.size() == 0 ) {
+ states[i][0].num = 0;
+ states[i][1].num = 1;
+ continue;
+ }
+ State st0, st1;
+ for (size_t j = 0; j < employees[i].subordinates.size(); ++j) {
+ int c = employees[i].subordinates[j];
+ if ( states[c][0].num == states[c][1].num ) {
+ st0.num += states[c][0].num;
+ st0.unique = false;
+ } else if ( states[c][0].num > states[c][1].num ) {
+ st0.num += states[c][0].num;
+ st0.unique &= states[c][0].unique;
+ } else {
+ st0.num += states[c][1].num;
+ st0.unique &= states[c][1].unique;
+ }
+ st1.num += states[c][0].num;
+ st1.unique &= states[c][0].unique;
+ }
+ states[i][0].num = st0.num; states[i][0].unique = st0.unique;
+ states[i][1].num = st1.num + 1; states[i][1].unique = st1.unique;
+ }
+
+ if ( states[0][0].num > states[0][1].num ) {
+ num = states[0][0].num;
+ unique = states[0][0].unique;
+ } else if ( states[0][0].num < states[0][1].num ) {
+ num = states[0][1].num;
+ unique = states[0][1].unique;
+ } else {
+ num = states[0][0].num;
+ unique = false;
+ }
+} /* ----- end of function solve ----- */
+
+/*
+ * === FUNCTION ======================================================================
+ * Name: main
+ * Description:
+ * =====================================================================================
+ */
+ int
+main ( int argc, char *argv[] )
+{
+ int n = 0;
+ while ( cin >> n ) {
+ if (n == 0) break;
+
+ map<string, int> names;
+ multimap<string, string> buffer;
+ vector<Employee> employees(n);
+ string head;
+ cin >> head;
+ names[head] = 0;
+
+ for (int i = 1; i < n; ++i) {
+ string name, boss;
+ cin >> name >> boss;
+ if ( names.count(boss) == 0 ) { // defer insersion
+ buffer.insert(make_pair(boss, name));
+ } else {
+ addname(name, boss, names, employees, buffer);
+ }
+ }
+
+ int num;
+ bool unique;
+ solve(employees, num, unique);
+ cout << num << (unique ? " Yes" : " No") << endl;
+ }
+ return 0;
+} /* ---------- end of function main ---------- */
View
25 3342/party.in
@@ -0,0 +1,25 @@
+6
+Jason
+Jack Jason
+Joe Jack
+Jill Jason
+John Jack
+Jim Jill
+2
+Ming
+Cho Ming
+6
+A
+B A
+F C
+D A
+E A
+C A
+6
+A
+B A
+C A
+F C
+D A
+E A
+0
View
BIN  3342/party.o
Binary file not shown
Please sign in to comment.
Something went wrong with that request. Please try again.