竞赛讨论区 > 中兴捧月B题和D题求解答

中兴捧月B题和D题求解答

头像
潜水Y
发布于 2021-04-24 17:07:04 APP内打开
赞 1 | 收藏 0 | 回复14 | 浏览520

B和D题有AC的兄弟吗。。帮忙说下有什么坑?我测试用例过了,但最后提交都不过。。

附上我的代码

/**
*B
*/
import java.util.*;
public class Main{
    public static void main(String[] args){
        int i = 0;
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        ArrayList<Integer> length = new ArrayList<Integer>();
        i = n;
        while(i--!=0)  length.add(sc.nextInt());
        ArrayList<Integer> ans = new ArrayList<Integer>();
        i=0;
        while(i<n){
            ans.add(i,1);
            i++;
        }
        // Arrays.fill(ans,1);
        int cnt = 1;//从第一天开始算
        while(cnt<n+1){//执行n次
            while(length.get(cnt-1)>1){
                int left = (length.get(cnt-1))>>1;
                int right = length.get(cnt-1) - left;
                length.set(cnt-1,left>right? left : right);
                ans.set(cnt-1,ans.get(cnt-1)+1);
            }
            cnt++;
        }

        for(i=0;i<n;i++){
            System.out.println(ans.get(i));
        }
    }
}
/**
*D
*/
import java.util.*;
public class Main{
    public static class Node{
        int key;
        Node prev;
        Node next;
        public Node(int key){
            this.key = key;
        }
    }

    public static void main(String[] args){
        Map<Integer,Node> map;
        map = new HashMap<>();
        Node dummy1 = new Node(-1);
        Node tail1 = dummy1;
        Node dummy2 = new Node(-2);
        Node tail2 = dummy2;
        Scanner sc = new Scanner(System.in);
        String input1[] = sc.nextLine().trim().split(" {1,}");
        int n1 = Integer.parseInt(input1[0]);
        for(int i=1;i<=n1;i++){
            tail1.next = new Node(i);
            tail1.next.prev = tail1;
            tail1 = tail1.next;
            map.put(i,tail1);
        }
        /*
        Node temm1 = dummy1.next;
        while(temm1!=null){
            System.out.println(temm1.key);
            temm1 = temm1.next;
        }
        */

        int n2 = Integer.parseInt(input1[1]);
        for(int i = n1+1;i<=n1+n2;i++){
            tail2.next = new Node(i);
            tail2.next.prev = tail2;
            tail2 = tail2.next;
            map.put(i,tail2);
        }
        int q = Integer.parseInt(input1[2]);
        String input2[] = sc.nextLine().trim().split(" {1,}");
        int change = 0;
        for(int i=0;i<q;i++){
            change = Integer.parseInt(input2[i]);
            Node move = map.get(change);
//            System.out.println("========move=========\n" + move.key);
            move.prev.next = move.next;
            if(move.next!=null){//NOTE
                move.next.prev = move.prev;
            }
            if(change<=n1){
//                System.out.println("============move in queue1==========");
//                System.out.println("move.key=" + move.key);
//                System.out.println("tail1 = " + tail1.key);
//                System.out.println("tail2 = " + tail2.key);
                if(move == tail1){//NOTE
                    tail1 = tail1.prev;
                }
                tail2.next = move;
                move.prev = tail2;
                move.next = null;
                tail2 = move;//NOTE
            }
            else{//move in queue2
//                System.out.println("============move in queue2==========");
//                System.out.println("move.key=" + move.key);
//                System.out.println("tail1 = " + tail1.key);
//                System.out.println("tail2 = " + tail2.key);
                if(move == tail2){//NOTE
                    tail2 = tail2.prev;
                }
                tail1.next = move;
                move.prev = tail1;
                move.next = null;
                tail1 = move;//NOTE
            }

//            System.out.println("==========queue1========");
//            Node temm1 = dummy1.next;
//            while(temm1!=null){
//                System.out.println(temm1.key);
//                temm1 = temm1.next;
//            }
//            System.out.println("=======================");
//
//            System.out.println("==========queue2========");
//            Node temm2 = dummy2.next;
//            while(temm2!=null){
//                System.out.println(temm2.key);
//                temm2 = temm2.next;
//            }
//            System.out.println("=======================");
//
        }
        dummy1 = dummy1.next;
        System.out.print(dummy1.key);
        dummy1 = dummy1.next;
        while(dummy1!=null){
            System.out.print(" " + dummy1.key);
            dummy1 = dummy1.next;
        }
        System.out.print("\n");
        dummy2 = dummy2.next;
        System.out.print(dummy2.key);
        dummy2 = dummy2.next;
        while(dummy2!=null){
            System.out.print(" " + dummy2.key);
            dummy2 = dummy2.next;
        }
    }
}

14条回帖

回帖
加载中...
话题 回帖

等你来战

查看全部

热门推荐