Sicily 1004. I Conduit!
来源:互联网 发布:ubuntu安装muduo出错 编辑:程序博客网 时间:2024/05/06 05:45
题目链接在此
参考的是这位大神的解题方法
这题要考虑的情况有很多。
另外两个double类型数据不能简单地以==来判断相等!
以下是源代码:
#include<iostream>#include<cmath>#include<algorithm>using namespace std;#define INFINITE 10000.00bool equal(double a, double b) { //两个double类型数据不能简单地以==来判断相等return fabs(a - b) < 1e-6;}struct line {double k, b;double x1, y1, x2, y2;line() {}line(double xx1, double yy1, double xx2, double yy2, double kk, double bb): x1(xx1), y1(yy1), x2(xx2), y2(yy2), k(kk), b(bb) {}// 对线段(x1,y1)-(x2,y2)转化成 y=kx+b 形式,进行排序,按照 k 升序排序,如果 k 值相等,则按 b 值升序排序,// 如果 k,b都相等,则按 x1 升序排序,若 x1 还相等的话,则按 x2 升序排序。// 要对 平行于 y轴,即斜率不存在的线段 加以判断,// 先按照 b 值(b==x1)升序排序,再按 y1 升序排序,最后按 y2 升序排序bool operator<(const line& other) const{ // 自定义比较函数if (equal(k, other.k)){if (equal(k, INFINITE)) {if (equal(b, other.b)) {if (equal(y1, other.y1))return y2 < other.y2;elsereturn y1 < other.y1;}elsereturn b < other.b;}else if (equal(b, other.b)) {if (x1 == other.x1)return x2 < other.x2;elsereturn x1 < other.x1;}elsereturn b < other.b;}elsereturn k<other.k;}} s[10000];int main() {int lineNum;double x1, y1, x2, y2;while (cin >> lineNum && lineNum != 0) {for (int i = 0; i < lineNum; i++) {cin >> x1 >> y1 >> x2 >> y2;if (x1 > x2 || (equal(x1, x2) && y1 > y2)) { //保证x1<x2或斜率不存在时有y1<y2swap(x1, x2); swap(y1, y2); }if (equal(x1, x2))s[i] = line(x1, y1, x2, y2,INFINITE,x1);else {s[i] = line(x1, y1, x2, y2, (y2 - y1) / (x2 - x1), 0);s[i].b = y1 - s[i].k * x1;}}sort(s, s + lineNum);int counter = 1;double farX = s[0].x2, farY = s[0].y2; //(farX,farY)记录当前线段的终点for (int i = 1; i < lineNum; i++) {//如果k 或者b 不相等,则肯定不在同一直线上if (!equal(s[i].k, s[i - 1].k) || !equal(s[i].b, s[i - 1].b)) {farX = s[i].x2;farY = s[i].y2;counter++;}//在同一直线上但没有公共部分else if ((s[i].k != INFINITE && s[i].x1 > farX) || (s[i].k == INFINITE && s[i].y1 > farY)) {farX = s[i].x2;farY = s[i].y2;counter++;}//在同一直线上而且有公共部分,如果当前线段的终点更远,那也要更新(x,y)else if ((s[i].k != INFINITE && farX < s[i].x2) || (s[i].k == INFINITE && farY < s[i].y2)) {farX = s[i].x2;farY = s[i].y2;}}cout << counter << endl;}return 0;}
0 0
- Sicily.1004. I Conduit!
- Sicily 1004. I Conduit!
- Sicily 1004. I Conduit!
- sicily 1004.I Conduit!
- Sicily 1004 I Conduit!
- 1004. I Conduit!
- 1004. I Conduit!
- 1004. I Conduit!
- 1004: I Conduit!
- POJ 2036 I Conduit!
- soj.1004 I Conduit!
- POJ 2036 I Conduit! 笔记
- East Central North America 2004 I Conduit!
- Codeforces GYM 100651 D I Conduit! (水计算几何)
- Sicily 13295. Kingdom Rush I
- sicily 1155 Can I Post the letter
- Sicily 1155 Can I Post the letter
- Sicily 1155. Can I Post the lette
- 生活的大门向你敞开……
- 多媒体短信服务----MMS
- 阿里注册双十一 其他电商不服气
- CodeIgniter框架中关于DB事务处理的设计缺陷
- 获取android sd卡中某个文件夹下的所有图片
- Sicily 1004. I Conduit!
- [Leetcode]Two Sum
- Android 监听软键盘按键的三种方式
- 乡爱制片人持诈弹强卖古玩涉编虚假恐怖信息罪
- bind-geoip编译使用说明
- dsa算法(15)
- WEB界面客制化之采购审批界面(直接改WF程序包)
- Linux 下的memcache+php工作原理及安装
- Android实现微信底部的带消息提示数量