2021年csp认证题解


2021年csp认证题解

202109

T1-数组推导

简单贪心

import java.util.Scanner;

public class Maincsp2 {//202109-1	数组推导	100
    static int n,sum1,sum2;
    static int [] a,b = new int[1000];
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        for(int i = 1;i <= n;i ++){
            b[i] = in.nextInt();
            sum1 += b[i];
            if(b[i] != b[i - 1])sum2 += b[i];
        }
        System.out.println(sum1);
        System.out.println(sum2);
    }
}

T2-非零段划分

一道很有意思的差分题。
考虑会出现非零段的条件,当 \(a_{i - 1} < a_i\)\(a_{i - 1}\le x < a_i\) 时,会贡献一个非零段
\(x \ge a_i\) 时此非零段消失,所以通过差分可以实现此操作。

import java.util.Scanner;
import java.util.Vector;

public class Maincsp4 {//202109-2	非零段划分
    static int n,ans,tmp;
    static int [] a = new int[500100];
    static int [] b = new int[10101];
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        for(int i = 1;i <= n;i ++) {
            a[i] = in.nextInt();
            if(a[i - 1] < a[i]){
                b[a[i - 1]] ++ ;
                b[a[i]] --;
            }
        }

        for(int i = 0;i <= 10000;i ++){
            tmp += b[i];
            ans = Math.max(ans,tmp);
        }
        System.out.println(ans);
    }
}

T3-脉冲神经网络

大模拟。这利用邻接表进行建图,对于当前时间下对\(N - 1\)~\(N + P - 1\)的子节点传递脉冲,利用一个二维数组来记录延迟后的时间和子节点对应的脉冲值。
考虑延迟的时间最多为1000,所以可以用循环数组来减少空间。
重点是读清题意,注意小坑。

//import java.util.ArrayList;
import java.util.Scanner;

public class Maincsp6 {//202109-3	脉冲神经网络
    static long next = 1;
    static int N,P,S,T,l;
    static double deltT,ans1,ans2;
    static double [] u = new double[1010];
    static double [] v = new double[1010];
    static double [] a = new double[1010];
    static double [] b = new double[1010];
    static double [] c = new double[1010];
    static double [] d = new double[1010];
    static double [][] I = new double[2110][1010];
    static double [] w = new double[1010];
    static int [] pre = new int[1010];
    static int [] last = new int[2010];
    static int [] other = new int[1010];
    static int [] len = new int[1010];
    static int [] r = new int[1010];
    static int [] cnt = new int[1010];
    static int myrand() {
        next = next * 1103515245 + 12345;
        return (int)((Long.divideUnsigned(next, 65536)) % 32768);
    }
    static void add(int x,int y,double z,int t){
        l ++;
        pre[l] = last[x];
        last[x] = l;
        other[l] = y;
        w[l] = z;
        len[l] = t;
    }
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        N = in.nextInt();S = in.nextInt();
        P = in.nextInt();T = in.nextInt();
        deltT = in.nextDouble();
        int tmpsum = 0;
        while(true){
            if(tmpsum == N)break;
            int R = in.nextInt();
            double V = in.nextDouble(),U = in.nextDouble(),A = in.nextDouble();
            double B = in.nextDouble(),C = in.nextDouble(),D = in.nextDouble();
            for(int i = tmpsum;i <= tmpsum + R;i ++){
                u[i] = U;v[i] = V;a[i] = A;b[i] = B;c[i] = C;d[i] = D;
            }
            tmpsum += R;
        }
        for(int i = 0;i < P;i ++){
            r[i] = in.nextInt();
        }
        for(int i = 1;i <= S;i ++){
            int s = in.nextInt(),t = in.nextInt();
            double W = in.nextDouble();int D = in.nextInt();
            add(s,t,W,D);
        }
        for(int i = 1;i <= T;i ++){
            for(int j = 0;j < P;j ++){
                int ff = myrand();
                if(r[j] > ff){
                    for(int p = last[j + N];p != 0;p = pre[p]){
                        int v1 = other[p];
                        if(i + len[p] <= T)I[(i + len[p]) % 2000][v1] += w[p];
                    }
                }
            }
            for(int j = 0;j < N;j ++){
                double tmp1 = v[j],tmp2 = u[j];
                v[j] = tmp1 + deltT * (0.04 * tmp1 * tmp1 + 5 * tmp1 + 140 - tmp2) + I[i % 2000][j];
                u[j] = tmp2 + deltT * a[j] * (b[j] * tmp1 - tmp2);
                if(v[j] >= 30){
                    v[j] = c[j];u[j] += d[j];
                    cnt[j] ++;
                    for(int p = last[j];p != 0;p = pre[p]){
                        int v1 = other[p];
                        if(i + len[p] <= T)I[(i + len[p]) % 2000][v1] += w[p];
                    }
                }
               I[i % 2000][j] = 0;
            }
        }
        ans2 = ans1 = v[0];
        int ans3 = cnt[0],ans4 = cnt[0];
        for(int i = 0;i < N;i ++) {
            ans1 = Math.max(ans1,v[i]);
            ans2 = Math.min(ans2,v[i]);
            ans3 = Math.max(ans3,cnt[i]);
            ans4 = Math.min(ans4,cnt[i]);
        }
        System.out.printf("%.3f %.3f\n",ans2,ans1);
        System.out.print(ans4 + " " + ans3);
    }
}

