Codeforces Round #849 (Div. 4)

A. Codeforces Checking

题意

每个案例给一个字符,如果在 ”codeforces“ 中出现过,输出 YES,否则输出 NO

code

/**
 * @author :Changersh
 * @date : 2023/2/3 22:37
 */

import java.io.*;
import java.util.*;
import java.lang.*;

public class Main {
    private static boolean[] a = new boolean[26];
    public static void main(String[] args) {
        String s = "codeforces";
        for (int i = 0; i < s.length(); i++)
            a[s.charAt(i) - 'a'] = true;

        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            char t = sc.next().charAt(0);
            if (a[t - 'a']) out.println("YES");
            else out.println("NO");
        }

        out.close();
    }
    static class FastScanner{
        // 看看有没有溢出,是否要用 long
        // sc.xxx;
        // out.print();
        // out.flush();
        // out.close();
        BufferedReader br;
        StringTokenizer st;
        public FastScanner(InputStream in) {
            br=new BufferedReader( new InputStreamReader(System.in));
            eat("");
        }
        public void eat(String s) {
            st=new StringTokenizer(s);
        }

        public String nextLine() {
            try {
                return br.readLine();
            }catch(IOException e) {
                return null;
            }
        }

        public boolean hasNext() {
            while(!st.hasMoreTokens()) {
                String s=nextLine();
                if(s==null)return false;
                eat(s);
            }

            return true;
        }

        public String next() {
            hasNext();
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }
    }

    static FastScanner sc=new FastScanner(System.in);
    static PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
}

B. Following Directions

题意

每个案例给一串字符串,包含 U、D、L、R,是上下左右四个方向,从 (0, 0) 开始,问是否经过 (1, 1)

code

直接模拟即可

/**
 * @author :Changersh
 * @date : 2023/2/3 22:43
 */

import java.io.*;
import java.util.*;
import java.lang.*;

public class Main {
    private static int N = 55, n, T;
    public static void main(String[] args) {
        T = sc.nextInt();
        while (T-- > 0) {
            out.println(solve() ? "YES" : "NO");
        }

        out.close();
    }

    private static boolean solve() {
        n = sc.nextInt();
        char[] c = sc.next().toCharArray();
        int x = 0, y = 0;
        for (int i = 0; i < n; i++) {
            if (c[i] == 'U') x++;
            else if (c[i] == 'D') x--;
            else if (c[i] == 'L') y--;
            else y++;
            if (x == 1 && y == 1) return true;
        }

        return false;
    }

    static class FastScanner {
        // 看看有没有溢出,是否要用 long
        // sc.xxx;
        // out.print();
        // out.flush();
        // out.close();
        BufferedReader br;
        StringTokenizer st;

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

        public void eat(String s) {
            st = new StringTokenizer(s);
        }

        public String nextLine() {
            try {
                return br.readLine();
            } catch (IOException e) {
                return null;
            }
        }

        public boolean hasNext() {
            while (!st.hasMoreTokens()) {
                String s = nextLine();
                if (s == null) return false;
                eat(s);
            }

            return true;
        }

        public String next() {
            hasNext();
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }
    }

    static FastScanner sc = new FastScanner(System.in);
    static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
}

C. Prepend and Append

题意

给你一串由 0 1 组成的字符串,如果第一个和最后一个是 0 1或者 1 0 ,可以消掉,这种操作可以进行任意次,问,任意次操作后,字符串剩下的最短长度是多少?

code

双指针判断第一个和最后一个字符,模拟即可

/**
 * @author :Changersh
 * @date : 2023/2/3 22:51
 */

import java.io.*;
import java.util.*;
import java.lang.*;

public class Main {
    private static int T;
    public static void main(String[] args) {
        T = sc.nextInt();
        while (T-- > 0)
            solve();

        out.close();
    }
    private static void solve() {
        int n = sc.nextInt();
        char[] c = sc.next().toCharArray();

        int l = 0, r = n - 1;
        while (l < r) {
            if ((c[l] == '0' && c[r] == '1') || (c[l] == '1' && c[r] == '0')) {
                l++;
                r--;
            }
            else break;
        }

        out.println(r - l + 1);
    }
    static class FastScanner{
        // 看看有没有溢出,是否要用 long
        // sc.xxx;
        // out.print();
        // out.flush();
        // out.close();
        BufferedReader br;
        StringTokenizer st;
        public FastScanner(InputStream in) {
            br=new BufferedReader( new InputStreamReader(System.in));
            eat("");
        }
        public void eat(String s) {
            st=new StringTokenizer(s);
        }

        public String nextLine() {
            try {
                return br.readLine();
            }catch(IOException e) {
                return null;
            }
        }

        public boolean hasNext() {
            while(!st.hasMoreTokens()) {
                String s=nextLine();
                if(s==null)return false;
                eat(s);
            }

            return true;
        }

        public String next() {
            hasNext();
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }
    }

    static FastScanner sc=new FastScanner(System.in);
    static PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
}

D. Distinct Split

题意

每个案例给定一个由小写字母组成的字符串,求将字符串从中间劈开,分成两个字串中,出现的不同的字符数的和的最大值?

