This is a programing problem on codefores with a difficulty score of 1400.
It presents an intresting challenge that can be solved using the principle of greediness.
Initially, it's evident that we need to move each shape one by one and gather them with leaving any empty space between them.
Finding a bruteForce solution seems challenging.Perhaps, we might consider trying all possible segments along the line to find the answer....However, since the length of the segment is constant, we only need to try at most n segments.Then, by moving every element into the segment, we arrive at an O(n²) solution.
Returning the main point, assuming we already find the segment, there must be one point where all shapes to the left of this point are aligned to the left, and similarly, shapes to the righg are aligned to the right.Our task is to find this point.
https://codeforces.com/problemset/problem/1520/E
void solve(){
int n;
cin >> n;
string s;
cin >> s;
vector<long long> left(n + 1);
vector<long long> right(n + 1);
for (int i = 0, cur = 0; i < n; ++i){
left[i + 1] = left[i];
if (s[i] == '.'){
left[i + 1] += cur;
}
else{
cur ++;
}
}
for (int i = n - 1, cur = 0; i >= 0; --i){
right[i] = right[i + 1];
if (s[i] == '.'){
right[i] += cur;
}
else{
cur ++;
}
}
long long ans = (long long)1e14;
for (int i = 1; i <= n; ++i){
ans = min(ans, left[i] + right[i - 1]);
}
cout << ans << '\n';
}
标签:Sheep,right,cur,point,int,long,Arranging,left
From: https://www.cnblogs.com/yxcblogs/p/18041824