POJ3258-River Hopscotch-二分+贪心【最小值最大化】
2016-02-27 11:15
429 查看
给你L,n,m
L是一条路总长度
n是路上n个石头
m是要移走m个石头(第一个和最后一个石头不能移走)
设X为剩下的n-m个石头里,石头之间相邻最近的距离
求这个X的最大值
二分X,对于每个X,我们贪心,从a[0]开始,把a[i]+x范围内的j个石头都移掉,接下来从i+j+1开始重复移石头。
如果最后移动的石头超过了m个,那么说明 这个X太大了答案取【left,mid-1】,如果小于等于m个,那么说明答案在[mid,right]之间
L是一条路总长度
n是路上n个石头
m是要移走m个石头(第一个和最后一个石头不能移走)
设X为剩下的n-m个石头里,石头之间相邻最近的距离
求这个X的最大值
二分X,对于每个X,我们贪心,从a[0]开始,把a[i]+x范围内的j个石头都移掉,接下来从i+j+1开始重复移石头。
如果最后移动的石头超过了m个,那么说明 这个X太大了答案取【left,mid-1】,如果小于等于m个,那么说明答案在[mid,right]之间
#include <cstdio> #include <cmath> #include <cstring> #include <string> #include <algorithm> #include <queue> #include <map> #include <set> #include <vector> #include <iostream> using namespace std; const double pi=acos(-1.0); double eps=0.000001; int n,m; int tm[100005]; int bin(int x) { int i; int num=0; for (i=0;i<=n+1;i++) { int j=i+1; while(tm[j]-tm[i]<x&&j<=n) j++; num+=j-i-1; i=j-1; } return num; } int main() { int i; int ll; cin>>ll; cin>>n>>m; int minn=2147483647; for (i=1;i<=n;i++) { scanf("%d",&tm[i]); } sort(tm+1,tm+1+n); tm[0]=0; tm[n+1]=ll; int l=0; int r=ll; int ans=-1; while(l<=r) { if (r-l<=1) { if (bin(r)== m) ans =r; else ans=l; break; } int mid=(l+r)>>1; int ret=bin(mid); if (ret<=m) l=mid; else if (ret>m) r=mid-1; } printf("%d\n",ans ); return 0; }
相关文章推荐
- 利用Tomcat建立多个Web Server的方法
- Libev库学习3---watcher与loop的操作
- 架构上的些许积累
- hadoop2.5.2 mahout0.10.1 测试文本分类器
- 架构上的些许积累
- 三层架构实战篇 下
- spark-shell和scala错误
- spark-shell和scala错误
- 深入理解 Looper、Handler、Message三者关系
- 如何快速配置CentOS的时间同步
- linux服务器 LAMP平台的部署
- 关于fork()函数父子进程之间的问题
- 20135316王剑桥Linux内核学习笔记
- Linux看门狗脚本 1.4
- linux新增用户并设置密码以及删除用户
- linux新增用户并设置密码以及删除用户
- apache AH00558 错误的解决
- Linux如何查看JDK的安装路径
- linux内核file结构
- 如何不加完全路径直接输入linux命令