-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy path1044_Shopping in Mars (25).cpp
66 lines (64 loc) · 1.47 KB
/
1044_Shopping in Mars (25).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
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std;
int e[100005];
int main()
{
int n,key,i;
while(scanf("%d %d", &n,&key) == 2)
{
for(i = 0; i < n; i++)
{
scanf("%d", &e[i]);
}
int l,r;
l = r = 0;
int val = e[l];
int ok = 0;
vector<pair<int,int> > ans;
int min = 0x7fffffff;
while(1)
{
if(val < key)
{
if(r+1==n) break;
r = r+1;
val += e[r];
}
else if(val == key)
{
ok = 1;
printf("%d-%d\n",l+1,r+1);
if(r+1==n) break;
r = r+1;
val += e[r];
}
else if(val > key)
{
if(val < min)
{
ans.clear();
min = val;
ans.push_back(make_pair(l,r));
}
else if(val == min)
{
ans.push_back(make_pair(l,r));
}
val -= e[l];
if(l+1==n) break;
l = l+1;
}
}
if(!ok)
{
for(vector<pair<int,int> >::iterator iter=ans.begin(); iter!=ans.end(); iter++)
{
printf("%d-%d\n",iter->first+1,iter->second+1);
}
}
}
return 0;
}