给定两个上升的数组,一个数组任取一个数,求两个数差的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 ", i, j, ans); } printf("%d ", ans); return 0; }