1 条题解

  • 0
    @ 2021-6-15 14:22:10

    C++ :

    #pragma GCC optimize ("O2")
    #include <cstdio>
    #include <cassert>
    
    #define iter(i, n) for (int i = 1; i <= n; ++i)
    #define iter0(i, n) for (int i = 0; i < n; ++i)
    #define forw(i, a, b) for (int i = a; i <= b; ++i)
    
    const int inf = 1e9;
    
    #define NR 410
    
    const int D = 9;
    
    int n, L, R, f[D + 1][NR][NR], g[D + 1][NR][NR], F[D + 1][NR][NR], G[D + 1][NR][NR], a[NR];
    bool vis[NR][NR];
    
    inline void relax(int &x, int y) { if (x > y) x = y; }
    
    void dp(int l, int r) {
    	if (vis[l][r]) return;
    	vis[l][r] = true;
    
    	if (l == r) {
    		f[0][l][r] = 0;
    		g[0][l][r] = 0;
    		F[0][l][r] = (L & 1) ? 0 : inf;
    		G[0][l][r] = ((R - L) & 1) ? 0 : inf;
    
    		iter(k, D) {
    			g[k][l][r] = 0;
    			if (!(L >> k & 1)) relax(F[k][l][r], F[k - 1][l][r]);
    			relax(G[k][l][r], G[k - 1][l][r]);
    			if ((R - L) >> k & 1) G[k][l][r] = 0;
    		}
    		return;
    	}
    	
    	
    
    	forw(i, l, r) dp(l, i), dp(i, r);
    
    	iter(k, D) {
    		f[k][l][r] = inf;
    		g[k][l][r] = g[k - 1][l][r];
    		F[k][l][r] = inf;
    		G[k][l][r] = G[k - 1][l][r];
    		forw(i, l, r) {
    			relax(f[k][l][r], f[k - 1][l][i] + f[k - 1][i + 1][r]);
    			relax(g[k][l][r], g[k - 1][l][i] + g[k - 1][i + 1][r]);
    		}
    		assert(f[k][l][r] >= g[k][l][r]);
    	//	printf("f[%d][%d][%d]=%d\n", k, l, r);
    		//printf("G[%d][%d][%d]=%d\n", k, l, r);
    
    		if (L >> k & 1) {
    			forw(i, l, r) {
    				relax(F[k][l][r], F[k - 1][i + 1][r] + f[k][l][i]);
    			//	printf("!F %d %d %d %d %d\n", k, l, r, i, F[k - 1][i + 1][r]);
    			}
    		} else {
    			F[k][l][r] = F[k - 1][l][r];
    		}
    
    		if ((R - L) >> k & 1) {
    			forw(i, l, r)
    				relax(G[k][l][r], g[k][l][i] + G[k - 1][i + 1][r]);
    		}
    	}
    
    	int res = inf;
    	forw(i, l, r) {
    		relax(res, F[D][l][i] + G[D][i + 1][r]);
    	//	printf("[%d %d %d] %d\n", l, i, r, F[D][l][i] + G[D][i + 1][r]);
    	}
    	forw(i, l, r) res += a[i];
    	f[0][l][r] = res;
    	g[0][l][r] = res;
    
    	//printf("!%d %d %d\n", l, r, res);
    
    	if (L & 1) F[0][l][r] = res;
    	if ((R - L) & 1) G[0][l][r] = res;
    
    	iter(k, D) {
    		relax(g[k][l][r], g[k - 1][l][r]);
    		if (!(L >> k & 1)) relax(F[k][l][r], F[k - 1][l][r]);
    		relax(G[k][l][r], G[k - 1][l][r]);
    		if ((R - L) >> k & 1) relax(G[k][l][r], res);
    	}
    }
    
    int T;
    
    int main() {
    	//freopen("A.in", "r", stdin);
    	//freopen("A.out", "w", stdout);
    	scanf("%d", &T);
    	while (scanf("%d%d%d", &n, &L, &R) != EOF) {
    		iter(i, n) scanf("%d", &a[i]);
    		iter(i, n) iter(j, n) vis[i][j] = false;
    		forw(k, 0, D) forw(i, 0, n + 2) forw(j, 0, n + 2) {
    			f[k][i][j] = F[k][i][j] = inf;
    			g[k][i][j] = G[k][i][j] = (i > j ? 0 : inf);
    		}
    		if (!(L & 1)) {
    			forw(i, 0, n + 2) forw(j, 0, n + 2) F[0][i][j] = (i > j ? 0 : inf);
    			iter(k, D) {
    				if (!(L >> k & 1)) {
    					forw(i, 0, n + 2) forw(j, 0, n + 2) relax(F[k][i][j], F[k - 1][i][j]);
    				}
    			}
    		}
    		
    		dp(1, n);
    		printf("%d\n", f[0][1][n] > inf - 100 ? 0 : f[0][1][n]);
    	}
    	return 0;
    }
    
    
    • 1

    信息

    ID
    993
    时间
    20000ms
    内存
    512MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者