码迷,mamicode.com
首页 > 其他好文 > 详细

UVA11898 - Killer Problem(暴力)

时间:2014-10-21 12:22:26      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:style   http   color   io   os   ar   for   sp   on   

UVA11898 - Killer Problem(暴力)

题目链接

题目大意:给你n个数字(有顺序),然后给你范围l和r,让你在这个范围内找出最小的绝对差值。

解题思路:因为这个数字的范围是从1到10000,这样就说明长度大于10000的时候,肯定会有相同的数字出现,所以结果一定是0;同样也说明最多只需要判断10000个数字,这是最极端的情况,所以暴力可以过。

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int M = 1e4;
const int N = 2e5 + 5;
const int INF = 0x3f3f3f3f;

int arr[N];
int tmp[N];

int Min_value (int l, int r) {

    if (r - l + 1 > M)
        return 0;

    memset (tmp, 0, sizeof (tmp));
    for (int i = l; i <= r; i++) {
        if (tmp[arr[i]])
            return 0;
        tmp[arr[i]]++;
    }

    int mval = INF, pre = -INF;
    for (int i = 1; i <= M; i++) {

        if (tmp[i]) {
            mval = min (mval, i - pre);
            pre = i;
        }
    }
    return mval;
}

int main () {

    int T, n, q;
    int l, r;
    scanf ("%d", &T);

    while (T--) {

        scanf ("%d", &n);
        for (int i = 0; i < n; i++)
            scanf ("%d", &arr[i]);

        scanf ("%d", &q);
        while (q--) {

            scanf ("%d%d", &l, &r);
            printf ("%d\n", Min_value(l - 1, r - 1));
        }
    }
    return 0;
}

UVA11898 - Killer Problem(暴力)

标签:style   http   color   io   os   ar   for   sp   on   

原文地址:http://blog.csdn.net/u012997373/article/details/40341885

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!