F - Earn to Advance

 

 

 解题思路

  • 由于对于一点不知道后面得花费,所以无法决策当前是否要停下赚钱或要停下多久
  • 考虑一点(i,j),可以由其左上方的所有点(x,y)到达
  • 所以从(i,j)往前推,得出(x,y)(i,j)的总花费
  • 然后考虑从(x,y)之后不赚钱直接到(i,j)最终所用次数和剩余钱
  • 若存在,在(x,y)后面点(x2,y2)赚钱更优的情况,则会被从(x2,y2)(i,j)情况包含
  • 因为处理(i,j)时,(x2,y2)已经是最优状态
  • (x2,y2)状态的更新必然考虑了从(x,y)(x2,y2)
  • 至于最优状态的选择
  • 次数越少越好,在次数相同的情况下剩余钱越多越好
  • 由于若次数少,可以通过增加次数换增加钱,所以次数的优先级更高
  • O(n^4)Dp
import java.io.*;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Objects;
import java.util.PriorityQueue;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.Vector;








//implements Runnable
public class Main{
	static long md=(long)20020219;
	static long Linf=Long.MAX_VALUE/2;
	static int inf=Integer.MAX_VALUE/2;
	static int M=10000;
	static int N=300;
	static int n=0;
	static int m=0;
	static int k=0;
	
	static
	class Node{
		long x;//time
		long y;//money
		public Node() {
			x=Linf;
			y=0;
		}
		void get(long u,long v) {
			x=u;
			y=v;
		}
	}

	public static void main(String[] args) throws Exception{
		AReader input=new AReader();
		PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));	
		int n=input.nextInt();
		long[][] p=new long[n+1][n+1];
		long[][] r=new long[n+1][n+1];
		long[][] d=new long[n+1][n+1];
		for(int i=1;i<=n;++i) {
			for(int j=1;j<=n;++j) {
				p[i][j]=input.nextLong();
			}
		}
		for(int i=1;i<=n;++i) {
			for(int j=1;j<n;++j) {
				r[i][j]=input.nextLong();
			}
		}
		for(int i=1;i<n;++i) {
			for(int j=1;j<=n;++j) {
				d[i][j]=input.nextLong();
			}
		}
		Node[][] f=new Node[n+1][n+1];
		for(int i=1;i<=n;++i) {
			for(int j=1;j<=n;++j) {
				f[i][j]=new Node();
			}
		}
		f[1][1].get(0, 0);;
		long[][] dis=new long[n+1][n+1];
		for(int i=1;i<=n;++i) {
			for(int j=1;j<=n;++j) {
				for(int x=1;x<=i;++x) {
					Arrays.fill(dis[x], Linf);
				}
				dis[i][j]=0;
				for(int x=i;x>=1;--x) {
					for(int y=j;y>=1;--y) {
						if(x<i)	dis[x][y]=Math.min(dis[x][y], d[x][y]+dis[x+1][y]);
						if(y<j) dis[x][y]=Math.min(dis[x][y], r[x][y]+dis[x][y+1]);
						long c=Math.max(0, dis[x][y]-f[x][y].y+(p[x][y]-1))/p[x][y];//上取整
						long t=f[x][y].x+c+i-x+j-y;
						long m=f[x][y].y+p[x][y]*c-dis[x][y];
						if(t<f[i][j].x||t==f[i][j].x&&m>f[i][j].y) {
							f[i][j].get(t, m);
						}
					}
				}
			}
		}
		out.print(f[n][n].x);
		out.flush();
	    out.close();
	}
//	public static final void main(String[] args) throws Exception {
//		  new Thread(null, new Main(), "线程名字", 1 << 27).start();
//	}
//		@Override
//		public void run() {
//			try {
//				//原本main函数的内容
//				solve();
//
//			} catch (Exception e) {
//			}
//		}
//	static 
//	class Node{
//		int x;
//		int y;
//		public Node(int u,int v) {
//			x=u;
//			y=v;
//		}
//		@Override
//	    public boolean equals(Object o) {
//	        if (this == o) return true;
//	        if (o == null || getClass() != o.getClass()) return false;
//	        Node may = (Node) o;
//	        return x == may.x && y==may.y;
//	    }
//
//	    @Override
//	    public int hashCode() {
//	        return Objects.hash(x, y);
//	    }
//	}
		static
		class AReader{ 
		    BufferedReader bf;
		    StringTokenizer st;
		    BufferedWriter bw;

		    public AReader(){
		        bf=new BufferedReader(new InputStreamReader(System.in));
		        st=new StringTokenizer("");
		        bw=new BufferedWriter(new OutputStreamWriter(System.out));
		    }
		    public String nextLine() throws IOException{
		        return bf.readLine();
		    }
		    public String next() throws IOException{
		        while(!st.hasMoreTokens()){
		            st=new StringTokenizer(bf.readLine());
		        }
		        return st.nextToken();
		    }
		    public char nextChar() throws IOException{
		        //确定下一个token只有一个字符的时候再用
		        return next().charAt(0);
		    }
		    public int nextInt() throws IOException{
		        return Integer.parseInt(next());
		    }
		    public long nextLong() throws IOException{
		        return Long.parseLong(next());
		    }
		    public double nextDouble() throws IOException{
		        return Double.parseDouble(next());
		    }
		    public float nextFloat() throws IOException{
		        return Float.parseFloat(next());
		    }
		    public byte nextByte() throws IOException{
		        return Byte.parseByte(next());
		    }
		    public short nextShort() throws IOException{
		        return Short.parseShort(next());
		    }
		    public BigInteger nextBigInteger() throws IOException{
		        return new BigInteger(next());
		    }
		    public void println() throws IOException {
		        bw.newLine();
		    }
		    public void println(int[] arr) throws IOException{
		        for (int value : arr) {
		            bw.write(value + " ");
		        }
		        println();
		    }
		    public void println(int l, int r, int[] arr) throws IOException{
		        for (int i = l; i <= r; i ++) {
		            bw.write(arr[i] + " ");
		        }
		        println();
		    }
		    public void println(int a) throws IOException{
		        bw.write(String.valueOf(a));
		        bw.newLine();
		    }
		    public void print(int a) throws IOException{
		        bw.write(String.valueOf(a));
		    }
		    public void println(String a) throws IOException{
		        bw.write(a);
		        bw.newLine();
		    }
		    public void print(String a) throws IOException{
		        bw.write(a);
		    }
		    public void println(long a) throws IOException{
		        bw.write(String.valueOf(a));
		        bw.newLine();
		    }
		    public void print(long a) throws IOException{
		        bw.write(String.valueOf(a));
		    }
		    public void println(double a) throws IOException{
		        bw.write(String.valueOf(a));
		        bw.newLine();
		    }
		    public void print(double a) throws IOException{
		        bw.write(String.valueOf(a));
		    }
		    public void print(char a) throws IOException{
		        bw.write(String.valueOf(a));
		    }
		    public void println(char a) throws IOException{
		        bw.write(String.valueOf(a));
		        bw.newLine();
		    }
		}
	}

		

	

 

相关推荐

  1. F. Maximum White Subtree

    2024-03-11 03:16:04       32 阅读
  2. STM32<span style='color:red;'>F</span>103

    STM32F103

    2024-03-11 03:16:04      49 阅读
  3. 01矩阵(课程F)

    2024-03-11 03:16:04       42 阅读
  4. codeforces 1676F

    2024-03-11 03:16:04       41 阅读
  5. taskkill /F /PID 1764

    2024-03-11 03:16:04       26 阅读
  6. 问题 F: 分巧克力

    2024-03-11 03:16:04       41 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-11 03:16:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-11 03:16:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-11 03:16:04       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-11 03:16:04       20 阅读

热门阅读

  1. 【c++】模板的使用

    2024-03-11 03:16:04       25 阅读
  2. 设计模式 | 单例模式 | 懒汉&饿汉

    2024-03-11 03:16:04       22 阅读
  3. python的类修饰器

    2024-03-11 03:16:04       25 阅读
  4. LeetCode1547. Minimum Cost to Cut a Stick——区间dp

    2024-03-11 03:16:04       27 阅读
  5. 前端缓存使用规范

    2024-03-11 03:16:04       20 阅读
  6. 深入了解 Jetpack Compose 中的 Modifier

    2024-03-11 03:16:04       24 阅读
  7. 基于单片机的四路抢答器的设计

    2024-03-11 03:16:04       24 阅读