这题有直接套公式的解法

这里提供一个O(n)的解法。

package practice;

import java.io.BufferedInputStream;
import java.util.Scanner; /**
*
*
* @author caiyu
* @date 2014-11-4
*/
public class POJ1006 {
public static void main(String[] args) {
Scanner cin = new Scanner(new BufferedInputStream(System.in));
while (true) {
int p = cin.nextInt();
if (p < )
break;
int e = cin.nextInt();
if (e < )
break;
int i = cin.nextInt();
if (i < )
break;
int d = cin.nextInt();
if (d < )
break;
int x = , y = , z = ;
int ty = , tx = , tz = ;
boolean fx = true, fy = true, fz = true;
int temp = ;
while (true) { if (fy) {
if (ty < )
ty = e + * y++ - d;
else
break;
if ((ty - p + d) % == ) {
if (ty > temp) {
temp = ty;
fz = true;
fx = true;
fy = false;
} else if (ty == temp) {
fy = false;
}
}
} if (fz) {
if (tz < )
tz = i + * z++ - d;
else
break;
if ((tz - p + d) % == ) {
if (tz > temp) {
temp = tz;
fx = true;
fy = true;
fz = false;
} else if (tz == temp) {
fz = false;
}
}
} if (fx) {
if (tx < )
tx = p + * x++ - d;
else
break;
if ((tx - e + d) % == ) {
if (tx > temp) {
temp = tx;
fy = true;
fz = true;
fx = false;
} else if (tx == temp) {
fx = false;
}
}
}
if (!fx && !fy && !fz)
break; } System.out.println("Case 1: the next triple peak occurs in " + temp
+ " days.");
} }
}

算法:POJ1006 三重峰值问题的更多相关文章

  1. [MIT6.006] 1. Algorithmic Thinking, Peak Finding 算法思维,峰值寻找

    [MIT6.006] 系列笔记将记录我观看<MIT6.006 Introduction to Algorithms, Fall 2011>的课程内容和一些自己补充扩展的知识点.该课程主要介 ...

  2. poj1006

    题目大意:生物节律 一些人相信有三种循环在一个人生命中从他或者她出生的那天起,这个三个周期是,身体,情绪,智力, 并且他们有23,28,和33天的时间,在每一个周期里面都有一个周期,在一个周期的高峰期 ...

  3. 排序算法之堆排序(Heapsort)解析

    一.堆排序的优缺点(pros and cons) (还是简单的说说这个,毕竟没有必要浪费时间去理解一个糟糕的的算法) 优点: 堆排序的效率与快排.归并相同,都达到了基于比较的排序算法效率的峰值(时间复 ...

  4. 简单易学的机器学习算法——基于密度的聚类算法DBSCAN

    一.基于密度的聚类算法的概述     最近在Science上的一篇基于密度的聚类算法<Clustering by fast search and find of density peaks> ...

  5. 【BZOJ 3049】【USACO2013 Jan】Island Travels BFS+状压DP

    这是今天下午的互测题,只得了60多分 分析一下错因: $dis[i][j]$只记录了相邻的两个岛屿之间的距离,我一开始以为可以,后来$charge$提醒我有可能会出现来回走的情况,而状压转移就一次,无 ...

  6. 探讨.NET Core数据加密和解密问题

    前言 一直困扰着我关于数据加密这一块,24号晚上用了接近3个小时去完成一项任务,本以为立马能解决,但是为了保证数据的安全性,我们开始去对数据进行加密,然后接下来3个小时专门去研究加密这一块,然而用着用 ...

  7. 4 Values whose Sum is 0 [POJ2785] [折半搜索]

    题意 给你长度为n四个数列,每个数列选一个数使总和为4,有多少种选法(不同选法仅当起码有一个元素的下标不同) 输入 第一行,n 下面n行,每行四个数,代表ai,bi,ci,di 输出 选法数量 样例输 ...

  8. 探讨.NET Core数据进行3DES加密和解密问题

    前言 一直困扰着我关于数据加密这一块,24号晚上用了接近3个小时去完成一项任务,本以为立马能解决,但是为了保证数据的安全性,我们开始去对数据进行加密,然后接下来3个小时专门去研究加密这一块,然而用着用 ...

  9. NYOJ 211 Cow Contest (弗洛伊德+传递闭包 )

    title: Cow Contest 弗洛伊德+传递闭包 nyoj211 tags: [弗洛伊德,传递闭包] 题目链接 描述 N (1 ≤ N ≤ 100) cows, conveniently nu ...

随机推荐

  1. asp.net下AjaxMethod的使用方法

    使用AjaxMethod可以在客户端异步调用服务端方法,简单地说就是在JS里调用后台.cs文件里的方法, 做一些JS无法做到的操作,如查询数据库 使用AjaxMethod要满足一下几点: 1.如果还没 ...

  2. iOS:xCode7版本运行xCode8.0的代码

    怎么在xCode7版本上运行xCode8.0的代码? 1.右键你的"LaunchScreen.sb"文件并用编辑器打开sb 2.删掉"<capability nam ...

  3. js中typeof与instanceof用法区别

    今天写JS代码,遇到动态生成多个名称相同的input复选按钮 需要判断其是否是数组,用到了if (typeof(document.MapCheckMgr.checkid)!="undefin ...

  4. GitHub安装配置

    GitHub安装配置1.安装Git-1.9.5-preview20141217 2.配置config文件Windows(在命令行下)cd /d %userprofile%if not exist &q ...

  5. [08]APUE:进程控制

    [a] getpid / getppid / getuid / geteuid / getgid / getegid #include <unistd.h> pid_t getpid(vo ...

  6. 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)'

    error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Tra ...

  7. echarts绘制甘特图

      在setoption之后添加这段代码: window.addEventListener('resize', function () { myChart.resize();   }); 图表就能随着 ...

  8. java-base64编码和解码

    一.反射/*** * encode by Base64 */ public static String encodeBase64(byte[]input) throws Exception{ Clas ...

  9. app之间的跳转,进入二级界面

    功能实现:A跳到B并打开B中指定页面.http://blog.csdn.net/dollyyang/article/details/50325307 点击页面判断是否安装app并打开,否则跳转app ...

  10. ZOJ3774_Power of Fibonacci

    求fibonacci数列前N个数的K次方和. 通项公式:F[n]=((1+sqrt(5))/sqrt(5)-(1-sqrt(5))/sqrt(5))/sqrt(5). 有点乱,不过由于可以保证最后的结 ...