lightoj Again Array Queries
2015-10-28 22:27
344 查看
1100 - Again Array Queries
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.
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).
巧妙暴力:
其实set写简单一些;
PDF (English) | Statistics | Forum |
Time Limit: 3 second(s) | Memory Limit: 32 MB |
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+10; int m[MAXN]; int cnt[1010]; void getans(int l,int r){ if(r-l>=1000){//因为数字范围1-1000; puts("0");return; } mem(cnt,0); for(int i=l;i<=r;i++) cnt[m[i]]++; int k=-1,ans=1000; for(int i=1;i<=1000;i++){ if(cnt[i]>1){ ans=0;break; } if(cnt[i]){ if(k!=-1&&i-k<ans)ans=i-k; k=i; } } printf("%d\n",ans); } int main(){ int T,n,q,flot=0; scanf("%d",&T); while(T--){ scanf("%d%d",&n,&q); for(int i=0;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 0; }
其实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+10; set<int>st; int m[MAXN]; int cnt[1010]; void getans(int l,int r){ if(r-l>=1000){//因为数字范围1-1000; puts("0");return; } st.clear(); mem(cnt,0); for(int i=l;i<=r;i++) st.insert(m[i]),cnt[m[i]]++; int ans=1000,k=-1; set<int>::iterator iter; for(iter=st.begin();iter!=st.end();iter++){ if(cnt[*iter]>1){ ans=0;break; } if(k!=-1&&*iter-k<ans)ans=*iter-k; k=*iter; } printf("%d\n",ans); } int main(){ int T,n,q,flot=0; scanf("%d",&T); while(T--){ scanf("%d%d",&n,&q); for(int i=0;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 0; }
相关文章推荐
- Android开发UI之给ImageView添加蒙版
- Proving Equivalences(加多少边使其强联通)
- 【iOS】UIImage 等比率缩放
- 解决方案--duilib中禁止一个窗口双击最大化
- 解决方案--duilib中禁止一个窗口双击最大化
- hdu 4632 Palindrome subsequence(dp)
- leetcode笔记:Unique Paths
- 【UI】android如何绘制一个饼图
- iOS中UIView翻转效果实现
- 利用UIScrollView实现展示图片的无限滚动及自动滚动
- ios开发之UIView的frame、bounds跟center属性的区别(附图)
- UIAlertController 警告框
- 浅析UITableViewCell重用机制
- java Vamei快速教程20 GUI
- UVA——10534 Wavio Sequence
- poj 1458 Common Subsequence (最长公共子序列)
- iOS开发UI篇—CAlayer层的属性(一)
- 对QT namespace UI的理解
- jira插件带ui界面和几种方式
- UISlider滑条