解題思路
用任意升降key最小化兩旋律的面積差。
用雙指標同步掃描兩條按拍數分段的旋律,每段重疊時間w的音高差d固定,因此對升降key t的貢獻為w×abs(d−t)。
將所有差值d依序排序,取累積時間首次達總時間一半的加權中位數t;絕對值和在中位數處最小,因往右移的左側增加量與右側減少量恰以權重相抵。
用精確有理數處理小數或分數拍子,最後sum w×abs(d−t)以最簡分數輸出。
音符數A、B,時間 O((A+B) log(A+B))次精確有理數操作,空間 O(A+B),另計分數位數成本。
Step-by-step animation
兩個半拍差值1與3的加權中位數示例
步驟 1 / 3
步驟 1
示例兩段音高差為1與3,各重疊1/2拍。
(d=1,w=1/2)
(d=3,w=1/2)
- 步驟
- 1
#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
using boost::multiprecision::cpp_int;
using ll = long long;
#include <boost/rational.hpp>
using Q = boost::rational<cpp_int>;
Q rat(cpp_int a, cpp_int b) {
if (b < 0) {
a = -a;
b = -b;
}
return Q(a, b);
}
cpp_int abig(cpp_int a) {
return a < 0 ? -a : a;
}
cpp_int ten(int n) {
cpp_int a = 1;
while (n--)
a *= 10;
return a;
}
Q dec(string s) {
auto slash = s.find('/');
if (slash != string::npos)
return Q(cpp_int(s.substr(0, slash)), cpp_int(s.substr(slash + 1)));
bool neg = !s.empty() && s[0] == '-';
if (s[0] == '-' || s[0] == '+')
s.erase(s.begin());
auto p = s.find('.');
int k = p == string::npos ? 0 : s.size() - p - 1;
if (p != string::npos)
s.erase(p, 1);
size_t z = s.find_first_not_of('0');
cpp_int a = z == string::npos ? cpp_int(0) : cpp_int(s.substr(z));
return Q(neg ? -a : a, ten(k));
}
string fixedq(Q q, int digits) {
cpp_int a = q.numerator(), b = q.denominator(), scale = ten(digits);
bool neg = a < 0;
a = abig(a);
cpp_int k = (2 * a * scale + b) / (2 * b);
string s = k.convert_to<string>();
if (digits) {
if (s.size() <= digits)
s = string(digits + 1 - s.size(), '0') + s;
s.insert(s.size() - digits, ".");
}
return (neg && k != 0 ? "-" : "") + s;
}
string finiteq(Q q) {
cpp_int a = q.numerator(), b = q.denominator();
bool neg = a < 0;
a = abig(a);
cpp_int whole = a / b, r = a % b;
string s = whole.convert_to<string>();
if (r != 0) {
s += '.';
while (r != 0) {
r *= 10;
cpp_int z = r / b;
s += char('0' + z.convert_to<int>());
r %= b;
}
}
return (neg ? "-" : "") + s;
}
vector<pair<int, Q>> parse(string s) {
vector<pair<int, Q>> v;
size_t p = 0;
while ((p = s.find('<', p)) != string::npos) {
size_t q = s.find(',', p), e = s.find('>', q);
string t = s.substr(q + 1, e - q - 1);
t.erase(remove_if(t.begin(), t.end(),
[](char c) { return isspace((unsigned char)c); }),
t.end());
v.push_back({stoi(s.substr(p + 1, q - p - 1)), dec(t)});
p = e + 1;
}
return v;
}
int main() {
string s, t;
getline(cin, s);
getline(cin, t);
auto a = parse(s), b = parse(t);
int i = 0, j = 0;
vector<pair<int, Q>> v;
Q total(0);
while (i < (int)a.size() && j < (int)b.size()) {
Q w = min(a[i].second, b[j].second);
v.push_back({a[i].first - b[j].first, w});
total += w;
a[i].second -= w;
b[j].second -= w;
if (a[i].second == 0)
i++;
if (b[j].second == 0)
j++;
}
sort(v.begin(), v.end(), [](auto a, auto b) { return a.first < b.first; });
Q sum(0);
int med = 0;
for (auto [d, w] : v) {
sum += w;
if (2 * sum >= total) {
med = d;
break;
}
}
Q ans(0);
for (auto [d, w] : v)
ans += abs(d - med) * w;
cout << ans.numerator() << '/' << ans.denominator() << '\n';
}
先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。