题目链接

题意理解

这道题目如果从后往前推,那就比较简单。状态转移方程直接看源代码。其中 dp[i] 表示第i分钟时可以休息多久。

代码

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    static int N, K;
    static final int maxn = 10010;
    static int[] dp = new int[maxn];
    static int[] s = new int[maxn];
    static int[] e = new int[maxn];
    static int[] start = new int[maxn];
    public static void main(String[] args) {
        FastScanner fs = new FastScanner();
        N = fs.nextInt();
        K = fs.nextInt();
        for(int i = 1; i <= K; i++) {
            s[i] = fs.nextInt();
            e[i] = fs.nextInt();
            start[s[i]]++;
        }
        for(int i = N; i > 0; i--) {
            if(start[i] == 0) {
                dp[i] = dp[i+1] + 1;
            } else {
                for(int j = 1; j <= K; j++) {
                    if(s[j] == i) {
                        dp[i] = Math.max(dp[i], dp[i+e[j]]);
                    }
                }
            }
        }
        System.out.println(dp[1]);
    }

    public static class FastScanner {
        private BufferedReader br;
        private StringTokenizer st;

        public FastScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        public String nextToken() {
            while(st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.valueOf(nextToken());
        }
    }
}
Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