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: ...
随机推荐
- php循环跳出
PHP中的循环结构大致有for循环,while循环,do{} while 循环以及foreach循环几种,不管哪种循环中,在PHP中跳出循环大致有这么几种方式: 代码: <?php $i = 1 ...
- Python27天 反射 ,isinstance与ssubclass 内置方法
所学内容 反射 1.hasattr ( 判断一个属性在对象里有没有 ) -------------------- [对象,字符串属性]本质是:# 判断 ' name ' in obj.__dict__ ...
- CAS配置(2)之主配置
WEB-INF目录 1.cas.properties文件(打开关闭SSL,主题,定制页面设置) #默认端口配置 #server.name=http://localhost:8080server.nam ...
- ACM_一道耗时间的水题
一道耗时间的水题 Time Limit: 2000/1000ms (Java/Others) Problem Description: Do you know how to read the phon ...
- Git教程(3)git工作区与文件状态及简单示例
基础 目录: working driectory 工作目录,就是我们的工作目录,其中包括未跟踪文件及暂存区和仓库目录. staging area 暂存区,不对应一个具体目录,其实只是git di ...
- html中<a>标签_top和_parent的区别
在html中,<a>标签有个target属性,而targe属性有四个值,分别是:_blank._self._top._parent.前两个相信很好理解,第一个就是在新窗口中打开的意思,第二 ...
- C#之仿魔兽登录
不多废话,直接上效果图: 1录窗体 对应的代码: using System; using System.Collections.Generic; using System.ComponentModel ...
- 【JAVA练习】- 给定精度求圆周率π
给定一个精度求圆周率π的近似值 给定公式:π/4=1-1/3+1/5-1/7+1/9-... public static void main(String[] args) { System.out.p ...
- poj3009 Curling 2.0 深搜
PS:以前看到题目这么长就没写下去了.今天做了半天,没做出来.准备看题解,打开了网站都忍住了,最后还是靠自己做出来的.算是一点进步吧. 分析: 题目的意思没明白或者理解有偏差都没办法做题.看样例3和样 ...
- Spring AOP之动态代理
软件151 李飞瑶 一.Spring 动态代理中的基本概念 1.关注点(concern) 一个关注点可以是一个特定的问题,概念.或者应用程序的兴趣点.总而言之,应用程序必须达到一个目标 ...