尺取法 || emmmm

给定两个上升的数组,一个数组任取一个数,求两个数差的min

尺取法emm

也不知道对不对

#include <stdio.h>
#include <stdlib.h>
#define SZ 10010
int min(int x, int y)
{
    if(x < y) return x;
    else return y;
}
int main()
{
    int M,N;
    scanf("%d %d",&N,&M);
    int x[SZ],y[SZ];
    int i,j, ans = 1e9, tmp = 0;
    for(i=0; i<N; i++)
        scanf("%d",&x[i]);
    for(j=0; j<M; j++)
        scanf("%d",&y[j]);
    for(i = 0; i < N; i++)
    {
        if(x[i] > y[tmp]) break;
        for(j = tmp; j < M; j++)
        {
            if(x[i] < y[j]) break;
        }
        tmp = j - 1;
        if(j == 0) tmp++, ans = min(ans, y[j] - x[i]);
        else ans = min(ans, min(x[i] - y[j - 1], y[j] - x[i]));
        //printf("%d %d %d\n", i, j, ans);
    }
    printf("%d\n", ans);
    return 0;
}

 

posted @ 2018-04-20 01:47  舒羽倾  阅读(104)  评论(0编辑  收藏  举报