解决此题方法类似于凸包,先把所有直线按照斜率a由小到大排序
斜率相同取b较大的,扔掉b小的 (可以在遍历的时候忽视)。
于是所有直线斜率不同。 准备一个栈 (手动模拟),
栈里面存放上一次能看到的“最上面”的直线以及这条直线能看到的范围x (x值右边的部分可以被看到)。
初始时,把斜率最小的直线入栈,并记录x值为-inf。然后对第i条直线, 所做的是用第i条直线和栈顶直线求交点x,如果这个x值不大于栈顶的x值,
则把栈顶元素弹出,继续求交,否则退出。
这种判断操作直到栈为空,
或者当前栈顶的x值大于栈顶的x值。然后把第i条直线入栈,
继续,看后面的直线。最后栈中的直线数就是能看到的。
这种做法类似于凸包的方法,除去排序外,每条直线至多出入栈一次 复杂度O(n)。总复杂度是O(nlogn)。

Source Code:

//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0) using namespace std; typedef long long ll ;
typedef unsigned long long ull ;
typedef unsigned int uint ;
typedef unsigned char uchar ; template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;} const double eps = 1e- ;
const int N = ;
const int M = * ;
const ll P = 10000000097ll ;
const int MAXN = ;
const double MINN = -999999999.9; int n; struct Line{
double a, b;
}line[]; struct Point{
int ID;
double x; //Crossover point
}point[]; bool cmp (Line a , Line b){
if(a.a != b.a) return a.a < b.a ;
return a.b > b.b;
} int main(){
int i, j, t, k, u, v, numCase = ;
cin >> t;
while (t--){
int sum = ;
cin >> n;
for (i = ; i < n; ++i) {
cin >> line[i].a >> line[i].b;
}
sort (line , line + n, cmp); point[sum].ID = ;
point[sum].x = MINN; for (i = ; i < n; ++i) {
if (Abs (line[i].a - line[point[sum].ID].a) < eps) continue; //Equal slope will be ignored
for (;;) {
if (sum < ) {
++sum;
point[sum].ID = i;
point[sum].x = MINN;
break;
}
double x = (line[point[sum].ID].b - line[i].b) / (line[i].a - line[point[sum].ID].a);
if (point[sum].x + eps > x) {
--sum;
} else {
++sum;
point[sum].ID = i;
point[sum].x = x;
break;
}
}
}
printf("%d\n", + sum);
} return ;
}

以下是另一种 O(n lg n) 的算法 @Sake

