洛谷P2514||bzoj2426 [HAOI2010]工厂选址
其实是个简单的贪心,然而不适合在脑子不清醒的时候做...看不懂题意续了1个小时
很容易发现应该枚举新建哪个发电厂,对于这种方案就是取其中b吨煤运到原来发电厂,取剩下(suma-b)吨煤运到新发电厂。首先假设全部都运到原来发电厂,然后把其中suma-b吨代价最小的改为运到新发电厂即可
1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #include<vector> 5 using namespace std; 6 #define fi first 7 #define se second 8 #define mp make_pair 9 #define pb push_back 10 typedef long long ll; 11 typedef unsigned long long ull; 12 ll m,b,n; 13 ll h[100100]; 14 ll a[100100],c[100100],d[100100],e[100100]; 15 ll s1,s2,s3,s; 16 ll ansx,ansy=0x3f3f3f3f3f3f3f3f; 17 bool c1(ll x,ll y) 18 { 19 return d[x]-c[x]<d[y]-c[y]; 20 } 21 int main() 22 { 23 ll i,j,t; 24 scanf("%lld%lld%lld%lld",&m,&b,&h[0],&n); 25 for(i=1;i<=m;++i) 26 { 27 scanf("%lld",&a[i]); 28 s+=a[i]; 29 } 30 s-=b; 31 for(i=1;i<=n;++i) 32 scanf("%lld",&h[i]); 33 for(i=1;i<=m;++i) 34 { 35 scanf("%lld",&c[i]); 36 s1+=a[i]*c[i]; 37 } 38 for(i=1;i<=n;++i) 39 { 40 for(j=1;j<=m;++j) 41 scanf("%lld",&d[j]); 42 for(j=1;j<=m;++j) 43 e[j]=j; 44 sort(e+1,e+m+1,c1); 45 s2=s1;s3=0; 46 for(j=1;j<=m;++j) 47 { 48 t=min(a[e[j]],s-s3); 49 s2+=t*(d[e[j]]-c[e[j]]); 50 s3+=t; 51 } 52 s2+=h[0];s2+=h[i]; 53 if(s2<ansy) {ansy=s2;ansx=i;} 54 } 55 printf("%lld\n%lld\n",ansx,ansy); 56 return 0; 57 }