【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 ...
随机推荐
- 命令行导入导出Mysql数据库
MySQL命令行导出数据库:1,进入MySQL目录下的bin文件夹:cd MySQL中到bin文件夹的目录,如我输入的命令行:cd C:\Program Files\MySQL\MySQL Serve ...
- Svn 提交新文件
1.右击文件: 2.按图做:
- Thunder9(迅雷9)去掉右侧浏览器广告的方法
1.打开文件夹C:\Program Files (x86)\Thunder Network\Thunder9\Program\TBC 2.找到 ThunderBrowser.exe 3.重命名为任意名 ...
- angular-数据库
<div ng-app="myApp" ng-controller="customersCtrl"> <table> <tr ng ...
- maven打包可运行的jar包(包含依赖工程)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- scp报错:Host key verification failed. REMOTE HOST IDENTIFICATION HAS CHANGED!
1 scp报错:REMOTE HOST IDENTIFICATION HAS CHANGED! [root@xx ~]# scp yum-3.4.3.tar.gz 10.xx.xx.12:/root ...
- Linux文件系统(七)---系统调用之open操作(一)
(内核2.4.37) 一. 当我们打开一个文件的时候.须要获得文件的文件描写叙述符(前面已经说过事实上就是文件数组下标).通常是通过函数open来完毕.这个系统调用在<unistd.h>头 ...
- Extjs4.2 ajax请求url中传中文參数乱码问题
今天有个需求须要在url中传入中文參数.结果在后台取得时出现乱码,怀疑可能是编码问题.上网查询了资料,试了几种办法.发现有一种可行,记录在此,以便查阅. url中用encodeURI 进行2次编码: ...
- hpc-ai比赛相关资料
http://follitude.com/blog/Use-RDMA-Emulation-in-Software-Using-SoftiWARP/ https://www.reflectionsoft ...
- 51nod 1158 全是1的最大子矩阵(单调栈 ,o(n*m))
前置问题:51nod 1102 面积最大的矩形 附上链接: 51nod 1102 面积最大的矩形 这题的题解博客 需要了解的知识:单调栈,在前置问题中已经讲解. 解题思路 对每行求左边连续1的个数,得 ...