PHP Google CodeJam 2010 Round 1C - Problem A. Rope Intranet

AceInfinity

Emeritus, Contributor
Joined
Feb 21, 2012
Posts
1,728
Location
Canada
Here's another one I solved from the last round before world finals from the 2010 CodeJam.

Problem

A company is located in two very tall buildings. The company intranet connecting the buildings consists of many wires, each connecting a window on the first building to a window on the second building.

You are looking at those buildings from the side, so that one of the buildings is to the left and one is to the right. The windows on the left building are seen as points on its right wall, and the windows on the right building are seen as points on its left wall. Wires are straight segments connecting a window on the left building to a window on the right building.

You've noticed that no two wires share an endpoint (in other words, there's at most one wire going out of each window). However, from your viewpoint, some of the wires intersect midway. You've also noticed that exactly two wires meet at each intersection point.

On the above picture, the intersection points are the black circles, while the windows are the white circles.

How many intersection points do you see?
images


The first line of the input gives the number of test cases, T. T test cases follow. Each case begins with a line containing an integer N, denoting the number of wires you see.

The next N lines each describe one wire with two integers Ai and Bi. These describe the windows that this wire connects: Ai is the height of the window on the left building, and Bi is the height of the window on the right building.
Output

For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is the number of intersection points you see.
Limits

1 ≤ T ≤ 15.
1 ≤ Ai ≤ 104.
1 ≤ Bi ≤ 104.


Within each test case, all Ai are different.
Within each test case, all Bi are different.
No three wires intersect at the same point.
Small dataset: 1 ≤ N ≤ 2.
Large dataset: 1 ≤ N ≤ 1000.

SAMPLE:

Input:
Code:
[NO-PARSE]2
3
1 10
5 5
7 7
2
1 1
2 2[/NO-PARSE]

Output:
Code:
[NO-PARSE]Case #1: 2
Case #2: 0[/NO-PARSE]

Code:
[NO-PARSE]#include <iostream>
#include <fstream>
#include <cmath>
#include <string>
#include <sstream>
#include <iterator>
#include <vector>
#include <map>
#include <algorithm>

/* solves the case in question, and writes the result to the
 * output filestream, where k represents the current case #.
 *   - Output Format -> Case #k: {results}
 *
 * N      - number of wires
 * Ai Bi  - height of left endpoint, height of right endpoint
 *
 * Find # of intersecting points
 * ---------------------------------------------------------------------- */
void solve_case(int k, std::ofstream &ofs, std::ifstream &ifs)
{
  int N; ifs >> N; ifs.get(); // number of wires
  int tmp1, tmp2;
  std::map<int, int> m;
  for (int i = 0; i < N; ++i)
  {
    ifs >> tmp1; ifs.get();
    ifs >> tmp2; ifs.get();
    m.insert(std::make_pair(tmp1, tmp2));
  }

  int intersections = 0;
  auto it1 = m.begin();
  while (it1 != m.end())
  {
    auto it2 = it1;
    while (++it2 != m.end())
    {
      if (it1->second > it2->second) ++intersections;
    }
    ++it1;
  }

  ofs << "Case #" << k << ": " << intersections;
  std::endl(ofs);
}

/* ---------------------------------------------------------------------- */

int open_filestreams(const char *input_file, std::ofstream &ofs, std::ifstream &ifs)
{
  std::string output_file(input_file);
  output_file.append("-result.txt");

  ofs.open(output_file, std::ofstream::out);
  if (!ofs.is_open()) return 0;

  ifs.open(input_file, std::ifstream::in | std::ifstream::binary);
  if (!ifs.is_open()) return 0;

  return 1;
}

int main(int argc, char const *argv[])
{
  if (argc < 2) return 1;

  std::ofstream ofs; // open output file for case results
  std::ifstream ifs; // open input file for reading
  if (!open_filestreams(argv[1], ofs, ifs)) return 1;

  int N, k = 0;        // N total cases, k case number
  ifs >> N; ifs.get(); // grab total cases, junk '\n' character
  do { solve_case(++k, ofs, ifs); }
  while (--N);

  ifs.close(); ofs.close(); // close streams
  // std::cin.sync(); std::cin.get();
}[/NO-PARSE]

Link to problem: Dashboard - Round 1C 2010 - Google Code Jam
 

Has Sysnative Forums helped you? Please consider donating to help us support the site!

Back
Top