Again Array Queries---Lightoj1100(循环暴力)

题目链接:http://lightoj.com/volume_showproblem.php?problem=1100

题意是给你n个数,q个询问,每次求出 a 到 b(从0开始)最小差值;

直接暴力就能过:

 

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<iostream>
using namespace std;
#define N 1010
#define INF 0xfffffff
int num[N*100], cnt[N], vis[N];

int main()
{
    int T, t=1, n, a, b, q;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d%d", &n, &q);
        for(int i=0; i<n; i++)
            scanf("%d", &num[i]);
        printf("Case %d:\n", t++);
        while(q--)
        {
            memset(vis, 0, sizeof(vis));
            scanf("%d%d", &a, &b);
            int flag = 0, Min = INF;
            for(int i=a; i<=b; i++)
            {
                if(vis[num[i]]==1)
                {
                    flag=1;
                    break;
                }
                else
                {
                    for(int j=a; j<i; j++)
                        Min = min(Min, abs(num[i]-num[j]));
                    vis[num[i]] = 1;
                }
            }
            if(flag == 1)
                Min = 0;
            printf("%d\n", Min);
        }
    }
    return 0;
}
View Code

 

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<iostream>
using namespace std;
#define N 1010
#define INF 0xfffffff
int num[N*100], cnt[N];

int main()
{
    int T, t=1, n, a, b, q;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d%d", &n, &q);
        for(int i=0; i<n; i++)
            scanf("%d", &num[i]);
        printf("Case %d:\n", t++);
        while(q--)
        {
            memset(cnt, 0, sizeof(cnt));
            scanf("%d%d", &a, &b);
            int flag = 0, Min = INF, pre = 0;
            for(int i=a; i<=b; i++)
                cnt[num[i]]++;
            for(int i=1; i<=1000; i++)
            {
                if(flag == 1)break;
                if(cnt[i]>=2)
                {
                    flag = 1;break;
                }
                if(cnt[i]==1)
                {
                    if(pre == 0) pre = i;
                    else
                    {
                        Min = min(Min, i-pre);
                        if(Min == 0)flag = 1;
                        pre = i;
                    }
                }
            }
            if(flag == 1)
                Min = 0;
            printf("%d\n", Min);
        }
    }
    return 0;
}
View Code

 

posted @ 2015-10-26 19:37  西瓜不懂柠檬的酸  Views(125)  Comments(0Edit  收藏  举报
levels of contents