两道贪心题
来源:互联网 发布:淘宝拍a发b教程 编辑:程序博客网 时间:2024/06/14 00:07
一、
P1809 渡河问题
二、
P1325 雷达覆盖
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#define LL long longusing namespace std;const int N=100009;int n,a[N];LL ans;int main(){ scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]); sort(a+1,a+n+1); int m=n; while(m) { if(m==1) ans+=a[1]; if(m==2) ans+=a[2]; if(m==3) ans+=a[1]+a[2]+a[3]; if(m<=3) break; LL s=a[2]+a[1]+a[m]+a[2];//1,2过去,1回来,m,m-1过去,2回来 LL ss=a[m]+a[1]+a[m-1]+a[1];//1,m过去,1回来,1,m-1过去,1回来 ans+=min(ss,s); m-=2; } printf("%lld",ans); return 0;}
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath> #define LL long longusing namespace std;int n,d,ans;struct H{ int l,r;}q[100009]; bool vis[100009];bool cmp(H a,H b){ return a.r<b.r;}int main(){ scanf("%d%d",&n,&d); for(int i=1;i<=n;i++) { int x,y; scanf("%d%d",&x,&y); if(d<abs(y)) {cout<<-1;return 0;} int len=sqrt(d*d-y*y); q[i].l=x-len,q[i].r=x+len; } sort(q+1,q+n+1,cmp); for(int i=1;i<=n;i++) { if(!vis[i]) { ans++; for(int j=i+1;j<=n;j++) if(q[j].l<=q[i].r&&q[j].r>=q[i].l) vis[j]=1; } } printf("%d\n",ans); return 0;}
阅读全文
1 0
- 两道贪心题
- 两道中等贪心题,poj 3040, poj 3190
- 从两道简单HDU题来了解贪心思想
- 贪心好题(两种变量的贪心)poj3262 Protecting the Flowers
- Codeforces Round #193 (Div. 2) C. Students' Revenge (两种优先级的贪心 好题)
- 贪心两题(活动安排问题+删除最少字符得到目标字符)
- 过河问题,谈论两种贪心策略
- 两道算法题
- 两道算法题
- 两道算法题
- 两道汇编程序题
- 两道编程题
- 两道笔试题
- 两道笔试题
- 两道笔试题
- 两道操作系统题
- 两道程序题
- 两道交互题
- XML编程(CRUD)
- C++ template
- SpringMVC知识汇总
- Linux内核笔记(1)
- linux下的C语言开发(gdb调试)
- 两道贪心题
- Retroft2源码解析01-创建过程
- ssh免登陆验证
- es6笔记·····find()和findIndex()
- bzoj 5027: 数学题
- 树的重心
- linux学习第十八篇:vim编辑模式和命令模式
- 面试必备:ArrayMap源码解析
- 4.1(3)