首页 > 其他分享 >ATCoder 332 A-D

ATCoder 332 A-D

时间:2023-12-11 11:12:44浏览次数:30  
标签:ATCoder Scanner int 332 static ans sc new

A : Online Shopping (读懂题即可)

package AtCoder.begin332;

import java.util.Scanner;

public class A {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt(), s = sc.nextInt(), k = sc.nextInt();
        int[] p = new int[n + 10], q = new int[n + 10];

        for (int i = 1; i <= n; i ++ ) {
            p[i] = sc.nextInt();
            q[i] = sc.nextInt();
        }

        long ans = 0;
        for (int i = 1; i <= n; i ++ ) ans += (long) p[i] * q[i];

        if (ans >= s) System.out.println(ans);
        else System.out.println(ans + k);
    }
}




B : Glass and Mug (读懂题然后模拟即可)

package AtCoder.begin332;

import java.util.Scanner;

public class B {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int k = sc.nextInt(), g = sc.nextInt(), m = sc.nextInt();
        int ng = 0, nm = 0;
        for (int i = 1; i <= k; i ++ ) {
            if (ng == g) ng = 0;
            else if (nm == 0) nm = m;
            else {
                ng += nm;
                if (ng >= g) {
                    nm = ng - g;
                    ng = g;
                } else nm = 0;
            }
        }

        System.out.println(ng + " " + nm);
    }
}




C : T-shirts (读清题意然后求解即可)

package AtCoder.begin332;

import java.util.Scanner;

public class C {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt(), m = sc.nextInt();
        String s = sc.next();

        int np = 0, nl = 0;
        int ans = 0;
        for (int i = 0; i < s.length(); i ++ ) {
            if (s.charAt(i) == '0') {
                ans = Math.max(ans, Math.max(np - m, 0) + nl);
                np = 0;
                nl = 0;
            } else if (s.charAt(i) == '1') np ++ ;
            else nl ++ ;
        }

        ans = Math.max(ans, Math.max(np - m, 0) + nl);
        System.out.println(ans);
    }
}




D : Swapping Puzzle (枚举全排列 + 求逆序数)
package AtCoder.begin332;

import javax.swing.plaf.nimbus.State;
import java.util.*;

public class D {

    /**
     * 思路比较简单 :
     *      因为交换相邻的行和相邻的列其实就等同于随意交换 -> 可以自己试一试
     *      dfs枚举行和列的全排列, 然后判断每一个状态的 a 是否和 b 相同
     *      相同了如何更新答案 -> 这个状态的交换次数就是行排列和列排列的逆序数之和
     *
     * 实现过程被edu了 :
     *      不必真的取交换, 直接记录行号和列号, 然后到时候根据行号和列号去原数据查就好了
     *      因为交换行和交换列是互不影响的
     *
     * 总结 :
     *      dfs还是需要多练习一下, 光有思路无法实现是最难受的
     * */

    static int[][] a, b;
    static int[] ri, wi;
    static int n, m, ans = Integer.MAX_VALUE;
    static Set<Integer> sr = new HashSet<>();
    static Set<Integer> sw = new HashSet<>();

    private static void init() {
        a = new int[n + 10][m + 10];
        b = new int[n + 10][m + 10];
        ri = new int[n + 10]; // 记录行当前的排列
        wi = new int[m + 10]; // 记录列当前的排列
    }

    private static void dfs_row(int u) { // 枚举行的全排列
        if (u > n) dfs_col(1);
        for (int i = 1; i <= n; i ++ ) {
            if (sr.contains(i)) continue;
            ri[u] = i;
            sr.add(i);
            dfs_row(u + 1);
            sr.remove(i);
        }
    }

    private static void dfs_col(int u) { // 枚举列的全排列
        if (u > m) {
            for (int i = 1; i <= n; i ++ )
                for (int j = 1; j <= m; j ++ )
                    if (a[ri[i]][wi[j]] != b[i][j]) return ; // 不符合要求

            // 算出这个状态的答案
            int res = 0;
            for (int i = 1; i <= n; i ++ )
                for (int j = i + 1; j <= n; j ++ )
                    if (ri[i] > ri[j]) res ++ ;

            for (int i = 1; i <= m; i ++ )
                for (int j = i + 1; j <= m; j ++ )
                    if (wi[i] > wi[j]) res ++ ;

            ans = Math.min(ans, res); // 尝试更新答案
            return ;
        }

        for (int i = 1; i <= m; i ++ ) {
            if (sw.contains(i)) continue;
            wi[u] = i;
            sw.add(i);
            dfs_col(u + 1);
            sw.remove(i);
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        n = sc.nextInt(); m = sc.nextInt();
        init();

        for (int i = 1; i <= n; i ++ )
            for (int j = 1; j <= m; j ++ )
                a[i][j] = sc.nextInt();

        for (int i = 1; i <= n; i ++ )
            for (int j = 1; j <= m; j ++ )
                b[i][j] = sc.nextInt();

        dfs_row(1);
        if (ans == Integer.MAX_VALUE) System.out.println(-1); // 无解
        else System.out.println(ans);
    }
}

标签:ATCoder,Scanner,int,332,static,ans,sc,new
From: https://www.cnblogs.com/llihaotian666/p/17893912.html

相关文章

