Advertisement
Guest User

Untitled

a guest
Sep 8th, 2013
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.75 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5. #include <stack>
  6. #include <queue>
  7. #include <set>
  8. #include <map>
  9. #include <string>
  10. #include <algorithm>
  11. #include <iostream>
  12. #include <functional>
  13. using namespace std;
  14.  
  15. struct Point {
  16.     double x, y;
  17.     Point(double X = 0, double Y = 0) : x(X), y(Y) {}
  18. } points[60];
  19.  
  20. bool between(double v, double a, double b) {
  21.     return (a <= v && v <= b) || (a >= v && v >= b);
  22. }
  23.  
  24. pair<bool, double> inter(Point a, Point b, double y) {
  25.     double x = a.x + (y - a.y) * (b.x - a.x) / (b.y - a.y);
  26.     return make_pair(between(x, a.x, b.x) && between(y, a.y, b.y), x);
  27. }
  28.  
  29. int p, n, maxY, minY;
  30. map<int, pair<int, int> > ans;
  31.  
  32. int main() {
  33.     //freopen("input.txt", "r", stdin);
  34.     //freopen("output.txt", "w", stdout);
  35.  
  36.     scanf("%d", &p);
  37.     for (int tst = 1; tst <= p; tst++) {
  38.         ans.clear();
  39.         scanf("%*d%d", &n);
  40.         for (int i = 0; i < n; i++) {
  41.             scanf("%lf%lf", &points[i].x, &points[i].y);
  42.             if (i == 0)
  43.                 minY = maxY = points[i].y;
  44.             minY = min(minY, (int)points[i].y);
  45.             maxY = max(maxY, (int)points[i].y);
  46.         }
  47.         for (int y = maxY - 1; y > minY; y--) {
  48.             vector<double> line;
  49.             for (int i = 0; i < n; i++) {
  50.                 pair<bool, double> ix = inter(points[i], points[(i + 1) % n], y);
  51.                 if (ix.first)
  52.                     line.push_back(ix.second);
  53.             }
  54.             sort(line.begin(), line.end());
  55.             if (!line.empty() && ceil(*line.begin() + 1e-12) <= floor(*line.rbegin() - 1e-12))
  56.                 ans.insert(make_pair(y, make_pair(ceil(*line.begin() + 1e-12), floor(*line.rbegin() - 1e-12))));
  57.         }
  58.         printf("%d %d\n", tst, ans.size());
  59.         for (map<int, pair<int, int> >::reverse_iterator i = ans.rbegin(); i != ans.rend(); i++)
  60.             printf("%d %d %d\n", i->first, i->second.first, i->second.second);     
  61.     }
  62.    
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement