https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js

# 【BZOJ2059】Buying Feed 购买饲料

1

## 数据范围

1 ≤ V≤ 10000 , 1 ≤ E ≤ 500 , 1 ≤ N ≤ 500；

0 < Xi < E, 1 ≤ Fi ≤ 10000, 1 ≤ Ci ≤ 10^7

## 思路

sort一遍xi，然后三重循环dp，方程为dp[i][j]=min(dp[i][j],dp[i-1][j-k]+a[i].c*k+(a[i].x-a[i-1].x)*(j-k)*(j-k));

#include<bits/stdc++.h>
using namespace std;
struct node{int x,f,c;}a[505];
long long v,e,n,dp[505][10005];
bool cmp(node p,node q){return p.x<q.x;}
int ret=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9') {if (ch=='-') f=-f;ch=getchar();}
while (ch>='0'&&ch<='9') ret=ret*10+ch-'0',ch=getchar();
return ret*f;
}
int main(){
freopen("d.in","r",stdin);
freopen("d.out","w",stdout);
memset(dp,0x7f,sizeof(dp));
sort(a+1,a+n+1,cmp);
dp[0][0]=0;
for (int i=1;i<=n;i++)
for (int j=0;j<=v;j++)
for (int k=0;k<=a[i].f&&k<=j;k++)
dp[i][j]=min(dp[i][j],dp[i-1][j-k]+a[i].c*k+(a[i].x-a[i-1].x)*(j-k)*(j-k));
cout<<dp[n][v]+(e-a[n].x)*v*v<<endl;
return 0;
}

 1 #include<bits/stdc++.h>
2 using namespace std;
3 struct node{long long x,f,c;}a[505];
4 long long v,e,n,dp[505][10005];
5 bool cmp(node p,node q){return p.x<q.x;}
6 int main(){
7     freopen("d.in","r",stdin);
8     freopen("d.out","w",stdout);
9     cin>>v>>e>>n;
10     memset(dp,0x7f,sizeof(dp));
11     for (int i=1;i<=n;i++) cin>>a[i].x>>a[i].f>>a[i].c;
12     n++;
13     a[n]=(node){e,0,0};
14     sort(a+1,a+n+1,cmp);
15     dp[0][0]=0;
16     for (int i=1;i<=n;i++)
17     {
18         deque<long long>q;
19         for (int j=0;j<=v;j++)
20         {
21             while (!q.empty()&&j-q.front()>a[i-1].f) q.pop_front();
22             if (dp[i-1][j]!=0x7f)
23             {
24                 while (!q.empty()&&dp[i-1][q.back()]-a[i-1].c*q.back()>=dp[i-1][j]-a[i-1].c*j) q.pop_back();
25                 q.push_back(j);
26             }
27             int top=q.front();
28             if (!q.empty()) dp[i][j]=dp[i-1][top]-a[i-1].c*top+(a[i].x-a[i-1].x)*j*j+a[i-1].c*j;
29         }
30     }
31      cout<<dp[n][v]<<endl;
32     return 0;
33 }    

posted @ 2018-08-27 14:32  GREED_VI  阅读(...)  评论(...编辑  收藏