CodeForces 148E Porcelain dp+背包(水

题目链接:点击打开链接

题意:

给定1个n层书架,1共取m本书。

下面n行给出每层书的价值。

每次可以取任意1层的最左端或最右真个1本书。

问能取得的最大价值。

思路:

1、明显是先求出对每层任取任意本书能取得的最大价值。

2、然后背包1下。

1:

对1层书任意j本,那末1定是从左端取k本,右端取 j-k本,求个前缀和然后枚举 j和k便可。每层n^2的dp

2:

分组背包。

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Queue;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class Main {
int m, n;
int[] sum = new int[N], num = new int[N];
int[][] dp = new int[N][N], h = new int[2][M];
void work() throws Exception{
n = Int(); m = Int();
for(int i = 1; i <= n; i++){
num[i] = Int();
sum[0] = 0;
for(int j = 1; j <= num[i]; j++){ sum[j] = sum[j⑴]+Int(); }
dp[i][0] = 0;
for(int j = 1; j <= num[i]; j++){
dp[i][j] = 0;
for(int k = 0; k <= j; k++)
dp[i][j] = max(dp[i][j], sum[k] + sum[num[i]] – sum[num[i]-j+k]);
}
}
int cur = 0, old = 1;
for(int i = 0; i <= m; i++)h[cur][i] = 0;
for(int i = 1; i <= n; i++)
{
cur ^= 1; old ^= 1;
for(int j = 0; j <= m; j++)h[cur][j] = h[old][j];
for(int j = 0; j <= num[i]; j++)
{
for(int k = j; k <= m; k++)
h[cur][k] = max(h[cur][k], h[old][k-j]+dp[i][j]);
}
}
out.println(h[cur][m]);
}

public static void main(String[] args) throws Exception{
Main wo = new Main();
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
// in = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
// out = new PrintWriter(new File("output.txt"));
wo.work();
out.close();
}

static int N = 101;
static int M = 10001;
DecimalFormat df=new DecimalFormat("0.0000");
static int inf = (int)1e9;
static long inf64 = (long) 1e18;
static double eps = 1e⑻;
static double Pi = Math.PI;
static int mod = (int)1e9 + 7 ;

private String Next() throws Exception{
while (str == null || !str.hasMoreElements())
str = new StringTokenizer(in.readLine());
return str.nextToken();
}
private int Int() throws Exception{
return Integer.parseInt(Next());
}
private long Long() throws Exception{
return Long.parseLong(Next());
}
private double Double() throws Exception{
return Double.parseDouble(Next());
}
StringTokenizer str;
static Scanner cin = new Scanner(System.in);
static BufferedReader in;
static PrintWriter out;
/*
class Edge{
int from, to, dis, nex;
Edge(){}
Edge(int from, int to, int dis, int nex){
this.from = from;
this.to = to;
this.dis = dis;
this.nex = nex;
}
}
Edge[] edge = new Edge[M<<1];
int[] head = new int[N];
int edgenum;
void init_edge(){for(int i = 0; i < N; i++)head[i] = ⑴; edgenum = 0;}
void add(int u, int v, int dis){
edge[edgenum] = new Edge(u, v, dis, head[u]);
head[u] = edgenum++;
}/**/
int upper_bound(int[] A, int l, int r, int val) {// upper_bound(A+l,A+r,val)-A;
int pos = r;
r–;
while (l <= r) {
int mid = (l + r) >> 1;
if (A[mid] <= val) {
l = mid + 1;
} else {
pos = mid;
r = mid – 1;
}
}
return pos;
}

int Pow(int x, int y) {
int ans = 1;
while (y > 0) {
if ((y & 1) > 0)
ans *= x;
y >>= 1;
x = x * x;
}
return ans;
}
double Pow(double x, int y) {
double ans = 1;
while (y > 0) {
if ((y & 1) > 0)
ans *= x;
y >>= 1;
x = x * x;
}
return ans;
}
int Pow_Mod(int x, int y, int mod) {
int ans = 1;
while (y > 0) {
if ((y & 1) > 0)
ans *= x;
ans %= mod;
y >>= 1;
x = x * x;
x %= mod;
}
return ans;
}
long Pow(long x, long y) {
long ans = 1;
while (y > 0) {
if ((y & 1) > 0)
ans *= x;
y >>= 1;
x = x * x;
}
return ans;
}
long Pow_Mod(long x, long y, long mod) {
long ans = 1;
while (y > 0) {
if ((y & 1) > 0)
ans *= x;
ans %= mod;
y >>= 1;
x = x * x;
x %= mod;
}
return ans;
}

int gcd(int x, int y){
if(x>y){int tmp = x; x = y; y = tmp;}
while(x>0){
y %= x;
int tmp = x; x = y; y = tmp;
}
return y;
}
int max(int x, int y) {
return x > y ? x : y;
}

int min(int x, int y) {
return x < y ? x : y;
}

double max(double x, double y) {
return x > y ? x : y;
}

double min(double x, double y) {
return x < y ? x : y;
}

long max(long x, long y) {
return x > y ? x : y;
}

long min(long x, long y) {
return x < y ? x : y;
}

int abs(int x) {
return x > 0 ? x : -x;
}

double abs(double x) {
return x > 0 ? x : -x;
}

long abs(long x) {
return x > 0 ? x : -x;
}

boolean zero(double x) {
return abs(x) < eps;
}
double sin(double x){return Math.sin(x);}
double cos(double x){return Math.cos(x);}
double tan(double x){return Math.tan(x);}
double sqrt(double x){return Math.sqrt(x);}
}

波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!

波比源码 » CodeForces 148E Porcelain dp+背包(水

发表评论

Hi, 如果你对这款模板有疑问,可以跟我联系哦!

联系站长
赞助VIP 享更多特权,建议使用 QQ 登录
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