头像

xxy0727




离线:1小时前


最近来访(13)
用户头像
im0use
用户头像
一万小时定律
用户头像
怀瑾握瑜_38
用户头像
大雪龙骑下江南
用户头像
霓虹之夜
用户头像
小白_90
用户头像
笙笙笙笙
用户头像
Eure_ka
用户头像
johnmayer000


xxy0727
7小时前

java 代码,一定要用BufferedReader不然超时

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
    public static void main(String[] args) throws  Exception{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        Long[] a = new Long[n+10] ;
        TreeSet<Long> set = new TreeSet<>();
        //把值作为键,下标作为值
        TreeMap<Long,Integer> map=new TreeMap<>();
        String[] str=br.readLine().split(" ");
        for(int i =0;i < n ; i ++ ){
            a[i] = Long.parseLong(str[i]);
            map.put(a[i],i);
            if(i>0)
            {
                int index=0;
                Long upper = set.ceiling(a[i]);
                Long lower = set.floor(a[i]);
                if(lower==null&&upper!=null){
                    index=map.get(upper)+1;
                    System.out.println(Math.abs(a[i]-upper)+" "+index);
                }
                else if(upper==null&&lower!=null){
                    index=map.get(lower)+1;
                    System.out.println(Math.abs(a[i]-lower)+" "+index);
                }else if(Math.abs(a[i]-lower)==Math.abs(a[i]-upper)){
                    index=map.get(lower)+1;
                    System.out.println(Math.abs(a[i]-lower)+" "+index);
                }else if(Math.abs(a[i]-lower)>Math.abs(a[i]-upper)){
                    index=map.get(upper)+1;
                    System.out.println(Math.abs(a[i]-upper)+" "+index);
                }else{
                    index=map.get(lower)+1;
                    System.out.println(Math.abs(a[i]-lower)+" "+index);
                }

            }
            set.add(a[i]);//数据存入集合
        }
    }
}



活动打卡代码 AcWing 136. 邻值查找

xxy0727
7小时前
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
    public static void main(String[] args) throws  Exception{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        Long[] a = new Long[n+10] ;
        TreeSet<Long> set = new TreeSet<>();
        //把值作为键,下标作为值
        TreeMap<Long,Integer> map=new TreeMap<>();
        String[] str=br.readLine().split(" ");
        for(int i =0;i < n ; i ++ ){
            a[i] = Long.parseLong(str[i]);
            map.put(a[i],i);
            if(i>0)
            {
                int index=0;
                Long upper = set.ceiling(a[i]);
                Long lower = set.floor(a[i]);
                if(lower==null&&upper!=null){
                    index=map.get(upper)+1;
                    System.out.println(Math.abs(a[i]-upper)+" "+index);
                }
                else if(upper==null&&lower!=null){
                    index=map.get(lower)+1;
                    System.out.println(Math.abs(a[i]-lower)+" "+index);
                }else if(Math.abs(a[i]-lower)==Math.abs(a[i]-upper)){
                    index=map.get(lower)+1;
                    System.out.println(Math.abs(a[i]-lower)+" "+index);
                }else if(Math.abs(a[i]-lower)>Math.abs(a[i]-upper)){
                    index=map.get(upper)+1;
                    System.out.println(Math.abs(a[i]-upper)+" "+index);
                }else{
                    index=map.get(lower)+1;
                    System.out.println(Math.abs(a[i]-lower)+" "+index);
                }

            }
            set.add(a[i]);//数据存入集合
        }
    }
}



活动打卡代码 AcWing 862. 三元组排序

xxy0727
9小时前
import java.util.*;

