codeforces round 820 A~F

文章目录

A

水题

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

public class Main {
    static FastScanner fs;
    public static void main(String[] args) {
        fs=new FastScanner();
        int t=fs.nextInt();
        for( int i =0;i<t;i++){
            System.out.printf("%d\n",solve());
        }


    }
    public static int solve(){
        int a,b,c;
        a=fs.nextInt();
        b=fs.nextInt();
        c=fs.nextInt();
        if(a-1<Math.abs(b-c)+c-1){
            return 1;
        }
        else if(a-1>Math.abs(b-c)+c-1){
            return 2;
        }
        else {
            return 3;
        }
    }


    public static class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(String s) {
            try {
                br = new BufferedReader(new FileReader(s));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

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

        String nextToken() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(nextToken());
        }

        long nextLong() {
            return Long.parseLong(nextToken());
        }

        double nextDouble() {
            return Double.parseDouble(nextToken());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
}

B

从后向前遍历,遇到0则为两位数,否则为一位数

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

public class Main {
    static FastScanner fs;
    public static void main(String[] args) {
        fs=new FastScanner();
        int t=fs.nextInt();
        for( int i =0;i<t;i++){
            solve();
        }


    }
    public static void solve(){
        int n=fs.nextInt();
//        String s=fs.nextLine();
        StringBuffer s=new StringBuffer(fs.nextLine());
        StringBuffer res=new StringBuffer();
        for( int i=s.length()-1;i>=0;i--){
            if(s.charAt(i)=='0'){
                int x=Integer.parseInt(s.substring(i-2,i));
                res.append((char)(x+96));
                i-=2;
            }
            else{
                int x=Integer.parseInt(s.substring(i,i+1));
                res.append((char)(x+96));
            }
        }
        System.out.println(res.reverse());
    }


    public static class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(String s) {
            try {
                br = new BufferedReader(new FileReader(s));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

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

        String nextToken() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(nextToken());
        }

        long nextLong() {
            return Long.parseLong(nextToken());
        }

        double nextDouble() {
            return Double.parseDouble(nextToken());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
}

C

使用二维变长数组,按照字母分类,将每个字母出现的位置存起来。

import java.io.*;

import java.util.*;

public class Main {
    static FastScanner fs;
    public static void main(String[] args) {
        fs=new FastScanner();
        int t=fs.nextInt();
        for( int i =0;i<t;i++){
            solve();
        }
    }
    public static void solve(){
        String s=fs.nextLine();
        int n=s.length();
        Vector<Integer>[]v=new Vector[26];
        for( int i=0;i<26;i++){
            v[i]=new Vector<Integer>();
        }
        for( int i=0;i<s.length();i++){
            v[s.charAt(i)-97].add(i);
        }
        Vector<Integer>res=new Vector<Integer>();
        int ans=Math.abs(s.charAt(0)-s.charAt(n-1));
        if(s.charAt(0)<=s.charAt(n-1)){

            for( int i=s.charAt(0);i<=s.charAt(n-1);i++){
                for (int it:v[i-97]) {
                    res.add(it);
                }
            }
        }
        else{
            for( int i=s.charAt(0);i>=s.charAt(n-1);i--){
                for (int it:v[i-97]) {
                    res.add(it);
                }
            }
        }
        System.out.print(ans+" "+res.size());
        System.out.println();
        for( int i=0;i<res.size();i++){
            System.out.print((res.elementAt(i)+1)+" ");
        }
        System.out.println();
    }


    public static class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(String s) {
            try {
                br = new BufferedReader(new FileReader(s));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

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

        String nextToken() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(nextToken());
        }

        long nextLong() {
            return Long.parseLong(nextToken());
        }

        double nextDouble() {
            return Double.parseDouble(nextToken());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
}

D

贪心算法
先将两个数列做差,变为一个数列。
每个正数优先和一个负数配对,如果不能和负数配对,就和正数配对。
最后输出配对的组数。

import java.io.*;

import java.lang.reflect.Array;
import java.util.*;

public class Main {
    static FastScanner fs;
    public static void main(String[] args) {
        fs=new FastScanner();
        int t=fs.nextInt();
        for( int i =0;i<t;i++){
            solve();
        }
    }
    public static void solve(){
        int n=fs.nextInt();
        int []v=new int [n];
        for( int i=0;i<n;i++){
            v[i]=fs.nextInt();
        }
        for( int i=0;i<n;i++){
            int x=fs.nextInt();
            v[i]=x-v[i];
        }
//        Arrays.sort(v);
        ArrayList<Integer>pos=new ArrayList<Integer>();
        ArrayList<Integer>neg=new ArrayList<Integer>();
        for( int i=0;i<n;i++){
            if(v[i]>=0) pos.add(v[i]);
            else neg.add(v[i]);
        }
        Collections.sort(pos);
        Collections.sort(neg);
        int p=0;
        int ans=0;
        for( int i=pos.size()-1;i>=0;i--){
            while(p<neg.size()&&neg.get(p)+pos.get(i)<0) p++;
            if(p<neg.size()){
                ans++;
                p++;
            }
            else{
                if(i!=0){
                    i--;
                    ans++;
                }
                else break;
            }
        }
        System.out.println(ans);


    }


