pku 1328 第一周训练 ——贪心

http://poj.org/problem?id=1328

这个题目就是每个岛屿对应一个雷达区间,然后确定好雷达区间后,然后在将区间的s或者e从小到大排序,然后贪心。

第一种按s从小到大排序:

View Code
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define maxn 1007
using namespace std;
struct node
{
int x,y;
}tagp[maxn];
struct mode
{
double s,e;
}tagq[maxn];
int cmp(mode a,mode b)
{
return a.s < b.s;
}

int main()
{
//freopen("d.in","r",stdin);
int i,n,d;
int cas = 1;
while (scanf("%d%d",&n,&d))
{
if (!n && !d) break;

for (i = 0; i < n; ++i)
scanf("%d%d",&tagp[i].x,&tagp[i].y);
int flag = 0;
for (i = 0; i < n; ++i)
{
if (tagp[i].y > d)
{
flag = 1;
break;
}
double tmp = sqrt((1.0*d*d) - (1.0*tagp[i].y*tagp[i].y));
double s = 1.0*tagp[i].x - tmp;
double e = 1.0*tagp[i].x + tmp;
tagq[i].s = s;
tagq[i].e = e;

}
if (flag)
{
printf("Case %d: %d\n",cas++,-1);
continue;
}
sort(tagq,tagq + n,cmp);
double e = tagq[0].e;
int count = 1;
for (i = 1; i < n; ++i)
{
if (tagq[i].s > e)
{
count++;
e = tagq[i].e;
}
else
{
if (tagq[i].e < e)//注意新的终点的确定
{
e = tagq[i].e;
}
}
}
printf("Case %d: %d\n",cas++,count);

}
return 0;
}

第二中是按e从小到大排序,这里定义int型来接受double型的数据,悲催死我了。。检查了很长时间。。就是经典贪心之会场安排,不过还是有点差别的。

View Code
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define maxn 1007
using namespace std;
struct node
{
int x,y;
}tagp[maxn];
struct mode
{
double s,e;
}tagq[maxn];
bool hash[maxn];
int cmp(mode a,mode b)
{
return a.e < b.e;
}
int main()
{
//freopen("d.in","r",stdin);
int i,count;
int n,d;
int cas = 1;
while (scanf("%d%d",&n,&d))
{
if (!n && !d) break;
int flag = 0;
for (i = 0; i < n; ++i)
{
scanf("%d%d",&tagp[i].x,&tagp[i].y);
if (tagp[i].y > d)
{
flag = 1;
}
double tmp = sqrt((1.0*d*d) - 1.0*(tagp[i].y*tagp[i].y));
tagq[i].s = 1.0*tagp[i].x - tmp;
tagq[i].e = 1.0*tagp[i].x + tmp;
}
if (flag)
{
printf("Case %d: %d\n",cas++,-1);
continue;
}
sort(tagq,tagq + n,cmp);
//for (i = 0; i < n; ++i)
//printf("%.2lf %.2lf\n",tagq[i].s,tagq[i].e);
int mark;
memset(hash,false,sizeof(hash));
count = 0;
int j;
j = 0;
while (1)
{
mark = 0;
double e = tagq[j].e;
for(i = 0; i < n; ++i)
{
//printf(">>%.2lf %.2lf\n",tagq[i].s,e);
if (!hash[i] && tagq[i].s <= e)//只有起点小于这个区间的终点的才能公用一个雷达
{
hash[i] = true;
}
}
count++;
for (i = 0; i < n; ++i)
{
if (!hash[i])
{
j = i;
mark = 1;
break;
}
}
if (!mark)
{
printf("Case %d: %d\n",cas++,count);
break;
}
}
}
return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值