P1912 [NOI2009]诗人小G

P1912 [NOI2009]诗人小G

思路:

平行四边形不等式优化dp

因为f(j, i) = abs(sum[i]-sum[j]+i-j-1-l)^p 满足平行四边形不等式

j < i

f(j, i+1) + f(j+1, i) >= f(j, i) + f(j+1, i+1)

所以dp[i]具有决策单调性

代码:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define y1 y11
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
#define LD long double
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pli pair<LL, int>
#define pii pair<int, int>
#define piii pair<int, pii>
#define pdd pair<long double, long double>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head const int N = 1e5 + ;
const LL UP = 1e18;
string s[N];
int T, n, l, p, sum[N], pre[N];
LD dp[N];
vector<int> vc;
struct Node {
int l, r, j;
};
deque<Node> q;
inline LD Pow(int x) {
LD res = ;
for (int i = ; i <= p; ++i) res *= x;
return res;
}
inline LD cal(int j, int i) {
return dp[j] + Pow(abs(sum[i]-sum[j]+i-j--l));
}
inline int srch(int l, int r, int i, int j) {
int m = l+r >> ;
while(l < r) {
if(cal(i, m) <= cal(j, m)) r = m;
else l = m+;
m = l+r >> ;
}
return m;
}
int main() {
fio;
cin >> T;
while(T--) {
cin >> n >> l >> p;
for (int i = ; i <= n; ++i) cin >> s[i];
for (int i = ; i <= n; ++i) sum[i] = sum[i-] + s[i].size();
while(!q.empty()) q.pop_back();
q.push_back({, n, });
dp[] = ;
for (int i = ; i <= n; ++i) {
if(q.front().r == i-) q.pop_front();
else q.front().l = i;
pre[i] = q.front().j;
dp[i] = cal(q.front().j, i);
int pos = -;
while(!q.empty()) {
if(cal(i, q.back().l) <= cal(q.back().j, q.back().l)) {
pos = q.back().l;
q.pop_back();
}
else {
if(cal(i, q.back().r) <= cal(q.back().j, q.back().r)) {
pos = srch(q.back().l, q.back().r, i, q.back().j);
q.back().r = pos-;
q.push_back({pos, n, i});
}
else {
if(~pos) q.push_back({pos, n, i});
break;
}
}
} }
if(dp[n] > UP) cout << "Too hard to arrange\n";
else {
cout <<fixed<<setprecision()<< dp[n] << "\n";
int now = n;
while(now) {
vc.pb(now);
now = pre[now];
}
vc.pb();
reverse(vc.begin(), vc.end());
for (int i = ; i < vc.size(); ++i) {
for(int j = vc[i-]+; j <= vc[i]; ++j) {
cout << s[j];
if(j != vc[i]) cout << " ";
else cout << "\n";
}
}
vc.clear();
}
cout << "--------------------\n";
}
return ;
}
上一篇:vue中文字向上滚动效果


下一篇:函数putText()在图片上写文字