試題編號: | 201709-2 |
試題名稱: | 公共鑰匙盒 |
時間限制: | 1.0s |
內存限制: | 256.0MB |
問題描述: |
問題描述 有一個學校的老師共用N個教室,按照規定,所有的鑰匙都必須放在公共鑰匙盒裏,老師不能帶鑰匙回家。每次老師上課前,都從公共鑰匙盒裏找到自己上課的教室的鑰匙去開門,上完課後,再將鑰匙放回到鑰匙盒中。 輸入格式 輸入的第一行包含兩個整數N, K。 輸出格式 輸出一行,包含N個整數,相鄰整數間用一個空格分隔,依次表示每個掛鉤上掛的鑰匙編號。 樣例輸入 5 2 樣例輸出 1 4 3 2 5 樣例說明 第一位老師從時刻3開始使用4號教室的鑰匙,使用3單位時間,所以在時刻6還鑰匙。第二位老師從時刻2開始使用鑰匙,使用7單位時間,所以在時刻9還鑰匙。 樣例輸入 5 7 樣例輸出 1 2 3 5 4 評測用例規模與約定 對於30%的評測用例,1 ≤ N, K ≤ 10, 1 ≤ w ≤ N, 1 ≤ s, c ≤ 30; |
java:(100分通過)
法一:只用二維數組排序
轉載自 https://blog.csdn.net/qq_26580757/article/details/78513540
其中:多維數組按某列排序可參看https://www.cnblogs.com/whaozl/p/4061993.html
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
static int res[];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int a[][] = new int[k][3];
for (int i = 0; i < a.length; i++) {
a[i][0] = sc.nextInt();
a[i][1] = sc.nextInt();
a[i][2] = sc.nextInt()+a[i][1];
}
int as[][] = a.clone();
int ae[][] = a.clone();
Sort(as,1);
Sort(ae,2);
res = new int[n+1];
for (int i = 1; i < res.length; i++) {
res[i] = i;
}
int t = 0;
int s = 0,e = 0;
while((t++)<10101){//10001不夠,只有70分;所以記得加上上課時間c,最長爲100;
for (; e < ae.length; e++) {
if(t<ae[e][2]) break;
if(t==ae[e][2]){
PBack(ae[e][0]);
}
}
for (; s < as.length; s++) {
if(t<as[s][1]) break;
if(t==as[s][1]){
Use(as[s][0]);
}
}
if(s==as.length&&e==ae.length) break;
}
ShowAns();
}
private static void PBack(int name) {
for (int i = 1; i < res.length; i++) {
if(res[i] == -1){
res[i] = name;
break;
}
}
}
private static void Use(int name) {
for (int i = 1; i < res.length; i++) {
if(res[i] == name){
res[i] = -1;
break;
}
}
}
private static void Sort(int[][] a,int i) {
Arrays.sort(a,new Comparator<int[]>(){
public int compare(int[] o1, int[] o2) {
if(o1[i]==o2[i]) return o1[0]-o2[0];
return o1[i]-o2[i] ;
}
});
}
public static void ShowAns(){
for (int i = 1; i < res.length; i++) {
System.out.print(res[i]+" ");
}
System.out.println();
}
}
法二:
轉載自:https://blog.csdn.net/hunjue0915/article/details/80670903
import java.util.*;
public class Main {
static int time=1,maxtime=0,keynum;
static int[] keylist;
static List<Teacher> teacherList=new ArrayList<>();
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
keynum=in.nextInt();
int teachernum=in.nextInt();
for(int i=0;i<teachernum;i++){
int key=in.nextInt();
int s=in.nextInt();
int c=in.nextInt();
if (s+c>maxtime){
maxtime=s+c;
}
Teacher teacher=new Teacher(key,s,s+c);
teacherList.add(teacher);
}
keylist=new int[keynum];
for (int i=0;i<keynum;i++){
keylist[i]=i+1;
}
while (time<=maxtime){
returnkey();
getkey();
time++;
}
for (int i=0;i<keylist.length;i++){
System.out.print(keylist[i]+" ");
}
}
private static void getkey() {
for (int i=0;i<teacherList.size();i++){
if (teacherList.get(i).s==time){
int temp=teacherList.get(i).key;
for (int j=0;j<keylist.length;j++){
if (keylist[j]==temp){
keylist[j]=0;
}
}
}
}
}
private static void returnkey() {
List<Integer> returnlist=new ArrayList<>();
for (int i=0;i<teacherList.size();i++){
int temp=teacherList.get(i).end;
if (temp==time){
returnlist.add(teacherList.get(i).key);
}
}
if (returnlist.isEmpty()){
return;
}else {
//將要歸還的鑰匙從小到大排序
for (int i = 0; i < returnlist.size() - 1; i++) {
for (int j = 0; j < returnlist.size() - 1 - i; j++) {
if (returnlist.get(j) > returnlist.get(j + 1)) {
int temp = returnlist.get(j);
returnlist.set(j, returnlist.get(j + 1));
returnlist.set(j + 1, temp);
}
}
}
}
int m=0;
for (int i=0;i<keylist.length;i++){
if (keylist[i]==0){
keylist[i]=returnlist.get(m);
if (m<returnlist.size()-1){
m++;
}else {
break;
}
}
}
}
static class Teacher{
int key,s,end;
public Teacher(int key, int s, int end) {
this.key = key;
this.s = s;
this.end = end;
}
}
}
c++:
#include<bits/stdc++.h>
using namespace std;
//發生事件的結構體
class node{
public:
int bt;//發生的時間
int key;//鑰匙編號
int act;//借或者換,表示爲1和0
};
//排序函數,按時間、動作、鑰匙編號升序排列
bool cmpnode(const node &a,const node &b){
if(a.bt!=b.bt) return a.bt<b.bt;
if(a.act!=b.act) return a.act<b.act;
return a.key<b.key;
}
int main(){
int n,k; cin>>n>>k;
// 產生鑰匙盒與事件數組
int *keys=new int[n];
node *acts=new node[2*k];
// 初始化鑰匙盒數組
for(int i=0;i<n;i++)
keys[i]=i+1;
// 讀取上下課時間,拆分爲取和換鑰匙事件
for(int i=0;i<k;i++){
int w, s, c;cin>>w>>s>>c;
acts[2*i].key=w;
acts[2*i].bt=s;
acts[2*i].act=1;
acts[2*i+1].key=w;
acts[2*i+1].bt=s+c;
acts[2*i+1].act=0;
}
// 多條件排序(時間、動作、鑰匙編號)
sort(acts,acts+2*k,cmpnode);
for(int i=0;i<2*k;i++){
if(acts[i].act==1){//取鑰匙 key->0
for(int j=0;j<n;j++){
if(keys[j]==acts[i].key){
keys[j]=0;
break;
}
}
}
else{ //還鑰匙 0->key
for(int j=0;j<n;j++){
if(keys[j]==0){
keys[j]=acts[i].key;
break;
}
}
}
}
// 打印輸出
for(int i=0;i<n;i++){
cout<<keys[i];
if(i!=n-1) cout<<" ";//特殊處理最後一個鑰匙
}
// 刪除動態數組
delete []acts;
delete []keys;
return 0;
}