lightoj 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的更多相关文章
- Codeforces 797E - Array Queries
E. Array Queries 题目链接:http://codeforces.com/problemset/problem/797/E time limit per test 2 seconds m ...
- codeforces 797 E. Array Queries【dp,暴力】
题目链接:codeforces 797 E. Array Queries 题意:给你一个长度为n的数组a,和q个询问,每次询问为(p,k),相应的把p转换为p+a[p]+k,直到p > n为 ...
- AC日记——Array Queries codeforces 797e
797E - Array Queries 思路: 分段处理: 当k小于根号n时记忆化搜索: 否则暴力: 来,上代码: #include <cmath> #include <cstdi ...
- Light OJ-1082 - Array Queries,线段树区间查询最大值,哈哈,水过~~
...
- Light oj-1100 - Again Array Queries,又是这个题,上次那个题用的线段树,这题差点就陷坑里了,简单的抽屉原理加暴力就可以了,真是坑~~
1100 - Again Array Queries ...
- LightOJ 1188 Fast Queries(简单莫队)
1188 - Fast Queries PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 64 MB Gi ...
- [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: ...
- 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 ...
- LightOJ - 1369 - Answering Queries(规律)
链接: https://vjudge.net/problem/LightOJ-1369 题意: The problem you need to solve here is pretty simple. ...
随机推荐
- linux杂记(四)热键[Tab],[ctrl]-c,[ctrl]-d,在线求助man page/info page
[Tab]按键 他具有[命令补全](接在一串指令的第一个字后面)与[档案补齐](接在第一串指令的第二字以后时)的功能.如 [KANO@kelvin ~]$ ca[tab][tab] cabextrac ...
- printf与scanf的返回值、注意点
printf()的返回值为其输出字符串常量的字符数(注意字符数与字数的区别)注意计数针对所有的打印字符,包括空格和不可见的换行字符(不包括字符串的空字符). scanf()函数返回成功读入的项目的个数 ...
- Garbage
垃圾回收 http://journal.stuffwithstuff.com/2013/12/08/babys-first-garbage-collector/ http://blog.jobbole ...
- android小知识之邮箱地址输入自动完成
虽然不难,但是容易忘记,做个备忘吧 package com.guet.zhuge; import android.app.Activity; import android.os.Bundle; imp ...
- android小知识之EditText输入框之值监控以及类型限制(数字,英语字母,下划线,是否为星号密码)
1.设置EditText的值监听事件 . <span style="font-size:14px;color:#990000;"> EditText ed=new Ed ...
- MySQL Binlog的介绍
binlog基本定义:二进制日志,也成为二进制日志,记录对数据发生或潜在发生更改的SQL语句,并以二进制的形式保存在磁盘中: 作用:MySQL的作用类似于Oracle的归档日志,可以用来查看数据库的变 ...
- 设置android:supportsRtl="true"无效问题
今天解bug时,遇到这样一个问题: 问题描写叙述:切换系统语言为阿拉伯文时,actionbar布局没有变为从右向左排列. 于是,我在Androidmanifest.xml文件里的 appli ...
- Glog
Glog的简单入门,glog虽然在配置参数方面比较麻烦,但是在小规模程序中,由于其简单灵活,也许会有优势. 0, glog 是google的开源日志系统,相比较log4系列的日志系统,它更加轻巧灵活 ...
- iOS 饼状图
首先先看一下效果: 一.创建饼状图对象 创建饼状图对象用到类是PieChartView.h, 代码如下: self.pieChartView = [[PieChartView alloc] ini ...
- 求新的集合 A=AUB(顺序表)
#include<stdio.h> typedef int A; const int LIST_INIT_SIZE=100; const int LISTINCREMENT=10; typ ...