class S implements Comparable<S>
{
    int x;
    double y;
    String z;
    public S(int x ,double y,String z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }
    public int compareTo(S a)
    {
        if(this.x-a.x != 0)
            return this.x-a.x;  //按x升序排序
       return 0;
    }
}
public class Main
{
    public static void main(String args[])
    {
        Scanner in=new Scanner(System.in);
        int n;
        n=in.nextInt();
        S[] arrsy=new S[n];
        for(int i=0; i<n; i++)
        {
            int k1 = in.nextInt();
            double k2 = in.nextDouble();
            String k3=in.next();
           arrsy[i] = new S(k1,k2,k3);
        }
        Arrays.sort(arrsy, 0, n); //排n个数,Arrays.sort(arrsy)则默认排全部
        for(int i=0; i<n; i++){
            System.out.printf("%d %.2f ",arrsy[i].x,arrsy[i].y);
            System.out.println(arrsy[i].z);
        }

    }
}


活动打卡代码 AcWing 829. 模拟队列

xxy0727
9小时前
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int m=sc.nextInt();
        Queue<Integer> k=new LinkedList<>();
        for(int i=0;i<m;i++)
        {
            String s=sc.next();
            if(s.equals("push")){
                int num=sc.nextInt();
                k.add(num);
            }
            else if(s.equals("pop")){
                k.remove();
            }
            else if(s.equals("query")) {
                System.out.println(k.peek());
            }
            else{
                if(k.isEmpty())
                    System.out.println("YES");
                else
                    System.out.println("NO");
            }
        }
    }
}


活动打卡代码 AcWing 828. 模拟栈

xxy0727
9小时前
import java.util.Scanner;
import java.util.Stack;

public class Main {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int m=sc.nextInt();
        Stack<Integer> k=new Stack<Integer>();
        for(int i=0;i<m;i++)
        {
            String s=sc.next();
            if(s.equals("push")){
                int num=sc.nextInt();
                k.push(num);
            }
            else if(s.equals("pop")){
                k.pop();
            }
            else if(s.equals("query")) {
                System.out.println(k.peek());
            }
            else{
                if(k.empty())
                    System.out.println("YES");
                else
                    System.out.println("NO");
            }
        }
    }
}



xxy0727
1天前
class Solution {
    public int[] findNumbersWithSum(int[] nums, int target) {
        Arrays.sort(nums);
        int[] num=new int[2];
        for(int i=0;i<nums.length;i++){
            int k=target-nums[i];
            if(Arrays.binarySearch(nums,k)>=0)
            {
                num[0]=nums[i];
                num[1]=k;
                return num;
            }
        }
        return num;
    }
}


活动打卡代码 AcWing 53. 最小的k个数

xxy0727
1天前
class Solution {
    public List<Integer> getLeastNumbers_Solution(int [] input, int k) {
        List<Integer>a=new ArrayList<>();
        Arrays.sort(input);
        for(int i=0;i<k;i++)a.add(input[i]);
        return a;
    }
}



xxy0727
1天前
class Solution {
    public ListNode deleteDuplication(ListNode head) {
        ListNode p=new ListNode(-1);
        p.next=head;
        ListNode p2=p;
        while(p2.next!=null)
        {
            ListNode q=p2.next;
            while(q!=null&&p2.next.val==q.val)
                q=q.next;
            if(p2.next.next==q)p2=p2.next;
            else p2.next=q;

        }
        return p.next;
    }
}



xxy0727
1天前
class Solution {
    public int[] printListReversingly(ListNode head) {
        ListNode pre=null;
        ListNode cur=head;
        int y=0;
        while(cur!=null)
        {
            ListNode next=cur.next;
            cur.next=pre;
            pre=cur;
            cur=next;
            y++;
        }
        int[] k=new int [y];
        int x=0;
        while(pre!=null)
        {
            k[x++]=pre.val;
            pre=pre.next;
        }
        return k;
    }
}


活动打卡代码 AcWing 35. 反转链表

xxy0727
1天前
class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode pre=null;
        ListNode cur=head;
        while(cur!=null)
        {
            ListNode next=cur.next;
            cur.next=pre;
            pre=cur;
            cur=next;
        }
        return pre;
    }
}