标签:
4
3
2 2
5 8
6 1
8 7
56
#include<iostream> #include<cstdio> #include<string> #include<cstring> #include<algorithm> using namespace std; struct MM{ int d; int w; }; int n,v; long long lf[1005][1005],rf[1005][1005],sumw[1005]; MM mm[1005]; bool cmp(MM a,MM b){ return a.d < b.d; } int main(){ cin>>n>>v; for(int i = 1;i <= n;i++){ scanf("%d%d",&mm[i].d,&mm[i].w); } sort(mm+1,mm+1+n,cmp); for(int i = 0;i <= n;i++){ for(int j = 0;j <= n;j++){ lf[i][j] = rf[i][j] = 1087654321; } } for(int i = 1;i <= n;i++) sumw[i] = sumw[i-1] + mm[i].w; lf[v][v] = rf[v][v] = 0; for(int l = 1;l <= n - 1;l++){ for(int i = max(v - l,1);i <= v;i++){ int j = i + l; if(j > n) continue; lf[i][j] = min(lf[i+1][j] + (mm[i+1].d - mm[i].d) * (sumw[i] + sumw[n] - sumw[j]),rf[i+1][j] + (mm[j].d - mm[i].d) * (sumw[i] + sumw[n] - sumw[j])); rf[i][j] = min(rf[i][j-1] + (mm[j].d - mm[j-1].d) * (sumw[i-1] + sumw[n] - sumw[j-1]),lf[i][j-1] + (mm[j].d - mm[i].d) *(sumw[i-1] + sumw[n] - sumw[j-1])); } } cout<<(lf[1][n] < rf[1][n] ? lf[1][n] : rf[1][n]); return 0; }
标签:
原文地址:http://www.cnblogs.com/hyfer/p/5791412.html