首页 > 其他分享 >【atcoder 293 F - Erase Subarrays】【动态规划】

【atcoder 293 F - Erase Subarrays】【动态规划】

时间:2022-10-31 12:35:40浏览次数:66  
标签:atcoder java io int Subarrays valueOf Erase words new

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String[] words = in.readLine().split("\\s+");
        int n, m;
        n = Integer.valueOf(words[0]);
        m = Integer.valueOf(words[1]);
        words = in.readLine().split("\\s+");
        int[] arr = new int[n];
        for (int i = 0; i < words.length; i++) {
            arr[i] = Integer.valueOf(words[i]);
        }
        int[][][] dp = new int[n+1][m + 1][2];
        for (int i = 0; i <= n; i++) {
            for (int j = 0; j <= m; j++) {
                dp[i][j][0] = 1000000000;
                dp[i][j][1] = 1000000000;
            }
        }
        dp[1][0][0] = 1;
        if (arr[0] <= m) {
            dp[1][arr[0]][1] = 0;
        }

        for (int i = 2; i <= n; i++) {
            dp[i][0][0] = 1;
            for (int j = 1; j <= m; j++) {
                dp[i][j][0] = Math.min(dp[i - 1][j][0], dp[i - 1][j][1] + 1);
                if (arr[i - 1] <= j) {
                    dp[i][j][1] = dp[i - 1][j - arr[i-1]][0];
                    dp[i][j][1] = Math.min(dp[i][j][1], dp[i - 1][j - arr[i - 1]][0]);
                    dp[i][j][1] = Math.min(dp[i][j][1], dp[i - 1][j - arr[i - 1]][1]);
                }
            }
        }

        for (int j = 1; j <= m; j++) {
            int x = Math.min(dp[n][j][0],dp[n][j][1]);
            System.out.println(x>=1000000000?-1:x);
        }

        in.close();
    }

}

 

标签:atcoder,java,io,int,Subarrays,valueOf,Erase,words,new
From: https://www.cnblogs.com/fishcanfly/p/16843878.html

相关文章

  • Atcoder ABC 273、 272、271的C、 D
    ABC273C(K+1)-thLargestNumber题意:给予你一个长度是N的数组a,对于每一个k(0,1,2,...N-1),完成一下问题:找到1~N中的数字a[i],找到大于a[i]的数目恰好是k个不同数的......
  • Atcoder试题乱做 Part5
    名言,解决不了题目,那就解决你自己./ybyb\(\text{[ARC136E]Non-coprimeDAG}\)\(\color{blue}{\text{[NORMAL]}}\)考虑\(i\)什么时候能到达\(j\),令\(f_x\)......
  • 【atcoder 293 E - Sugoroku 4】【动态规划,递推】
    importjava.io.IOException;importjava.util.Arrays;importjava.util.Scanner;publicclassMain{staticintn,m,k;staticintMOD=998244353;......
  • AtCoder Beginner Contest 275 E F
    E-Sugoroku4题意:从0走到n,有k次操作,每次会丢出一个骰子,骰子上的数字为\([1,m]\),等概率出现问到达n的概率思路:\(dp[i][k]\)表示用了k次操作到达位置i的概......
  • Atcoder Beginner Contest 275(A~F)
    我好菜啊……又没切掉F,40+min切掉A~E成功滚粗。希望能越打越好吧……赛时A求序列最大值,B简单计算,C数正方形,跳过。D递推不太行,N的范围有点大。但是除法的转移......
  • AtCoder Beginner Contest 275
    咕咕咕咕咕咕。G-InfiniteKnapsack做法1-二分假设第\(i\)个物品选了\(x_i\)个,\(x_i\)为非负整数,有\[\lim_{x\to+\infin}\frac{f(x)}{x}=\frac{\sum_ic_......
  • AtCoder Regular Contest 060
    F题有循环节,一看就有KMP,比较难,太晚了,早上起来再补。C-TakandCards\(n\)个整数\(a_1\sima_n\),问有多少种选数方案,使得选出来的数平均值为\(A\)。\(1\len,a_i......
  • LeetCode 1248. Count Number of Nice Subarrays
    原题链接在这里:https://leetcode.com/problems/count-number-of-nice-subarrays/题目:Givenanarrayofintegers nums andaninteger k.Acontinuoussubarrayis......
  • AtCoder Regular Contest 059
    EducationalDPRound(确信C-BeTogether给定\(n\)个数\(a_{1}\sima_n\),你至多可以对每个\(a_i\)操作一次,以\((a_i-y)^2\)的代价令\(a_i\getsy\),求\(a\)全......
  • Leetcode第907题:子数组的最小值之和(Sum of subarrays minimums )
    解题思路既然我们不能先遍历区间,然后找最小值,那么我们不如顺序倒过来,对于每个值,我们找有多少区间里面,它是最小值。对于一个数字A[i]来说,如果在某个区间[j,k]里面它......