【基础练习】【二分】codevs2072 分配房间题解
2015-10-15 16:48
274 查看
题目描述 Description
yh拥有一条街道,街道上共有n间房子,每间房子的坐标为xi(yh的房子比较神奇,可能重叠)。
同时,yh有m个女朋友(这是事实),yh打算给每位女朋友分配一间房子。两个女朋友间的距离相隔越近,她们之间产生冲突的可能就越高。yh想尽可能的减小女朋友间的冲突,于是他打算让他的女朋友间的最小距离最大,你能帮yh找出这个最大值吗?
输入描述 Input Description
第一行两个整数,n,m,表示yh有n间房子,有m个女朋友
第二行为n个整数,xi,表示各间房子的坐标。
输出描述 Output Description
输出1行,表示yh女朋友间的最小距离的最大值
样例输入 Sample Input
5 3
1 2 8 4 9
样例输出 Sample Output
3
数据范围及提示 Data Size & Hint
对于30%的数据,n<=100,m<=n,0<=xi<=10000;
对于100%的数据,n<=100000 ,m<=n,0 <= xi <= 1000000000
如今这种层次的二分完全是水题一道···
——天时不如地利,地利不如人和
yh拥有一条街道,街道上共有n间房子,每间房子的坐标为xi(yh的房子比较神奇,可能重叠)。
同时,yh有m个女朋友(这是事实),yh打算给每位女朋友分配一间房子。两个女朋友间的距离相隔越近,她们之间产生冲突的可能就越高。yh想尽可能的减小女朋友间的冲突,于是他打算让他的女朋友间的最小距离最大,你能帮yh找出这个最大值吗?
输入描述 Input Description
第一行两个整数,n,m,表示yh有n间房子,有m个女朋友
第二行为n个整数,xi,表示各间房子的坐标。
输出描述 Output Description
输出1行,表示yh女朋友间的最小距离的最大值
样例输入 Sample Input
5 3
1 2 8 4 9
样例输出 Sample Output
3
数据范围及提示 Data Size & Hint
对于30%的数据,n<=100,m<=n,0<=xi<=10000;
对于100%的数据,n<=100000 ,m<=n,0 <= xi <= 1000000000
如今这种层次的二分完全是水题一道···
//codevs2072 ·ÖÅä·¿¼ä ¶þ·Ö //copyright by ametake #include #include #include using namespace std; const int maxn=100000+10; int a[maxn]; int n,m; bool can(int x) { int cnt=0,last=1; int i=1; while (i<=n) { while (a[i]-a[last]=m; } int main() { freopen("1.txt","r",stdin); freopen("2.txt","w",stdout); scanf("%d%d",&n,&m); int l=1,r=0; for (int i=1;i<=n;i++) { scanf("%d",&a[i]); } sort(a+1,a+n+1); r=a ; while (l>1; if (can(mid)) { l=mid; } else r=mid-1; } printf("%d\n",l); return 0; }
——天时不如地利,地利不如人和
相关文章推荐
- 计算机科学中32个常用的基础算法
- 快速排序里的学问:从猜数字开始
- HDU 4898 The Revenge of the Princess’ Knight ( 2014 Multi-University Training Contest 4 )
- Search Insert Position,Search for a Range,Pow(x, n),Sqrt(x)
- Find Minimum in Rotated Sorted Array II
- [LeetCode] Sqrt(x)
- [LeetCode] Pow(x, n)
- [LeetCode] Search Insert Position
- [LeetCode] Search for a Range
- [LeetCode] Search in Rotated Sorted Array
- PAT 1057 Stack (30)
- int sqrt(int x)
- Pow(x, n)
- Find Minimum in Rotated Sorted Array
- Divide Two Integers
- acm解题报告 HDU 2141 Can you find it?
- acm解题报告 HDU 2199 Can you solve this equation?
- acm解题报告 HDU 2899 Strange fuction
- acm解题报告 HDU 1969 Pie
- acm解题报告 HDU 1061 Rightmost Digit