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 ,暴力贪心!的更多相关文章

  1. hdu 3697 Selecting courses (暴力+贪心)

    Selecting courses Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 62768/32768 K (Java/Others ...

  2. HDU 3723 Delta Wave (高精度+calelan数)

    题意:给定一个图,问你只能向上向下,或者平着走,有多少种方法可以走到最后一个格. 析:首先先考虑,如果没有平的情况就是calelan数了,现在有平的情况,那么就枚举呗,因为数很大,所以要用高精度. 答 ...

  3. hdu 4825 Xor Sum(trie+贪心)

    hdu 4825 Xor Sum(trie+贪心) 刚刚补了前天的CF的D题再做这题感觉轻松了许多.简直一个模子啊...跑树上异或x最大值.贪心地让某位的值与x对应位的值不同即可. #include ...

  4. uva 1478 - Delta Wave(递推+大数+卡特兰数+组合数学)

    option=com_onlinejudge&Itemid=8&category=471&page=show_problem&problem=4224" st ...

  5. HDU3723 Delta Wave —— 卡特兰数

    题目链接:https://vjudge.net/problem/HDU-3723 Delta Wave Time Limit: 6000/3000 MS (Java/Others)    Memory ...

  6. Delta-wave HDU - 1030

    Delta-wave HDU - 1030 A triangle field is numbered with successive integers in the way shown on the ...

  7. 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 ...

  8. HDU - 6570 - Wave(暴力)

    Avin is studying series. A series is called "wave" if the following conditions are satisfi ...

  9. HDU 3697 Selecting courses(贪心+暴力)(2010 Asia Fuzhou Regional Contest)

    Description     A new Semester is coming and students are troubling for selecting courses. Students ...

  10. 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 ...

随机推荐

  1. 使用JDBC进行简单的增删改查

    JDBC为java的基础.用jdbc实现对数据库的增删改查的功能是程序员的基本要求.本例以mysql为例,首先要使用本例需要添加mysql-connector-java-5.1.7-bin.jar包. ...

  2. JS权威指南-概述学习

    <script src="/javascripts/application.js" type="text/javascript" charset=&quo ...

  3. MFC程序添加快捷键

    [问题提出]     有的程序需要自定义组合键完成一定功能,如何实现? [解决方法]     RegisterHotKey函数原型及说明:     BOOL RegisterHotKey(     H ...

  4. jsp中<c:forEach varStatus="status">的属性值问题

    c:forEach varStatus属性 current当前这次迭代的(集合中的)项 index当前这次迭代从 0 开始的迭代索引 count当前这次迭代从 1 开始的迭代计数 first用来表明当 ...

  5. shell脚本自动部署及监控

    一.shell脚本部署nginx反向代理和三个web服务 1 对反向代理服务器进行配置 #!/bin/bash #修改用户交互页面 用户输入参数执行相应的参数 #安装epel扩展包和nginx fun ...

  6. gitlab autuo devops

    [参考文章] Chengzi_comm的专栏 use gitlab ci docker run gitlab-runner gitlab-runner register 1. 在虚拟机或服务器运行gi ...

  7. Linux下MongoDB的安装、配置、启动

    下载 MongoDB下载地址为 https://www.mongodb.org/downloads 安装 将文件放到/usr/local/目录下并解压 tar -zxvf mongodb-linux- ...

  8. Authentication token manipulation error报错解决办法

    Authentication token manipulation error报错解决办法 #参考http://blog.163.com/junwu_lb/blog/static/1916798920 ...

  9. 长链剖分优化dp三例题

    首先,重链剖分我们有所认识,在dsu on tree和数据结构维护链时我们都用过他的性质. 在这里,我们要介绍一种新的剖分方式,我们求出这个点到子树中的最长链长,这个链长最终从哪个儿子更新而来,那个儿 ...

  10. 初涉期望dp/概率dp【在更】

    大致可以分为两种:爆精度:小数取模.