当前位置: 首页 > news >正文

AtCoder Beginner Contest 431 A~E 题解

A - Robot Balance

计算B要加多少才大于A

    public static void solve() throws IOException {int a = nextInt(), b = nextInt();out.println(Math.max(0, a - b));}

B - Robot Weight
用一个Bool数组记录一下连接情况即可。

    public static void solve() throws IOException {int X = nextInt(), n = nextInt();ArrayList<Integer> w = new ArrayList<>(n);for(int i = 0; i < n; i++){w.add(nextInt());}ArrayList<Boolean> st = new ArrayList<>(Collections.nCopies(n, false));int Q = nextInt();while(Q-- > 0){int p = nextInt();p--;if(st.get(p))X -= w.get(p);else X += w.get(p);st.set(p, !st.get(p));out.println(X);}}

C - Robot Factory
贪心的将最轻的K个头和最重和K个身体匹配

    public static void solve() throws IOException {int n = nextInt(), m = nextInt(), k = nextInt();ArrayList<Integer> H = new ArrayList<>();ArrayList<Integer> B = new ArrayList<>();for(int i = 0; i < n; i++){H.add(nextInt());}for(int i = 0; i < m; i++){B.add(nextInt());}Collections.sort(H);Collections.sort(B);for(int i = 0; i < k ;i++){if(H.get(i) > B.get(m - k + i)){out.println("No");return;}}out.println("Yes");}

D - Robot Customize
使用f[x]来表示头部为X重量时,最大的开心值。可以发现就是一个01背包。

public static void solve() throws IOException {int n = nextInt();int MAX_TOTAL_WEIGHT = 500 * 500;long[] dp = new long[MAX_TOTAL_WEIGHT + 10];Arrays.fill(dp, -1);dp[0] = 0;int totalWeight = 0;for (int i = 0; i < n; i++) {int w = nextInt();int h = nextInt();int b = nextInt();totalWeight += w;for (int j = totalWeight; j >= 0; j--) {long happinessFromBody = -1;if (dp[j] != -1) {happinessFromBody = dp[j] + b;}long happinessFromHead = -1;if (j >= w && dp[j - w] != -1) {happinessFromHead = dp[j - w] + h;}dp[j] = Math.max(happinessFromBody, happinessFromHead);}}long maxHappiness = 0;for (int j = 0; j * 2 <= totalWeight; j++) {maxHappiness = Math.max(maxHappiness, dp[j]);}out.println(maxHappiness);}

E - Reflection on Grid
其实是一个01边权的图论问题,我们用f[x][y][dir]来表示在(x, y)这个点保持dir方向的最小代价(即节点类型变化次数)

static int[] dx = {1, 0, -1, 0};static int[] dy = {0, 1, 0, -1};public static void solve() throws IOException {int n = nextInt(), m = nextInt();String[] g = new String[n];for(int  i = 0; i < n; i++){g[i] = next();}HashMap<Character, Integer> mp = new HashMap<Character, Integer>();mp.put('A', 0);mp.put('B', 1);mp.put('C', 3);int[][][] f = new int[n][m][4];for(int i = 0; i < n; i++)for(int j = 0; j < m; j++)Arrays.fill(f[i][j], 0x3f3f3f3f);// 使用Deque做0-1BFSArrayDeque<int[]> dq = new ArrayDeque<>();dq.addFirst(new int[]{0, -1, 1, 0});while(!dq.isEmpty()){int[] t = dq.pollFirst();int x = t[0], y = t[1], dir = t[2], c = t[3];int a = x + dx[dir], b = y + dy[dir];for(int ndir = 0; ndir < 4; ndir++){if((dir ^ ndir) == 2) continue;if(a < 0 || a >= n || b < 0 || b >= m)continue;// Java怎么这么麻烦...int cost = ((dir ^ ndir) == mp.get(g[a].charAt(b))) ? 0 : 1;if(c + cost < f[a][b][ndir]){f[a][b][ndir] = c + cost;if(cost == 0) dq.addFirst(new int[]{a, b, ndir, f[a][b][ndir]});else dq.addLast(new int[]{a, b, ndir, f[a][b][ndir]});}}}out.println(f[n - 1][m - 1][1]);}
http://www.zskr.cn/news/52962.html

相关文章:

  • 2025 最新推荐!恒温恒湿试验箱厂家排行榜 涵盖立式 / 可程式 / 防爆等多类型设备优质厂家精选
  • 2025年口碑好的校园智慧体育热门品牌权威推荐榜
  • 2025年比较B2C网站谷歌优化专业机构推荐榜
  • 2025年纯化器分析优质厂家权威推荐榜单:气体纯化器/纯化器单位/纯化器单位源头厂家精选
  • 2025年评价高的KNX智能家居系统厂家最新权威实力榜
  • 2025年国标隔热条品牌综合实力排行榜TOP10推荐
  • linux 5 下载
  • 2025年热门的无锡写字楼绿植租赁厂家最新推荐排行榜
  • 2025年口碑好的悉尼澳洲海外仓中转配送品牌推荐榜
  • 2025年口碑好的设计师三段力缓冲铰链厂家最新热销排行
  • 2025年知名的贴片盖子厂家推荐及采购指南
  • 2025年质量好的光通信检测仪器厂家最新TOP实力排行
  • 2025年评价高的长毛绒滤袋厂家实力及用户口碑排行榜
  • 2025年质量好的旋喷钻机用户好评厂家排行
  • 2025年全封闭工地洗轮机厂家权威推荐榜单:封闭式冲洗台/全封闭洗车台/封闭型洗轮机源头厂家精选
  • 2025年口碑好的冷拉型钢光圆厂家最新推荐权威榜
  • 2025年口碑好的冷拉异型钢光圆厂家推荐及选购指南
  • 2025年苗木批发基地十大口碑批发商权威排行,樱花/白蜡/紫薇/无刺枸骨球/红叶石楠/金森女贞/金叶女贞/青叶复叶槭/金叶复叶槭供应商排行榜
  • matplotlib plot 折线图使用体验
  • 后厨合规智能监控方案:食材损耗率降 40%
  • 新能源动力域系统级测试系统解决方案
  • 拒绝 “厂商绑定”:MyEMS 凭什么成为工业企业的 “能源管理自主选择权”?
  • 传统化工企业的 “能耗死循环”:MyEMS 如何用 “数据中台” 打通设备孤岛?
  • 2025年苗木批发基地实力批发商排行榜全新发布,丝棉木/红叶石楠/苗木/红叶李/国槐/青叶复叶槭/紫薇/油松/白蜡/金叶女贞/栾树种植哪家好
  • 2025年11月国内矿山设备检测检验公司权威推荐榜单:专业选择指南
  • 2025留学机构哪家比较好一点
  • 实用指南:用 FPGA 实现 PCIe 传输,开源核 LitePCIe 深度解读
  • linux 3g驱动
  • linux 3g上网卡
  • 批量处理工具类 用于解决大批量数据操作时的数据库性能问题