-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path4886.cpp
More file actions
118 lines (98 loc) · 1.63 KB
/
4886.cpp
File metadata and controls
118 lines (98 loc) · 1.63 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
int pages;
int numPages;
int low;
int high;
char separator;
int currentPage;
vector<pair<int, int> > pageRanges;
string rangesInput;
void solve();
void process();
void calc();
int main()
{
while(cin >> pages)
{
if(pages)
{
getline(cin, rangesInput);
solve();
}
else
{
break;
}
}
return 0;
}
void solve()
{
pageRanges.clear();
numPages = 0;
currentPage = 0;
getline(cin, rangesInput);
process();
calc();
cout << numPages << endl;
}
void process()
{
stringstream inputstringstream(rangesInput);
while(inputstringstream >> low)
{
inputstringstream >> separator;
if(separator == ',')
{
if(low <= pages)
{
pageRanges.push_back(make_pair(low, low));
}
}
else
{
if(separator == '-')
{
inputstringstream >> high;
inputstringstream >> separator;
high = min(high, pages);
if(low <= high)
{
pageRanges.push_back(make_pair(low, high));
}
}
}
}
sort(pageRanges.begin(), pageRanges.end());
}
void calc()
{
for(int i = 0; i < pageRanges.size(); i++)
{
if((pageRanges[i].first <= currentPage) && (pageRanges[i].second <= currentPage))
{
continue;
}
else
{
if((pageRanges[i].first <= currentPage) && (pageRanges[i].second > currentPage))
{
numPages += pageRanges[i].second - currentPage;
currentPage = pageRanges[i].second;
}
else
{
if(pageRanges[i].first > currentPage)
{
numPages += pageRanges[i].second - pageRanges[i].first + 1;
currentPage = pageRanges[i].second;
}
}
}
}
}