对于直线l1: y = a1x +b1 和直线l2: y = a2x + b2,假设 a2 > a1,同时已知l1和前面面
一一个斜率更小小的交点横坐标为x1,则l2和l1的交点横坐标为x2,如果x2 < x1,那么l2可以覆盖
l1的可⻅见段。这样对于l2处理栈中的栈顶直线,若可以覆盖,栈顶弹出,循环直到栈中只有一一条直线或者不能继续覆盖,最后将这条直线压入入栈。对于只有1条或者2条直线的情况,特殊处理即可。
效率为O(n lg n)。
当然这题,可以按题⺫目目描述模拟暴力力O(n^2)过。
/*
Author:
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
sakeven
<cstdio>
<cstdlib>
<cassert>
<iostream>
<algorithm>
<queue>
<stack>
<cmath>
<cstring>
using namespace std;
struct node{
double a,b;
}lines[];
struct point{
int index;
double x;
point(){}
point(int index, double x):index(index), x(x){}
};
bool cmp(const node &A, const node &B){
return A.a < B.a || (A.a == B.a && A.b < B.b);
}
int main(){
int t, n;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (int i = ; i < n; i ++) {
scanf("%lf%lf", &lines[i].a , &lines[i].b);
}
sort(lines, lines+n, cmp);
int k = ;
for (int i = ; i < n-; i ++) {
if (lines[i].a == lines[i+].a) {
continue;
}
lines[k++] = lines[i];
}
lines[k++] = lines[n-];
if (k <= ) {
printf("%d\n", k);
continue;
}point p;
stack<point>st;
st.push(point(, ));
st.push(point(, (lines[].b - lines[].b)/(lines[].a - lines[].a)));
for (int i = ; i < k; i ++) {
double x();
while (st.size() > ) {
p = st.top();
x = (lines[p.index].b - lines[i].b)/(lines[i].a - lines[p.index].a);
if (x <= p.x) {
st.pop();
if (st.size() == ) {
x = (lines[].b - lines[i].b)/(lines[i].a - lines[].a);
break;
}
} else {
break;
}
}
st.push(point(i, x));
}
printf("%lu\n", st.size());
}
return ;
}

  

ZOJ 2967 Colorful Rainbows 【Stack】的更多相关文章

  1. ZOJ 2967 Colorful Rainbows

    暴力. 先删掉一些边,平行的线只保留$b$最大的.然后暴力,每次放入第$i$条边,和还没有被完全覆盖的边都算一遍,更新一下. #pragma comment(linker, "/STACK: ...

  2. hdu 1237 简单计算器 (表达式求值)【stack】

    <题目链接> 题目大意: 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值.  Input测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符, ...

  3. ZOJ 3959 Problem Preparation 【水】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3959 AC代码 #include <cstdio> ...

  4. ZOJ 3958 Cooking Competition 【水】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3958 AC代码 #include <cstdio> ...

  5. ZOJ - 4020 Traffic Light 【BFS】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4020 题意 给出一张地图 以及起点和终点 求是否能从起点走到终点 ...

  6. ZOJ - 3930 Dice Notation 【模拟】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3930 题意 给出一串字符串 如果是 '+' '-' '*' '/ ...

  7. ZOJ - 3866 Cylinder Candy 【数学】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3866 思路 积分 参考博客 https://blog.csdn. ...

  8. ZOJ - 3948 Marjar Cola 【循环】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3948 题意 用 x 个 瓶身 可以 换 一瓶饮料 用 y 个 瓶 ...

  9. CF 286(div 2) B Mr. Kitayuta's Colorful Graph【传递闭包】

    解题思路:给出n个点,m条边(即题目中所说的两点之间相连的颜色) 询问任意两点之间由多少种不同的颜色连接 最开始想的时候可以用传递闭包或者并查集来做,可是并查集现在还不会做,就说下用传递闭包来做的这种 ...

随机推荐

  1. 解决eclipse中maven出现的Failure to transfer XXX.jar的问题

    这个问题很烦,试了好几次都没有彻底解决,今天终于找到解决办法了. 问题主要出在,maven在下载jar的过程中出现了中断或者错误问题(不仅仅是eclipse,其他IDE也一样) 解决办法: 移除之前的 ...

  2. nrf51 SDK自带例程的解读

    简单的pwm电机控制示例 simple_pwm_motor_control_example 其实就是pwm控制led的亮度 1.首先设置gpiote 设置初始为高电平2.接着设置ppi 定时器time ...

  3. 【转】AC算法详解

    原文转自:http://blog.csdn.net/joylnwang/article/details/6793192 AC算法是Alfred V.Aho(<编译原理>(龙书)的作者),和 ...

  4. document.compatMode简介

    对于document.compatMode,很多朋友可能很少接触,知道他的存在却不清楚他的用途.今天在ext中看到 document.compatMode的使用,感觉这个对于我们开发兼容性的web页面 ...

  5. installscript类型 完成时实现推荐安装其他产品的功能

    目前好多软件在安装完成时都有什么 立刻运行.打开网址.推荐安装其他工具等功能 我司领导也追时髦要求了这个功能而且要推荐多个,所以这个功能实现起来就需要自己去写代码了.陆陆续续研究了研究了好长时间,由于 ...

  6. Hierarchical Storage structure

    1.hierarchical storage structure      This notion of inserting a smaller, faster storage device (e.g ...

  7. GLSL中的各种变量总结

    1.uint CreateShader(enum type) : 创建空的shader object; type: VERTEX_SHADER, 2.void ShaderSource(uint sh ...

  8. javadoc入门

    斌斌 (给我写信) 原创博文(http://blog.csdn.net/binbinxyz),转载请注明出处! java凝视 java里面有两种类型的凝视.一种是以"/*"起头,以 ...

  9. HDU Computer Transformation1041 题解

    Problem Description A sequence consisting of one digit, the number 1 is initially written into a com ...

  10. qnx:从API开始理解QNX -- 消息传递

    从API开始理解QNX -- 消息传递    http://www.openqnx.com/chinese/viewtopic.php?f=5&t=2161 1. 频道与连接    Chann ...