T4-收集卡牌

import java.util.Scanner;

public class Maincsp5 {//202109-4 收集卡牌
    static int n,k;
    static double ans;
    static double [] p = new double[20];
    static double [][] dp = new double[65550][100];
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        n = in.nextInt();k = in.nextInt();
        for(int i = 1;i <= n;i ++){
            p[i] = in.nextDouble();
        }
        dp[0][0] = 1;
        for(int s = 0;s < (1 << n);s ++){
            for(int i = 0;i <= n * k;i ++){
                int cnt = 0;
                for(int j = 0;j < n;j ++)if((s & (1 << j)) != 0)cnt ++;
                if(cnt + i / k >= n){
                    ans += dp[s][i] * (cnt + i);
                    continue;
                }
                for(int j = 0;j < n;j ++){
                    if((s & (1 << j)) != 0) {
                        dp[s][i + 1] += dp[s][i] * p[j + 1];
                   //     System.out.println(dp[s][i]);
                    }
                    else dp[s|(1 << j)][i] += dp[s][i] * p[j + 1];
                }
            }
        }
        System.out.printf("%.10f",ans);
    }
}

202104

T1-灰度直方图

简单模拟

import java.util.Scanner;

public class Maincsp1 {//202104-1	灰度直方图
    static int n,m,l;
    static int [][] a = new int[510][510];
    static int [] h = new int [300];
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        m = in.nextInt();l = in.nextInt();
        for(int i = 1;i <= n;i ++){
            for(int j = 1;j <= m;j ++){
                a[i][j] = in.nextInt();
                h[a[i][j]] ++;
            }
        }
        for(int i = 0;i < l;i ++){
            System.out.print(h[i] + " ");
        }
    }
}

T2-邻域均值

简单二维前缀和

import java.util.Scanner;

public class Maincsp3 {//202104-2	邻域均值
    static int n,l,t,r,ans;
    static int [][] a = new int[610][610];
    static int [][] sum = new int[610][610];
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        n = in.nextInt();l = in.nextInt();
        r = in.nextInt();t = in.nextInt();
        for(int i = 1;i <= n;i ++){
            for(int j = 1;j <= n;j ++) {
                a[i][j] = in.nextInt();
                sum[i][j] = sum[i][j - 1] + sum[i - 1][j] + a[i][j] - sum[i - 1][j - 1];
            }
        }
        for(int i = 1;i <= n;i ++){
            for(int j = 1;j <= n;j ++){
                int x = Math.max(i - r,1);
                int y = Math.max(j - r,1);
                int xx = Math.min(i + r,n);
                int yy = Math.min(j + r,n);
                int tmp = sum[xx][yy] - sum[xx][y - 1] - sum[x - 1][yy] + sum[x - 1][y - 1];
                if(t * (xx - x + 1) * (yy - y + 1) >= tmp)ans ++;
            }
        }
        System.out.println(ans);
    }

}

T3-DHCP服务器

import java.util.*;

