poj1328

首先先贴上一个经典的错误代码,就是按照贪心算法使得圆尽可能的向右靠拢,但是其实这样是不对滴,原因是因为可能由于为了能够迎合左边的点,尽可能的使圆心向右压缩,那么就可能忽略掉高处的点。但是换一种思路,我们可以发现,如果先包含更高的点,那么稍微低一些的点可能自然的也被包含进来了。

正确的方法其实是这样的,首先对于需要进行检索的岛屿进行一个海岸线上的起始点和终点的计算,然后去重即可。
在输入数据之后就进行判断,看是否有距离海岸的岛屿的距离要大于d,这样直接失败,然后——先cin>>n>>d;然后再continue;忘记去cin简直坑爹。
关于策略,选择能够包含最左边的一些岛屿的最靠右的一条线段,可以看到last变量在不断减小,就是为了保证能够包含所有争取最靠左边的岛屿。

while(index<n){
            double last;
            last = st[index].second;
            far = index;
            while(far<n && st[far].first<=last){
                last = min(last,st[far].second);
                ++far;
            }
            index = far;
            ++count;
        }
test case 1:
4 5
-5 3
-3 5
2 3
3 3

Case 1: 2

test case 2:
2 5
-3 4
-6 3


4 5
-5 3
-3 5
2 3
3 3

20 8
-20 7
-18 6
-5 8
-21 8
-15 7
-17 5
-1 5
-2 3
-9 6
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 7
9 6
10 5
0 0

2 3
0 2
2 3

2 3
0 2
1 3

3 3
1 2
-3 2
2 4

8 5
2 4
-4 4
-3 3
-3 1
-3 0
-1 0
0 5
6 0

3 0
1 2
-3 1
2 1

3 2
1 2
-3 1
2 1

1 2
0 2


2 3
0 2
2 3

4 -5
4 3
4 3
2 3
6 -9



3 -3
1 2
-3 2
2 1

6 2
1 2
1 2
1 2
-3 1
2 1
0 0

1 2
0 2

2 3
0 2
1 3

3 10
1 10
2 3
4 5

3 5
1 10
2 3
4 5

4 7
1 10
2 3
4 5
0 0

3 9
1 10
2 3
4 5

0 0

output: 
Case 1: 1
Case 2: 2
Case 3: 4
Case 4: 1
Case 5: 1
Case 6: -1
Case 7: 3
Case 8: -1
Case 9: 2
Case 10: 1
Case 11: 1
Case 12: -1
Case 13: -1
Case 14: 2
Case 15: 1
Case 16: 1
Case 17: 1
Case 18: -1
Case 19: -1
Case 20: -1
//wrong code
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
#define maxn 1024

pair<double,double> pos[maxn];
bool flag[maxn];
bool cmp(pair<double,double> a,pair<double,double> b){
    return a.first>b.first;
}
int main(int argc, const char * argv[]) {
    int n;
    double d;
    cin>>n>>d;
    int kase=0;
    while( (d!=0)||(n!=0) ){
        ++kase;
        memset(flag,0,sizeof(flag));
        double maxy=0;
        for(int i=0;i<n;i++){
            scanf("%lf%lf",&pos[i].first,&pos[i].second);
            maxy = max(pos[i].second,maxy);
        }
        if(maxy>d){
            cout<<"Case "<<kase<<": "<<-1<<endl;
            continue;
        }
        sort(pos,pos+maxn,cmp);

        int count = 0;
        int index = 0;
        while(index<n){
            double setx = pos[index].first + sqrt(pow(d,2) - pow(pos[index].second,2));
            for(int i = index;i<n && pos[i].first<=setx+d;i++){
                if( (pow(pos[i].first-setx,2) + pow(pos[i].second,2)) <=pow(d,2)){
                    flag[i] = true;

                }
            }
            while(index<n && flag[index]==true)++index;
            ++count;
        }
        cout<<"Case "<<kase<<": "<<count<<endl;
        cin>>n>>d;
    }
    return 0;
}
//correct code
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
#define maxn 1024

pair<double,double> pos[maxn];
pair<double,double> st[maxn];

bool cmp(pair<double,double> a,pair<double,double> b){
    return a.first>b.first;
}

int main(int argc, const char * argv[]) {
    int n;
    double d;
    cin>>n>>d;
    int kase=0;
    while( (d!=0)||(n!=0) ){
        ++kase;
        double maxy=0;
        for(int i=0;i<n;i++){
            scanf("%lf%lf",&pos[i].first,&pos[i].second);
            maxy = max(pos[i].second,maxy);
        }
        if(maxy>d){
            cout<<"Case "<<kase<<": "<<-1<<endl;
            cin>>n>>d;
            continue;
        }
        for(int i=0;i<n;i++){
            st[i].first = pos[i].first - sqrt(pow(d,2) - pow(pos[i].second,2));
            st[i].second = pos[i].first + sqrt(pow(d,2) - pow(pos[i].second,2));
        }
        sort(st,st+n);
        /*for(int i=0;i<n;i++){
            cout<<st[i].first<<' '<<st[i].second<<endl;
        }*/
        int index = 0 , count = 0 , far = 0;
        while(index<n){
            double last;
            last = st[index].second;
            far = index;
            while(far<n && st[far].first<=last){
                last = min(last,st[far].second);
                ++far;
            }
            index = far;
            ++count;
        }
        cout<<"Case "<<kase<<": "<<count<<endl;
        cin>>n>>d;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值