JZOJ 4210. 【五校联考1day1】我才不是萝莉控呢

题目

    

Description

小Y:“小R 你是萝莉控吗。”小R:“...”
为了避免这个尴尬的话题,小R 决定给小Y 做一道题。
有一个长度为n 的正整数数组A,满足Ai >= Ai+1,现在构造一个数组B,令Bi =
现在,有一个n * n 的网格图,左下角坐标是(1, 1),右上角坐标是(n, n)。有一个小SB正在坐标为(n, 1) 的位置,每一时刻,如果他现在在(x, y),他可以选择走到(x 􀀀-1,y + 1) 或者(x, (y + 1) div 2),如果选择后者,他要支付Bx的代价。
现在他想走到(1, 1),你可以告诉他他支付的代价最少是多少吗?注意在任何时候他都不能离开这个网格图。
 

Input

第一行输入一个正整数T 表示数据组数。
对于每组数据,第一行是一个整数n,接下来一行n 个整数表示数组A。

Output

对于每组数据,输出一个整数表示答案。
 

Sample Input

1
3
1 1 1

Sample Output

5
样例解释:
选择的路径可以是:(3, 1)->(2, 2)->(2, 1)->(1, 2)->(1, 1)
 

Data Constraint

对于30% 的数据,n <= 10
对于50% 的数据,n <=1000
对于100% 的数据,n<= 10^5,1 <= T<= 10,1 <= Ai<= 10^4

分析

      不懂 orz 哈夫曼树  合并果子???
      50%

      DP   50

      

 1 #include<iostream>
 2 #include<cstring>
 3 using namespace std;
 4 int a[100001],b[100001];
 5 int f[10001][10001];
 6 int main ()
 7 {
 8     int T,n;
 9     cin>>T;
10     for (int i=1;i<=T;i++)
11     {
12         cin>>n;
13         memset(f,0x3f,sizeof(f));
14         for (int i=1;i<=n;i++)
15             cin>>a[i];
16         b[n]=a[n];
17         for (int i=n-1;i>=1;i--)
18             b[i]=b[i+1]+a[i];
19         f[n][1]=0;
20         for (int i=n;i>=1;i--)
21         {
22             for (int j=1;j<=n;j++)
23             {
24                 if (i+1<=n&&j-1>=1) f[i][j]=min(f[i][j],f[i+1][j-1]);
25                 if (j*2-1<=n)f[i][j]=min(f[i][j],f[i][j*2-1]+b[i]);
26                 if (j*2<=n) f[i][j]=min(f[i][j],f[i][j*2]+b[i]);
27             }
28             for (int j=n;j>=1;j--)
29             {
30                 if (i+1<=n&&j-1>=1) f[i][j]=min(f[i][j],f[i+1][j-1]);
31                 if (j*2<=n)f[i][j]=min(f[i][j],f[i][j*2]+b[i]);
32                 if (j*2-1<=n) f[i][j]=min(f[i][j],f[i][j*2-1]+b[i]);
33             }
34         }
35         cout<<f[1][1]<<endl;
36     }
37     
38 }
View Code

 

代码

 1 #include <iostream>
 2 #include <queue>
 3 #include <cstdio>
 4 using namespace std;
 5 int T,n;
 6 long long ans;
 7 priority_queue<int,vector<int>,greater<int> >Q;
 8 int main()
 9 {
10     scanf("%d",&T);
11     while (T--)
12     {
13         scanf("%d",&n),ans=0;
14         for (int i=1,x;i<=n;i++) scanf("%d",&x),Q.push(x);
15         for (int i=n,x;i>1;i--) x=Q.top(),Q.pop(),x+=Q.top(),Q.pop(),ans+=x,Q.push(x);
16         printf("%lld\n",ans);
17         while (!Q.empty()) Q.pop();
18     }
19 }

 

posted @ 2019-01-21 21:13  Melted_czj  阅读(181)  评论(0编辑  收藏  举报
body { background-color:whitesmoke; } // 修改背景颜色为半透明 #home,#sideBarMain>div,#blog-sidecolumn>div>div,.catListView{ background-color:rgba(255,255,255,0); } // 修改其他边框的颜色