public class Maincsp11 {//202104-3	DHCP服务器
    static int N,Tdef,Tmax,Tmin,n;
    static String H;
    static Map<String,Integer> map = new HashMap<>();
    static Map<Integer,String> mp = new HashMap<>();
    static int [] vis = new int[10110];// 0 - 未分配 1 - 待分配 2 - 占用 3 - 过期
    static int [] iplast = new int[10010];//地址过期时刻
    static String [] ipnow = new String[10010];//地址占用者
    static int find(int x) {
        for(int i = 1;i <= N;i ++) {
            if(vis[i] == x) {
                return i;
            }
        }
        return 0;
    }
    static int cale(int last,int t) {
        if(last == 0) return t + Tdef;
        else {
            if(last - t >= Tmin) {
                if(last - t <= Tmax)return last;
                else return t + Tmax;
            }
            else return t + Tmin;
        }
    }
    static int check(String x) {
        for(int i = 1;i <= N;i ++) {
            if(ipnow[i].equals(x)) {
                return i;
            }
        }
        return 0;
    }
    static void clear(int x) {
       for(int i = 1;i <= N;i ++) {
            if(iplast[i] <= x) {
                if(vis[i] == 1) {
                    vis[i] = 0;ipnow[i] = "A";iplast[i] = 0;
                }
                else if(vis[i] == 2) {
                    vis[i] = 3;iplast[i] = 0;
                }
                else iplast[i] = 0;
            }
       }
    }
    static void init() {
        for(int i = 1;i <= N;i ++)ipnow[i] = "A";
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        N = in.nextInt();Tdef = in.nextInt();Tmax = in.nextInt();Tmin = in.nextInt();
        H = in.next();
        n = in.nextInt();
        init();
        for(int i = 1;i <= n;i ++) {
            int t = in.nextInt();
            clear(t);
            String send = in.next(),receive = in.next(),type = in.next();
            int ip = in.nextInt(),last = in.nextInt();
            if(!receive.equals(send) && !receive.equals("*")) {
                if(!type.equals("REQ"))continue;
            }
            if(!type.equals("DIS") && !type.equals("REQ"))continue;
            if(receive.equals("*") && !type.equals("DIS"))continue;
            if(receive.equals(send) && type.equals("DIS"))continue;
            int nowip,nowlast;
            if(type.equals("DIS")) {
                int pd = check(send);
                if(pd != 0) {
                    nowip = pd;
                    vis[nowip] = 1;
                }//dis 1.1
                else {
                    int tmp = find(0);//dis1.2
                    if(tmp == 0) {
                        tmp = find(3);//dis1.3
                        if(tmp == 0)continue;//dis1.4
                        else vis[tmp] = 1;
                    }
                    else {
                        vis[tmp] = 1;
                    }//dis 1
                    nowip = tmp;
                }//dis1
                ipnow[nowip] = send;//dis2
                nowlast = cale(last,t);//dis 3
                iplast[nowip] = nowlast;
                System.out.println(H + " " + send + " OFR " + nowip + " " +nowlast);//ofr
            }
            else {
                if(!receive.equals(H)) {
                    for(int j = 1;j <= N;j ++) {
                        if(ipnow[j].equals(send) && vis[j] == 1) {
                            vis[j] = 0;
                            ipnow[j] = "A";
                            iplast[j] = 0;
                        }
                    }
                }
                else {
                   // System.out.println(ipnow[ip]);
                    if(ip <= N && ip >= 1 && ipnow[ip].equals(send)) {
                        nowlast = cale(last,t);
                        vis[ip] = 2;
                        iplast[ip] = nowlast;
                        System.out.println(H + " " + send + " ACK " + ip + " " + nowlast);//ack
                    }
                    else{
                        nowlast = 0;
                        System.out.println(H + " " + send + " NAK " + ip + " " + nowlast);//nak
                    }
                }
            }
        }
    }
}

T4-校门外的树

import java.util.ArrayList;
import java.util.Scanner;

public class Maincsp7 {//202104-4	校门外的树
    static int n;
    static final long mo = 1000000007;
    static int [] a = new int[1010];
    static int [] vis = new int[100010];
    static long [] dp = new long[1010];
    static ArrayList<Integer> p[];
    static long solve(int x,int y){
        long cnt = 0;
        int len = a[x] - a[y];
       // System.out.println(x + " " + y);
        vis[len] = 1;
        for(Integer i:p[len]){
            if(vis[i] == 1){
                continue;
            }
            else {
                cnt ++;vis[i] = 1;
            }
        }
        //System.out.println(cnt);
        return cnt;
    }
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        for(int i = 1;i <= n;i ++){
            a[i] = in.nextInt();
        }
        p = new ArrayList[100010];
        for(int i = 1;i <= a[n];i ++){
            p[i] = new ArrayList<Integer>();
            p[i].add(1);
            for(int j = 2;j <= Math.sqrt(i);j ++){
                if(i % j == 0){
                    p[i].add(j);
                    if(j != i / j)p[i].add(i / j);
                }
            }
        }
        dp[1] = 1;
        for(int i = 2;i <= n;i ++){
            for(int j = i - 1;j >= 1;j --){
                dp[i] = (dp[i] + dp[j] * solve(i,j) % mo) % mo;
            }
            for(int j = 1;j <= a[n];j ++)vis[j] = 0;
        }
        System.out.println(dp[n]);
    }
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM