提示: 从这篇文章开始将要使用MarkDown编辑器啦~ 学习ing~ 所以字体大概会有些变化咯

题目描述

In an attempt to escape the Mischievous Mess Makers’ antics, Farmer John has abandoned his farm and is traveling to the other side of Bovinia. During the journey, he and his kkk cows have decided to stay at the luxurious Grand Moo-dapest Hotel. The hotel consists of nnn rooms located in a row, some of which are occupied.

Farmer John wants to book a set of k+1k + 1k + 1 currently unoccupied rooms for him and his cows. He wants his cows to stay as safe as possible, so he wishes to minimize the maximum distance from his room to the room of his cow. The distance between rooms iii and jjj is defined as ∣j−i∣|j - i|∣j − i∣. Help Farmer John protect his cows by calculating this minimum possible distance.

输入格式

The first line of the input contains two integers nnn and k(1≤k<n≤105)k(1 \leq k < n \leq 10^5)k(1 ≤k < n ≤105) — the number of rooms in the hotel and the number of cows travelling with Farmer John.

The second line contains a string of length nnn describing the rooms. The iii-th character of the string will be ‘0’ if the iii-th room is free, and ‘1’ if the iii-th room is occupied. It is guaranteed that at least k+1k + 1k + 1 characters of this string are ‘0’, so there exists at least one possible choice of k+1k + 1k + 1 rooms for Farmer John and his cows to stay in.

输出格式

Print the minimum possible distance between Farmer John’s room and his farthest cow.

样例输入

Sample Input 1

7 2
0100100

Sample Input 2

5 1
01010

样例输出

Sample Output 1

2

Sample Output 2

2

数据范围

1≤k<n≤1051 \leq k < n \leq 10^51 ≤k < n ≤105

题目翻译

题意:有n个房间,每个房间1代表满了,0代表空可住。有k个奶牛和一个人,房间在一维方向上线性排列,一牛或一人均可选择一个空房入住,求min(人离最远的牛的距离)

题目解答

首先这个玩野有了之前题目的基础就不是很难,首先我们要存储有空的房间,比如,我们先把空间开满:(空间肯定够100010)

数组下标 1 2 3 4 5 6 7 8 9 …… 100010
对应值 1 5 6 8 10 14 15 18 21 ……

我们只需要在读取第二行输入的时候在这个数组里面标记就可以了,第一个有空的房间标记在a[1]里面,其余的类似,同时也要统计出来总共有空的房间是多少个。

问题的关键

这个题关键在于确定那个人的位置。举个例子,假设我们就恰找到了九间空房如下:

数组下标 1 2 3 4 5 6 7 8 9
对应值 1 5 6 8 10 14 15 18 21

其实要找到那个min(人离最远的牛的距离),我们首先要看最左端,最右端,然后求一下平均值,这里计算结果是(1+21)/2=11(1+21)/2=11(1+21)/2=11
       下面我们要寻找11,如果11存在,那么11就是人应该居住的位置了,如果不存在,那么人居住的位置也要尽可能的靠近11。
       11显然没有,那么我们只能找11周围的,发现最接近的是10与14,我们选择最近的10号房间,然后再看10号房间距离最远与最近的房间编号。

如果不好理解,我们可以假设有一条线,下面这一条线


我们假设线段上有一个点,点代表人居住的位置,点从左端滑到右端。求min(人离最远的牛的距离),可以考虑这三种情况

  1. 当点在左半边时候。
  2. 当点在右半边时候。
  3. 当点在中间。
// Solution:
// 先用a[cnt]数组存储空房间的位置,然后要明确一点欲使值更小,则这k+1个对象必然是相连入住的,
// 问题就转化为长为cnt的数组中一个长为k+1的滑动窗口,每个窗口需要输出一个该区间内的最小值,问题就又能简化为长为k+1的区间内min(人离最远的牛的距离),
// 可以用二分法去寻找,nlogn不会超时#include <iostream>
#include <cmath>
using namespace std;int array[100010] = {0};
char ch;// FindMin用于查找从array[startnum]~array[endnum]人离最远的牛的距离
// 方法 人肯定尽量居中住,牛往两边靠,mid最终找出人的位置(array[mid]要最接近(array[startnum]+array[endnum])/2.0)
// 最后比较array[mid]与array[startnum] 、与array[endnum]差的绝对值,返回答案int FindMin(int startnum, int endnum)
{int mid;int l = startnum;int r = endnum;double arrayaverage = (array[startnum] + array[endnum]) / 2.0;while (l <= r){mid = (l + r) / 2;if (array[mid] >= arrayaverage && array[mid - 1] < arrayaverage){break;}if (arrayaverage > array[mid])l = mid + 1;if (arrayaverage <= array[mid - 1])r = mid - 1;}//找到mid这个数字,让它对应的数列大于或等于均值,且前面一项小于均值if (array[mid] - arrayaverage <= arrayaverage - array[mid - 1])//找到最接近average的那一项,判断实现return max(array[mid] - array[startnum], array[endnum] - array[mid]);//找到这个滑动窗口的:人距离牛的距离else if (array[mid] - arrayaverage > arrayaverage - array[mid - 1])//找到最接近average的那一项return max(array[mid - 1] - array[startnum], array[endnum] - array[mid - 1]);
}int main()
{int n, k;cin >> n >> k;int ct = 0;for (int i = 0; i < n + 1; i++){ch = cin.get();if (ch=='0'){array[ct + 1] = i;ct++;}  }int ans = array[ct] - array[1];// for (int i = 1; i <= ct; i++)// {//     cout << array[i] << endl;// }for (int z = 1; z <= ct - k; z++)//滑动窗口{// cout << "test :" << z << "hanshuzhi " << FindMin(z, z + k) << endl;if (ans > FindMin(z, z + k))ans = FindMin(z, z + k);}cout << ans;// system("pause");return 0;
}