    public static class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(String s) {
            try {
                br = new BufferedReader(new FileReader(s));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

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

        String nextToken() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(nextToken());
        }

        long nextLong() {
            return Long.parseLong(nextToken());
        }

        double nextDouble() {
            return Double.parseDouble(nextToken());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
}

E

对于询问(x,y)和(y,x)返回有50%不同,如果返回不同,则答案就是两个返回值相加。

import java.util.Scanner;

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

        long []rem=new long[25];
        long ans=0;
        for( int i=0;i<25;i++){
            rem[i]=query(1,i+2);
        }
        for( int i=0;i<25;i++){
            if(rem[i]<0){
                ans=i+1;
                break;
            }
            long temp=query(i+2,1);
            if(temp!=rem[i]){
                ans=Math.max(rem[i]+temp,ans);
            }
        }
        
        System.out.println("! "+ans);
    }
    public static long query( int x,int y){
        System.out.println("? "+x+" "+y);
        System.out.flush();
        long res=Long.parseLong( sc.nextLine());
        return res;
    }

}

 

F

利用前缀和的方法快速计算区间的值。

import javax.xml.stream.FactoryConfigurationError;
import java.io.*;
import java.util.*;

public class Main {
    static FastScanner sc=new FastScanner();
    static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
    static int []fac;
    static final int N=200100;
    public static void main(String[] args) {
        fac=new int[N];
        fac[0]=1;
        for( int i=1;i<N;i++){
            fac[i]=fac[i-1]*10%9;
        }
        sc=new FastScanner();
        int t=sc.nextInt();
        for( int i=0;i<t;i++){
            solve();
        }
    }
    static void solve(){
        String s=sc.next();
        int w,m;
        w=sc.nextInt();
        m=sc.nextInt();
        int cur=0;
        int prev[]=new int[s.length()];
        prev[0]=(s.charAt(0)-'0')%9;
        for( int i=1;i<s.length();i++){
            prev[i]=(prev[i-1]*10+(s.charAt(i)-'0'))%9;
        }
        Vector<Vector<Integer>>v=new Vector<Vector<Integer>>();
        for( int i=0;i<9;i++){
            v.add(new Vector<Integer>());
        }
        for( int i=0;i+w-1<s.length();i++){
            int x=(prev[i+w-1]+9-(i==0?0:prev[i-1])*fac[w-1]%9)%9;
            v.get(x).add(i);
        }
        for( int i=0;i<9;i++){
            Collections.sort(v.elementAt(i));
        }
        for( int i=0;i<m;i++){

            int l,r,k;

            l=sc.nextInt();
            r=sc.nextInt();
            k=sc.nextInt();
            --l;--r;
            int x=(prev[r]+9-(l==0?0:prev[l-1])*fac[r-l]%9)%9;
            int l1=s.length()+1,l2=s.length()+1;
//            for( int j=0;j<9;j++){
//
//                System.out.println(v.elementAt(j).elementAt(0));
//            }
//            System.out.println(x);
            for( int j=0;j<9;j++){
                for( int p=0;p<9;p++){
                    if((j*x+p)%9!=k) continue ;
                    if(j!=p){
                        if(v.elementAt(j).size()==0||v.elementAt(p).size()==0) continue;
                        else {
                            int y=v.elementAt(j).elementAt(0)+1;
                            int z=v.elementAt(p).elementAt(0)+1;
                            if(y<l1){
                                l1=y;
                                l2=z;
                            }
                            else if(y==l1){
                                l2=Math.min(l2,z);
                            }
                        }
                    }
                    else {
                        if(v.elementAt(j).size()<2) continue;
                        else {
                            int y=v.elementAt(j).elementAt(0)+1;
                            int z=v.elementAt(p).elementAt(1)+1;
                            if(y<l1){
                                l1=y;
                                l2=z;
                            }
                            else if(y==l1){
                                l2=Math.min(l2,z);
                            }
                        }
                    }

                }
            }


            if(l1>s.length()) {
                out.printf("%d %d\n",-1,-1);
            }
            else{
                out.printf("%d %d\n",l1,l2);

            }
            out.flush();

        }



    }

    static class FastScanner
    {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer("");

        String next()
        {
            while (!st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

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

        long nextLong()
        {

            return Long.parseLong(next());
        }

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

        String nextLine() throws IOException
        {
            return br.readLine();
        }
    }
}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值