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. Hibernate session.saveOrUpdate()方法

    saveOrUpdate()方法同时包含了save()与update()方法的功能, 如果传入的参数是临时对象,就调用save()方法: 如果传入的参数是游离对象,就调用update()方法: 如果传 ...

  2. 线性插值(linear interpolation)

    线性插值通常用于:使用离散的样本来重建连续的信号.在计算机图形学中,这些样本可能是纹理.动画关键帧等. template <class T> T Lerp(T& a, T& ...

  3. 关于eclipse(64位)下aptana插件安装报错问题解决

    最近一直没有写过js,换了新电脑以后,eclipse下的aptana插件也没有装过,这几天要写js想重新把aptana装上,但是不知怎的,link方式.在线安装方式还有离线包下载下来利用eclipse ...

  4. linux杂记(五)正确关机方法(shutdown,reboot,init,halt)

    前言:由于在linux底下,每个程序(或者说是服务)都是在背景下运行的,因此,在你看不到的屏幕背后其实可能有相当多人同时在你的主机上面工作,如果 你直接按下电源开关来关机,则可能导致其他人的数据就此中 ...

  5. 用python 10min手写一个简易的实时内存监控系统

    简易的内存监控系统 本文需要有一定的python和前端基础,如果没基础的,请关注我后续的基础教程系列博客 文章github源地址,还可以看到具体的代码,喜欢请在原链接右上角加个star 腾讯视频链接 ...

  6. iOS开发的准备

    一.程序设计语言 上一讲已经说到:要想开发一款软件,首先得学习一些相应的程序设计语言.至于iOS开发,需要学习的语言主要有:C.C++.Objective-C. 二.是否需要计算机专业知识 可能很多人 ...

  7. 面试常用算法——Longest Palindromic Substring(最长回文子串)

    第一种: public static void main(String[] args) { String s = "abcbaaaaabcdcba"; int n,m; Strin ...

  8. 解决android TextView多行文本(超过3行)使用ellipsize属性无效问题

    布局文件中的TextView属性 <TextView android:id="@+id/businesscardsingle_content_abstract" androi ...

  9. Address already in use: JVM_Bind错误的解决

    1,独立运行的Tomcat没有关闭. 自安装的tomcat程序设置开机自动运行,或者在之前运行过,先关闭ecplipse或jbuilder,在任务管理器中找到Tomcat的进程,将其 kill掉,即可 ...

  10. RedisService

    package com.sprucetec.bone.common.redis;import com.alibaba.fastjson.JSON;import org.springframework. ...