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. ZOJ2477 Magic Cube

    题目: This is a very popular game for children. In this game, there's a cube, which consists of 3 * 3 ...

  2. Springboot+hibernate简单的增删改查

    1.创建好项目之后在配置端口号(也可以不用配置,默认端口8080) #server server.port= server.tomcat.uri-encoding=utf- 2.配置mysql #My ...

  3. Android Activity has leaked window that was originally added

    今天调试程序时log中突然打印这样的错误,但是程序并没有crash,为了不放过一个错误,我决定调查一下. 当时是离开一个activity,然后提示是否退出此界面,接下来就打印此错误: - ::): A ...

  4. angular js 球星

    <!DOCTYPE html>   <html lang="en">   <head>   <meta charset="UTF ...

  5. poj2376 Cleaning Shifts 区间贪心

    题目大意: (不说牛了) 给出n个区间,选出个数最少的区间来覆盖区间[1,t].n,t都是给出的. 题目中默认情况是[1,x],[x+1,t]也是可以的.也就是两个相邻的区间之间可以是小区间的右端与大 ...

  6. 如何在编辑器打开Java程序

    我们都知道运行JAVA文件,可以从软件控制台运行我们写好的java文件,也可以从windows窗口运行,我们最开始接触的是通过windows窗口来运行java文件,下面简单介绍一下如何如何在编辑器打开 ...

  7. Android开发之拍照功能实现

    参考链接:http://www.linuxidc.com/Linux/2013-11/92892p3.htm 原文链接:http://blog.csdn.net/tangcheng_ok/articl ...

  8. mvc登录授权特性

    public class CommonAuthorize : AuthorizeAttribute { protected override bool AuthorizeCore(HttpContex ...

  9. 移动APP 微信支付完整过程(wxPay 方案一)

    apicloud.weixinpay官方提供了两种方案. 本模块封装了两套支付方案: 方案一:开发者通过 getOrderId.payOrder 自己处理签名过程(微信开放平台建议把 getOrder ...

  10. Asp.Net Core 自动适应Windows服务、Linux服务、手动启动时的内容路径的扩展方法

    public static IWebHostBuilder UseContentRootAsEnv(this IWebHostBuilder hostBuilder) { bool IsWindows ...