HDU-1030 Delta-wave ,暴力贪心!
题意:用图中所示的数字填满这个三角形,求n到m的最短路径,也就是最少通过几条边。
思路:简单贪心就可以了,本人是先将每一层的左右端点用结构体存起来,然后分奇偶判断。
这题必须理清思路才更省时间,二分、搜索都试过,没有什么结果,突然发现当前点正对的另一层的点可以用来判断终点方向。
被以前做的一个题误导了一下,其实理清思路10分钟就可以写出来,结果近一小时左调右调,删了再写写了又删,,,结局悲惨。。。
const int N=1e6+10;
struct node
{
ll l,r;
} a[N];
int n,m,mi;
void init()
{
a[1].l=a[1].r=1;
ll l=1,r=1;
for(int i=2;; i++)
{
l=r+1,r=l+2*(i-1);
a[i].l=l,a[i].r=r;
if(l>1e9) break;
}
}
int main()
{
init();
while(~scanf("%d%d",&n,&m))
{
if(n==m)
{
puts("0");
continue;
}
if(n>m) swap(n,m);
ll k1=1,k2=1;
for(int i=1;; i++)
if(n>=a[i].l&&n<=a[i].r)
{
k1=ll(i);
break;
}
for(int i=1;; i++)
if(m>=a[i].l&&m<=a[i].r)
{
k2=ll(i);
break;
}
// printf("%I64d %I64d\n",k1,k2);
int ans=0;
while(1)
{
if(k1==k2)
{
ans+=abs(n-m);
break;
}
ll tmp=n+(k1+k2-1)*(k2-k1);//正下方
if(k1&1)
{
if(n&1)//奇数
n=n+k1*2,k1++;
else//偶数
{
if(tmp<m) n++;
else n--;
}
}
else
{
if(n&1)//奇数
{
if(tmp<m) n++;
else n--;
}
else//偶数
n=n+k1*2,k1++;
}
// printf("%d %d\n",n,k1);
ans++;
}
printf("%d\n",ans);
}
return 0;
}
HDU-1030 Delta-wave ,暴力贪心!的更多相关文章
- hdu 3697 Selecting courses (暴力+贪心)
Selecting courses Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 62768/32768 K (Java/Others ...
- HDU 3723 Delta Wave (高精度+calelan数)
题意:给定一个图,问你只能向上向下,或者平着走,有多少种方法可以走到最后一个格. 析:首先先考虑,如果没有平的情况就是calelan数了,现在有平的情况,那么就枚举呗,因为数很大,所以要用高精度. 答 ...
- hdu 4825 Xor Sum(trie+贪心)
hdu 4825 Xor Sum(trie+贪心) 刚刚补了前天的CF的D题再做这题感觉轻松了许多.简直一个模子啊...跑树上异或x最大值.贪心地让某位的值与x对应位的值不同即可. #include ...
- uva 1478 - Delta Wave(递推+大数+卡特兰数+组合数学)
option=com_onlinejudge&Itemid=8&category=471&page=show_problem&problem=4224" st ...
- HDU3723 Delta Wave —— 卡特兰数
题目链接:https://vjudge.net/problem/HDU-3723 Delta Wave Time Limit: 6000/3000 MS (Java/Others) Memory ...
- Delta-wave HDU - 1030
Delta-wave HDU - 1030 A triangle field is numbered with successive integers in the way shown on the ...
- Codeforces Round #503 (by SIS, Div. 2) C. Elections (暴力+贪心)
[题目描述] Elections are coming. You know the number of voters and the number of parties — n and m respe ...
- HDU - 6570 - Wave(暴力)
Avin is studying series. A series is called "wave" if the following conditions are satisfi ...
- HDU 3697 Selecting courses(贪心+暴力)(2010 Asia Fuzhou Regional Contest)
Description A new Semester is coming and students are troubling for selecting courses. Students ...
- hdu 1030 Delta-wave (C++, 0ms, explanatory comments.) 分类: hdoj 2015-06-15 12:21 45人阅读 评论(0) 收藏
problem description http://acm.hdu.edu.cn/showproblem.php?pid=1030 #include <cstdio> #include ...
随机推荐
- CSS子元素设置margin-top作用于父容器?
CSS子元素设置margin-top作用于父容器? 原因: In this specification, the expression collapsing margins means that ad ...
- 【javascript】操作给定的二叉树,将其变换为源二叉树的镜像。
操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ 11 9 ...
- Android GreenDao 深查询 n:m 的关系
在我的应用程序这样设计的关系:和我想选择至少一个用户作为一个朋友的所有聊天. 基本上,我想要执行以下查询:\ SELECT c.* FROM CHAT c, USER u, UserChats uc ...
- css布局两边固定中间自适应的四种方法
第一种:左右侧采用浮动 中间采用margin-left 和 margin-right 方法. 代码如下: <div style="width:100%; margin:0 auto;& ...
- Yii 2.0排序功能的使用
在Yii2.0项目的实际开发中,经常会遇到使用Yii2.0自带的排序功能.下面是排序功能的具体使用方法. 一.设置排序规则 注意引入Sort类,如:use yii\data\Sort; // 设置排序 ...
- Azure 项目构建 – 构建和部署 .NET 应用程序
本课程主要介绍了如何在 Azure 平台上快速构建和部署基于 .NET 语言的 Web 应用, 实践讲解如何使用 Azure 门户创建 Web 应用, 部署 ASP.NET 代码, 连接 Azure ...
- ActiveMQ消息丢失怎么解决?
在消息发送过程中消息丢失的话该怎么解决(包括网络原因): 解决思路: 可以把消息唯一ID,存到表里面,当消息接受端可以获取到这个ID,就给服务端一个回复IF,消息发送出去,没有回复,THEN一直循环发 ...
- windows/Linux 常用命令
windows 文件操作命令 cd 切换文件目录 dir 显示文件目录内容 md 创建文件夹 rd 删除文件夹 copy 拷贝文件 move 移动文件 del 删除文件 replace 替换文件 mk ...
- 中国剩余定理&Lucas定理&按位与——hdu 5446
链接: hdu 5446 http://acm.hdu.edu.cn/showproblem.php?pid=5446 题意: 给你三个数$n, m, k$ 第二行是$k$个数,$p_1,p_2,p_ ...
- python 基础之运算符
运算符 a=10 ,b=20 运算符 描述 实例 + 加 - 两个对象相加 a + b 输出结果 30 - 减 - 得到负数或是一个数减去另一个数 a - b 输出结果 -10 * 乘 - 两个数相乘 ...