记录编号 587062 评测结果 AAAAAAAAAA
题目名称 [NOI 2009]诗人小G 最终得分 100
用户昵称 Gravatar┭┮﹏┭┮ 是否通过 通过
代码语言 C++ 运行时间 1.177 s
提交时间 2024-03-11 16:53:40 内存使用 9.35 MiB
显示代码纯文本
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. //二分队列-决策单调性优化dp
  4. #define ll long long
  5. #define ld long double
  6. const int N = 1e5+10;
  7. const ll inf = 1e17;
  8. int t,n,p,l,r,la[N];
  9. ld f[N];
  10. ll L,s[N];
  11. char c[N][40];
  12. struct made{
  13. int p,l,r;
  14. }q[N];
  15. ld power(ld x,int y){
  16. ld ans = 1;
  17. while(y){
  18. if(y & 1)ans *= x;
  19. x *= x,y >>= 1;
  20. }return ans;
  21. }
  22. ll ab(ll x){return x < 0 ? -x : x;}
  23. ld W(int l,int r){return power(ab(s[r]-s[l]-L),p);}
  24. ld val(int l,int r){return f[l] + W(l,r);}
  25. int work(int i,int L,int R){
  26. while(L < R){
  27. int mid = L + R >> 1;
  28. if(val(i,mid) <= val(q[r].p,mid))R = mid;
  29. else L = mid + 1;
  30. }
  31. return L;
  32. }
  33. void prin(int x){
  34. if(la[x] == x)return;
  35. prin(la[x]);
  36. for(int i = la[x]+1;i <= x;i++)printf("%s",c[i]),printf(i==x?"\n":" ");
  37. }
  38. void solve(){
  39. scanf("%d%lld%d",&n,&L,&p);L++;
  40. for(int i = 1;i <= n;i++)scanf("%s",c[i]),s[i] = s[i-1] + strlen(c[i]) + 1;
  41. l = r = 1,q[1] = {0,1,n};
  42. for(int i = 1;i <= n;i++){
  43. while(l < r && q[l].r < i)l++;q[l].l = i;
  44. int j = q[l].p;la[i] = j;
  45. f[i] = f[j] + W(j,i);
  46. while(l < r && val(i,q[r].l) <= val(q[r].p,q[r].l))r--;
  47. j = work(i,q[r].l,q[r].r+1);
  48. if(j <= n)q[r].r = j-1,q[++r] = {i,j,n};
  49. }
  50. if(f[n] > 1e18)printf("Too hard to arrange\n");
  51. else printf("%lld\n",(ll)f[n]),prin(n);
  52. printf("--------------------\n");
  53. }
  54. int main(){
  55. freopen("noi09_poet.in","r",stdin);
  56. freopen("noi09_poet.out","w",stdout);
  57. scanf("%d",&t);
  58. while(t--)solve();
  59.  
  60. return 0;
  61.  
  62. }
  63.