  • 【题解】AtCoder abc322_f Random Update Query
    传送门:https://atcoder.jp/contests/abc332/tasks/abc332_f容易发现,对于一个位置$i$,$A_i$的最终值是由对$i$的最后一次赋值操作决定的;因此,将所有操作按时间顺序倒过来考虑,则由第$j$次操作决定$A_i$最终值的概率为"在第$(j+1)$~$m$次操作中没有修改过$i$的概率"与"第......
  • AtCoder Regular Contest 169 (ARC169)
    怎么有人ARCA卡了半天的?A.PleaseSign考虑\(i\)位置上的数,下次它被加到\(P_i\),再下次被加到\(P_{P_i}\),因为这个序列有性质\(P_i<i\),这样加若干轮一定会到达\(1\)。令所有的\(i\)向\(P_i\)连边,则这是一棵以\(1\)为根的树。设\(f_i=\sum\limits_{j=1}^n[dep_......
  • AtCoder_abc332
    AtCoder_abc332比赛链接A-OnlineShoppingA题链接题目大意这里有\(N\)件商品,第\(i\)件商品价格为\(P_i\),你要购买\(Q_i\)件,除了购买的费用外,他还要支付运费。如果购买的总价大于\(S\),运费为0元,否则他需要支付\(K\)元的运费。他一共要花多少钱呢?解题思路无代码//Prob......
  • 【题解】AtCoder abc332_g Not Too Many Balls
    传送门:https://atcoder.jp/contests/abc332/tasks/abc332_g看完题,第一眼反应为最大流。建模方式为:以颜色为左部点,盒子为右部点,源点$S$向颜色$i$连一条容量为$A_i$的边,盒子$j$向汇点$T$连一条容量为$B_j$的边,颜色$i$向盒子$j$连一条容量为$ij$的边;在这张图......
  • AtCoder Beginner Contest 332
    AtCoderBeginnerContest332A-OnlineShoppingintmain(){IOS;for(_=1;_;--_){cin>>n>>m>>k;llans=0;rep(i,1,n){lla,b;cin>>a>>b;ans+=a......
  • Atcoder abc301 复盘(更新中)
    跳转比赛链接A-OverallWinner简述:高桥和青木下了\(N\)盘棋。给你一个长度为\(N\)的字符串\(S\),表示这两盘棋的结果。如果\(S\)的\(i\)个字符是t,那么高桥赢得了\(i\)局;如果\(S\)的\(i\)个字符是A,那么青木赢得了这局。高桥和青木之间的胜负关系是谁赢的局......
  • AtCoder Beginner Contest 331 G - Collect Them All【概率期望+容斥+多项式】
    题目链接:ABC331_G写在前面将来如果回顾这道题,建议自己看完题意一定先重新推一遍。如果还是不够熟练,多去做一些同类型的题目吧。题意:盒子里有\(N\)张卡片,每张卡片上写着一个数字,数字的范围是\(1,...,M\),写着数字\(i\)的卡片有\(C_i\)张\((C_i>0)\)。有放回地抽取卡片,每......
  • Daiwa Securities Co. Ltd. Programming Contest 2023(AtCoder Beginner Contest 331)
    DaiwaSecuritiesCo.Ltd.ProgrammingContest2023(AtCoderBeginnerContest331)A-Tomorrow解题思路:模拟。代码:#include<bits/stdc++.h>usingnamespacestd;usingll=longlong;typedefpair<int,int>pii;#definefifirst#definesesecondcons......
  • AtCoder Beginner Contest 331
    A-Tomorrow(abc331A)题目大意给定一年的月数和一月的天数,以及当天日期,问次日的日期。解题思路一个简单的进制加法运算,超出进制数则向前加一。神奇的代码#include<bits/stdc++.h>usingnamespacestd;usingLL=longlong;intmain(void){ios::sync_with_st......
  • AtCoder Beginner Contest 328
    AtCoderBeginnerContest328链接:ToyotaProgrammingContest2023#7(AtCoderBeginnerContest328)-AtCoderA题意:给定n个数,将小于等于x的数加起来输出。#include<bits/stdc++.h>#defineintlonglong#defineendl'\n';usingnamespacestd;voidsolve(){......