package 蓝桥杯;
import java.util.Scanner;
public class VO动规_数字三角形 {
static int n;
static int[][] a=new int[101][101];
static int[][] MaxSum=new int[101][101];
public static void main(String[] args) {
Scanner reader=new Scanner(System.in);
int n=reader.nextInt();
for(int i=1;i<=n;i++)
for(int j=1;j<=i;j++) {
a[i][j]=reader.nextInt();
MaxSum[i][j]=-1;
}
System.out.println(maxSum(1,1));
}
static int maxSum(int i,int j) {
if(MaxSum[i][j]!=-1)
return MaxSum[i][j];
if(i==n)
MaxSum[i][j]=a[i][j];
else {
int x=maxSum(i+1, j);
int y=maxSum(i+1, j+1);
MaxSum[i][j]=Math.max(x, y)+a[i][j];
}
return MaxSum[i][j];
}
}
package 蓝桥杯;
import java.util.Scanner;
public class VO动规_数字三角形的递推和优化 {
static int n;
static int[][] a=new int[101][101];
static int[] MaxSum;
public static void main(String[] args) {
Scanner reader=new Scanner(System.in);
int n=reader.nextInt();
for(int i=1;i<=n;i++)
for(int j=1;j<=i;j++)
a[i][j]=reader.nextInt();
MaxSum=a[n];
for(int i=n-1;i>=1;i--)
for(int j=1;j<=i;j++)
MaxSum[j]=Math.max(MaxSum[j], MaxSum[j+1])+a[i][j];
System.out.println(MaxSum[1]);
}
}
package 蓝桥杯;
import java.util.Arrays;
import java.util.Scanner;
public class VO动规_最佳加法表达式 {
static int n,m;
static String numberstring;
static int[] a=new int[1005];
static int[][] num=new int[1005][1005];
static int[][] dp=new int[1005][1005];
public static void main(String[] args) {
Scanner reader=new Scanner(System.in);
while(reader.hasNext()) {
m=reader.nextInt();
numberstring = reader.next();
n=numberstring.length();
for(int i=1;i<=n;i++)
a[i]=numberstring.charAt(i-1)-'0';
for(int i=1;i<=n;i++) {
num[i][i]=a[i];
for(int j=i+1;j<=n;j++)
num[i][j]=num[i][j-1]*10+a[j];
}
for(int i=0;i<=m;i++)
Arrays.fill(dp[i],0x3f3f3f3f);
for(int j=1;j<=n;j++)
dp[0][j]=num[1][j];
for(int i=1;i<=m;i++)
for(int j=i;j<=n;j++)
for(int k=i;k<=j;k++)
dp[i][j]=Math.min(dp[i][j], dp[i-1][k]+num[k+1][j]);
System.out.println(dp[m][n]);
}
}
}
package 蓝桥杯;
import java.util.Scanner;
public class VO动规_最长公共子序列 {
public static void main(String[] args) {
int[][] maxlen;
Scanner reader=new Scanner(System.in);
while(reader.hasNext()) {
String str1=reader.next();
String str2=reader.next();
int length1=str1.length();
int length2=str2.length();
maxlen=new int[length1+1][length2+1];
for(int i=0;i<=length1;i++)
maxlen[i][0]=0;
for(int j=0;j<=length2;j++)
maxlen[0][j]=0;
for(int i=1;i<=length1;i++)
for(int j=1;j<=length2;j++)
if(str1.charAt(i-1)==str2.charAt(j-1))
maxlen[i][j]=maxlen[i-1][j-1]+1;
else
maxlen[i][j]=Math.max(maxlen[i][j-1], maxlen[i-1][j]);
System.out.println(maxlen[length1][length2]);
}
}
}
package 蓝桥杯;
import java.util.Arrays;
import java.util.Scanner;
public class VO动规_最长上升子序列 {
public static void main(String[] args) {
Scanner reader=new Scanner(System.in);
int n=reader.nextInt();
int[] a=new int[n+1],maxlen=new int[n+1];
for(int i=1;i<=n;i++){
a[i]=reader.nextInt();
maxlen[i]=1;
}
for(int i=2;i<=n;i++)
for(int j=1;j<i;j++)
if(a[i]>a[j])
maxlen[i]=Math.max(maxlen[i], maxlen[j]+1);
Arrays.sort(maxlen);
System.out.println(maxlen[n]);
}
}
package 蓝桥杯;
import java.util.Scanner;import javax.swing.plaf.basic.BasicInternalFrameTitlePane.MaximizeAction;
public class VO动规_滑雪 {
static int[][] Hight;
static int[][] Lenght;
static int row,col;
public static void main(String[] args) {
Scanner reader=new Scanner(System.in);
row=reader.nextInt();
col=reader.nextInt();
Hight=new int[row][col];
Lenght=new int[row][col];
for(int i=0;i<row;i++)
for(int j=0;j<col;j++) {
Hight[i][j]=reader.nextInt();
}
int Max=0;
for(int i=0;i<row;i++)
for(int j=0;j<col;j++) {
int temp=find(i,j,Integer.MAX_VALUE);
if(temp>Max) Max=temp;
}
System.out.println(Max);
}
static int find(int i,int j,int value) {
if(i<0||j<0||i>=row||j>=col||value<=Hight[i][j])
return 0;
if(Lenght[i][j]>0)
return Lenght[i][j];
int up=find(i-1, j,Hight[i][j]);
int down=find(i+1, j,Hight[i][j]);
int left=find(i, j-1,Hight[i][j]);
int right=find(i, j+1,Hight[i][j]);
Lenght[i][j]=my_max(up,down,left,right)+1;
return Lenght[i][j];
}
static int my_max(int a,int b,int c,int d) {
return Math.max(Math.max(a, b), Math.max(c, d));
}
}
package 蓝桥杯;
import java.util.Scanner;
public class VO动规ex_HelpJ {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
int S=0;
while(t>0)
{
int N=sc.nextInt();
int X=sc.nextInt();
int Y=sc.nextInt();
int MAX=sc.nextInt();
int[] X1=new int[N];
int[] X2=new int[N];
int[] H=new int[N];
for(int i=0;i<N;i++)
{
X1[i]=sc.nextInt();
X2[i]=sc.nextInt();
H[i]=sc.nextInt();
}
int[][] d=new int[N][2];
for(int i=0;i<N-1;i++)
{
for(int j=0;j<N-1-i;j++)
{
if(H[j]>H[j+1])
{
int temp;
temp=H[j];
H[j]=H[j+1];
H[j+1]=temp;
temp=X1[j];
X1[j]=X1[j+1];
X1[j+1]=temp;
temp=X2[j];
X2[j]=X2[j+1];
X2[j+1]=temp;
}
}
}
d[0][0]=H[0];
d[0][1]=H[0];
for(int i=1;i<N;i++)
{
int k=i-1;
while(k>=0&&H[i]-H[k]<=MAX)
{
if(X1[i]>=X1[k]&&X1[i]<=X2[k])
{
d[i][0]=H[i]-H[k]+min(X1[i]-X1[k]+d[k][0],X2[k]-X1[i]+d[k][1]);
break;
}
k--;
}
if(k!=-1&&H[i]-H[k]>MAX)
{
d[i][0]=999999;
}
if(k==-1)
{
if(H[i]<=MAX)
d[i][0]=H[i];
}
if(k==-1&&H[i]>MAX)
{
d[i][0]=999999;
}
int p=i-1;
while(p>=0&&H[i]-H[p]<=MAX)
{
if(X2[i]>=X1[p]&&X2[i]<=X2[p])
{
d[i][1]=H[i]-H[p]+min(X2[i]-X1[p]+d[p][0],X2[p]-X2[i]+d[p][1]);
break;
}
p--;
}
if(p!=-1&&H[i]-H[p]>MAX)
{
d[i][1]=999999;
}
if(p==-1)
{
if(H[i]<=MAX)
d[i][1]=H[i];
}
if(p==-1&&H[i]>MAX)
{
d[i][1]=999999;
}
}
int q=N-1;
while(q>=0)
{
if(X>=X1[q]&&X<=X2[q])
{
S=min(Y-H[q]+X-X1[q]+d[q][0],Y-H[q]+X2[q]-X+d[q][1]);
break;
}
q--;
}
if(q==-1)
{
S=Y;
}
System.out.println(S);
t--;
}
}
public static int min(int a,int b){
if(a<b)
return a;
else
return b;
}
}