首页 > 编程语言 >AtCoder Beginner Contest 263(Java)

AtCoder Beginner Contest 263(Java)

时间:2022-08-23 21:26:33浏览次数:185  
标签:AtCoder Java linkedList int System 263 static new public

A题

桶排序

 1 import java.util.*;
 2 public class Main {
 3     public static void main(String[] args) {
 4         Scanner sc=new Scanner(System.in);
 5         int[] arr=new int[10];
 6         int[] vis=new int[100];
 7         for(int i=0;i<5;i++){
 8             arr[i]=sc.nextInt();
 9             vis[arr[i]]+=1;
10         }
11         int f=0,g=0;
12         for(int i=1;i<=13;i++){
13             if(vis[i]==2) f=1;
14             if(vis[i]==3) g=1;
15         }
16         if((f+g)==2) System.out.println("Yes");
17         else System.out.println("No");
18     }
19 }

B题

简单DFS计数

 1 import java.util.*;
 2 public class Main {
 3     public static int[] p=new int[666];
 4     public static int sum;
 5 
 6     public static void main(String[] args) {
 7         Scanner sc=new Scanner(System.in);
 8         int n=sc.nextInt();
 9         sum=0;
10         for(int i=2;i<=n;i++){
11             p[i]=sc.nextInt();
12         }
13         dfs(n);
14         System.out.println(sum);
15     }
16 
17     private static void dfs(int u) {
18         if(u==1)
19             return ;
20         sum++;
21         dfs(p[u]);
22     }
23 }

C题

题意:

在1 - m的数中,选n个,且保证序列递增。输出所有情况

思路:

暴力DFS即可

 1 import java.util.*;
 2 public class Main {
 3 
 4     public static LinkedList<LinkedList<Integer>> linkedLists=new LinkedList<>();
 5     public static int n,m;
 6     public static void main(String[] args) {
 7         Scanner sc=new Scanner(System.in);
 8         n=sc.nextInt();
 9         m=sc.nextInt();
10 //        System.out.println("fuck1");
11 
12         for(int i=1;i<=m;i++){
13             if(i+n-1>m) break;
14             LinkedList<Integer> linkedList=new LinkedList<>();
15             linkedList.add(i);
16             dfs(linkedList,1);
17         }
18 //        System.out.println("fuck2");
19 //        for(LinkedList<Integer> x:linkedLists){
20 //
21 //            for(int j:x)
22 //                System.out.print(j+" ");
23 //            System.out.println();
24 //        }
25     }
26 
27     private static void dfs(LinkedList<Integer> linkedList,int sum) {
28 
29         if(linkedList.size()==n){
30 //            System.out.println(linkedList);
31 //            linkedLists.add(linkedList);
32             for(int j:linkedList) System.out.print(j+" ");
33             System.out.println();
34             return ;
35         }
36         int last=linkedList.getLast();
37         if(last>m)
38             return;
39         for(int i=last+1;i<=m;i++){
40             linkedList.add(i);
41             dfs(linkedList,sum+1);
42             linkedList.removeLast();
43         }
44     }
45 
46 
47 }

D题

题意:

找出前后两端并赋予相应的值,保证数组之和最小。

思路:

利用前缀和后缀枚举判断

 1 #include<bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 #define int long long
 6 
 7 const int N=2e5+100 ,inf=0x3f3f3f3f;
 8 int arr[N];
 9 int f[N],g[N];
10 signed main(){
11     int n,l,r;
12     cin>>n>>l>>r;    
13     int sum=0;
14     for(int i=1;i<=n;i++){
15         cin>>arr[i];sum+=arr[i];
16     }
17     int s=0;
18     for(int i=1;i<=n;i++){
19         f[i]=min(i*l,f[i-1]+arr[i]);
20     }
21     for(int i=n;i>=1;i--){
22         g[i]=min((n-i+1)*r,g[i+1]+arr[i]);
23     }
24     int ans=min(f[n],g[1]);
25     for(int i=1;i<n;i++){
26         ans=min(ans,(f[i]+g[i+1]));
27     }
28     cout<<ans;
29     return 0;
30 }

 

标签:AtCoder,Java,linkedList,int,System,263,static,new,public
From: https://www.cnblogs.com/pengge666/p/16617840.html

相关文章