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的更多相关文章

  1. Tableview中Dynamic Prototypes动态表的使用

    Tableview时IOS中应用非常广泛的控件,当需要动态的添加多条不同的数据时,需要用动态表来实现,下面给出一个小例子,适用于不确定Section的数目,并且每个Section中的行数也不同的情况, ...

  2. python设计模式

    本节内容 设计模式介绍 设计模式分类 设计模式6大原则 1.设计模式介绍 设计模式(Design Patterns) --可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复 ...

  3. Python学习路程-常用设计模式学习

    本节内容 设计模式介绍 设计模式分类 设计模式6大原则 1.设计模式介绍 设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复 ...

  4. IOS , plist 配置项说明

    本文转载至 http://blog.csdn.net/fengsh998/article/details/8307424 Key:Application can be killed immediate ...

  5. 通过PowerShell获取域名whois信息

    Whois 简单来说,就是一个用来查询域名是否已经被注册,以及注册域名的详细信息的数据库(如域名所有人.域名注册商.域名注册日期和过期日期等).通过域名Whois服务器查询,可以查询域名归属者联系方式 ...

  6. HDU 1890:Robotic Sort(Splay)

    http://acm.hdu.edu.cn/showproblem.php?pid=1890 题意:有一个无序序列,经过不断地翻转,使得最后的序列是一个升序的序列,而且如果相同数字要使在原本序列靠前的 ...

  7. CART

    一.为什么有CART回归树 以前学过全局回归,顾名思义,就是指全部数据符合某种曲线.比如线性回归,多项式拟合(泰勒)等等.可是这些数学规律多强,硬硬地将全部数据逼近一些特殊的曲线.生活中的数据可是千变 ...

  8. HDU 4118 Holiday's Accommodation

    Holiday's Accommodation Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 200000/200000 K (Jav ...

  9. BZOJ2553: [BeiJing2011]禁忌

    2553: [BeiJing2011]禁忌 Time Limit: 20 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 203  Solved: ...

随机推荐

  1. 0509 关于Ajax + 三级联动示例

    关于Ajax 1.干什么的? ajax负责抓取用户名信息,传递给服务器进行校验: 2.属性: onreadystatechange:事件,该事件可以感知ajax状态(readyState)的变化.aj ...

  2. elasticearch 归并策略

    归并线程配置 segment 归并的过程,需要先读取 segment,归并计算,再写一遍 segment,最后还要保证刷到磁盘.可以说,这是一个非常消耗磁盘 IO 和 CPU 的任务.所以,ES 提供 ...

  3. CSS3之 transform和animation区别

    CSS3 有3种和动画相关的属性:transform, transition, animation.其中 transform 描述了元素静态样式.而transition 和 animation 却都能 ...

  4. 日期对话框(DatePickerDialog)和时间对话框(TimePickerDialog)

    效果图 布局 <Button android:id="@+id/btn_date" android:text="弹出日期选择对话框" android:la ...

  5. android黑科技系列——Apk混淆成中文语言代码

    一.前言 最近想爆破一个app,没有加壳,简单的使用Jadx打开查看源码,结果把我逗乐了,代码中既然都是中文,而且是一些比较奇葩的中文字句,如图所示: 瞬间感觉懵逼了,这app真会玩,我们知道因为Ja ...

  6. CXF-JAX-WS开发(一)入门案例

    一.Web Service 1.定义 W3C定义,Web服务(Web service)应当是一个软件系统,用以支持网络间不同机器的互动操作. 2.作用 多系统间数据通信 二.CXF是什么? CXF是目 ...

  7. 图像局部显著性—点特征(SURF)

    1999年的SIFT(ICCV 1999,并改进发表于IJCV 2004,本文描述):参考描述:图像特征点描述. 参考原文:SURF特征提取分析 本文有大量删除,如有疑义,请参考原文. SURF对SI ...

  8. 揭开jQuery的面纱

    简单地说,jQuery是一个优秀的JavaScript类库,也就是使用JavaScript面向对象的性质编写的一个JavaScript类的集合.jQuery究竟能为我们提供哪些功能呢?简单地说可以从七 ...

  9. Windows Phone 应用程序的生命周期(二)

    一.App.xaml.cs /// <summary> /// Application 对象的构造函数. /// </summary> public App() { // 未捕 ...

  10. 用cmd查看win8版本 激活等详细信息命令

    Win+x===>选择以管理员身份运行,输入: slmgr /dlv   显示:最为详尽的激活信息,包括:激活ID.安装ID.激活截止日期slmgr /dli 显示:操作系统版本.部分产品密钥. ...