Description

Mr. Bender has a digital table of size n × n, each cell can be switched on or off. He wants the field to have at least c switched on squares. When this condition is fulfilled, Mr Bender will be happy.

We'll consider the table rows numbered from top to bottom from 1 to n, and the columns — numbered from left to right from 1 to n. Initially there is exactly one switched on cell with coordinates (x, y) (x is the row number, y is the column number), and all other cells are switched off. Then each second we switch on the cells that are off but have the side-adjacent cells that are on.

For a cell with coordinates (x, y) the side-adjacent cells are cells with coordinates (x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1).

In how many seconds will Mr. Bender get happy?

Input

The first line contains four space-separated integers n, x, y, c(1 ≤ n, c ≤ 109; 1 ≤ x, y ≤ nc ≤ n2).

Output

In a single line print a single integer — the answer to the problem.

Sample Input

Input
6 4 3 1

Output
0

Input
9 3 8 10

Output
2

Hint

Initially the first test has one painted cell, so the answer is 0. In the second test all events will go as is shown on the figure. .

//这题就是先算总面积,再减去超出面积,再加上重叠面积。关键要注意细节。
#include<iostream>
using namespace std;
int main()
{
     long long x,n,y,c;
     cin>>n>>y>>x>>c;
     long long i,xr,xl,yu,yd,d;
     x--;y--;
     long now=0;
     for(i=1;now<c;i++)
     {
         now=i*i+(i-1)*(i-1);
         if(now<c)
         continue;
         i--;
         xr=x+i;
         xl=x-i;
         yu=y-i;
         yd=y+i;
         if(xl<0) now-=xl*xl;
         if(xr>n-1) now-=(xr-n+1)*(xr-n+1);
          if(yu<0)
          {
              now-=yu*yu;
              yu++;
              d-=yu;
              if(x+d>n-1)
                now+=(x+d-n+2)*(x+d-n+1)/2;
              if(x+yu<0)
                now+=(x-d)*(x-d-1)/2;
          }
          if(yd>n-1)
          {
               now-=(yd-n+1)*(yd-n+1);
               d=yd-n+1;
               d--;
               if(x+d>n-1) now+=(x+d-n+2)*(x+d-n+1)/2;
               if(x-d<0) now+=(x-d)*(x-d-1)/2;
          }
          i++;
     }
     cout<<i-2<<endl;
}

Mr. Bender and Square相关推荐

  1. 寻根究底,探讨 chi -square特征词选择方法后面的数学支持

    寻根究底,探讨 chi -square特征词选择方法后面的数学支持 最近研究特征词选择算法,主要在研究chi方统计量的方法. Christopher D Manning的书<信息检索导论> ...

  2. 367. Valid Perfect Square

    题目: Given a positive integer num, write a function which returns True if num is a perfect square els ...

  3. 使用Pixel Bender Toolkit制作特效——多像素采样(Part 4)

    要求 用户级别 初级 必需产品 Flash Player 10 Flash Professional CS4 (下载试用版) 范例文件 pixel_bender_04.zip (5 KB) 其他要求 ...

  4. Mr. Process的一生-Linux内核的社会视角 (2)启动

    原文地址: http://www.manio.org/cn/startup-of-linux-view-of-society.html 其实这才应该是这一系列文章的第一节,因为这篇文章讲的是盘古开天地 ...

  5. 数据挖掘公司D square nv 完成500万欧元B轮融资

    D square nv日前完成 500 万欧元 B 轮融资,融资方为 Gemma Frisius Fonds, Jürgen Ingels, Limburgse Reconversie Maatsch ...

  6. Mr Big [To be with you]

    很惊喜,在musictea上看到了Mr Big的专辑Lean Into It,里面则是包含了他最出名的To be with you 赶忙去听,果然是原汁原味的To be with you,相比而言,w ...

  7. MapReduce对交易日志进行排序的Demo(MR的二次排序)

    1.日志源文件 (各个列分别是: 账户,营业额,花费,日期) zhangsan@163.com 6000 0 2014-02-20 lisi@163.com 2000 0 2014-02-20 lis ...

  8. 一 梳理 从 HDFS 到 MR。

    MapReduce 不仅仅是一个工具,更是一个框架.我们必须拿问题解决方案去适配框架的 map 和 reduce 过程   很多情况下,需要关注 MapReduce 作业所需要的系统资源,尤其是集群内 ...

  9. 经典算法题之Maximal Square

    点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 作者:叶    虎 Maximal Square是道非常有意思的算 ...

最新文章

  1. 情感分析研究:还未结束!
  2. 几处早莺争暖树下一句是什么,几处早莺争暖树后一句
  3. css div漂浮向下,DIV+CSS固定底部的漂浮广告
  4. SQL HAVING 子句
  5. 一些javascript免费中文书籍
  6. 正在进行时 Order 1
  7. 单例模式以及在C#中的使用
  8. JAVA手写ArrayList以及LinkedList
  9. Yii2语言国际化配置Twig翻译解决方案
  10. LINE: 在windows上运行原生linux程序 (2) : demo完成
  11. Motion 5 for Mac(专业视频编辑软件)v5.3.2永久破解版
  12. java.lang.ClassNotFoundException: org.jaxen.JaxenException 解决方法
  13. python全栈开发工程师_老男孩Python高级全栈开发工程师三期完整无加密带课件(共104天)...
  14. 网页视频抓取插件-自动网页视频抓取插件
  15. 计算机桌面不同步,怎样使电脑桌面文件在不同桌面位置上显示
  16. 锐捷路由器如何配置虚拟服务器,锐捷路由器配置命令完美宝典
  17. Linux节点释放,关于linux:如何释放Inode的使用量?
  18. HTTPS双向认证+USB硬件加密锁(加密狗)配置
  19. java和以太坊交互_Android怎么和以太坊智能合约交互
  20. python的truncate()函数

热门文章

  1. 以实际产品为例, 进行软件工程训练的作业
  2. react实现异步插件_React-loadable实现组件进行异步加载
  3. python创建数据库计算机积极拒绝、无法连接_Python3 请求网页源码 目标计算机积极拒绝,无法连接...
  4. JAVA入门级教学之(内存中的空指针异常)
  5. python递归_python3之递归
  6. python ide在哪个文件夹_Python IDE使用汇总
  7. win10去掉快捷方式小箭头_简单几步,教你怎么去掉win10电脑图标小箭头
  8. Oracle数据库迁移测试数据,Oracle数据库的数据迁移方法
  9. Java后端开发需要具备哪些知识结构
  10. Java虚拟机组成详解