【个人训练】(POJ1328)Radar Installation
来源:互联网 发布:中航led控制软件 编辑:程序博客网 时间:2024/06/03 16:16
这条题目A了十次。。。emmmmm 其实不难就是一个贪心。。。。
先说下算法(之前的和现在的)
之前考虑的其实很简单。用平面几何即可将雷达可以放置的区域转化为区间(顺便判断是否无解。问题就比较简单了:先排序。然后我们第一个区间我们肯定是放在区间的最右边,然后对后面的我们如何处理呢?若后一个区间的左边在目前区间的右边,我们肯定是不用动这个雷达的摆放位置的;如果不满足,那么我们就要看看后一个区间的右边和这个区间的摆放位置了。如果后一个区间的右边在目前区间的右边的左边,那么我们就应当放在后一个区间的右边。按照这个思路就可以了。
再谈下自己错误的地方
- 浮点数,我前面几次全部都是用int保存的,省略了很多精度,导致了错误还死活调不出来。(这个应该是主要错误。)
- 这题有大量i/o,应当采用c-style的输入输出或者关掉tie。
- 各种代码细节问题,这个是编程基本功,以后慢慢提升吧。
代码
原题的代码套用了一个网上的模板,看起来有点恐怖233
#include <cctype>#include <numeric>#include <climits>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <string>#include <sstream>#include <algorithm>#include <complex>#include <deque>#include <list>#include <map>#include <queue>#include <set>#include <stack>#include <vector>#include <utility>#include <bitset>using namespace std;#define REP(i,n) for(int i=0;i!=static_cast<int>(n);++i)#define REP_1(i,n) for(int i=1;i<=static_cast<int>(n);++i)#define FOR(i,a,b) for(int i=static_cast<int>(a);i<static_cast<int>(b);++i)#define FOR_1(i,a,b) for(int i=static_cast<int>(a);i<=static_cast<int>(b);++i)#define DWN(i,b,a) for(int i=static_cast<int>(b);i>static_cast<int>(a);--i)#define DWN_1(i,b,a) for(int i=static_cast<int>(b);i>=static_cast<int>(a);--i)#define ECH(it,A) for(auto it=A.begin();it!=A.end();++it)#define REP_2(i,j,n,m) REP(i,n) REP(j,m)#define REP_2_1(i,j,n,m) REP_1(i,n) REP_1(j,m)#define REP_3(i,j,k,n,m,l) REP(i,n) REP(j,m) REP(k,l)#define REP_3_1(i,j,k,n,m,l) REP_1(i,n) REP_1(j,m) REP_1(k,l)#define ALL(A) A.begin(), A.end()#define LLA(A) A.rbegin(), A.rend()#define CPY(A,B) memcpy(A,B,sizeof(A))#define INS(A,P,B) A.insert(A.begin()+P,B)#define ERS(A,P) A.erase(A.begin()+P)#define BSC(A,X) find(ALL(A),X)// != A.end()#define CTN(T,X) (T.find(X)!=T.end())#define SZ(A) static_cast<int>(A.size())#define PB push_back#define MP(A,B) make_pair(A,B)#define P(T) pair<T,T>#define fi first#define se second#define D(v) (cout<< #v <<" is equal to "<<v<<endl)#define DP(v) (cout<< #v << " is equal to "<<v.fi<<" "<<v.se<<endl)#define ZERO(v) memset(v,0,sizeof(v))#define MIO(v) memset(v,-1,sizeof(v))#define IOS std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr);const int inf=0x3f3f3f3f;//const int MAXN;const double eps=1e-6;const double pi=acos(-1.0);typedef long long ll;typedef unsigned long long ull;typedef long double ld;inline int sgn(double x){ return (x>eps)-(x<-eps);}int d;struct Interval{ double l,r; Interval(double _l=0,double _r=0):l(_l),r(_r) {} Interval(P(double) p):l(p.fi-sqrt(d*d-p.se*p.se)),r(p.fi+sqrt(d*d-p.se*p.se)) {} bool operator < (const Interval& rhs) const { if (r==rhs.r) return l<rhs.l; else return r<rhs.r; }};int main(){ int n; int kase=0; while(scanf("%d%d",&n,&d)==2) { if(!n&&!d) break; int ans=1; bool isVaild=true; if(d<0)//d能小于0 isVaild=false; vector<Interval> vec; REP(i,n) { double x,y; scanf("%lf%lf",&x,&y); if(double(d)<fabs(y)) { isVaild=false;ans=-1; } else vec.PB(MP(x,y)); //cout<<vec[i].l<<" "<<vec[i].r<<endl; } if(ans!=-1) { sort(ALL(vec)); int tmp=0; int len=vec.size(); for(int i=1;i!=len;++i) { if(vec[i].l>vec[tmp].r) { ans++; tmp=i; } else if(vec[i].r<vec[tmp].r) tmp=i; } } printf("Case %d: %d\n",++kase,ans); } return 0;}
ps:话说哪里改博客字体欸。。。看起来好丑
阅读全文
0 0
- 【个人训练】(POJ1328)Radar Installation
- poj1328 Radar Installation(贪心)
- POJ1328--Radar Installation(贪心)
- poj1328 Radar Installation(贪心)
- poj1328 Radar Installation(贪心)
- POJ1328-Radar Installation(贪心)
- POJ1328 Radar Installation(贪心)
- #POJ1328#Radar Installation(贪心)
- POJ1328 Radar Installation(贪心)
- Radar Installation(POJ1328, 贪心)
- Radar Installation(poj1328)(贪心)
- (贪心+排序)poj1328 Radar Installation
- poj1328 Radar Installation 贪心 (手动翻译)
- poj1328——Radar Installation(贪心)
- POJ1328(贪心) 之 Radar Installation
- POJ1328---Radar Installation(贪心算法)
- POJ1328 Radar Installation
- POJ1328 Radar Installation 贪心
- Spring分模块开发
- Improving Deep Neural Networks Regularization 参考答案
- Spark action算子案例
- LeetCode: Palindrome Number
- C#基础学习第三篇:继承
- 【个人训练】(POJ1328)Radar Installation
- c提高1
- OpenJudge百炼-2809-计算2的N次方-C语言-高精度计算
- 可扩展对话框
- spring + shiro + cas 实现sso单点登录
- PAT乙级1069. 微博转发抽奖(20)
- 循环&条件
- (一)SDN基本架构
- 适用TP3.2.3分页样式