CF1256(div3 java題解)

A:java

題意:給定A個N元,B個一元,問是否能夠湊成S元。數組

思路:A*i+j=S 即 A*I<=S<=A*I+B 即min(S/N,A)+B>=S;ide

/*
@author nimphy
@create 2019-11-05-10:34
about:CF1256A
*/

import java.util.*;

public class CF1256 {
    public static void main(String[] args) {
        Scanner In = new Scanner(System.in);
        int Q, A, B, N, S;
        Q = In.nextInt();
        while (Q-- > 0) {
            A = In.nextInt();
            B = In.nextInt();
            N = In.nextInt();
            S = In.nextInt();
            int I = Math.min(S / N, A) * N;
            //System.out.println(I);
            if (I + B >= S) System.out.println("YES");
            else System.out.println("NO");
        }
    }
}
View Code

 

B:優化

題意:給定一個排列,如今讓你作一套操做,使得字典序最小。spa

思路:貪心,先儘可能把1提到前面,而後是2....,若是知足{位置交換沒用過,並且比左邊的小就換}code

/*
@author nimphy
@create 2019-11-05-11:34
about:CF1256B
*/

import java.util.*;

public class CF1256 {
    static int[] a = new int[1010];
    static boolean[] vis = new boolean[1010];

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

        int Q, N;
        Q = In.nextInt();
        while (Q-- > 0) {
            N = In.nextInt();
            for (int i = 1; i <= N; i++) {
                a[i] = In.nextInt();
                vis[i] = false;
            }
            for (int i = 1; i <= N; i++) {
                int pos=0;
                for (int j = 1; j <= N; j++) {
                    if (a[j] == i) {
                        pos = j;
                        break;
                    }
                }
                while (pos > i && !vis[pos]&&a[pos]<a[pos-1]) {
                    int t = a[pos];
                    a[pos] = a[pos - 1];
                    a[pos - 1] = t;
                    vis[pos] = true;
                    pos--;
                }
            }
            for (int i = 1; i <= N; i++) {
                System.out.print(a[i]+" ");
            }
            System.out.println();
        }
    }
}
View Code

 

C:blog

思路:貪心+狀況可能多-----忽略。排序

 

------------------------------白嫖了輸入優化--------------------------------------get

 

D:string

題意: 給定N,K和一個01串,你能夠交換相鄰的字符,可是次數不超過K次,求最後的最小字典序串。

思路:結論是,把前面的0移動到越前面,字典序最小。 那麼咱們從前日後掃‘0’,若是前面有x個‘1’,那麼它能夠和前面第min(x,K)個位置交換。

/*
@author nimphy
@create 2019-11-05-11:34
about:CF1256C
*/

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

public class CF1256 {

    private static boolean doLocalTest = System.getSecurityManager() == null;
    private Scanner sc = new Scanner(System.in);
    private PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));

    public static void main(String[] args) {
        long executionTime = 0;
        if (doLocalTest) {
            executionTime = System.currentTimeMillis();
            try {
                System.setIn((new FileInputStream("in")));
//                System.setOut(new PrintStream(new File("out")));
            } catch (FileNotFoundException e) {
                doLocalTest = false;
            }
        }
        CF1256 cf1256 = new CF1256();
        cf1256.solve();
        if (doLocalTest) {
            cf1256.out.println("======== [ End of Output] ========");
            cf1256.out.println("> Time Spent: " + (System.currentTimeMillis() - executionTime) + " ms");
        }
        cf1256.out.flush();
    }

    static char[] a;
    static char[] ans = new char[10010];

    void solve() {
        int T, N;
        long K;
        T = sc.nextInt();
        while (T-- > 0) {
            N = sc.nextInt();
            K = sc.nextLong();
            a = sc.next().toCharArray();
//            System.out.println(Arrays.toString(a));
            int pre = 0;
            for (int i = 0; i < N && K > 0; i++) {
                if (a[i] == '1') {
                    pre++;
                    continue;
                }
                if (pre == 0) continue;
                int t = pre;
                if (K < t) t = (int) K;
                a[i - t] = '0';
                a[i] = '1';
                K -= t;
            }
            for (int i = 0; i < N; i++) out.print(a[i]);
            out.println();
        }
    }
}

class Scanner {
    private BufferedReader bufferedReader;
    private StringTokenizer stringTokenizer;

    Scanner(InputStream in) {
        bufferedReader = new BufferedReader(new InputStreamReader(in));
        stringTokenizer = new StringTokenizer("");
    }

