Questions G: takeaways priority
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
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);
}
}