-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefensive_line.cpp
150 lines (128 loc) · 3.61 KB
/
defensive_line.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//
// defensive_line.cpp
// Algorithms Lab
//
// Created by Jonas Gessner
// Copyright © 2019 Jonas Gessner. All rights reserved.
//
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <limits>
#include <stdexcept>
using namespace std;
int findLongestDisjointRanges(int rangeIndex, int attacksLeft, const vector<pair<int, int>> &ranges, const vector<int> &independentPredecessorLookup, vector<vector<int>> &memo)
{
if (attacksLeft == 0)
{
return 0;
}
if (rangeIndex < 0)
{
return -1;
}
const int memoVal = memo.at(rangeIndex).at(attacksLeft);
if (memoVal != -1)
{
if (memoVal == -2)
{
return -1;
}
else
{
return memoVal;
}
}
const int skipRange = findLongestDisjointRanges(rangeIndex - 1, attacksLeft, ranges, independentPredecessorLookup, memo);
int rangeIndexForFit = independentPredecessorLookup.at(ranges.at(rangeIndex).first);
const int useThisRange = rangeIndexForFit >= 0 ? findLongestDisjointRanges(rangeIndexForFit, attacksLeft - 1, ranges, independentPredecessorLookup, memo) : (attacksLeft == 1 ? 0 : -1); // If this range is the only range that fits, it is only accepted if we are only looking for one attack
if (skipRange == -1 && useThisRange == -1)
{
memo.at(rangeIndex).at(attacksLeft) = -2;
return -1;
}
else
{
int best = max(skipRange, useThisRange + ranges.at(rangeIndex).second - ranges.at(rangeIndex).first);
memo.at(rangeIndex).at(attacksLeft) = best;
return best;
}
}
void runTestCase()
{
int n, m, k;
cin >> n >> m >> k;
vector<int> values;
values.reserve(n);
for (int i = 0; i < n; i++)
{
int val;
cin >> val;
values.push_back(val);
}
// Use sliding window to find all intervals that sum to k
int l = 0;
int r = 0;
int currentSum = 0;
vector<pair<int, int>> ranges; // [l, r)
while (r <= n)
{
while (currentSum > k) // Keep up with left
{
currentSum -= values.at(l);
l++;
}
if (currentSum == k) // If left and right match
{
ranges.push_back({l, r});
}
if (r < n)
{
int atR = values.at(r); // Advance right
currentSum += atR;
}
r++;
}
sort(ranges.begin(), ranges.end(), [](const pair<int, int> &lhs, const pair<int, int> &rhs) {
return lhs.second < rhs.second;
});
if (ranges.size() < m)
{
cout << "fail\n";
return;
}
vector<int> independentPredecessorLookup(n); // For a given index, this gives the index of the closest predecessor range that is independent
int currentRangeIndex = -1;
for (int i = 0; i < n; i++)
{
int nextIndex = currentRangeIndex;
while (nextIndex + 1 < ranges.size() && ranges.at(nextIndex + 1).second <= i)
{
nextIndex++;
}
currentRangeIndex = nextIndex;
independentPredecessorLookup.at(i) = currentRangeIndex;
}
vector<vector<int>> memo(ranges.size(), vector<int>(m + 1, -1));
// Find the m largest disjoint ranges in the ranges vector, if exists
int value = findLongestDisjointRanges(ranges.size() - 1, m, ranges, independentPredecessorLookup, memo);
if (value == -1)
{
cout << "fail\n";
}
else
{
cout << value << "\n";
}
}
int main(int argc, char const *argv[])
{
ios_base::sync_with_stdio(false);
int t;
cin >> t;
while (t-- > 0)
{
runTestCase();
}
}