1100 - Again Array Queries
Time Limit: 3 second(s) Memory Limit: 32 MB

Given an array with n integers, and you are given two indices i and j (i ≠ j) in the array. You have to find two integers in the range whose difference is minimum. You have to print this value. The array is indexed from 0 to n-1.

Input

Input starts with an integer T (≤ 5), denoting the number of test cases.

Each case contains two integers n (2 ≤ n ≤ 105) and q (1 ≤ q ≤ 10000). The next line contains n space separated integers which form the array. These integers range in [1, 1000].

Each of the next q lines contains two integers i and j (0 ≤ i < j < n).

Output

For each test case, print the case number in a line. Then for each query, print the desired result.

Sample Input

Output for Sample Input

2

5 3

10 2 3 12 7

0 2

0 4

2 4

2 1

1 2

0 1

Case 1:

1

1

4

Case 2:

1

巧妙暴力:

 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const int MAXN=1e5+;
int m[MAXN];
int cnt[];
void getans(int l,int r){
if(r-l>=){//因为数字范围1-1000;
puts("");return;
}
mem(cnt,);
for(int i=l;i<=r;i++)
cnt[m[i]]++;
int k=-,ans=;
for(int i=;i<=;i++){
if(cnt[i]>){
ans=;break;
}
if(cnt[i]){
if(k!=-&&i-k<ans)ans=i-k;
k=i;
}
}
printf("%d\n",ans);
}
int main(){
int T,n,q,flot=;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&q);
for(int i=;i<n;i++)scanf("%d",m+i);
printf("Case %d:\n",++flot);
while(q--){
int l,r;
scanf("%d%d",&l,&r);
getans(l,r);
}
}
return ;
}

其实set写简单一些;

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<set>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const int MAXN=1e5+;
set<int>st;
int m[MAXN];
int cnt[];
void getans(int l,int r){
if(r-l>=){//因为数字范围1-1000;
puts("");return;
}
st.clear();
mem(cnt,);
for(int i=l;i<=r;i++)
st.insert(m[i]),cnt[m[i]]++;
int ans=,k=-;
set<int>::iterator iter;
for(iter=st.begin();iter!=st.end();iter++){
if(cnt[*iter]>){
ans=;break;
}
if(k!=-&&*iter-k<ans)ans=*iter-k;
k=*iter;
}
printf("%d\n",ans);
}
int main(){
int T,n,q,flot=;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&q);
for(int i=;i<n;i++)scanf("%d",m+i);
printf("Case %d:\n",++flot);
while(q--){
int l,r;
scanf("%d%d",&l,&r);
getans(l,r);
}
}
return ;
}

lightoj Again Array Queries的更多相关文章

  1. Codeforces 797E - Array Queries

    E. Array Queries 题目链接:http://codeforces.com/problemset/problem/797/E time limit per test 2 seconds m ...

  2. codeforces 797 E. Array Queries【dp,暴力】

    题目链接:codeforces 797 E. Array Queries   题意:给你一个长度为n的数组a,和q个询问,每次询问为(p,k),相应的把p转换为p+a[p]+k,直到p > n为 ...

  3. AC日记——Array Queries codeforces 797e

    797E - Array Queries 思路: 分段处理: 当k小于根号n时记忆化搜索: 否则暴力: 来,上代码: #include <cmath> #include <cstdi ...

  4. Light OJ-1082 - Array Queries,线段树区间查询最大值,哈哈,水过~~

                                                                                                        ...

  5. Light oj-1100 - Again Array Queries,又是这个题,上次那个题用的线段树,这题差点就陷坑里了,简单的抽屉原理加暴力就可以了,真是坑~~

                                                                              1100 - Again Array Queries ...

  6. LightOJ 1188 Fast Queries(简单莫队)

    1188 - Fast Queries    PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 64 MB Gi ...

  7. [Codeforces 863D]Yet Another Array Queries Problem

    Description You are given an array a of size n, and q queries to it. There are queries of two types: ...

  8. CF797E. Array Queries

    a is an array of n positive integers, all of which are not greater than n. You have to process q que ...

  9. LightOJ - 1369 - Answering Queries(规律)

    链接: https://vjudge.net/problem/LightOJ-1369 题意: The problem you need to solve here is pretty simple. ...

随机推荐

  1. win8 iis安装及网站发布(转)

    系统:win8 环境:vs2012 一:安装IIS 比较win7的安装来说,多选了几个钩钩,不然会报错,偶就遇到这样的错误. 控制面板->程序和功能->启动和关闭windows功能,钩钩图 ...

  2. 从零开始PHP学习 - 第五天

    写这个系列文章主要是为了督促自己  每天定时 定量消化一些知识! 同时也为了让需要的人 学到点啥~! 本人技术实在不高!本文中可能会有错误!希望大家发现后能提醒一下我和大家! 偷偷说下 本教程最后的目 ...

  3. hdu 4033 Regular Polygon 计算几何 二分+余弦定理

    题目链接 给一个n个顶点的正多边形, 给出多边形内部一个点到n个顶点的距离, 让你求出这个多边形的边长. 二分边长, 然后用余弦定理求出给出的相邻的两个边之间的夹角, 看所有的加起来是不是2Pi. # ...

  4. MYSQL ERROR 1045 (28000): Access denied for user 'neeky'@'Nee' (using password: YES)

    情况: mysql -h 192.168.1.7 -u neeky -p 本来这样就可以连接上mysql服务的了, 可是它会报这个错“ERROR 1045 (28000): Access denied ...

  5. Oracle字符集转换

            这几天在工作中碰到一个字符乱码的问题,发现在cmd窗口的sqlplus中直接update一个中文和使用@调用一个文件作同样更新的时候,存储的结果 竟不一样.一时比较迷惑,对Oracle ...

  6. 终于懂了:两个UI组件同时在操作是不可能实现的

    // 目的:从某个对话框里,选择一些路径,然后用Tree自动展开这些路径,但至少需要几秒钟时间 // 问题:在这几秒钟期间,显示一个等待对话框,只能开多线程,因为后台继续要处理tree的一些事情.等待 ...

  7. C++的类型萃取技术

    应该说,迭代器就是一种智能指针,因此,它也就拥有了一般指针的所有特点——能够对其进行*和->操作.但是在遍历容器的时候,不可避免的要对遍历的容器内部有所了解,所以,设计一个迭代器也就自然而然的变 ...

  8. VS2010/MFC对话框:字体对话框

    字体对话框) 在上一节为大家讲解了文件对话框的使用,本节则主要介绍字体对话框如何应用. 字体对话框的作用是用来选择字体.我们也经常能够见到.MFC使用CFontDialog类封装了字体对话框的所有操作 ...

  9. oracle中clob字段的使用

    oracle中定义了一个字段是clob的,由于用的是ssh的框架,结果在面向对象存取的时候出现clob类型字段和String类型字段的转换问题.开始查阅了clob字段和String字段的相互转换的方法 ...

  10. 浅析Thinkphp框架中运用phprpc扩展模式

    浅析Thinkphp框架中应用phprpc扩展模式 这次的项目舍弃了原来使用Axis2做web服务端的 方案,改用phprpc实现,其一是服务端的thinkphp已集成有该模式接口,其二是phprpc ...