【题目链接】: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



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("D:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0),cin.tie(0) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 310; LL n,x,y,c; LL sqr(LL x){ return x*x;} int main(){
//Open();
Close();//scanf,puts,printf not use
//init??????
cin >> n >> x >> y >> c;
LL L = 0,R = 1e5,ans = -1;
while (L <= R){
LL mid = (L+R)>>1;
//mid = 2;
LL temp = 2*sqr(mid+1)-2*(mid+1)+1;
if (temp<c){
L = mid + 1;
continue;
}
LL r = x + mid,l = x - mid,u = y + mid,d = y - mid;
if (r > n)
temp-=1LL*(r-n)*(r-n);
if (l < 1)
temp-=1LL*(1-l)*(1-l);
if (u > n){
temp-=sqr(u-n);
LL t = u-n;
t--;
if (x+t>n){
temp+=(x+t-n+1)*(x+t-n)/2;
}
if (x-t<1){
temp+=(1-x+t)*(1-x+t+1)/2;
}
}
if (d < 1){
temp-=sqr(1-d);
LL t = 1-d;
t--;
if(x+t>n){
temp+=(x+t-n+1)*(x+t-n)/2;
}
if(x-t<1){
temp+=(1-x+t)*(1-x+t+1)/2;
}
}
// cout << temp << endl;
//return 0;
//cout <<mid<<"->"<<temp<<endl;
//cout << endl;
if (temp>=c){
ans = mid;
R = mid-1;
}
else
L = mid+1;
}
cout << ans << endl;
return 0;
}

【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. Shell(一)变量

    一.简介 Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁.Shell 既是一种命令语言,又是一种程序设计语言. Shell 是指一种应用程序,这个应用程序提供了一个界面,用 ...

  2. 优化VR体验的7个建议

    本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接: http://blog.csdn.net/cartzhang/article/details/50392607 作者:ca ...

  3. 2015 Multi-University Training Contest 7 hdu 5379 Mahjong tree

    Mahjong tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  4. HTTP请求和响应模式(B/S)(2)

    B/S          及浏览器/客服端模式 根据发送的状态码不同,显示response的状态不同

  5. maven打包可运行的jar包(包含依赖工程)

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  6. oracle学习之第一个存储过程:打印Hello World

    数据库对象:表.视图.索引.序列.同义词.存储过程.存储函数 存储过程:指的是存储在数据库中供全部用户程序调用的子程序叫存储过程.存储函数 存储过程和存储函数的同样点:完毕特定功能的程序 存储过程和存 ...

  7. HDU 3723

    把向上看成+1,向下看成-1.可以知道符合卡特兰数的一般解释了.记作Can(i) 中间平过的即是0.亦即是C(n,2*i),i表示向上的数. 于是总的就是sum(C(n,2*i)*Can(i)),i从 ...

  8. 设计模式入门之代理模式Proxy

    //代理模式定义:为其它对象提供一种代理以控制对这个对象的訪问 //实例:鉴于书中给出的样例不太好.并且有些疑问,所以直接用保护代理作为实例 //要求,一旦订单被创建,仅仅有订单的创建人才干够改动订单 ...

  9. How to get the MouseEvent coordinates for an element that has CSS3 Transform?

    I want to detect where a MouseEvent has occurred, in coordinates relative to the clicked element. Wh ...

  10. 【翻译自mos文章】怎么正确的计算一个ip地址的subnet id?

    怎么正确的计算一个ip地址的subnet id? 来源于: How to calculate the correct subnet for an interface (文档 ID 1059759.1) ...