|
发表于 2022-7-6 14:26:13
|
显示全部楼层
本帖最后由 风车呼呼呼 于 2022-7-6 14:28 编辑
转换为数学问题要好理解的多,即:对于一副散点图,区间内最大值与最小值之差 >= s 的最小区间宽度。
遍历所有满足条件的子区间,取其最小值即可
- #include <stdio.h>
- #include <stdlib.h>
- int current_s(int* py, int ns, int ne) {
- int max=py[ns], min=py[ns], i;
- for (i = ns; i <= ne; i++) {
- max = py[i] > max ? py[i] : max;
- min = py[i] < min ? py[i] : min;
- }
- return max - min;
- }
- int current_w(int* py, int *px, int ns, int ne, int s) {
- int t = current_s(py, ns, ne);
- return t >= s ? px[ne] - px[ns] > 0 ? px[ne] - px[ns] : px[ns] - px[ne] : -1;
- }
- int main(void) {
- int n = 0, s = 0, pot_width=-1, temp_w;
- scanf("%d %d", &n, &s);
- int* px = (int*)malloc(sizeof(int) * n);
- int* py = (int*)malloc(sizeof(int) * n);
- for (int i = 0; i < n; i++) {
- scanf("%d %d", px + i, py + i);
- }
- for (int x_left = 0; x_left < n; x_left++) {
- for (int x_right = 1; x_right < n; x_right++) {
- temp_w = current_w(py, px, x_left, x_right, s);
- if (temp_w != -1 && (pot_width == -1 || temp_w < pot_width) ) {
- pot_width = temp_w;
- //break;
- }
- }
- }
- printf("最小花盆宽度: %d\n", pot_width);
- return 0;
- }
复制代码 |
评分
-
查看全部评分
|