hdu 2829 Lawrence(四边形不等式优化dp)
T. E. Lawrence was a controversial figure during World War I. He was a British officer who served in the Arabian theater and led a group of Arab nationals in guerilla strikes against the Ottoman Empire. His primary targets were the railroads. A highly fictionalized version of his exploits was presented in the blockbuster movie, "Lawrence of Arabia".
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.
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<string> #include<vector> #include<stack> #include<bitset> #include<cstdlib> #include<cmath> #include<set> #include<list> #include<deque> #include<map> #include<queue> #define ll long long int using namespace std; inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;} int moth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int dir[4][2]={1,0 ,0,1 ,-1,0 ,0,-1}; int dirs[8][2]={1,0 ,0,1 ,-1,0 ,0,-1, -1,-1 ,-1,1 ,1,-1 ,1,1}; const int inf=0x3f3f3f3f; const ll mod=1e9+7; int a[1007]; int sum[1007]; int d[1007]; int dp[1007][1007]; //前i个点分成j组 int s[1007][1007]; int main(){ ios::sync_with_stdio(false); int n,m; while(cin>>n>>m){ if(!n&&!m) break; memset(dp,inf,sizeof(dp)); for(int i=1;i<=n;i++) cin>>a[i],sum[i]=sum[i-1]+a[i]; for(int i=1;i<=n;i++){ d[i]=a[i]*sum[i-1]+d[i-1]; } for(int i=1;i<=n;i++){ dp[i][1]=d[i]; s[i][1]=1; } for(int j=2;j<=m+1;j++){ s[n+1][j]=n; for(int i=n;i>=j;i--){ for(int k=s[i][j-1];k<=s[i+1][j];k++){ if(dp[i][j]>dp[k][j-1]+d[i]-(sum[i]-sum[k])*sum[k]-d[k]){ dp[i][j]=dp[k][j-1]+d[i]-(sum[i]-sum[k])*sum[k]-d[k]; s[i][j]=k; } } } } cout<<dp[n][m+1]<<endl; } return 0; }