zoj2967(堆栈应用)

本文针对ACM ZJU 2967题“ColorfulRainbows”,解析了如何通过筛选和排序来确定哪些彩虹(直线)能独立可见。具体方法包括去重、排序以及使用栈结构来找出可视彩虹的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2967

Colorful Rainbows

Time Limit: 2 Seconds       Memory Limit: 65536 KB

Evelyn likes drawing very much. Today, she draws lots of rainbows on white paper of infinite size, each using a different color. Since there're too many rainbows now, she wonders, how many of them can be seen?

For simplicity, each rainbow Li is represented as a non-vertical line specified by the equation: y=aix+bi. A rainbow Li can be seen if there exists some x-coordinate x0 at which, its y-coordinate is strictly greater than y-coordinates of any other rainbows: aix0+bi > ajx0+bj for all j != i.

Now, your task is, given the set of rainbows drawn, figure out the number of rainbows that can be seen.

Input

Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 60) which is the number of test cases. And it will be followed by T consecutive test cases.

There's a blank line before every case. In each test case, there will first be an integer n (1 <= n <= 5000), which is the number of rainbows. Then n consecutive real number pairs follow. Each pair contains two real numbers, ai and bi, representing rainbow Li: y=aix+bi. No two rainbows will be the same, that is to say, have the same a and b.

Output

Results should be directed to standard output. The output of each test case should be a single integer, which is the number of rainbows that can be seen.

Sample Input
2

1
1 1

3
1 0
2 0
3 0
Sample Output
1
2

题意:给出n条y=ai*x+bi的直线。对于这些直线,如果存在x使得该直线y大于其他任意一直线,那么这条直线可以被看见,问有多少条直线可以被看见。

思路:首先去重,将那些a值相同的直线取其中b最大的那条保留下来,其他的全删掉。

            其次将直线按照a值从小到大排序,因为斜率不同,所以任意两条直线都会相交。而这些直线是按照斜率从小到大进行排序,所以当x小于其交点x值时,斜率小的y值大。

            利用这一特性将,先让线入栈。若将入栈的线与栈顶线的交点x值小于栈顶两条线的的交点的x值,则将栈顶线出栈,继续进行上一次判断,知道所有线都判定过。

#include<stdio.h>
#include<cmath>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
struct node{
    double a,b;
}s[5010];
bool kmp(node x,node y)  //高端sort
{
    return (x.a<y.a)||(x.a==y.a&&x.b<y.b);
}
int main()
{
    int t,m,n,k,maxx,ans;
    double d,p;
    node a[5010];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%lf%lf",&s[i].a,&s[i].b);
        sort(s,s+n,kmp);k=0;
        for(int i=0;i<n-1;i++)  //去重
        {
            if(s[i].a==s[i+1].a)
                continue;
            s[k++]=s[i];
        }
        s[k++]=s[n-1];
        if(k<2)
        {
            printf("%d\n",k);
            continue;
        }
        ans=2;a[0]=s[0];a[1]=s[1];
        d=(a[1].b-a[0].b)*1.0/(a[0].a-a[1].a);
        for(int i=2;i<k;i++)  //入栈出栈操作
        {
            p=(s[i].b-a[ans-1].b)*1.0/(a[ans-1].a-s[i].a);
            while(p<=d)
            {
                ans--;
                if(ans>1)
                {
                    d=(a[ans-1].b-a[ans-2].b)*1.0/(a[ans-2].a-a[ans-1].a);
                    p=(s[i].b-a[ans-1].b)*1.0/(a[ans-1].a-s[i].a);
                }
                else
                {
                    p=(s[i].b-a[ans-1].b)*1.0/(a[ans-1].a-s[i].a);
                    break;
                }
            }
            a[ans++]=s[i];
            d=p;
        }
        printf("%d\n",ans);  //栈的长度就是答案
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值