帮我添加注释:帮我添加注释:#include <stdio.h> #include <string.h> #include <stdlib.h> #include <stdbool.h> #define N 1000000 int a[N], b[N]; int n = 0; int m = 0; bool check(int x) { int cnt = 0; for (i = 1; i <= n; i++) { if (a[i] < x) { continue; } cnt += 1 + (a[i] - x) / b[i]; if (cnt >= m) { return true; } } return false; } int main() { int i = 0; int t = 0; int mid = 0; printf("请输入两个整数n和m:\n"); scanf("%d %d", &n, &m); for (i = 1; i <= n; i++) { scanf("%d %d", a + i, b + i); } int l = 0; int r = 1e6; while (l < r - 1) { mid = (l + r + 1) >> 1; if (check(mid)) { l = mid; } else { r = mid - 1; } } int x = l; long long res = 0, cc = 0; for (i = 1; i <= n; i++) { if (a[i] < x) { continue; } t = (a[i] - x) / b[i] + 1; if (a[i] - (t - 1) * b[i] == x) { t--; cc++; } if (m >= t) { m -= t; res += (long long)t * (a[i] + a[i] - (t - 1) * b[i]) / 2; } else { t = m; m = 0; res += (long long)t * (a[i] + a[i] - (t - 1) * b[i]) / 2; break; } } res = res + (long long)(m < cc) * x * m; printf("%lld\n", res); return
时间: 2024-02-14 17:13:34 浏览: 111
VC编写贪吃蛇,超详细注释
4星 · 用户满意度95%
帮我添加注释:
```c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define N 1000000
int a[N], b[N]; // 定义两个数组a和b
int n = 0; // a和b数组的长度
int m = 0; // 需要选出的数的个数
// 判断是否满足选出的数的个数
bool check(int x) {
int cnt = 0;
for (int i = 1; i <= n; i++) {
if (a[i] < x) {
continue;
}
cnt += 1 + (a[i] - x) / b[i];
if (cnt >= m) {
return true;
}
}
return false;
}
int main() {
int i = 0;
int t = 0;
int mid = 0;
// 输入a和b数组的长度n和需要选出的数的个数m
printf("请输入两个整数n和m:\n");
scanf("%d %d", &n, &m);
// 输入n个a和b的值
for (i = 1; i <= n; i++) {
scanf("%d %d", a + i, b + i);
}
int l = 0; // 二分查找左端点
int r = 1e6; // 二分查找右端点
while (l < r - 1) {
mid = (l + r + 1) >> 1; // 取中间值
if (check(mid)) {
l = mid; // 中间值满足条件,将左端点移动到中间值
} else {
r = mid - 1; // 中间值不满足条件,将右端点移动到中间值
}
}
int x = l; // 选出的数的最大值
long long res = 0, cc = 0;
for (i = 1; i <= n; i++) {
if (a[i] < x) {
continue;
}
t = (a[i] - x) / b[i] + 1;
if (a[i] - (t - 1) * b[i] == x) {
t--;
cc++;
}
if (m >= t) {
m -= t;
// 累加等差数列和
res += (long long)t * (a[i] + a[i] - (t - 1) * b[i]) / 2;
} else {
t = m;
m = 0;
// 累加等差数列和
res += (long long)t * (a[i] + a[i] - (t - 1) * b[i]) / 2;
break;
}
}
// 如果选出的数的个数小于等于x的个数,将x乘以剩余的个数累加到结果中
res = res + (long long)(m < cc) * x * m;
printf("%lld\n", res);
return 0;
}
```
阅读全文