Problem 1862 QueryProblem

Accept: 100    Submit: 249
Time Limit: 2000 mSec    Memory Limit : 32768 KB

Problem Description

There
are N numbers (non-negative integers) in a circle. Now your task is
quite simple, just tell me the largest number between L and R.

The Figure 1 is a sample of five integers in a circle. (marked with their index, but not their exact value.)

Figure 1.

The Figure 2,3 show how we count the number.



Figure 2.



Figure 3.

Input

There are no more than 10 test cases;

For each case, the first line contains only one integer N, indicates the size of the circle.

The following one line contains N non-negative integers where Mi
indicates the i-th integers whose index is i. (1 <= N <= 1000, 1
<= i <= N, 0 <= Mi <= 10^9)

Then one line contains Q indicates the number of querys. (1 <= Q <= 10^5)

Then the next Q lines, each line contains only two integers indicate L and R (1 <= L,R <= N)

Output

For each case, please output “Case #index:” in a single line, here index is the case index starts from one.

For each query just output a single line indicates the largest number between L and R.

Output a blank line after each case.

Sample Input

2
3 8
3
1 1
1 2
2 1
1
9
1
1 1

Sample Output

Case #1:
3
8
8

Case #2:
9

Hint

Huge Input, please “scanf” to avoid time limit exceed.

题意:在给定的区间中查询最大的数。当L>R时,R =R +n 来改变R这也是2*n的原因;

方法1:线段树

收获:函数中的num指的是线段树上的编号,而当le == ri时,le 或 ri指的是最低层的编号。

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
using namespace std; const int INF=0x3f3f3f3f;
const double eps=1e-;
const double PI=acos(-1.0);
#define maxn 8006
int tre[maxn];
int a[maxn/];
int n;
void build(int num, int le, int ri)
{
if(le == ri)
{
if(le > n)
tre[num] = a[le-n];//函数中的num指的是线段树上的编号,而当le == ri时,le 或 ri指的是最低层的编号。
else
tre[num] = a[le];
return;
}
int mid = (le + ri)/;
build(num*, le, mid);
build(num*+, mid+, ri);
tre[num] = max(tre[num*], tre[num*+]);
}
int query(int num,int le,int ri,int x,int y)
{
if(x<=le&&y>=ri)
return tre[num];
int mid=(le+ri)/;
int ans=;
if(x<=mid)
ans=max(ans,query(num*,le,mid,x,y)); //先查询左边
if(y>mid)
ans=max(ans,query(num*+,mid+,ri,x,y)); //再查询右边
return ans;
}
int main()
{
int cas = ;
while(~scanf("%d", &n))
{
for(int i = ; i <= n; i++)
scanf("%d", &a[i]);
build(, , *n);
int m;
scanf("%d", &m);
int a, b;
printf("Case #%d:\n", cas++);
for(int j = ; j < m; j++)
{
scanf("%d%d", &a, &b);
if(b < a)
b = b + n;
printf("%d\n", query(, , *n, a, b));
}
puts("");
}
}

方法2:DP

收获:对dp有了更多的了解。

dp[i][j]指的是i到j这个区间内保存的最大值。

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
#define C 0.57721566490153286060651209
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 using namespace std; typedef long long LL;
const int INF=0x3f3f3f3f;
const double eps=1e-;
const double PI=acos(-1.0); const int maxn=;
int dp[][];
int a[];
int main()
{
int n, b;
int sum=;
while(~scanf("%d", &n))
{
for(int i = ; i <= n; i++)
{
scanf("%d", &b);
a[i]=a[i+n]=b;
}
for(int i = ; i <=*n; i++)
{
dp[i][i]=a[i];
for(int j=i+;j<=*n;j++)
{
if(a[j]>dp[i][j-])
dp[i][j]=a[j];
else
dp[i][j]=dp[i][j-];
}
}
int m;
scanf("%d", &m);
int L, R;
printf("Case #%d:\n",++sum);
for(int i=;i<=m;i++)
{
int q,w;
scanf("%d%d",&q,&w);
if(w<q)
w=w+n;
printf("%d\n",dp[q][w]);
}
puts("");
}
return ;
}

