【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 ...
随机推荐
- HDU-2955 Robberies 浮点数01背包 自变量和因变量位置互换
题目链接:https://cn.vjudge.net/problem/HDU-2955 题意 突然想找几个银行抢钱. 给出各银行的钱数和被抓的概率,以及能容忍的最大被抓概率. 问他最多能抢到多少钱? ...
- NOIP2016 DAY1 T3 换教室
换教室 Description 对于刚上大学的牛牛来说,他面临的第一个问题是如何根据实际情况申请合适的课程.在可以选择的课程中,有2n节 课程安排在n个时间段上.在第i(1≤i≤n)个时间段上,两节内 ...
- python_形参、实参
#参数:形参.实参'''def display_message(title): print("My favourite book is %s" %title) #return 0 ...
- Linux5355端口被0.0.0.0监听
Linux后台有个systemd-resolv进程,占用5355等端口 博主在一次网络安全加固行动中,netstat -anp发现Linux后台有一个被0.0.0.0监听的端口,5355,显示被sys ...
- HDU 4320 Contest 3
只需A的全部质因数包含在B中即可. #include <iostream> #include <cstdio> #define LL __int64 #include < ...
- WPF中多线程统计拆箱装箱和泛型的运行效率
WPF中多线程统计拆箱装箱和泛型的执行效率.使用的知识点有泛型.多线程.托付.从样例中能够看到使用泛型的效率至少提升2倍 MainWindow.xaml <Window x:Class=&quo ...
- 上机题目(0基础)-计算两个正整数的最大公约数和最小公倍数(Java)
题目例如以下:
- volley源代码解析(六)--HurlStack与HttpClientStack之争
Volley中网络载入有两种方式,各自是HurlStack与HttpClientStack.我们来看Volley.java中的一段代码 if (stack == null) {//假设没有限定stac ...
- HDU 5353 Average
Problem Description There are n soda sitting around a round table. soda are numbered from 1 to n and ...
- Effective Java(一)—— 创建和销毁对象
在客户端(调用端)获取自身实例的方法: 公有的构造器: 类的静态工厂方法: 1. 使用静态工厂方法代替构造器 Boolean 是对基本类型 boolean 的包装类: public final cla ...