Ji Suanke 2019 Blue Bridge Cup National Championship Group B simulation competition interval merge (programming topic)

Summary of questions and solutions for the 2019 Blue Bridge Cup National Championship Group B mock competition:

https://blog.csdn.net/daixinliangwyx/article/details/90231587

 

Question 6

Title: Interval and


Solution: Only violent practices exceed 30%. Enumerate all answer intervals, and then enumerate two disjoint permutation subintervals to see if they can be intersected to form an answer interval. A slight optimization is that there is no need to traverse the right endpoint for the second subinterval, because there is already the length of the answer interval and the length of the first subinterval, the length of the second subinterval can be obtained, and there is a left endpoint, so you can directly Get the right endpoint.

30% code:

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;


public class Main {
    public static InputReader in = new InputReader(new BufferedInputStream(System.in));
    public static PrintWriter out = new PrintWriter(System.out);
    public static int n;
    public static int[] a = new int[300010];
    public static long ans;
    public static TreeSet<Integer> ts;

    public static void main(String[] args) {
        n = in.nextInt();
        ans = 0;
        for (int i = 1; i <= n; i++) {
            a[i] = in.nextInt();
        }
        for (int l = 1; l < n; l++) {
            for (int r = l+1; r <= n; r++) {//数值区间[l, r]
                int flag = 0;
                for (int l1 = 1; l1 < n; l1++) {
                    for (int r1 = l1; r1 < n; r1++) {//排列中的第一个区间 a[l1, r1]
                        for (int l2 = r1+1; l2 <= n; l2++) { //第二个区间 a[l2, l2+r-l-r1+l1-1]
                            ts = new TreeSet<>();
                            for (int i = l1; i <= r1; i++)
                                ts.add(a[i]);
                            for (int i = l2; i <= l2+r-l-r1+l1-1; i++)
                                ts.add(a[i]);
                            if (ts.last() == r && ts.first() == l) {
                                ans++;
                                flag = 1;
                                break;
                            }
                        }
                        if (flag == 1) break;
                    }
                    if (flag == 1) break;
                }
            }
        }
        out.println(ans);
        out.flush();
        out.close();
    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public String nextLine() {
            String str = null;
            try {
                str = reader.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

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

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

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

        public BigInteger nextBigInteger() {
            return new BigInteger(next());
        }

    }
}

100% code:

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
const int N = 1e6 + 7;
const int mod = 1e9 + 7;
int a[N+10];

int main(){
    int n,k,x;
    memset(a,0,sizeof(a));
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;i++){
        scanf("%d",&x);
        a[x]++;//x的个数+1 
    }
    int ans = 1;
    //枚举gcd可能出现的值:从i=2 ~ N; 
    for(int i = 2 ;i < N; i++){
        int cnt = 0;
        for(int j = i;j < N; j += i) cnt += a[j]; //能被i整除的数 一定是 i i+i i+i+i .... 
        if(cnt >= k) ans = i;//比k大 那么选出k个数 他们的gcd就是当前的i 
    }
    printf("%d\n",ans);
    return 0;
} 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326488478&siteId=291194637