Today, I'll explain the ruler taking method. Take the ruler, take the ruler, take the ruler? In fact, the ruler method in the algorithm can be used to solve the interval problem. Ruler taking method is a common optimization technique in algorithm competition. It is simple to operate and easy to program. Let's have a look!
catalogue
4, Blue Bridge real problem reappears!
1, What is ruler method?
- The ordinary double cycle
for(int i=0;i<n;i++){ //i front to back for(int j=n-1;j>= 0;j--){ //j from back to front //(a mysterious operation) } }
- Become a cycle
for(int i=0,j=n-1;i<j;i++,j--){ //i from front to back, j from back to front //(a mysterious operation) }
2, How to express it?
Loop statements in different programming languages are somewhat the same. When it comes to loops, our brain thinks that for and while are commonly used!
- for recycling method
for(int i=0,j=n-1;i<j;i++,j--){ //i from front to back, j from back to front //(a mysterious operation) }
- while recycling method
int i=0,j=n-1; while(i<j){ //(a mysterious operation) i++; j--; }
3, How to use?
- Scanning in the same direction
i. The direction of j is the same (the same + + or the same --), and the speed may be different, which is very much like the "pursuit and problem" of junior middle school.
Example: [find interval sum] given an array a [] with length n and a number s, find an interval in this array so that the sum of the intervals is equal to s, and output the starting and ending positions of the interval.
import java.util.*; public class Dome { public static void main(String[] args) { int t = 0; System.out.println("Please enter the array length:"); Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int[] a=new int[n]; System.out.println("Please enter the contents of the array:"); for(int i=0;i<n;i++) { a[i]=sc.nextInt(); } System.out.println("Please enter a number:"); Scanner scc=new Scanner(System.in); int s=sc.nextInt(); int sum=a[0],i=0,j=0; while(j<n) { if(sum>=s) { if(sum==s) { System.out.print("Interval starting point:"); System.out.println(i); System.out.println(" "); System.out.println("Interval end point:"); System.out.print(j); } sum-=a[i]; i++; if(i>j) { sum=a[i]; j++; } } if(sum<s) { j++; sum+=a[j]; } } } }
- Reverse scanning
i. The direction of j is different (one + + one --), and the speed may be different, which is very like the "encounter problem" learned in junior middle school.
Example: [palindrome determination] given a string s of length n, determine whether to palindrome.
import java.util.*; public class Dome { public static void main(String[] args) { int t = 0; System.out.println("Please enter a string:"); Scanner s=new Scanner(System.in); String l = s.nextLine(); char[] line=l.toCharArray(); int length=l.length(); if(length==0||length==1) { t=1; } else if(length>1){ for(int i=0,j=length-1;i<j;i++,j--) { if(line[i]==line[j]) { t=1; } else { t=0; } } } if(t==1) { System.out.println("Is palindrome number!"); } else if(t==0) { System.out.println("Not palindromes!"); } } }
4, Blue Bridge real problem reappears!
[log statistics] Xiao Ming maintains a programmer forum. Now he has collected a "like" log with N lines. The format of each line is: ts id. Indicates that the post with id number at ts time receives a "like".
Now Xiao Ming 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 D, Xiaoming thinks that the post was once a "hot post". Specifically, if there is a time when t satisfies that the post has received no less than K likes during the period of [T, T+D) (note that the left closed and right open interval), the post has been a "hot post". Given the log, please help Xiaoming count the number of all posts that have been "hot posts".
The first line contains three integers N, D, and K.
Each of the following N lines contains two integers ts and id.
For 50% of the data, 1 < = k < = n < = 1000
For 100% data, 1 < = k < = n < = 100000 0 < = TS < = 100000 0 < = ID < = 100000
public class Demo8 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int N=sc.nextInt();//Enter the number of log lines int D=sc.nextInt();//Enter time period D int K=sc.nextInt();//Enter the number of likes of hot posts //map is used to store the initial liking time of each post Map<Integer,Integer> map=new HashMap<Integer,Integer>(); //map1 is used to store the number of likes of each post in the specified time period D Map<Integer,Integer> map1=new HashMap<Integer,Integer>(); for(int i=0;i<N;i++) { int a=sc.nextInt();//Enter the post like time int b=sc.nextInt();//Enter post id if(map.get(b)==null) {//If the input post id does not exist in the map, that is, the post is liked for the first time map.put(b, a);//Store the id of the first liked post and the first liked time in the map map1.put(b, 1);//The number of likes for the first time is 1. Add the post id and number of likes to map1 }else {//If there is an input post id in the map, that is, the post is not liked for the first time //Judge whether the input praise time meets the requirements. During the time when the post is [T, t + D] (note that it is the left closed and right open interval), if it meets the requirements, add 1 to the praise number of the post with the corresponding id in map1 if(map.get(b)==a||map.get(b)+D>a) { map1.put(b, map1.get(b)+1); } } } sc.close(); //Traverse map1. If the number of likes of the post is not less than K, the post was once a hot post, and the id of the post is output for (Integer key : map1.keySet()) { if(map1.get(key)>=K) { System.out.println(key); } } } }