【HDOJ】【2829】Lawrence
DP/四边形不等式
做过POJ 1739 邮局那道题后就很容易写出动规方程:
dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价)
w(l,r)=∑ri=l∑rj=i+1a[i]∗a[j]w(l,r)=∑ri=l∑rj=i+1a[i]∗a[j]
那么就有 w(l,r+1)=w(l,r)+a[j]∗r∑i=la[i]w(l,r+1)=w(l,r)+a[j]∗r∑i=la[i]
所以:w[i][j]明显满足 关于区间包含的单调性
然后我们大胆猜想,小(bu)心(yong)证明,w[i][j]满足四边形不等式,所以这题就跟邮局那题一样了……
咳咳好吧作为一个有节操的人,我还是尝试着证明了一下(结果发现用来证明的时间比我写代码的时间要长……)
先把w(i,j)的定义搬下来:w(l,r)=r∑i=lr∑j=i+1a[i]∗a[j]w(l,r)=r∑i=lr∑j=i+1a[i]∗a[j]
形象一点来说就是:
对于i≤i′<j≤j′
中间的都是要算两次,剩下的部分:
(左)表示w(i,i'-1),[左]表示 ∑i′−1k=ia[k]
(中)表示w(i',j),[中]表示 ∑jk=i′a[k]
(右)表示w(j+1,j'),[右]表示 ∑j′k=j+1a[k]
w(i,j)+w(i′,j′)=(左)+[左]∗[中]+(右)+[右]∗[中]+(中)w(i,j′)+w(i′,j)=(左+右)+[左+右]∗[中]+(中)
其中[左+右]∗[中]=[左]∗[中]+[右]∗[中]
但(左+右)=(左)+(右)+[左]∗[右]
所以(左+右)>(左)+(右)
所以w(i,j)+w(i′,j′)≤w(i,j′)+w(i′,j)

1 //HDOJ 2829 2 #include<cmath> 3 #include<vector> 4 #include<cstdio> 5 #include<cstring> 6 #include<cstdlib> 7 #include<iostream> 8 #include<algorithm> 9 #define rep(i,n) for(int i=0;i<n;++i) 10 #define F(i,j,n) for(int i=j;i<=n;++i) 11 #define D(i,j,n) for(int i=j;i>=n;--i) 12 #define pb push_back 13 #define CC(a,b) memset(a,b,sizeof(a)) 14 using namespace std; 15 int getint(){ 16 int v=0,sign=1; char ch=getchar(); 17 while(!isdigit(ch)) {if(ch=='-') sign=-1; ch=getchar();} 18 while(isdigit(ch)) {v=v*10+ch-'0'; ch=getchar();} 19 return v*sign; 20 } 21 const int N=1010,INF=~0u>>2; 22 const double eps=1e-8; 23 #define debug 24 /*******************template********************/ 25 int dp[N][N],s[N][N],w[N][N],b[N],a[N],n,m; 26 27 int main(){ 28 while(scanf("%d%d",&n,&m)!=EOF && n){ 29 m++; 30 F(i,1,n) a[i]=getint(); 31 F(i,1,n){ 32 b[i]=a[i]; 33 w[i][i]=0; 34 F(j,i+1,n){ 35 w[i][j]=w[i][j-1]+a[j]*b[i]; 36 b[i]+=a[j]; 37 } 38 } 39 F(i,1,n) F(j,1,m) dp[j][i]=INF; 40 F(i,1,n){ 41 dp[1][i]=w[1][i]; 42 s[1][i]=0; 43 } 44 F(i,2,m){ 45 s[i][n+1]=n; 46 D(j,n,i) 47 F(k,s[i-1][j],s[i][j+1]) 48 if(dp[i-1][k]+w[k+1][j]<dp[i][j]){ 49 s[i][j]=k; 50 dp[i][j]=dp[i-1][k]+w[k+1][j]; 51 } 52 } 53 printf("%d\n",dp[m][n]); 54 } 55 return 0; 56 }
Lawrence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2448 Accepted Submission(s): 1093
You are to write a program to help Lawrence figure out how to best use his limited resources. You have some information from British Intelligence. First, the rail line is completely linear---there are no branches, no spurs. Next, British Intelligence has assigned a Strategic Importance to each depot---an integer from 1 to 100. A depot is of no use on its own, it only has value if it is connected to other depots. The Strategic Value of the entire railroad is calculated by adding up the products of the Strategic Values for every pair of depots that are connected, directly or indirectly, by the rail line. Consider this railroad:

Its Strategic Value is 4*5 + 4*1 + 4*2 + 5*1 + 5*2 + 1*2 = 49.
Now, suppose that Lawrence only has enough resources for one attack. He cannot attack the depots themselves---they are too well defended. He must attack the rail line between depots, in the middle of the desert. Consider what would happen if Lawrence attacked this rail line right in the middle:

The Strategic Value of the remaining railroad is 4*5 + 1*2 = 22. But, suppose Lawrence attacks between the 4 and 5 depots:

The Strategic Value of the remaining railroad is 5*1 + 5*2 + 1*2 = 17. This is Lawrence's best option.
Given a description of a railroad and the number of attacks that Lawrence can perform, figure out the smallest Strategic Value that he can achieve for that railroad.
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 大模型 Token 究竟是啥:图解大模型Token
· 35岁程序员的中年求职记:四次碰壁后的深度反思
· 继承的思维:从思维模式到架构设计的深度解析
· 如何在 .NET 中 使用 ANTLR4
· 后端思维之高并发处理方案
· BotSharp + MCP 三步实现智能体开发
· BotSharp 5.0 MCP:迈向更开放的AI Agent框架
· 5. RabbitMQ 消息队列中 Exchanges(交换机) 的详细说明
· 设计模式脉络
· 【ESP32】两种模拟 USB 鼠标的方法