【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 ...
随机推荐
- FZU 1692 Key problem( 循环矩阵优化 + 矩阵快速幂)
链接:传送门 题意: n个小朋友围成一个环( 2 <= n <= 100 )然后进行m次的游戏. 一开始,第 i 个小朋友有 Ai 个苹果. 定义游戏的规则为:每一次游戏处于 i 位置的小 ...
- Flask-RESTful(转载)
Flask-RESTful 是一个 Flask 扩展,它添加了快速构建 REST APIs 的支持.它当然也是一个能够跟你现有的ORM/库协同工作的轻量级的扩展.Flask-RESTful 鼓励以最小 ...
- leetcode小题解析
描述Given an array of integers, return indices of the two numbers such that they add up to a specific ...
- S3C6410 LCD Overlay Test Program
测试了一下S3C6410 LCD控制器提供的Overlay功能,写了一个简单的test,主要用于实现FB0和FB1之间的Overlay操作.内核选项的Windows至少要为2. 具体支持如下操作:1 ...
- MATLAB解析PFM格式图像
http://www.p-chao.com/ja/2016-09-27/matlab%E8%A7%A3%E6%9E%90pfm%E6%A0%BC%E5%BC%8F%E5%9B%BE%E5%83%8F/ ...
- Ajax兼容性问题
对于IE7及以上直接使用 XMLHttpRequest 就行,但对于过老版本IE建议直接提示用户下载新版浏览器更佳.或者用以下代码兼容IE6: function CreateXHR() { if(XM ...
- dubbo Failed to check the status of the service com.user.service.UserService. No provider available for the service
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'u ...
- 洛谷 P3912 素数个数
P3912 素数个数 题目描述 求1,2,\cdots,N1,2,⋯,N 中素数的个数. 输入输出格式 输入格式: 1 个整数NN. 输出格式: 1 个整数,表示素数的个数. 输入输出样例 输入样例# ...
- HDU 4323 Contest 3
编辑距离,经典的了.动态规划枚举即过. #include <iostream> #include <cstdio> #include <string.h> #inc ...
- <pre>标签
<pre>标签最主要的认识就是预格式化文本,被包围在 pre 元素中的文本一般会保留空格和换行符.而文本也会呈现为等宽字体.经经常使用于在网页中显示计算机源码. 1.格式化文本举例 &l ...