-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy path1044. Shopping in Mars.cpp
67 lines (67 loc) · 1.6 KB
/
1044. Shopping in Mars.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//二分查找
#include <bits/stdc++.h>
using namespace std;
using gg = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
gg ni, mi;
cin >> ni >> mi;
vector<gg> v(ni + 1); //注意v[0]=0
for (gg i = 1; i <= ni; ++i) {
cin >> v[i];
}
partial_sum(v.begin(), v.end(), v.begin()); //求前缀和
gg sum = INT_MAX;
vector<array<gg, 2>> ans;
for (gg i = 0; i < v.size(); ++i) {
gg j = lower_bound(v.begin(), v.end(), v[i] + mi) - v.begin();
if (j == v.size()) { //查找不到总和大于等于M的连续数字
continue;
}
gg s = v[j] - v[i];
if (sum > s) {
sum = s;
ans = {{i + 1, j}};
} else if (s == sum) {
ans.push_back({i + 1, j});
}
}
for (auto& i : ans) {
cout << i[0] << "-" << i[1] << "\n";
}
return 0;
}
// two pointers
#include <bits/stdc++.h>
using namespace std;
using gg = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
gg ni, mi;
cin >> ni >> mi;
vector<gg> v(ni);
for (gg& i : v) {
cin >> i;
}
gg s = 0, sum = INT_MAX;
vector<array<gg, 2>> ans;
for (gg i = 0, j = 0; i < ni and j <= ni;) {
if (s >= mi) {
if (sum > s) {
sum = s;
ans = {{i + 1, j}};
} else if (s == sum) {
ans.push_back({i + 1, j});
}
s -= v[i++];
} else {
s += v[j++];
}
}
for (auto& i : ans) {
cout << i[0] << "-" << i[1] << "\n";
}
return 0;
}