This commit is contained in:
Zengtudor 2024-08-03 14:05:21 +08:00
parent 0b730d44a6
commit 03ff5f65b6
6 changed files with 121 additions and 9 deletions

View File

@ -2,7 +2,7 @@
using namespace std;
#define int long long
const int MAX_N = 5'0000+5;
const int MAX_N = 10'0005;
int times;
int x[MAX_N];
@ -106,8 +106,6 @@ signed main(){
}
cout<<"\n";
*/
}
}

BIN
day2/U111091/chat Executable file

Binary file not shown.

114
day2/U111091/chat.cpp Normal file
View File

@ -0,0 +1,114 @@
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
#include <cstring>
using namespace std;
long long calculateInitialDirtLength(int n, const vector<int>& x, const string& road_types) {
long long dirt_length = 0;
int start = -1;
for (int i = 0; i < n - 1; ++i) {
if (road_types[i] == '1') {
if (start == -1) start = i;
} else {
if (start != -1) {
dirt_length += x[i + 1] - x[start];
start = -1;
}
}
}
if (start != -1) {
dirt_length += x[n - 1] - x[start];
}
return dirt_length;
}
long long getMaxReduction(const vector<pair<int, int>>& dirt_segments, int m) {
long long max_reduction = 0;
int l = 0, n = dirt_segments.size();
vector<long long> prefix_sums(n + 1, 0);
for (int i = 0; i < n; ++i) {
prefix_sums[i + 1] = prefix_sums[i] + dirt_segments[i].second - dirt_segments[i].first;
}
for (int r = 0; r < n; ++r) {
while (dirt_segments[r].second - dirt_segments[l].first > m) {
++l;
}
long long current_reduction = prefix_sums[r + 1] - prefix_sums[l];
long long total_length = dirt_segments[r].second - dirt_segments[l].first;
if (total_length > m) {
current_reduction -= (total_length - m);
}
max_reduction = max(max_reduction, current_reduction);
}
return max_reduction;
}
long long solveCase(int n, int k, int m, const vector<int>& x, const string& road_types) {
long long initial_dirt_length = calculateInitialDirtLength(n, x, road_types);
vector<pair<int, int>> dirt_segments;
int start = -1;
for (int i = 0; i < n - 1; ++i) {
if (road_types[i] == '1') {
if (start == -1) start = i;
} else {
if (start != -1) {
dirt_segments.emplace_back(start, i);
start = -1;
}
}
}
if (start != -1) {
dirt_segments.emplace_back(start, n - 1);
}
if (k == 1) {
return initial_dirt_length - getMaxReduction(dirt_segments, m);
}
long long best = 0;
for (int i = 0; i < dirt_segments.size(); ++i) {
auto first_segment = dirt_segments[i];
vector<pair<int, int>> temp_segments;
for (int j = 0; j < dirt_segments.size(); ++j) {
if (j != i) temp_segments.push_back(dirt_segments[j]);
}
long long first_reduction = getMaxReduction(vector<pair<int, int>>{first_segment}, m);
long long second_reduction = getMaxReduction(temp_segments, m);
best = max(best, first_reduction + second_reduction);
}
return initial_dirt_length - best;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
while (T--) {
int n, k, m;
cin >> n >> k >> m;
vector<int> x(n);
for (int i = 0; i < n; ++i) cin >> x[i];
string road_types;
cin >> road_types;
cout << solveCase(n, k, m, x, road_types) << endl;
}
return 0;
}

View File

@ -1,5 +1,5 @@
54
72
0
1
0
78
92
82
92
46

BIN
day2/U458258/chat Executable file

Binary file not shown.

View File

@ -1,7 +1,7 @@
#include <iostream>
#include <vector>
const int MOD = 1000000007;
const int MOD = 10'0000'0007;
// 快速幂计算
long long mod_pow(long long base, long long exp, long long mod) {