算法:POJ1006 三重峰值问题
这题有直接套公式的解法
这里提供一个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 三重峰值问题的更多相关文章
- [MIT6.006] 1. Algorithmic Thinking, Peak Finding 算法思维,峰值寻找
[MIT6.006] 系列笔记将记录我观看<MIT6.006 Introduction to Algorithms, Fall 2011>的课程内容和一些自己补充扩展的知识点.该课程主要介 ...
- poj1006
题目大意:生物节律 一些人相信有三种循环在一个人生命中从他或者她出生的那天起,这个三个周期是,身体,情绪,智力, 并且他们有23,28,和33天的时间,在每一个周期里面都有一个周期,在一个周期的高峰期 ...
- 排序算法之堆排序(Heapsort)解析
一.堆排序的优缺点(pros and cons) (还是简单的说说这个,毕竟没有必要浪费时间去理解一个糟糕的的算法) 优点: 堆排序的效率与快排.归并相同,都达到了基于比较的排序算法效率的峰值(时间复 ...
- 简单易学的机器学习算法——基于密度的聚类算法DBSCAN
一.基于密度的聚类算法的概述 最近在Science上的一篇基于密度的聚类算法<Clustering by fast search and find of density peaks> ...
- 【BZOJ 3049】【USACO2013 Jan】Island Travels BFS+状压DP
这是今天下午的互测题,只得了60多分 分析一下错因: $dis[i][j]$只记录了相邻的两个岛屿之间的距离,我一开始以为可以,后来$charge$提醒我有可能会出现来回走的情况,而状压转移就一次,无 ...
- 探讨.NET Core数据加密和解密问题
前言 一直困扰着我关于数据加密这一块,24号晚上用了接近3个小时去完成一项任务,本以为立马能解决,但是为了保证数据的安全性,我们开始去对数据进行加密,然后接下来3个小时专门去研究加密这一块,然而用着用 ...
- 4 Values whose Sum is 0 [POJ2785] [折半搜索]
题意 给你长度为n四个数列,每个数列选一个数使总和为4,有多少种选法(不同选法仅当起码有一个元素的下标不同) 输入 第一行,n 下面n行,每行四个数,代表ai,bi,ci,di 输出 选法数量 样例输 ...
- 探讨.NET Core数据进行3DES加密和解密问题
前言 一直困扰着我关于数据加密这一块,24号晚上用了接近3个小时去完成一项任务,本以为立马能解决,但是为了保证数据的安全性,我们开始去对数据进行加密,然后接下来3个小时专门去研究加密这一块,然而用着用 ...
- NYOJ 211 Cow Contest (弗洛伊德+传递闭包 )
title: Cow Contest 弗洛伊德+传递闭包 nyoj211 tags: [弗洛伊德,传递闭包] 题目链接 描述 N (1 ≤ N ≤ 100) cows, conveniently nu ...
随机推荐
- ng-init,ng-controller,ng-model
1.ng-init 用于初始化数据,跟在$scope插入数据一样,但是在配合repeat指令时候比较有用: <div ng-repeat="arrOuter in arr" ...
- SQL SERVER增加、删除、更改表中的字段名 (详询请加qq:2085920154)
1. 向表中添加新的字段 alter table table_name add column_name varchar2(20) not null 2. 删除表中的一个字段 delete t ...
- ListView13添加2
Columns=//添加列总行的标题 GridLines=true //显示网格线 添加数据------------- listView1.Items.Add("123123123" ...
- Docker-3:Data Volume
Sometimes, applications need to share access to data or persist data after a container is deleted. ...
- MCMC and Bayesian Data Analysis(PPT在文件模块)
How to generate a sample from $p(x)$? Let's first see how Matlab samples from a $p(x)$. In Matlab, t ...
- Windows Desktop 调用 WinRT api
<Reference Include="Windows"> <HintPath>..\..\..\..\..\..\Program Files (x86)\ ...
- 关于window.open()中文传值乱码问题的解决方法
最近组长叫我做一个把查询出的数据用POI导出Excel的功能,我前台把要查询的字段通过如下传回后台时,发现出现了乱码问题 window.open('user!export?name='+name); ...
- OD使用教程
OD使用教程: 跳转指令.满足才能跳转成功
- delegate notification kvo三者比较
IOS 如何选择delegate.notification.KVO? 前面分别讲了delegate.notification和KVO的实现原理,以及实际使用步骤,我们心中不禁有个疑问,他们的功能比较类 ...
- android免root兼容所有版本ui调试工具
SwissArmyKnife是什么 SwissArmyKnife 是一款方便调试android UI的工具,可以兼容所有android版本,不需要root权限.可以直接在android手机屏幕上显示当 ...