18110 Koishi's travel, Satori's travel
18110 Koishi's travel, Satori's travel
该题有题解
时间限制:4000MS 内存限制:65535K
提交次数:0 通过次数:0
题型: 编程题 语言: 不限定
Description
Koishi travel for a long time.
No one knows where she will go include herself because of her unconscious action.
She may be losting in the lost forest.
She may be fishing on the misty lake .
She may be playing in the Hakurei jinja.
She may be now behind you... Satori is worried about her younger sister, Koishi. Wanted to find Koishi, She embarked on a journey.
Because of the ability to read minds, She know where Koishi is seen by reading other people's mind
if they had seen her. She finds one after another place, one after another location.
Through her efforts, now she knows that Koishi has been to n places and she was in the (ai)th place when time is i.
She knows a lot about her sister. So She guess the next place she will go is the maximum of ai % aj where i < j.
If the result is 0. Satori will very happy because Koishi will go back home. But there are too many place. Satori can't calculate the result quickly.
If not calculate the result as soon as possible, Koishi maybe go to the next place.
Now Satori need your help. Can you help Satori to calculate the result?
输入格式
The input file begins with a line of integer T (T <= 100) indicating the number of test case.
Each test case contains two lines. The first line contains an integer n (2 <= n <= 50000). Then
the second line contains n positive integers in the array A, separated by spaces. No Ai will exceed n.
The sum of n will not exceed 500000.
输出格式
For each test case, print the answer in one line.
输入样例
2
5
1 2 3 4 5
5
5 4 3 2 1
输出样例
4
2
提示
sample1: 4 % 5 is the maximum
sample2: 5 % 3 is the maximum
作者
201330330101
题意:给你n个数,找出最大值的ai%aj(i<j)。
首先,题目里有一个很重要的条件:n最大为50000且数列中的n个数都不会大于n; 那么,就应该从这里作为突破口,寻找高效的算法。 然后再来看:对于每一个数aj,前面的元素取余它时,可能的结果只有0~aj-1; 所以我们就可以从左往右扫一遍数列,对于每一个元素aj,我们一段一段的考虑:即0~aj-1,aj+1~2*aj-1,....直至大于n就停止;在每一段中时要进行的操作就是判断前面的数列中是否存在在这个段中的元素,然后更新最大值(这里就用线段树来解决)。 这样一来,每个数要判断的次数就为n/aj,而判断的复杂度是logn。
另外,可以在开一个数组来标记重复出现的数字的最后出现的下标;因为重复出现的数字只要考虑最后一个,它覆盖到前面的元素是最多的,所以直接算最后一个可以节约重复计算的时间。 (整体的复杂度不清楚到底是多少。。。不过下面的AC代码是跑的2000ms多一些 =_=)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <map> //GL&HF
#include <set>
typedef long long ll;
const int inf=0x3f3f3f3f;
const int mod=(1e9)+;
const int MAXN=5e4+;
using namespace std; int a[MAXN],last[MAXN],bit[MAXN*+]; void build(int l,int r,int id) //建立线段树, id为树中结点的下标
{
bit[id]=;
if(l==r) return;
int mid=(l+r)>>;
build(l,mid,id<<);
build(mid+,r,id<<|);
}
void push(int id) //更行区间结点id的值
{
bit[id]=max(bit[id<<],bit[id<<|]);
}
void update(int value,int l,int r,int id)
{
if(l==r)
{
bit[id]=value;
return;
}
int mid=(l+r)>>;
if(value<=mid) update(value,l,mid,id<<);
else update(value,mid+,r,id<<|);
push(id);
}
int query(int ql,int qr,int l,int r,int id) //查询操作
{
if(ql<=l&&r<=qr) return bit[id];
//
int mid=(l+r)>>,res=;
if(ql<=mid) res=max(res,query(ql,qr,l,mid,id<<));
if(qr>mid) res=max(res,query(ql,qr,mid+,r,id<<|));
return res;
} int main()
{
//freopen("input.txt","r",stdin);
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&a[i]),last[a[i]]=i;
build(,n,);
//
int ans=-inf;
for(int i=;i<=n;i++)
{
if(last[a[i]]==i)
{
int l=,r;
while(l<=n)
{
r=min(n,l+a[i]-);
int temp=query(l,r,,n,);
//
ans=max(ans,temp%a[i]);
l+=a[i];
}
}
update(a[i],,n,);
}
//
printf("%d\n",ans);
}
return ;
}
18110 Koishi's travel, Satori's travel的更多相关文章
- Tableview中Dynamic Prototypes动态表的使用
Tableview时IOS中应用非常广泛的控件,当需要动态的添加多条不同的数据时,需要用动态表来实现,下面给出一个小例子,适用于不确定Section的数目,并且每个Section中的行数也不同的情况, ...
- python设计模式
本节内容 设计模式介绍 设计模式分类 设计模式6大原则 1.设计模式介绍 设计模式(Design Patterns) --可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复 ...
- Python学习路程-常用设计模式学习
本节内容 设计模式介绍 设计模式分类 设计模式6大原则 1.设计模式介绍 设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复 ...
- IOS , plist 配置项说明
本文转载至 http://blog.csdn.net/fengsh998/article/details/8307424 Key:Application can be killed immediate ...
- 通过PowerShell获取域名whois信息
Whois 简单来说,就是一个用来查询域名是否已经被注册,以及注册域名的详细信息的数据库(如域名所有人.域名注册商.域名注册日期和过期日期等).通过域名Whois服务器查询,可以查询域名归属者联系方式 ...
- HDU 1890:Robotic Sort(Splay)
http://acm.hdu.edu.cn/showproblem.php?pid=1890 题意:有一个无序序列,经过不断地翻转,使得最后的序列是一个升序的序列,而且如果相同数字要使在原本序列靠前的 ...
- CART
一.为什么有CART回归树 以前学过全局回归,顾名思义,就是指全部数据符合某种曲线.比如线性回归,多项式拟合(泰勒)等等.可是这些数学规律多强,硬硬地将全部数据逼近一些特殊的曲线.生活中的数据可是千变 ...
- HDU 4118 Holiday's Accommodation
Holiday's Accommodation Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 200000/200000 K (Jav ...
- BZOJ2553: [BeiJing2011]禁忌
2553: [BeiJing2011]禁忌 Time Limit: 20 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 203 Solved: ...
随机推荐
- (三)Appium-desktop 打包
appium-desktop经过二次开发后,需要打包为应用提供给其它同学使用.我们知道appium-desktop是使用electron来构建跨平台桌面应用程序.electron有electron-p ...
- vue 中数据没有同步渲染的解决方法
今天在做一个页面,遇到一个数据渲染不同步的问题,如下: 代码如下:原理:点击时,对应的banklist 的选项选项变为 true 选中状态 html: <div class="PayO ...
- Vue发布过程中遇到坑,以及webpack打包优化
前言 这段时间,本人自己做了一个vue画面部署到自己的服务器上,发现运行速度慢的的惊人,虽然服务器很渣(本人没什么钱,只能租最差的服务器,主要是给自己学习用的),但是这样开发出来的网站简直不能用,所以 ...
- CentOS7 搭建Kafka(一)zookeeper篇
CentOS7 搭建Kafka(一)zookeeper篇 近几年当红小生Kafka备受各路英雄好汉追捧,一点不比老前辈RabbitMQ和ActiveMQ差,因为流行,所以你就得学啊:我这么懒,肯定是不 ...
- SQLCE本地数据库
SQLCE是一个标准得关系数据库,可以使用 LINQ 和DateContext来处理本地数据库数据库. 使用SQLCE 要在代码中使用本地数据库功能,需要添加以下命名空间 : using System ...
- VMWare linux 打印太多,看不到之前的记录的解决方法总结
1.在命令后面加 | more. 可以每次按空格键或是回车键后翻.2.命令后面加| less ,可以前后翻.3.用重定向到文件 > 文件名,之后慢慢看 ----待补充 ------
- ApplicationLoader登录失败
报错:Please sign in with an app-specific password. You can create one at appleid.apple.com 是因为帐号开启了双重认 ...
- Tomcat的几种部署方式
1. 直接把项目的根目录放在: apache-tomcat-*.*\webapps\ROOT 这样即可以通过http://127.0.0.1:8080 来访问 2. 把项目根目录放在: apach ...
- spring boot注解
一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfiguration ...
- PAT_A1149#Dangerous Goods Packaging
Source: PAT A1149 Dangerous Goods Packaging (25 分) Description: When shipping goods with containers, ...