2019 Blue Bridge Cup group JavaB part solution to a problem

Questions G: takeaways priority

Time limit : 1.0s Memory Limit : 512.0MB this question Total: 20 points
【Problem Description】
"Full of it" take-away system maintains N home takeaways, No. 1
N . Each has takeaways
A priority, when the initial (0 time ) priority are 0 .
After every 1 time units, if there is no order takeaways, the priority will be to reduce 1 , the lowest reduction
To 0 ; and if there are orders takeaways, plus the priority unabated, each have a single priority plus 2 .
If a home at a certain time takeaway priority higher than 5 , it will be added to the system cache priority; if
Less than or equal priority 3 , it will be cleared out of the cache priorities.
Given T within moments M pieces order information, please calculate T how many takeaways in excellent time when
First cache.
[Input Format]
The first row contains . 3 integers N , M and T .
The following M lines contains two integers ts and id , it represents ts time numbered id takeaway received
An order.
[Output format]
Output An integer represents the answer.
[Sample input]
2 6 6
1 1
5 2
3 1
6 2
2 1
6 2
Questions G: takeaways priority 10 Tenth Blue Bridge Bowl game software category Province Java University B group
[Sample output]
1
Sample [explain]
6 at time, 1 Hao shop down the priority 3 , the priority is to remove the cache; 2 Hao stores rose priority 6 ,
Join cache priority. Therefore, there is a store (2 No. ) priority buffer.
[Evaluation] Examples scale and agreed with
For 80 % of cases with the evaluation, 1 ≦ N , M , T 10000 .
For the evaluation all use cases, 1 ≦ N , M , T 100000 , . 1 TS T , . 1 ID N .
 

Ideas:

Read Gangster code suddenly realized, not to be given by the data subject with a deviation, and pay attention to the subject let's seek time is T have the priority queue, there are several shops, do not care to achieve inter-j in the process, the creation of a one-dimensional array as a priority queue, then according to the time as the outermost loop, every time there are two cases. , There is no order or orders, not according to the number of orders M as the outermost loop, otherwise they will find it difficult to handle the moment no orders.

Code

But he knocked again, deepen understanding, in a comment. Note isorder [] that every time a record shop which there are orders,

In fact, each one will isorder [shop id] = 1, so isorder [j] = 0 && priority [j]> 0 easy to ensure the priority of the other points will not be operated to the reduction operation on the order of this shop. Title sounds simple, but a lot of pits, a lot of details, it is worth pondering slowly.

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

/**
 * @ClassName: TakeawayShopPriority外卖店优先级
 * @Description: 创建一个记录店铺优先级的数组来存储优先级,另外创建一个用来判断每个时间点是否有订单的数组(1代表有 ,0无)
 * 这里偷懒使用集合充当缓存,可以使用数组之类的。
 * @author: colou
 */
public class Main {

	public static void main(String[] args) {
		Scanner in= new Scanner(System.in);
		int N,M,T;
		N=in.nextInt();
		M=in.nextInt();
		T=in.nextInt();
		Set<Integer> set=new HashSet<Integer>();//优先缓存
		int[] priority=new int[N+1];//优先队列存放各店铺优先级
		int[] isorder=new int[N+1];//标记某一时刻有无订单(即那个店铺有订单),无订单即isorder[j]==0优先级就减少
		int[][] order=new int[M+1][2];//时刻,店埔
		for(int i=1;i<=M;i++){
			for(int j=0;j<2;j++){
				order[i][j]=in.nextInt();
			}
		}
		//以时刻为外循环,每个时刻有两种情况,有无订单
		for(int i=1;i<=T;i++){
			//有订单
			for(int j=1;j<=M;j++){
				//如果订单的时刻和当前时刻相同
				//不必先将数据按照时刻排序,因为循环找的是与当前时刻相同的时刻,即从小到大了
				if(order[j][0]==i){
					priority[order[j][1]]+=2;
					if(priority[order[j][1]]>5&&!set.contains(order[j][1])){
						set.add(order[j][1]);//将店铺加入到优先缓存中
					}
					isorder[order[j][1]]=1;//T时刻有订单
				}
				
			}
			//其他的没有订单的店铺优先级减少
			for(int j=1;j<=N;j++){
				if(isorder[j]==0&&priority[j]>0){
				   priority[j]--;
				}
				if(set.contains(j)&&priority[j]<=3){
					set.remove(j);
				}
			}
			isorder=new int[N+1];//相当于将isorder归零
		}
		System.out.println(set.size());
		in.close();
	}
}

