[HAOI2009] 巧克力

  贪心还是比较好想的。

  注意竖切次数为上下分离的巧克力块个数,横切次数为左右分离的巧克力个数。

         竖切增加左右分离的巧克力个数,横切增加上下分离的巧克力个数。

// q.c

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
const int M=20000+10;
int n,m; long long ans;
struct Data {
	long long w,f;
	Data():w(0),f(0) {}
	bool operator < (const Data &A) const {
		return w>A.w;
	}
}a[M];
int main() {
	freopen("chocolate.in","r",stdin);
	freopen("chocolate.out","w",stdout);
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n-1;i++) scanf("%lld",&a[i].w),a[i].f=0;
	for(int i=1;i<=m-1;i++) scanf("%lld",&a[i+n-1].w),a[i+n-1].f=1; // a[i+n-1].f写成a[i].f调了十分钟.
	sort(a+1,a+n+m-1);
	for(int i=1,x=1,y=1;i<=n+m-2;i++) {
		if(!a[i].f) ans+=a[i].w*y,x++; // 横切.
		else ans+=a[i].w*x,y++; // 竖切.
	}
	printf("%lld\n",ans);
	return 0;
}

 

posted @ 2018-04-12 08:34  qjs12  阅读(129)  评论(0编辑  收藏  举报