uva 100 The 3n + 1 problem (RMQ)
uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=36
预处理,RMQ求区间最大值。
代码如下:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath> using namespace std; typedef long long LL;
const int N = ;
const int M = ;
int pre[N], RMQ[M][N >> ];
int dfs(LL n) {
if (n <= ) cout << n << endl;
if (n < N && pre[n]) return pre[n];
int tmp;
if (n & ) tmp = dfs(n * + ) + ;
else tmp = dfs(n >> ) + ;
if (n < N) pre[n] = tmp;
return tmp;
} void PRE() {
pre[] = ;
for (int i = , end = N >> ; i < end; i++) if (pre[i] == ) dfs(i);
// for (int i = 1; i < 20; i++) cout << i << ' ' << pre[i] << endl;
// prepare RMQ
for (int i = , end = N >> ; i < end; i++) RMQ[][i] = pre[i];
for (int i = ; i < M; i++) {
for (int j = , end = (N >> ) - ( << i); j <= end; j++) {
RMQ[i][j] = max(RMQ[i - ][j], RMQ[i - ][j + ( << i - )]);
}
}
} int query(int l, int r) {
if (l > r) swap(l, r);
int ep = (int) log2((double) r - l + );
return max(RMQ[ep][l], RMQ[ep][r - ( << ep) + ]);
} int main() {
PRE();
int l, r;
while (cin >> l >> r) cout << l << ' ' << r << ' ' << query(l, r) << endl;
return ;
}
——written by Lyon
uva 100 The 3n + 1 problem (RMQ)的更多相关文章
- UVA 100 - The 3n+1 problem (3n+1 问题)
100 - The 3n+1 problem (3n+1 问题) /* * 100 - The 3n+1 problem (3n+1 问题) * 作者 仪冰 * QQ 974817955 * * [问 ...
- UVa 100 - The 3n + 1 problem(函数循环长度)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- 【转】UVa Problem 100 The 3n+1 problem (3n+1 问题)——(离线计算)
// The 3n+1 problem (3n+1 问题) // PC/UVa IDs: 110101/100, Popularity: A, Success rate: low Level: 1 / ...
- PC/UVa 题号: 110101/100 The 3n+1 problem (3n+1 问题)
The 3n + 1 problem Background Problems in Computer Science are often classified as belonging to a ...
- UVa Problem 100 The 3n+1 problem (3n+1 问题)
参考:https://blog.csdn.net/metaphysis/article/details/6431937 #include <iostream> #include <c ...
- UVA 100 The 3*n+1 problem
UVA 100 The 3*n+1 problem. 解题思路:对给定的边界m,n(m<n&&0<m,n<1 000 000);求X(m-1<X<n+ ...
- 100-The 3n + 1 problem
本文档下载 题目: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_pro ...
- OpenJudge/Poj 1207 The 3n + 1 problem
1.链接地址: http://bailian.openjudge.cn/practice/1207/ http://poj.org/problem?id=1207 2.题目: 总时间限制: 1000m ...
- The 3n + 1 problem
The 3n + 1 problem Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) ...
随机推荐
- Leetcode605.Can Place Flowers种花问题
假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有.可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去. 给定一个花坛(表示为一个数组包含0和1,其中0表示没种植花,1表示种植了花 ...
- mybatis中实现一对一,一对多查询
在实际的开发中我们经常用到的是一对一查询和一对多查询.而多对多的实现是通过中间来实现,这里就没有给出来了 比如: 订单和用户是一对一的关系(一个订单只能对应一个用户) 订单和订单明细是一对多的关系(一 ...
- 提交方式get和post有什么区别
提交方式post和get有什么区别? (1)post是向服务器传送数据:get是从服务器上获取数据. (2)在客户端,get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个 ...
- css Position 上下左中右布局
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...
- Linux下的MySQL主从同步
网上一些关于Linux下的MySQL主从同步教程非常之多,有些很简单的配置却弄的非常复杂,有些根本无法配通,下面是我通过简单的配置完成的主从同步过程,大家可以参考,此文章更适用于新手. 一.测试环境: ...
- go struct 工厂
- oracle 处理表的一列
---删除一列和数据一起删除了. ALTER TABLE 表名 DROP COLUMN 列名; ---添加一列 alert table 表名 add column 列名; ---只删除一列的数据 没有 ...
- qt获取本机ip
//获取本机IP QString getIP(QString localHost) { QString ipAddr; #if 0 QList<QHostAddress> AddressL ...
- 【OI】拓扑排序
拓扑排序 首先要求图为DAG 算法:首先将度为1的节点加入队列每次取出队首点u,在图中删去和u相邻的边继续将度数为1的点加入队列 到了最后, 如果没有度数为1的点,则图不是DAG 通过拓扑排序可以给D ...
- 关于 SSD 的接口和相关名词(2019-09-10)
关于 SSD 的接口和相关名词 了解了很多天的 SSD,太多的名词. 先记录一下. SATA MSATA M2 NVME NGFF U2 TODO: 后续收集相关信息.