ACM-ICPC 2017北京

J. Pangu and Stones

大意: 给定$n$堆石子, $(n\le 100)$, 每次操作任选连续的至少$L$堆至多$R$堆合并, 代价为合并石子的总数, 求合并为$1$堆的最少花费.

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr ({printf("dp[%d][%d][%d]=%d\n",l,r,k,dp[l][r][k]),puts("");})
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, P2 = 998244353, INF = 5e8;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head



const int N = 110;
int n,L,R,a[N];
int dp[N][N][N];

int main() {
	while (~scanf("%d%d%d", &n, &L, &R)) {
		REP(i,1,n) scanf("%d",a+i),a[i]+=a[i-1];
		REP(d,1,n) REP(k,1,n) {
			for(int l=1,r=l+d-1;r<=n;++l,++r) { 
				int &ans = dp[l][r][k]=INF;
				if (k==1) {
					if (l==r) ans=0;
					else if (r-l+1<L) ;
					else if (r-l+1<=R) ans=a[r]-a[l-1];
					else {
						REP(i,L,R) REP(j,l,r-1) {
							ans = min(ans,dp[l][j][1]+dp[j+1][r][i-1]);
						}
						ans += a[r]-a[l-1];
					}
				}
				else {
					REP(i,l,r-1) ans = min(ans, dp[l][i][1]+dp[i+1][r][k-1]);
				}
			}
		}
		printf("%d\n",dp[1][n][1]>=INF?0:dp[1][n][1]);
	}
}

猜你喜欢

转载自www.cnblogs.com/uid001/p/10968900.html