ACM訓練——Colorful Rainbows

題目鏈接:https://vjudge.net/problem/ZOJ-2967

 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

 暴力解法:依次判斷每條直線是否可以在某一個區間(l,r)內作爲所有直線中的最高點,那麼主要的任務就是求(l,r)

                  (1)如果L1的的斜率等於L2的斜率,這羊樣直接看截距,和(l,r)沒關係

                  (2)如果L1的斜率大於L2的斜率,假設交點爲x0,那麼區間(x0,+INF)內y1>y2(函數值),那麼相對於L1來說,                                 l=max(l,x0).更新l。

                  (3)如果L1的斜率小於L2的斜率,假設交點爲x0,那麼區間(-INF,x0)內y1>y2(函數值),那麼相對於L1來說,                                 r=min(r,x0).更新r。

最後判斷一條直線可不可以存在合理的(l,r)就可以看出該直線是否是答案中的一員

#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
#include <cstdio>
#include <string>
#include <stack>
#include <set>
#define IOS ios::sync_with_stdio(false), cin.tie(0)
using namespace std;
typedef long long ll;
const double INF=-10000000000000000;
double a[5010],b[5010];
bool vis[5010];//vis[j]=true表示直線j已經不可能作爲最大值
int main()
{
    IOS;
    ll t;
    ll n;
    double temp,l,r;
    cin>>t;
    while(t--){
        memset(vis,false,sizeof(vis));
        cin>>n;
        ll ans=0;
        for(int i=0;i<n;i++){
            cin>>a[i]>>b[i];
        }
        //我們把每條直線作爲最高值出現所在的x值稱爲xi。
        for(int i=0;i<n;i++){//遍歷每條直線
            if(vis[i])continue;//如果不可能在某個點作爲最高點直接遍歷下一條
            l=INF;//初始化xi的範圍(負無窮,正無窮)
            r=-INF;
            int j;
            //和其他直線比較,更新l,r
            for(j=0;j<n;j++){
                if(i==j)continue;
                if(a[i]==a[j]){//斜率相等看截距
                    if(b[i]<=b[j])break;
                    else vis[j]=true;//直線j不可能在某處作爲最高值
                }
                else {
                    //求出交點
                    temp=(b[j]-b[i])/(a[i]-a[j]);
                    if(a[i]>a[j]){//更新xi的左界
                        l=max(l,temp);
                        if(l>=r)break;//l必須小於r纔有可能
                    }
                    else {//更新xi的右界
                        r=min(r,temp);
                        if(l>=r)break;
                    }
                }
            }
            if(j==n)ans++;//沒有提前退出,說明該直線在(l,r)內值最大
        }
        cout<<ans<<endl;
    }
    getchar();
    getchar();
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章