Original title link: http://lx.lanqiao.cn/problem.page?gpid=T309
Welcome to my Blue Bridge Cup OJ Problem Solution~ https://blog.csdn.net/richenyunqi/article/details/80192062
Algorithms Improve New Microsoft Word Documents
Time limit: 1.0s Memory limit: 256.0MB
Problem Description
L was writing a question, and he created a new word document. He couldn't think of a name. Someone beside him asked in surprise, "Is your question called "New Microsoft Word Document"?" L was overjoyed, slapped the table, and said: " Okay, let’s call it this name.”
Observe carefully, when you create a new word document, you will get a file named “New Microsoft Word Document.doc”, and then create a new one named “New Microsoft Word Document (2 ).doc", and then create a new one, it is "New Microsoft Word Document (3).doc". Continue to build, the number keeps increasing. If you now create three new documents, and then delete "New Microsoft Word Document (2).doc", and then create a new one, you will get a "New Microsoft Word Document (2).doc" again.
Strictly speaking, every time Windows creates a new document, it will select a minimum positive integer that does not repeat the existing file number as the number of the new document.
Please program to simulate the above process and support the following two operations
New: Create a new word document and feedback the number of the newly created document
Delete id: Delete a word document with the number id and report back whether the deletion is successful
Initially , there is no file, "New Microsoft Word document.doc" counts as 1.
Observe carefully, when you create a new word document, you will get a file named “New Microsoft Word Document.doc”, and then create a new one named “New Microsoft Word Document (2 ).doc", and then create a new one, it is "New Microsoft Word Document (3).doc". Continue to build, the number keeps increasing. If you now create three new documents, and then delete "New Microsoft Word Document (2).doc", and then create a new one, you will get a "New Microsoft Word Document (2).doc" again.
Strictly speaking, every time Windows creates a new document, it will select a minimum positive integer that does not repeat the existing file number as the number of the new document.
Please program to simulate the above process and support the following two operations
New: Create a new word document and feedback the number of the newly created document
Delete id: Delete a word document with the number id and report back whether the deletion is successful
Initially , there is no file, "New Microsoft Word document.doc" counts as 1.
input format
In the first line, a positive integer n represents the number of operations, and in the next n lines, each line represents an operation. If the behavior is "New", it means new, and if it is "Delete id", it means to delete the document numbered id, where id is a positive integer. The operations are performed sequentially in the order in which they were entered.
output format
For each line of input, output its feedback result. For the new operation, output the number of the newly created document; for the delete operation, feedback whether the deletion is successful: if the deleted file exists, the deletion is successful, and "Successful" is output, otherwise, "Failed" is output.
sample input
12
New
New
New
Delete 2
New
Delete 4
Delete 3
Delete 1
New
New
New
Delete 4
New
New
New
Delete 2
New
Delete 4
Delete 3
Delete 1
New
New
New
Delete 4
Sample output
1
2
3
Successful
2
Failed
Successful
Successful
1
3
4
Successful
2
3
Successful
2
Failed
Successful
Successful
1
3
4
Successful
Data size and conventions
The number of operations (that is, the number of lines entered) does not exceed 1481
The number of deletions does not exceed 2012
The number of deletions does not exceed 2012
JAVA code:
import java.util. *; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); PriorityQueue<Integer> p=new PriorityQueue<>();//Priority queue, save the uncreated document number for(int i=0;i<n;++i)//将1~n这n个整数压入优先级队列中 p.add(i+1); for (int i = 0; i < n; ++i) { String s=in.next(); if(s.equals("New"))//新建文档 System.out.println(p.poll());//输出优先级队列队首元素编号,并弹出该元素 if(s.equals("Delete")){//删除文档 int a=in.nextInt(); if(p.contains(a))//该文档在优先级队列中,即并未创建,不可删除 System.out.println("Failed");//输出Failed else{//否则 System.out.println("Successful");//输出Successful p.add(a);//该文档被删除,将该文档编号压入优先级队列 } } } } }