加拿大資訊競賽 2015 junior problem 4 WAIT TIME
阿新 • • 發佈:2019-02-15
package junior2015;
import java.io.*;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
/*
*
* 方法1:用hashmap解決
*
* */
class MyTimer {
public int sytle;
public int time;
}
public class Junior2015p4 {
public static void main(String[] args) throws Exception {
//File f=new File("C://Users//lenovo//Desktop//hello.txt");
//BufferedReader br1=new BufferedReader(new FileReader(f));
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();
int sum=Integer.parseInt(str);
int count=0;
String letter=" ";
String friend=" ";
//一開始是0為時間開始算,總時間為time
int time=1;
//輸入處理
//用hashmap實現
Map<String,MyTimer> map=new HashMap<String,MyTimer>();
while(true)
{
//程式輸入進行處理
if(count==sum)
{
break;
}
str=br.readLine();
String strline[]=str.split(" ");
letter= strline[0];
friend= strline[1];
///////////////////////////////////
//輸入為w情況
if(letter.equals("W"))
{
//w後面跟著的是總時間
time=time+Integer.parseInt(friend)-1;
}
else{
time++;
}
//用Map實現
if(letter.equals("R"))
{
if(map.get(friend)==null)
{
MyTimer temp=new MyTimer();
temp.sytle=0;
temp.time=time;
map.put((String)friend,temp);
}
else{
map.get(friend).time=time;
}
}
//有s的此時為全部的time
if(letter.equals("S"))
{
map.get(friend).sytle=map.get(friend).sytle+time-map.get(friend).time;
map.get(friend).time=-1;
}
count++;
}
System.out.println();
//輸出處理
//排序處理
Map<String,MyTimer> sortmap=new TreeMap<String,MyTimer>(new MapKeyComparator());
sortmap.putAll(map);
for(Map.Entry<String,MyTimer> entry:sortmap.entrySet())
{
if(entry.getValue().time==-1)
System.out.println(entry.getKey() + " " + entry.getValue().sytle);
else
System.out.println(entry.getKey() + " " + -1);
}
}
}
class MapKeyComparator implements Comparator<String>{
@Override
public int compare(String str1, String str2) {
return str1.compareTo(str2);
}
}
import java.io.*;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
/*
*
* 方法1:用hashmap解決
*
* */
class MyTimer {
public int sytle;
public int time;
}
public class Junior2015p4 {
public static void main(String[] args) throws Exception {
//File f=new File("C://Users//lenovo//Desktop//hello.txt");
//BufferedReader br1=new BufferedReader(new FileReader(f));
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();
int sum=Integer.parseInt(str);
int count=0;
String letter=" ";
String friend=" ";
//一開始是0為時間開始算,總時間為time
int time=1;
//輸入處理
//用hashmap實現
Map<String,MyTimer> map=new HashMap<String,MyTimer>();
while(true)
{
//程式輸入進行處理
if(count==sum)
{
break;
}
str=br.readLine();
String strline[]=str.split(" ");
letter= strline[0];
friend= strline[1];
///////////////////////////////////
//輸入為w情況
if(letter.equals("W"))
{
//w後面跟著的是總時間
time=time+Integer.parseInt(friend)-1;
}
else{
time++;
}
//用Map實現
if(letter.equals("R"))
{
if(map.get(friend)==null)
{
MyTimer temp=new MyTimer();
temp.sytle=0;
temp.time=time;
map.put((String)friend,temp);
}
else{
map.get(friend).time=time;
}
}
//有s的此時為全部的time
if(letter.equals("S"))
{
map.get(friend).sytle=map.get(friend).sytle+time-map.get(friend).time;
map.get(friend).time=-1;
}
count++;
}
System.out.println();
//輸出處理
//排序處理
Map<String,MyTimer> sortmap=new TreeMap<String,MyTimer>(new MapKeyComparator());
sortmap.putAll(map);
for(Map.Entry<String,MyTimer> entry:sortmap.entrySet())
{
if(entry.getValue().time==-1)
System.out.println(entry.getKey() + " " + entry.getValue().sytle);
else
System.out.println(entry.getKey() + " " + -1);
}
}
}
class MapKeyComparator implements Comparator<String>{
@Override
public int compare(String str1, String str2) {
return str1.compareTo(str2);
}
}