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 ...
随机推荐
- 断言assert用法
本文转自:http://blog.jobbole.com/76285/ 这个问题是如何在一些场景下使用断言表达式,通常会有人误用它,所以我决定写一篇文章来说明何时使用断言,什么时候不用. 为那些还不清 ...
- 5、两个栈实现队列------------>剑指offer系列
题目 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 思路 栈1: 用于入队列存储 栈2: 出队列时将栈1的数据依次出栈,并入栈到栈2中 栈2出栈即栈1的底部数据 ...
- 洛谷[LnOI2019]长脖子鹿省选模拟赛t1 -> 快速多项式变换
快速多项式 做法:刚拿到此题有点蒙,一开始真没想出来怎么做,于是试着去自己写几个例子. 自己枚举几种情况之后就基本看出来了,其实本题中 n 就是f(m)在m进制下的位数,每项的系数就是f(m)在m进制 ...
- Number of 1 BitsWrite a function that takes an unsigned integer and returns the number of ’1' bits i
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
- 屏幕旋转时 Activity 的生命周期 —— 测试与结论
关于 Android 手机横竖屏切换时 Activity 的生命周期问题,网上有很多相似的文章,大多数都是说明在竖屏切换横屏时 Activity 会重启一次,而在横屏切换竖屏时 Activity 会重 ...
- UVM之uvm_phase
UVM中的phase机制很有意思,它能让UVM启动之后,自动执行所有的流程.UVM 的user guide 中对uvm_phase的定义如下: This base class defines ever ...
- jdbc接口的一种类比——打酱油
jdbc很简单,这里只是为了方便自己的记忆.模型也许有缺陷,但本质是相同的. jdbc可以屏蔽数据库的底层的不同,让我们有能力用java语言统一访问不同的数据库.就像打酱油一样,可以去超市买,也可以去 ...
- codevs 1606 台阶
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 话说某牛家门外有一台阶,这台阶可能会很高(总层数<=1000000). 这 ...
- 51nod 1174 区间中最大的数(送盾题)
基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出一个有N个数的序列,编号0 - N - 1.进行Q次查询,查询编号i至j的所有数中,最大的数是多少. ...
- The - Modcrab——使用贪心策略
一.题目信息 The - Modcrab 简单翻译一下:Vova有生命值h1,每次攻击值为a1,每瓶药水恢复生命值c1;Modcrab有生命值h2,每次攻击值为a2.在每个关卡开始,Vova有两种选择 ...