code

暴力写会tle,所以先预处理一下前缀和后缀,前缀是到 第 i 个字符截止,前面 i 个字符共出现多少个不同的字符
后缀:从

/**
 * @author :Changersh
 * @date : 2023/2/3 23:04
 */

import java.io.*;
import java.util.*;
import java.lang.*;

public class Main {
    private static int T, n;
    private static char[] c;

    public static void main(String[] args) {
        T = sc.nextInt();
        while (T-- > 0)
            solve();

        out.close();
    }

    private static void solve() {
        n = sc.nextInt();
        c = sc.next().toCharArray();
        int ans = 0;
        int[] l = new int[n + 2];
        int[] r = new int[n + 2];
        get(l, r);

        for (int i = 0; i < n; i++) {
            ans = Math.max(ans, l[i + 1] + r[i + 2]);
        }

        out.println(ans);
    }

    private static void get(int[] l, int[] r) {
        int ans = 0;
        HashSet<Character> vis = new HashSet<>();
        for (int i = 0; i < n; i++) {
            if (!vis.contains(c[i])) {
                vis.add(c[i]);
                ans++;
            }
            l[i + 1] = ans;
        }
        vis.clear();
        ans = 0;
        for (int i = n - 1; i >= 0; i--) {
            if (!vis.contains(c[i])) {
                vis.add(c[i]);
                ans++;
            }
            r[i + 1] = ans;
        }
    }

    static class FastScanner {
        // 看看有没有溢出,是否要用 long
        // sc.xxx;
        // out.print();
        // out.flush();
        // out.close();
        BufferedReader br;
        StringTokenizer st;

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

        public void eat(String s) {
            st = new StringTokenizer(s);
        }

        public String nextLine() {
            try {
                return br.readLine();
            } catch (IOException e) {
                return null;
            }
        }

        public boolean hasNext() {
            while (!st.hasMoreTokens()) {
                String s = nextLine();
                if (s == null) return false;
                eat(s);
            }

            return true;
        }

        public String next() {
            hasNext();
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }
    }

    static FastScanner sc = new FastScanner(System.in);
    static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
}

E. Negatives and Positives

题意

给定一串数字,你可以进行任意次以下的操作:
选择两个不同的数字,变成相反数

求,进行任意次之后,数组数字和的最大值是多少?

code

数字有正数、负数、0
翻转的时候,最佳的方法是把所有的负数都变成正数,是最佳情况

  1. 负数个数是偶数,完美,全部都可以翻转成正数
  2. 负数个数是奇数:
    1. 有 0,依然完美
    2. 找绝对值最小的,取负即可
    将第二种情况的两种小情况和一
    遍历数组的时候,统计负数个数,并且把负数都翻转,求和
    如果是偶数,返回答案
    如果是奇数,说明不能完美翻转,数组排序,得到最小的数字,无论是正数还是负数。抑或是 0,减两次即可。

因为最小的如果是 负数翻转的,减两次相当于没有翻转
如果是正数,相当于把 落单的负数和绝对值小于它的正数翻转,和变大
如果是 0,也是完美的情况

/**
 * @author :Changersh
 * @date : 2023/2/4 9:20
 */

import java.io.*;
import java.util.*;
import java.lang.*;

public class Main {
    private static int T, n, N = 200010;
    private static int[] a;
    public static void main(String[] args) {
        T = sc.nextInt();
        while (T-- > 0)
            solve();

        out.close();
    }
    private static void solve() {
        long sum = 0;
        int cnt = 0;
        n = sc.nextInt();
        a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
            if (a[i] < 0) {
                cnt++;
                a[i] = -a[i];
            }
            sum += a[i];
        }

        if ((cnt & 1) == 1) {
            Arrays.sort(a);
            sum -= 2 * a[0];
        }
        out.println(sum);
    }
    static class FastScanner{
        // 看看有没有溢出,是否要用 long
        // sc.xxx;
        // out.print();
        // out.flush();
        // out.close();
        BufferedReader br;
        StringTokenizer st;
        public FastScanner(InputStream in) {
            br=new BufferedReader( new InputStreamReader(System.in));
            eat("");
        }
        public void eat(String s) {
            st=new StringTokenizer(s);
        }

        public String nextLine() {
            try {
                return br.readLine();
            }catch(IOException e) {
                return null;
            }
        }

        public boolean hasNext() {
            while(!st.hasMoreTokens()) {
                String s=nextLine();
                if(s==null)return false;
                eat(s);
            }

            return true;
        }

        public String next() {
            hasNext();
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }
    }

    static FastScanner sc=new FastScanner(System.in);
    static PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
}

G1. Teleporters (Easy Version)

题意

有 0~n,n + 1个点,我们可以从 1 ~ n 号点用 传送器瞬间移动到 0号点,花费 a[i] 块钱
从一个点移动到隔壁,花费 1 块钱

给你一串 传送的花费 和现在总共有的钱 ,问从 0 出发最多能用几次传送

由题意得,传送的使用条件是:

  1. 先花费 i 块钱,从 0 走到 i
  2. 花费 a[i] 传送

code


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值