Uva11898-killer problem (violent)

Source: Internet
Author: User

Uva11898-killer problem (violent)

Question Link

Give you n numbers (in order), and then give you the range of L and R, so that you can find the smallest absolute difference in this range.

Solution: Because the range of the number is from 1 to 10000, it means that when the length is greater than 10000, the same number will certainly appear, so the result must be 0; it also means that you only need to judge a maximum of 10000 numbers. This is the most extreme situation, so it is acceptable to be violent.

Code:

#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 (violent)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.