Questions H: Character Correlation Analysis

Time limit : 1.0s Memory Limit : 512.0MB this question Total: 20 points
【Problem Description】
Xiao Ming is analyzing the correlation between a character in the novel. He wanted to know in the novel Alice and Bob
How many times simultaneously.
More precisely, Xiao Ming definition of Alice and Bob "simultaneous" means: in the novel text
In Alice and Bob does not exceed between K characters.
For example the following text:
This is a story about Alice and Bob. Alice wants to send a private message to Bob.
Assumed that K = 20 is , then Alice and Bob simultaneously occurred 2 times, respectively, "Alice and Bob"
And "Bob. Alice" . The former Alice and Bob there between . 5 characters, which has 2 characters.
Note :
1. Alice and Bob are case sensitive, Alice or bob like are not counted.
2. Alice and Bob should be separate words, punctuation and spaces before and after you can have, but can not
There are letters. For example Bobbi and there was not Bob .
[Input Format]
The first row contains an integer K .
The second line contains a line string contains only uppercase and lowercase letters, punctuation and spaces. No longer than
Over one million .
[Output format]
Output An integer representing Alice and Bob often occur simultaneously.
[Sample input]
20
This is a story about Alice and Bob. Alice wants to send a private message to Bob.
Questions H: Character Correlation Analysis 12 Tenth Blue Bridge Bowl game software category Province Java University B group
[Sample output]
2
[Evaluation] Examples scale and agreed with
For the evaluation all use cases, 1 ≦ K 1000000 .

Ideas:

Directly to a whole string split ( ""), that is, each of the characters (including spaces) are out separately, and then must determine whether the time-division two cases, Alice is in front, when the front Bob. Then both sides of each note name is either a space or a symbol, letter can not close, so that each character charAt out using the char ASCII value can be compared with a surrounding this determination is not a letter.

Overall logic is not difficult, directly on the code

Code:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

	public static void main(String[] args) throws IOException {
		// TODO 自动生成的方法存根
        BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
        int K=Integer.parseInt(br.readLine());
        String str=br.readLine();//以换行为结尾标志
        String[] text=str.split("");
        //A-Z:65~90,a~z:97~122
        int count=0;
        int flag=0;
        int L=text.length;
        //Alice在前
        for(int i=0;i<L;i++){
        	if(i+4<L&&text[i].equals("A")&&text[i+1].equals("l")&&text[i+2].equals("i")
        			&&text[i+3].equals("c")&&text[i+4].equals("e")){
        		for(int j=i+6;j<L;j++){
        			if(j+2<L&&j-1>=0&&text[j].equals("B")&&text[j+1].equals("o")&&text[j+2].equals("b")){
        				char a=text[j+3].charAt(0);
        				char b=text[j-1].charAt(0);
        				if((a<'a'||(a>'z'&&a<'A')||a>'Z')&&(b<'a'||(b>'z'&&b<'A')||b>'Z')){//前后不是字母
        					if((j-i-5)<=K){
            					flag=1;//相遇置为1
            				}
        				}
        				
                	}
        			if(flag==1){
        				count++;
        			}
        			flag=0;
        		}
        	}
        	
        }
        //Bob在前
        flag=0;
        for(int i=0;i<L;i++){
        	if(i+2<L&&text[i].equals("B")&&text[i+1].equals("o")&&text[i+2].equals("b")){
        		for(int j=i+4;j<L;j++){
        			if(j+5<L&&j-1>=0&&text[j].equals("A")&&text[j+1].equals("l")&&text[j+2].equals("i")
        					&&text[j+3].equals("c")&&text[j+4].equals("e")){
        				char a=text[j+5].charAt(0);
        				char b=text[j-1].charAt(0);
        				if((a<'a'||(a>'z'&&a<'A')||a>'Z')&&(b<'a'||(b>'z'&&b<'A')||b>'Z')){//前后不是字母
        					if((j-i-5)<=K){
            					flag=1;//相遇置为1
            				}
        				}
        				
                	}
        			if(flag==1){
        				count++;
        			}
        			flag=0;
        		}
        	}
        	
        }
        System.out.println(count);
		
	}

}

 

Published 81 original articles · won praise 91 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_44593822/article/details/103223250