【题目链接】:http://codeforces.com/problemset/problem/255/D

【题意】



给你一个n*n的方框;

给你一个方块;(以下说的方块都是单位方块)

每一秒钟,可以沿着当前有方块的地方往4个方向扩展一个方块;

问你最少要多少秒钟,平面上会有c个方块;

【题解】



画几张图可以发现,图形都是类似一个十字架的几何图案;

设a[n]表示第n-1秒时有多少个方块;

则有a[n] = a[n-1]+n*4

(a[1]=1)

递推一下能求出

a[n] = 2∗n 2 −2∗n+1 

但是可能会有一部分超出了格子的边界;

需要减去;超过的部分;

先求出这个图案的最左和最上、下、右的坐标;

看它在这4个方向上超过了多少;

减掉那个阶梯一样的部分;

因为4个方向都减掉了;

可能会有重复减掉的部分;

需要再加上;

不难发现;

设上半部分超过的部分为t

则如果y+t-1>n则右边和上边会有重复减掉的

如果y-(t-1)<1则左边和上边会有重复减掉的部分;

是一个阶梯(公差为1的等差数列);

….

下面和左边、右边重复的部分类似.

显然有单调性;

二分一下时间就好;



【Number Of WA】



0



【完整代码】

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define lson l,m,rt<<1
  4. #define rson m+1,r,rt<<1|1
  5. #define LL long long
  6. #define rep1(i,a,b) for (int i = a;i <= b;i++)
  7. #define rep2(i,a,b) for (int i = a;i >= b;i--)
  8. #define mp make_pair
  9. #define pb push_back
  10. #define fi first
  11. #define se second
  12. #define ms(x,y) memset(x,y,sizeof x)
  13. #define Open() freopen("D:\\rush.txt","r",stdin)
  14. #define Close() ios::sync_with_stdio(0),cin.tie(0)
  15. typedef pair<int,int> pii;
  16. typedef pair<LL,LL> pll;
  17. const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
  18. const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
  19. const double pi = acos(-1.0);
  20. const int N = 310;
  21. LL n,x,y,c;
  22. LL sqr(LL x){ return x*x;}
  23. int main(){
  24. //Open();
  25. Close();//scanf,puts,printf not use
  26. //init??????
  27. cin >> n >> x >> y >> c;
  28. LL L = 0,R = 1e5,ans = -1;
  29. while (L <= R){
  30. LL mid = (L+R)>>1;
  31. //mid = 2;
  32. LL temp = 2*sqr(mid+1)-2*(mid+1)+1;
  33. if (temp<c){
  34. L = mid + 1;
  35. continue;
  36. }
  37. LL r = x + mid,l = x - mid,u = y + mid,d = y - mid;
  38. if (r > n)
  39. temp-=1LL*(r-n)*(r-n);
  40. if (l < 1)
  41. temp-=1LL*(1-l)*(1-l);
  42. if (u > n){
  43. temp-=sqr(u-n);
  44. LL t = u-n;
  45. t--;
  46. if (x+t>n){
  47. temp+=(x+t-n+1)*(x+t-n)/2;
  48. }
  49. if (x-t<1){
  50. temp+=(1-x+t)*(1-x+t+1)/2;
  51. }
  52. }
  53. if (d < 1){
  54. temp-=sqr(1-d);
  55. LL t = 1-d;
  56. t--;
  57. if(x+t>n){
  58. temp+=(x+t-n+1)*(x+t-n)/2;
  59. }
  60. if(x-t<1){
  61. temp+=(1-x+t)*(1-x+t+1)/2;
  62. }
  63. }
  64. // cout << temp << endl;
  65. //return 0;
  66. //cout <<mid<<"->"<<temp<<endl;
  67. //cout << endl;
  68. if (temp>=c){
  69. ans = mid;
  70. R = mid-1;
  71. }
  72. else
  73. L = mid+1;
  74. }
  75. cout << ans << endl;
  76. return 0;
  77. }

