Codeforces Round #739 (Div. 3)

套题名称原题地址#题目分数是否ACA800✅B1000✅C2000✅D1800❌E2000❌F2300❌G2400❌H2400❌I2400❌J2400❌K2400❌L2400❌M2400❌N2400❌A. 题目题目类型   题意   分析   时间复杂度   O()O( )O()
摘要由CSDN通过智能技术生成

Codeforces Round #739 (Div. 3)

原题地址

#题目分数是否AC
ADislike of Threes800
BWho’s Opposite?800
CInfinity Table800
DMake a Power of Two1300
EPolycarp and String Transformation1800
F1Nearest Beautiful Number (easy version)1900
F2Nearest Beautiful Number (hard version)2100





A. Dislike of Threes

题目类型  

题意

  


分析

  


时间复杂度

   O ( 1000 ) O( 1000 ) O(1000)


代码

    static int[] TB = new int[10010];
 
    public static void solve() throws IOException {
        pw.println(TB[nextInt()]);
    }
 
    public static void makeTB() {
        int num = 1;
        for (int i = 1; i < TB.length; i++) {
            while (num % 10 == 3 || num % 3 == 0) num++;
            TB[i] = num++;
        }
    }
    





B. Who’s Opposite?

题目类型  

题意

  


分析

  


时间复杂度

   O ( 1 ) O( 1 ) O(1)


代码

    public static void solve() throws IOException {
        int a = nextInt();
        int b = nextInt();
        int c = nextInt();
 
        int sum = Math.abs(a - b) * 2;
 
        if (sum < a || sum < b || sum < c) {
            pw.println(-1);
            return ;
        }
 
 
        if (c > sum / 2) pw.println(c - sum / 2);
        else pw.println(c + sum / 2);
    }
    





C. Infinity Table

题目类型  

题意

  


分析

  


时间复杂度

   O ( ) O( ) O()


代码

    public static void solve() throws IOException {
        long k = nextLong();
 
        if (k == 1) {
            pw.println(1 + " " + 1);
            return;
        }
 
        for (int i = 100001; i >= 0; i--) {
            if ((i & 1) == 0) continue;
 
            long re = getSum(i);
            if (re < k) {
                int y = (i + 3) / 2;
 
                long t = k - re;
                if (t <= y) pw.println(t + " " + y);
                else pw.println(y + " " + (2 * y - t));
 
                return ;
            }
        }
    }
 
    public static long getSum(long x) {
        return (x + 1L) / 2L * (1L + x) / 2L;
    }
    





D. Make a Power of Two

题目类型  

题意

  


分析

  


时间复杂度

   O ( ) O( ) O()


代码

    public static void solve() throws IOException {
        char[] c = String.valueOf(nextInt()).toCharArray();
 
        int ans = Integer.MAX_VALUE;
        for (int i = 0; i < 63; i++) ans = Math.min(ans, change(String.valueOf(1L << i).toCharArray(), c));
 
        pw.println(ans);
    }
 
    public static int change(char[] s, char[] num) {
        int cnt = 0;
        for (char c : num) if (cnt < s.length && s[cnt] == c) cnt++;
 
        return num.length - cnt + s.length - cnt;
    }
    





E. Polycarp and String Transformation

题目类型  

题意

  


分析

  


时间复杂度

   O ( ) O( ) O()


代码
这里粘贴代码





F1. Nearest Beautiful Number (easy version)

题目类型  

题意

  


分析

  


时间复杂度

   O ( 10 ∗ 10 ∗ 10 ∗ 2 10 ) O( 10 * 10 * 10 * 2^{10} ) O(101010210)


代码

    static TreeSet<Long> set1 = new TreeSet<>();
    static TreeSet<Long> set2 = new TreeSet<>();
 
    public static void solve() throws IOException {
        long n = nextLong();
        int k = nextInt();
 
        if (k == 1) {
            pw.println(set1.ceiling(n));
        } else {
            pw.println(set2.ceiling(n));
        }
 
 
    }
 
    public static void makeTB() {
        for (int i = 0; i < 10; i++) {
            StringBuilder s = new StringBuilder();
            for (int j = 0; j < 10; j++) {
                s.append((char) ('0' + i));
                set1.add(getNum(s));
                set2.add(getNum(s));
 
                for (int k = 0; k < 10; k++) dp(s, (char) ('0' + k), 0);
            }
        }
    }
 
    public static void dp(StringBuilder s, char k, int step) {
        if (step == s.length()) return ;
 
        StringBuilder s1 = new StringBuilder(s);
        s1.replace(step, step + 1, String.valueOf(k));
        set2.add(getNum(s1));
        dp(s1, k, step + 1);
 
        StringBuilder s2 = new StringBuilder(s);
        dp(s2, k, step + 1);
    }
 
    public static long getNum(StringBuilder s) {
        return Long.parseLong(String.valueOf(s));
    }
    





F2. Nearest Beautiful Number (hard version)

题目类型  

题意

  


分析

  


时间复杂度

   O ( 10 ∗ 10 ) O( 10 * 10 ) O(1010)


代码

    public static void solve() throws IOException {
        int n = nextInt();
        int k = nextInt();
 
        char[] num = String.valueOf(n).toCharArray();
 
        TreeSet<Integer> set = new TreeSet<>();
 
        for (char i : num) set.add(i - '0');
 
        if (set.size() <= k) {
            pw.println(n);
            return ;
        }
 
        set.clear();
        for (char i : num) {
            if (set.size() == k - 1) break;
            set.add(i - '0');
        }
 
 
 
        long ans = Long.MAX_VALUE;
        for (int i = 0; i < 10; i++) {
            TreeSet<Integer> set1 = new TreeSet<>(set);
            set1.add(i);
            ans = Math.min(ans, fun(set1, num, n, 0));
        }
 
        pw.println(ans);
    }
 
    public static long fun(TreeSet<Integer> set, char[] num, int n, int step) {
        if (step == num.length) return Long.MAX_VALUE;
 
        long res = Long.MAX_VALUE;
 
        char[] ans = new char[num.length];
        System.arraycopy(num, 0, ans, 0, step);
 
        if (set.higher(num[step] - '0') != null) {
            ans[step] = (char) (set.higher(num[step] - '0') + '0');
            for (int i = step + 1; i < num.length; i++)  ans[i] = (char) (set.first() + '0');
 
            long re = Long.parseLong(String.valueOf(ans));
            if (re >= n) res = Math.min(res, re);
        }
 
        if (set.contains(num[step] - '0')) res = Math.min(res, fun(set, num, n, step + 1));
 
 
        return res;
    }
    





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值