    String nextLine() {
        try {
            return bufferedReader.readLine();
        } catch (IOException e) {
            throw new IOError(e);
        }
    }

    boolean hasNext() {
        while (!stringTokenizer.hasMoreTokens()) {
            String s = nextLine();
            if (s == null) {
                return false;
            }
            stringTokenizer = new StringTokenizer(s);
        }
        return true;
    }

    String next() {
        hasNext();
        return stringTokenizer.nextToken();
    }

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

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

 

E:

題意:給的大小爲N的集合,而後分組,每組元素個數不小於3個,代價是每組的最大值減最小值之和,求分組是的代價最小。

分組越多越好,那麼能夠假設最多6個一組。    懶得寫了,畢竟要輸出具體分組。(還不會java的結構體排序)

 

F:

題意:給定兩個字符數組A[] , B[],長度相同。 如今你能夠選擇能夠長度len,而後進行任意輪操做,每輪操做是反轉一段A和一段B,其長度都是len,問最後是否能夠相同。

思路:發現len選2最優,由於你不管len選多大,均可以由len=2轉移過來,達到一樣的效果;   那麼如今len=2了,又發現,若是某一組有相同的字符,那麼另一組能夠任意排列,因此YES;    而若是A和B都是各異的字符,那麼若是逆序對的奇偶性相同,則YES。

/*
@author nimphy
@create 2019-11-05-11:34
about:CF1256F
*/

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

public class Main {

    private static boolean doLocalTest = System.getSecurityManager() == null;
    private Scanner sc = new Scanner(System.in);
    private PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));

    public static void main(String[] args) {
        long executionTime = 0;
        if (doLocalTest) {
            executionTime = System.currentTimeMillis();
            try {
                System.setIn((new FileInputStream("in")));
//                System.setOut(new PrintStream(new File("out")));
            } catch (FileNotFoundException e) {
                doLocalTest = false;
            }
        }
        Main fcy = new Main();
        fcy.solve();
        if (doLocalTest) {
            fcy.out.println("======== [ End of Output] ========");
            fcy.out.println("> Time Spent: " + (System.currentTimeMillis() - executionTime) + " ms");
        }
        fcy.out.flush();
    }

    String a,b;
    int[] num1=new int[26];
    int[] num2=new int[26];
    void solve() {
        int T, N;
        long K;
        T = sc.nextInt();
        while (T-- > 0) {
            N = sc.nextInt();
            a=sc.next();
            b=sc.next();
            for(int i=0;i<26;i++) num1[i]=num2[i]=0;
            for(int i=0;i<N;i++) {
                num1[a.charAt(i)-'a']++;
                num2[b.charAt(i)-'a']++;
            }
            boolean Flag=true;
            for(int i=0;i<26;i++) if(num1[i]!=num2[i]) Flag=false;
            if(!Flag) {
                out.println("NO");
                continue;
            }
            for(int i=0;i<26;i++) if(num1[i]>1||num2[i]>1) Flag=false;
            if(!Flag) {
                out.println("YES");
                continue;
            }
            long inv1=0,inv2=0;
            for(int i=0;i<26;i++) num1[i]=num2[i]=0;
            for(int i=0;i<N;i++){
                for(int j=a.charAt(i)-'a'+1;j<26;j++) inv1+=num1[j];
                for(int j=b.charAt(i)-'a'+1;j<26;j++) inv2+=num2[j];
                num1[a.charAt(i)-'a']++;
                num2[b.charAt(i)-'a']++;
            }
            if(Math.abs(inv1-inv2)%2==0) out.println("YES");
            else out.println("NO");
        }
    }
}

class Scanner {
    private BufferedReader bufferedReader;
    private StringTokenizer stringTokenizer;

    Scanner(InputStream in) {
        bufferedReader = new BufferedReader(new InputStreamReader(in));
        stringTokenizer = new StringTokenizer("");
    }

    String nextLine() {
        try {
            return bufferedReader.readLine();
        } catch (IOException e) {
            throw new IOError(e);
        }
    }

    boolean hasNext() {
        while (!stringTokenizer.hasMoreTokens()) {
            String s = nextLine();
            if (s == null) {
                return false;
            }
            stringTokenizer = new StringTokenizer(s);
        }
        return true;
    }

    String next() {
        hasNext();
        return stringTokenizer.nextToken();
    }

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

    long nextLong() {
        return Long.parseLong(next());
    }
}
View Code
相關文章
相關標籤/搜索