Light oj--1100
2015-12-07 22:05
274 查看
Description
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
2
5 3
10 2 3 12 7
0 2
0 4
2 4
2 1
1 2
0 1
Sample Output
Case 1:
1
1
4
Case 2:
1
代码如下:
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
2
5 3
10 2 3 12 7
0 2
0 4
2 4
2 1
1 2
0 1
Sample Output
Case 1:
1
1
4
Case 2:
1
代码如下:
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int num[1010]; int a[101000]; int main(){ int t,cas; int n,q,u,v; cas=1; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&q); for(int i=0;i<n;i++){ scanf("%d",&a[i]); } printf("Case %d:\n",cas++); for(int i=1;i<=q;i++){ scanf("%d%d",&u,&v); if(v-u>=1000){ printf("0\n"); continue; } int ans=0; int d=1005;//设一个初始差值 int flag=0;//判断是否为第一个数 memset(num,0,sizeof(num)); for(int j=u;j<=v;j++){ num[a[j]]++;//在区间[u,v]中每一个数出现的次数 } for(int k=1;k<=1000&&ans<=v-u;k++){ if(num[k]>1){ d=0; break; } else if(num[k]==1){ ans++; if(flag==0){ flag=k;//因为u!=v } else{ d=min(d,k-flag); flag=k;//不断变换两个数 } } } printf("%d\n",d); } } return 0; }
相关文章推荐
- lintcode 二叉树的前序遍历
- 第一,永远不要跟银行借钱;第二,永远不要向民间借贷;第三,量力而行(转)
- APP界面的设计风格 & 界面交互设计规范
- 实验三 进程调度模拟程序
- stand up meeting 12/7/2015
- 我的编程之路
- JavaScript DOM操作表格及样式
- Longest Palindromic Substring
- BeanFactory与FactoryBean
- ASP.NET运行时详解 生命周期入口分析
- 子集生成
- 杭电校赛2015‘11 1005
- POJ 1637 Sightseeing tour
- 排序算法(2)-直接插入排序
- homerHEVC代码阅读(23)——去方块滤波、SAO、熵编码的主要流程
- 黑马程序员——java基础之继承和多态
- CGI编程完全手册
- 彩票生成器--36选7(不重复)
- NAND: U-Boot BUG at mtdcore.c:20
- 忘记Orcale sys、system密码解决办法