(新版)SJTU-OJ-1011. John and Cows相关推荐

  1. leetcode oj java Bulls and Cows

    一.问题描述: You are playing the following Bulls and Cows game with your friend: You write down a number ...

  2. 【算法学习笔记】65. 双向扫描 SJTU OJ 1382 畅畅的牙签盒

    http://acm.sjtu.edu.cn/OnlineJudge/problem/1382 注意到 排序之后 i从前向后扫描时,cur恰好是从后向前的,所以即使是双重循环,也是O(n)的算法. # ...

  3. bjfu oj 1011 将浮点数转换为分数相除的形式

    http://acm.bjfu.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1011 被编译器坑的我眼泪流下来,,VC6 ...

  4. 【算法学习笔记】43.动态规划 逆向思维 SJTU OJ 1012 增长率问题

    1012. 增长率问题 Description 有一个数列,它是由自然数组成的,并且严格单调上升.最小的数不小于S,最大的不超过T.现在知道这个数列有一个性质:后一个数相对于前一个数的增长率总是百分比 ...

  5. 【算法学习笔记】57. 前缀树 字典序优化技巧 STL学习 SJTU OJ 1366 前缀匹配

    Description 给出一个总字符个数大小不超过1,000,000的字典(这个字典的单词顺序不为字典序)和不超过1000个长度不超过1000的前缀,输出字典中匹配该前缀,字典序为K_i的单词在字典 ...

  6. 【算法学习笔记】35.高精度 竖式乘法 SJTU OJ 1274

    Description 输入a,b 输出a*b的竖式乘法,格式见样例. Sample Input1 11 9 Sample Output1 119 -- 99 Sample Input2 10 10 ...

  7. 九度OJ 1011:最大连续子序列 (DP)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5615 解决:2668 题目描述: 给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ...

  8. 【算法学习笔记】83.排序辅助 动态规划 SJTU OJ 1282 修路

    此题倒是能用贪心骗点分... 其实对于每一个位置 , 我们知道最后的改善结果一定是原数列中的数 . (因为要尽量减少消耗, 可以考虑减小至和相邻的相同) 有了这个结论之后, 我们就考虑用dp来做这件事 ...

  9. SJTU OJ 3046 足球 题解

    2019独角兽企业重金招聘Python工程师标准>>> 3046. 足球 Description 众所周知,一只足球队由11球员组成,其中有一名守门员和一名队长. 现在,有22人组成 ...

最新文章

  1. 2021年大数据ELK(二十四):安装Kibana
  2. 人工智能:从经典计算机到量子计算机,弱AI进阶到强AI时代?
  3. 揭开雷达的面纱(科普) 发射机
  4. 【java】简单的方式实现文本文件的读写
  5. 使用DelayQueue 和 FutureTask 实现java中的缓存
  6. 数据库异常关闭后无法启动问题处理
  7. 清华大学全面审查文科博士论文!
  8. 让UI设计显得魅力非凡,设计师少不了的渐变背景素材专辑,
  9. java byte 相等比较_Java字节码跟真正汇编的比较
  10. 如何从IP源地址角度,预防DDoS攻击?
  11. 蜗牛星际D款安装黑群晖教程
  12. cfiledialog对话框大小_CFileDialog类 通用对话框
  13. 2.1.1 理论模型
  14. QScrollArea 动态添加控件问题
  15. 【Golang】go中如何在浏览器上实时显示cmd控制台的输出(流式传输)
  16. EnvironmentNotWritableError:The current user does not have write permissions to the target...
  17. 炸了!3年图片都没了!
  18. BioPython ② | 面向对象编程Object Oriented Programming
  19. 8Flask-----------Flask框架------------安装使用、基本介绍
  20. uni-app 二维码生成(链接转二维码)

热门文章

  1. 自己做小程序开个社区团购可行吗?
  2. 浏览器下载blob文件流(兼容IE浏览器和其他主流浏览器)
  3. AutoLISP将图形中文字写入外部文件
  4. 英雄不问出处--十大名企用人理念
  5. DNS作用及工作原理
  6. 七彩虹将星x15xs 2022款 怎么样
  7. 佟年计算机大赛,佟年的人设是什么?
  8. C语言: “老板,来两份外卖,一份卤肉饭(%d),一份鸡汤面(%f)...“
  9. 【C++】变量与数据类型
  10. 检查文件编码是什么格式的