【codeforces 255D】Mr. Bender and Square
【题目链接】: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的更多相关文章
- 【codeforces 711B】Chris and Magic Square
[题目链接]:http://codeforces.com/contest/711/problem/B [题意] 让你在矩阵中一个空白的地方填上一个正数; 使得这个矩阵两个对角线上的和; 每一行的和,每 ...
- 【codeforces 505D】Mr. Kitayuta's Technology
[题目链接]:http://codeforces.com/problemset/problem/505/D [题意] 让你构造一张有向图; n个点; 以及所要求的m对联通关系(xi,yi) 即要求这张 ...
- 【codeforces 505C】Mr.Kitayuta,the Treasure Hunter
[题目链接]:http://codeforces.com/problemset/problem/505/C [题意] 一开始你跳一步长度为d; 之后你每步能跳d-1,d,d+1这3种步数; 然后在路上 ...
- 【Codeforces 506E】Mr.Kitayuta’s Gift&&【BZOJ 4214】黄昏下的礼物 dp转有限状态自动机+矩阵乘法优化
神题……胡乱讲述一下思维过程……首先,读懂题.然后,转化问题为构造一个长度为|T|+n的字符串,使其内含有T这个子序列.之后,想到一个简单的dp.由于是回文串,我们就增量构造半个回文串,设f(i,j, ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【59.49%】【codeforces 554B】Ohana Cleans Up
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【33.33%】【codeforces 586D】Phillip and Trains
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【20.51%】【codeforces 610D】Vika and Segments
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
随机推荐
- python之类与对象的属性
类相关的知识 在python2中的区分: 经典类: class School: pass 新式类: class School(object): pass 在python3中以上两种均为新式类 属性: ...
- Linux版本分支图
- Memcached的实战笔记
官网:http://memcached.org/ 优秀Blogs: http://blog.csdn.net/jingqiang521/article/details/48345021 开启telne ...
- python_函数传递列表
'''#创建一个空列表,包含一些要打印的设计unprinted_designs = ['iphone','robot','books']completed_modles = [] #模拟打印每个设计, ...
- C语言实现将一个整形数转换为两个字节16进制
有时候要用到这个转换,这里记录一下,例如把 int a = 164 转换储存在数组里为 uint8_t b[0]=0x00 , b[1]=0xA4 . 很简单,转换如下: b[0] = a > ...
- Vue生命周期函数的应用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- PHP读取XML数据中CDATA内数值
// 在开发过程中遇到对XML获取时候加载 CDATA 无法读取内部的数值(例如微信平台的返回值) $content = simplexml_load_string('<content>& ...
- python学习之--安装IDE(eclipse+pydev)
First steps download eclipse url:http://www.eclipse.org/downloads/ select Help -> Install New Sof ...
- hdoj 1013Digital Roots
/*Digital Roots Problem Description The digital root of a positive integer is found by summing th ...
- Java虚拟机的类载入机制
Java虚拟机类载入过程是把Class类文件载入到内存.并对Class文件里的数据进行校验.转换解析和初始化,终于形成能够被虚拟机直接使用的java类型的过程. 在载入阶段,java虚拟机须要完毕下面 ...