-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflexible.cpp
75 lines (60 loc) · 2.01 KB
/
flexible.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
68
69
70
71
72
73
74
75
/**
* @file flexible.cpp
* @author William Weston
* @brief Flexible Spaces Problem From Kattis
* @version 0.1
* @date 2023-08-14
*
* @copyright Copyright (c) 2023
*
* Source: https://open.kattis.com/problems/flexible
*/
#include <algorithm>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <iterator>
#include <vector>
template <typename RIter, typename OutputIter, typename BinaryOp = std::minus<typename std::iterator_traits<RIter>::value_type>>
auto unordered_pairwise( RIter first, RIter last, OutputIter d_first, BinaryOp op = BinaryOp{} ) -> void;
auto print( std::vector<int> const& container ) -> void
{
for ( auto const& elem : container )
std::cout << elem << ' ';
std::cout << '\n';
}
auto main() -> int
{
auto const W = []() { int tmp; std::cin >> tmp; return tmp; }();
auto const P = []() { int tmp; std::cin >> tmp; return tmp; }();
auto partitions = std::vector<int>{ 0 }; // start with a zero
partitions.reserve( P );
for ( auto count = 0; count < P; ++count )
{
int tmp;
std::cin >> tmp;
partitions.push_back( tmp );
}
partitions.push_back( W ); // add the total width
auto results = std::vector<int>();
unordered_pairwise( partitions.begin(), partitions.end(), std::back_inserter( results ) );
std::sort( results.begin(), results.end() );
auto const new_end = std::unique( results.begin(), results.end() );
if ( new_end != results.end() )
results.erase( new_end, results.end() );
auto const it = std::remove(results.begin(), results.end(), 0);
results.erase(it, results.end());
print( results );
return EXIT_SUCCESS;
}
template <typename RIter, typename OutputIter, typename BinaryOp>
auto unordered_pairwise( RIter first, RIter last, OutputIter d_first, BinaryOp op ) -> void
{
for ( auto const last_element = last - 1; first != last_element; ++first )
{
for( auto start = first + 1; start != last; ++start )
{
*d_first++ = op( *start, *first );
}
}
}