2013
11-10

# Naptime

Goneril is a very sleep-deprived cow. Her day is partitioned into N (3 <= N <= 3,830) equal time periods but she can spend only B (2 <= B < N) not necessarily contiguous periods in bed. Due to her bovine hormone levels, each period has its own utility U_i (0 <= U_i <= 200,000), which is the amount of rest derived from sleeping during that period. These utility values are fixed and are independent of what Goneril chooses to do, including when she decides to be in bed.

With the help of her alarm clock, she can choose exactly which periods to spend in bed and which periods to spend doing more critical items such as writing papers or watching baseball. However, she can only get in or out of bed on the boundaries of a period.

She wants to choose her sleeping periods to maximize the sum of the utilities over the periods during which she is in bed. Unfortunately, every time she climbs in bed, she has to spend the first period falling asleep and gets no sleep utility from that period.

The periods wrap around in a circle; if Goneril spends both periods N and 1 in bed, then she does get sleep utility out of period 1.

What is the maximum total sleep utility Goneril can achieve?

* Line 1: Two space-separated integers: N and B

* Lines 2..N+1: Line i+1 contains a single integer, U_i, between 0 and 200,000 inclusive

The day is divided into 5 periods, with utilities 2, 0, 3, 1, 4 in that order. Goneril must pick 3 periods.

5 3
2
0
3
1
4

6

INPUT DETAILS:

The day is divided into 5 periods, with utilities 2, 0, 3, 1, 4 in that order. Goneril must pick 3 periods.

OUTPUT DETAILS:

Goneril can get total utility 6 by being in bed during periods 4, 5, and 1, with utilities 0 [getting to sleep], 4, and 2 respectively.

/* @author: */
import java.util.Scanner;
import java.util.Arrays;
public class Main{

static  int maxs=Integer.MAX_VALUE/3;
int f[][][]=new int[2][3831][2];
int g[][][]=new int[2][3831][2];
int a[]=new int[3831];
int n,m;
int maxv(int x,int y){
return x>y?x:y;
}
void dp(){
int pre=0,cur=1;
for(int i=0;i<=m;i++) f[1][i][0]=f[1][i][1]=g[1][i][0]=g[1][i][1]=-maxs;
f[1][0][0]=f[1][1][1]=0;
g[1][0][0]=0; g[1][1][1]=a[1];
for(int i=2;i<=n;i++){
pre=cur; cur=(cur+1)%2;
f[cur][0][1]=-maxs;
g[cur][0][1]=-maxs;
for(int j=1;j<=m;j++){
f[cur][j][0]=maxv(f[pre][j][0],f[pre][j][1]);
f[cur][j][1]=maxv(f[pre][j-1][0],f[pre][j-1][1]+a[i]);
g[cur][j][0]=maxv(g[pre][j][0],g[pre][j][1]);
g[cur][j][1]=maxv(g[pre][j-1][0],g[pre][j-1][1]+a[i]);
}
}
int result=maxv(maxv(f[cur][m][0],f[cur][m][1]),g[cur][m][1]);
System.out.printf("%d\n",result);
}

void init(){
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
m=sc.nextInt();

for(int i=1;i<=n;i++)
a[i]=sc.nextInt();
}
public static void main(String args[]){
Main m=new Main();
m.init();
m.dp();
}
}

1. “再把所有不和该节点相邻的节点着相同的颜色”，程序中没有进行不和该节点相邻的其他节点是否相邻进行判断。再说求出来的也不一样是颜色数最少的

2. “再把所有不和该节点相邻的节点着相同的颜色”，程序中没有进行不和该节点相邻的其他节点是否相邻进行判断。再说求出来的也不一样是颜色数最少的