codevs 1507酒厂选址
#include<cstdio> #include<cstdlib> using namespace std; int n; int dis[10010],a[10010],x[10010],y[10010]; int min(int p,int q) { if (p<q) return p; return q; } int main() { long long minn; scanf("%d",&n); minn=0; for (int i=1;i<=n;i++) scanf("%d%d",&a[i],&dis[i]); x[2]=dis[1]; y[n]=dis[n]; for (int i=3;i<=n;i++) x[i]=x[i-1]+dis[i-1]; for (int i=n-1;i>1;i--) y[i]=y[i+1]+dis[i]; x[1]=0; y[1]=x[n]+dis[n]; int tot=y[1]; for (int i=2;i<=n;i++) minn+=a[i]*min(x[i],y[i]); for (int i=2;i<=n;i++) { x[i]=0; y[i]=tot; long long k=0; for (int j=1;j<i;j++) { x[j]+=dis[i-1]; y[j]-=dis[i-1]; k+=a[j]*min(x[j],y[j]); } for (int j=i+1;j<=n;j++) { x[j]-=dis[i-1]; y[j]+=dis[i-1]; k+=a[j]*min(x[j],y[j]); } if (k<minn) minn=k; } printf("%d",minn); }
两个方向上的枚举。。似乎还写的麻烦了一点。。但是不知道哪里错了只能过9个点。。先放着。。
Sometimes it s the very people who no one imagines anything of. who do the things that no one can imagine.