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: ...
随机推荐
- tomcat启动时出现以前删除的项目,导致无法启动
com.sun.faces.config.ConfigureListener contextInitialized 解决: 进入到你自己的tomcat安装目录:C:\Program Files\Apa ...
- 微信小程序特殊字符转义方法——&转义&等等
在我编写公司小程序的过程中,有一次在网页端添加了一张图片,结果在小程序端访问失败了,究其原因,竟然是因为该图片名称中有一个“&”符号,网页端添加后,自动转义成了“&”存储到了数据库.当 ...
- 5.29clone项目地址
- 日期Date和String/Long之间的转换
下面是关于日期的常见的几种类型转换: import java.text.ParseException; import java.text.SimpleDateFormat; import java.u ...
- WEB笔记-让HTML5向下兼容的策略
//给新标签增加块级元素声明 article,aside,dialog,figure,fotter,header,legend,nav,section{display:block} //添加css兹瓷 ...
- 小程序viewflex布局的对齐不对的问题
index.wxml: <view class="container"> <view class="nav-container"> &l ...
- eas之日期控件
日期选择框能进行日期和时间的编辑,默认情况下只能进行日期选择“××××年××月××日”,可通过调用用函数setTimeEnabled(boolean)来设置是否也有时间编辑.对日期进行编辑时,可手工直 ...
- 【JavaScript游戏开发】使用HTML5 canvas开发的网页版中国象棋项目
//V1.0 : 实现棋子的布局,画布及游戏场景的初始化 //V2.0 : 实现棋子的颜色改变 //V3.0 :实现所有象棋的走棋规则 //V4.0 : 实现所有棋子的吃子功能 完整的项目源码已经开源 ...
- grpc-web与react的集成
很久没写总结了,在这里跟大家分享一下自己踩的坑,同时也方便自己多记忆下. 大致流程: 使用create-react-app脚手架生成react相关部分,脚手架内部会通过node自动起一个客户端,然后和 ...
- 洛谷P1339 [USACO09OCT]热浪Heat Wave
思路:裸SPFA过一遍(建议使用邻接链表存储),无向图,无向图,无向图,重要的事情要说三遍!!!蜜汁RE是什么鬼????第九个点数组开到20K,第十个点数组开到30K才AC.或许我代码写的有bug?( ...