Blue Bridge Cup Question 20200405

1. Quick sort

public class KuaiSort {
	public static int quickSelect(int a[],int l,int r,int k) {
		Random rand=new Random();
		int p=rand.nextInt(r-l+1)+l;
		int x=a[p];
		int tmp=a[p];
		a[p]=a[r];
		a[r]=tmp;
		int i=l,j=r;
		while(i<j) {
			while(i<j&&a[i]<x) i++;
			if(i<j) {
				a[j]=a[i];
				j--;
			}
			while(i<j && a[j]>x) j--;
			if(i<j) {
				a[i]=a[j];
				i++;
			}
		
		}
		a[i]=x;
		p=i;
		if(i-l+1==k) return a[i];
		if(i-l+1<k) return quickSelect(a,i+1,r,k-i+l-1);
		else return quickSelect(a, l, i-1, k);
		
	}
	public static void main(String[] args) {
		int[] a= {1,4,2,8,5,7};
		System.out.println(quickSelect(a, 0, 5, 2));
	}
}

2. Spiral polyline

The spiral polyline shown in Figure p1.pgn passes through all the whole points on the plane exactly once.
For the whole point (X, Y), we define its distance to the origin, dis (X, Y), as the length of the spiral polyline segment from the origin to (X, Y).

For example, dis (0, 1) = 3, dis (-2, -1) = 9

Given the coordinates of the whole point (X, Y), can you calculate dis (X, Y)?

[Input format]
X and Y

For 40% data, -1000 <= X, Y <= 1000
For 70% data, -100000 <= X, Y <= 100000
For 100% data, -1000000000 <= X, Y <= 1000000000

[Output format]
output dis (X, Y)

【Input example】
0 1

[Sample output]
3

Resource convention:
peak memory consumption (including virtual machine) <256M
CPU consumption <1000ms

Please output strictly according to the requirements, and do not print extra content like: "Please input ..."

All code is placed in the same source file. After debugging, copy and submit the source code.
Don't use package statements. Do not use the features of jdk1.7 and above.
The name of the main class must be: Main, otherwise it is handled as invalid code.
Insert picture description here

public class LuoXuan {

    public static void main(String[] args){
       Scanner input=new Scanner(System.in);
       int x=input.nextInt();
       int y=input.nextInt();
       System.out.println(dis(x,y));
    }        
    public static int dis(int x,int y){
        if(Math.abs(x)==Math.abs(y))
        {
            if(x>0&&y>0)
                return 4*x*x;
            else if(x>0&&y<0)
                return 4*x*x+2*x;
            else if(x<0&&y>0)
                return 4*x*x-2*y;
            else
                return 4*x*x-4*x;
        }
        else
        {
            if(x>0)
            {
                if(y>0)
                {
                   int max=Math.max(x, y);
                   if(x<y)
                      return 4*max*max-Math.abs(x-y);
                   else
                      return 4*max*max+Math.abs(x-y);
                }
                else
                {
                    y=-y;
                    int max=Math.max(x, y);
                    if(x<y)
                          return 4*max*max+2*max+Math.abs(x-y);
                     else
                          return 4*max*max+2*max-Math.abs(x-y);
                }
            }
            else
            {
                if(y>0)
                {
                     x=-x;
                     int max=Math.max(x,y);
                     if(x<y)
                          return 4*max*max-2*max+Math.abs(x-y);
                     else
                         return 4*max*max-2*max-Math.abs(x-y);
                }
                else
                {
                     x=-x;
                     y=-y;
                    int max=Math.max(x,y);
                    if(x<y)
                       return 4*max*max+4*max-Math.abs(x-y);
                    else
                       return 4*max*max+4*max+Math.abs(x-y)-8*max;
                 }
            }
        }
    }
}



3. Log statistics

Xiaoming maintains a programmer forum. Now he has collected a "like" log with a total of N lines. The format of each line is:

ts id

Indicates that the post with the id number at the time of ts received a "Like".

Now Xiaoming wants to count which posts used to be "hot posts." If a post has received no less than K likes in any time period of length D, Xiao Ming thinks this post was a "hot post".

Specifically, if there is a certain time T that satisfies the post during the period of [T, T + D) (note that it is closed from the left to the right), the post was once "hot" Posts ".

Given the log, please help Xiaoming to count all the post numbers that were "hot posts".

[Input format] The
first line contains three integers N, D and K.
Each log in the following N lines contains two integers ts and id.

For 50% of the data, 1 <= K <= N <= 1000
For 100% of the data, 1 <= K <= N <= 100000 0 <= ts <= 100000 0 <= id <= 100000

[Output format]
Output hot post id in ascending order. One row for each id.

[Input example]
7 10 2
0 1
0 10
10 10
10 1
9 1
100 3
100 3

[Sample output]
1
3

Resource convention:
peak memory consumption (including virtual machine) <256M
CPU consumption <1000ms

Please output strictly according to the requirements, and do not print extra content like: "Please input ..."

import java.util.ArrayList;
import static java.util.Collections.sort;
import java.util.Scanner;

public class Login {
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        int N=input.nextInt();
        int D=input.nextInt();
        int K=input.nextInt();
        final int MAX=100001;
        ArrayList<Obj>[] tsList=new ArrayList[MAX];
        for(int i=0;i<N;i++)
        {
            int ts=input.nextInt();
            int id=input.nextInt();
            Obj obj=new Obj();
            obj.ts=ts;
            if(tsList[id]==null)
                tsList[id]=new ArrayList<Obj>();
            tsList[id].add(obj);
        }
        for(int i=0;i<MAX;i++)
        {
            if(tsList[i]==null) continue;
            ArrayList<Obj>list=(ArrayList<Obj>)tsList[i].clone();
            sort(list);
            boolean key=false;
            for(int j=0;j<list.size();j++)
            {
                for(int m=list.size()-1;m>=j;m--)
                {
                    if(list.get(m).ts-list.get(j).ts<D&&m-j+1>=K)
                    {
                        key=true;
                        break;
                    }
                }
                if(key)
                {
                    System.out.println(i);
                    break;
                }
            }
         }
    }
    public static class Obj implements Comparable<Obj>{
        public int ts;
        Obj()
        {
            ts=0;
        }
        @Override
        public int compareTo(Obj o)
        {
            if(ts>o.ts)
                return 1;
            else if(ts<o.ts)
                return -1;
            else
                return 0;
        }
    }
}


Published 44 original articles · Likes2 · Visits 540

Guess you like

Origin blog.csdn.net/qq_43699776/article/details/105333370