1044 Shopping in Mars
程序员文章站
2024-03-17 08:43:46
...
解题代码
#include<cstdio>
#include<algorithm>
using namespace std;
const int nmax = 100001;
int a[nmax], s[nmax], n, m;
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
s[i] = s[i - 1] + a[i];
}
int min=100000001, j;
for (int i = 1; i <= n; i++) {
j = lower_bound(s + i, s + n + 1, s[i - 1] + m) - s;
if (s[j] - s[i - 1] < min && j != n + 1) min = s[j] - s[i - 1];
}
int cnt = 0;
for (int i = 1; i <= n; i++) {
j = lower_bound(s + i, s + n + 1, s[i - 1] + m) - s;
if (s[j] - s[i - 1] == min) {
if (!cnt) cnt++;
else printf("\n");
printf("%d-%d", i, j);
}
}
return 0;
}
测试结果
问题整理
1.二分法。
2.upper_bound 返回第一个大于x的迭代器。
3.lower_bound 返回第一个大于等于x的迭代器。
上一篇: Shopping Offers
下一篇: C++函数重载
推荐阅读
-
Shopping Offers
-
Shopping Offers
-
638. Shopping Offers
-
shopping客户端
-
1044 Shopping in Mars
-
ERROR 1044 (42000)Access denied for user @localhost to database
-
mysql ERROR 1044 (42000): Access denied for user ''@'localhost' to database
-
mysql ERROR 1044 (42000): Access denied for user ''@'localhost' to database
-
Mysql安装 Navicat 出现1044/1045错误的解决方法
-
MySQL: ERROR 1044 (42000) Access denied for user ''@'localhost' to database 'aliendatabase'