FZU1862(线段树 或者 DP)的更多相关文章

  1. Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)

    题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...

  2. 2018.09.12 poj2376Cleaning Shifts(线段树+简单dp)

    传送门 貌似贪心能过啊%%%. 本蒟蒻写的线段树优化dp. 式子很好推啊. f[i]表示覆盖1~i所需的最小代价. 那么显然对于一个区间[li,ri]" role="present ...

  3. 2018.07.08 hdu4521 小明系列问题——小明序列(线段树+简单dp)

    小明系列问题--小明序列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Proble ...

  4. CF833B The Bakery 线段树,DP

    CF833B The Bakery LG传送门 线段树优化DP. 其实这是很久以前就应该做了的一道题,由于颓废一直咕在那里,其实还是挺不错的一道题. 先考虑\(O(n^2k)\)做法:设\(f[i][ ...

  5. Codeforces Round #426 (Div. 2) D 线段树优化dp

    D. The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard inp ...

  6. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

  7. BZOJ2090: [Poi2010]Monotonicity 2【线段树优化DP】

    BZOJ2090: [Poi2010]Monotonicity 2[线段树优化DP] Description 给出N个正整数a[1..N],再给出K个关系符号(>.<或=)s[1..k]. ...

  8. Codeforces 834D The Bakery 【线段树优化DP】*

    Codeforces 834D The Bakery LINK 题目大意是给你一个长度为n的序列分成k段,每一段的贡献是这一段中不同的数的个数,求最大贡献 是第一次做线段树维护DP值的题 感觉还可以, ...

  9. [AGC011F] Train Service Planning [线段树优化dp+思维]

    思路 模意义 这题真tm有意思 我上下楼梯了半天做出来的qwq 首先,考虑到每K分钟有一辆车,那么可以把所有的操作都放到模$K$意义下进行 这时,我们只需要考虑两边的两辆车就好了. 定义一些称呼: 上 ...

随机推荐

  1. Java通过JNI调用C/C++

    From:http://www.cnblogs.com/mandroid/archive/2011/06/15/2081093.html JNI是JAVA标准平台中的一个重要功能,它弥补了JAVA的与 ...

  2. as3 updateAfterEvent的作用

    flash中一共有三个类具有该属性,这三个类分别是:KeyboardEvent,MouseEvent,TimerEvent.调用updateAfterEvent 属性的事件,可强制立即执行呈现操作,而 ...

  3. 百度地图 Android SDK - 检索功能使用的简单演示样例

    百度地图 SDK 不仅为广大开发人员提供了炫酷的地图展示效果.丰富的覆盖物图层,更为广大开发人员提供了多种 LBS 检索的能力. 通过这些接口,开发人员能够轻松的訪问百度的 LBS 数据,丰富自己的移 ...

  4. [HeadFirst-HTMLCSS学习笔记][第十二章HTML5标记]

    考虑HTML结构 HTML5即是把原来<div>换成一些更特定的元素.能够更明确指示包含什么内容. (页眉,导航,页脚,文章) article nav 导航 header footer t ...

  5. MySql命令——命令行客户机的分隔符

    delimiter // create procedure productpricint() begin select avg(price) as priceaverage from product; ...

  6. Sql Server中charindex、patindex的区别

    SQL代码如下: select charindex('1,','121,1,1234') select patindex('%1,%','121,1,1234') ','121,1,1234') se ...

  7. asp.net UpdatePanel 不能局部刷新问题汇总

    1.web.config 配置问题.   关于web.config的配置方面网上有很多资料参考,按照其方法做即可实现. 2.网站 Framework 版本变化造成不能局部刷新问题    版本更新时,会 ...

  8. Ext Radio 取消选中

    今天,做项目的时候遇到了要吧Ext Radio单选按钮取消选中状态,由于没有在formpanel中写, 导致不能用reset()方法,试了各种方法,最后这样写管用. radio1.setValue(f ...

  9. Funsion Charts 学习(二)

    下载FusionCharts 3.1网址为 http://www.onlinedown.net/soft/92224.htm 第一个demo 新建一个文件夹,命名为demo 在文件夹中新建一个两个文件 ...

  10. NSURL

    1. NSURL的简介 URL是对可以从互联网上得到的资源的位置和访问方法的一种简介的表示,是互联网上标准资源的地址.URL可能包含远程服务器上的资源位置,本地磁盘上的文件的路径,甚至任意一段编码的数 ...