2022秋 C语言程序设计 作业6-24
作者:
GhostBlade
,
2022-12-19 21:31:30
,
所有人可见
,
阅读 160
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct {
int g;
int x;
} Node;
int CmpNode(const void *a, const void *b) {
Node *an = (Node *)a;
Node *bn = (Node *)b;
return an->x - bn->x;
}
int main() {
int n, k;
scanf("%d %d", &n, &k);
Node *list = (Node *)calloc(n, sizeof(Node));
for (int i = 0; i < n; i++) {
scanf("%d %d", &list[i].g, &list[i].x);
}
qsort(list, n, sizeof(Node), CmpNode);
int ans = 0;
int temp = 0;
for(int l = 0, r = 0; r <= n; r++) {
temp += list[r].g;
while (list[r].x - list[l].x > 2 * k) {
temp -= list[l++].g;
}
ans = fmax(ans, temp);
}
printf("%d", ans);
return 0;
}