【codeforces 255D】Mr. Bender and Square的更多相关文章

  1. 【codeforces 711B】Chris and Magic Square

    [题目链接]:http://codeforces.com/contest/711/problem/B [题意] 让你在矩阵中一个空白的地方填上一个正数; 使得这个矩阵两个对角线上的和; 每一行的和,每 ...

  2. 【codeforces 505D】Mr. Kitayuta's Technology

    [题目链接]:http://codeforces.com/problemset/problem/505/D [题意] 让你构造一张有向图; n个点; 以及所要求的m对联通关系(xi,yi) 即要求这张 ...

  3. 【codeforces 505C】Mr.Kitayuta,the Treasure Hunter

    [题目链接]:http://codeforces.com/problemset/problem/505/C [题意] 一开始你跳一步长度为d; 之后你每步能跳d-1,d,d+1这3种步数; 然后在路上 ...

  4. 【Codeforces 506E】Mr.Kitayuta’s Gift&&【BZOJ 4214】黄昏下的礼物 dp转有限状态自动机+矩阵乘法优化

    神题……胡乱讲述一下思维过程……首先,读懂题.然后,转化问题为构造一个长度为|T|+n的字符串,使其内含有T这个子序列.之后,想到一个简单的dp.由于是回文串,我们就增量构造半个回文串,设f(i,j, ...

  5. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  6. 【59.49%】【codeforces 554B】Ohana Cleans Up

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. 【33.33%】【codeforces 586D】Phillip and Trains

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  8. 【20.51%】【codeforces 610D】Vika and Segments

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

随机推荐

  1. 根据 thread id 停止一个线程

    出自 https://github.com/Bogdanp/dramatiq/blob/master/dramatiq/middleware/threading.py#L62 thread_id = ...

  2. Eclipse项目启动不了

    当你的Eclipse项目启动不了多半是[eclipse工程jdk版本]的问题 在eclipse中项目jdk版本不匹配的时候需要修改项目工程的jdk版本,但是网上的一些版本修改不是很完全,经过一些摸索之 ...

  3. Map的四种遍历方法

    1.取值遍历 for(String key:map.keySet()){ System.out.println("key="+key+"and value=" ...

  4. Spring中使用Quartz之MethodInvokingJobDetailFactoryBean配置任务

    Quartz是一个强大的企业级任务调度框架,Spring中继承并简化了Quartz. Spring中使用Quartz的3种方法(MethodInvokingJobDetailFactoryBean,i ...

  5. jvm 垃圾回收概念和算法

    1.概念 GC 中的垃圾,特指存在于内存中.不会再被使用的对象.垃圾回收有很多种算法,如引用计数法.复制算法.分代.分区的思想. 2.算法 1.引用计数法:对象被其他所引用时计数器加 1,而当引用失效 ...

  6. 2015 Multi-University Training Contest 8 hdu 5389 Zero Escape

    Zero Escape Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tot ...

  7. NYIST 860 又见01背包

    又见01背包时间限制:1000 ms | 内存限制:65535 KB难度:3 描述 有n个重量和价值分别为wi 和 vi 的 物品,从这些物品中选择总重量不超过 W 的物品,求所有挑选方案中物品价值总 ...

  8. (转)Epoll模型详解

    1. 内核中提高I/O性能的新方法epoll epoll是什么?按照man手册的说法:是为处理大批量句柄而作了改进的poll.要使用epoll只需要这三个系统调 用:epoll_create(2),  ...

  9. WinServer-AD操作常用powershell命令

    powershell 操作AD常用命令 查询AD中默认的密码策略 Get-ADDefaultDomainPasswordPolicy 查询AD中密码永不过期的用户 Get-ADUser -Filter ...

  10. SQLSERVER-存储过程知识点

    原文链接:http://www.qeefee.com/article/000566 存储过程是一组预编译的SQL语句,它可以包含数据操纵语句.变量.逻辑控制语句等. 存储过程允许带参数: 输入参数:可 ...