내 코드에 대한 테스트 케이스가 더 필요하지만 정확한 답을 표시하고 있지만 UVa는 여전히이를 받아들이지 않습니다.UVA 테스트 케이스 822
지침은 입니다. 요청이 접수되면 미리 지정된 주제 목록에 따라 분류됩니다. 지원 담당자의 각 구성원은 이러한 주제 중 하나 이상에 대한 책임이 있으며 각 주제에는 하나 이상의 지원 담당자가 있습니다. 직원은 각기 다른 수준의 전문성을 갖추고 있기 때문에 각 직원은 자신이 처리 할 수있는 주제의 우선 순위 목록을 가지고 있습니다. 교직원은 지정된 영역 밖에서 요청을 처리 할 수 없습니다.
스태프 멤버가 사용 가능 해지면 스태프 멤버는 우선 순위 목록에 따라 대기중인 요청 풀 중에서 선택합니다. 시간 t에 도착하는 모든 요청은 시간 t에 할당 할 수 있습니다. 두 명의 직원이 동시에 사용할 수있는 경우 가장 최근에 작업이 가장 일찍 계획된 사람에게 일정이 지정됩니다. 여전히 동점이있는 경우, 직원의 입력 목록에서 ID 번호가 먼저 나타나는 사람에게 일정이 우선됩니다. 개업 초기에는 모든 직원이 요청을 처리 할 수 있습니다.
입력은 여러 가지 시나리오로 구성됩니다. 각 시나리오는 요청 항목 수로 시작하며 20보다 크지 않은 양의 정수입니다. 각 항목에 대한 설명이 이어집니다. 각 설명은 고유 항목 식별자, 해당 항목에 대한 요청 수, 해당 항목에 대한 첫 번째 요청이 수신되기 전 경과 된 시간, 요청을 처리하는 데 필요한 시간 및 연속 요청 간의 시간이라는 5 개의 정수 값으로 구성됩니다. 이 값 중 세 번째를 제외한 모든 값은 양의 정수입니다. 첫 번째 요청이 경과 할 때까지의 경과 시간은 0 일 수 있습니다. 이것 다음에, 인원의 수는 주어진다. 이것은 5를 초과하지 않는 양의 정수입니다. 마지막으로, 각 사람에 대한 설명은 세 개 이상의 양의 정수 값의 형태로 주어집니다 : 사람의 고유 식별 번호,이 사람이 다루는 주제의 수 및 해당 인물에 대해 가장 높은 우선 순위에서 가장 낮은 우선 순위로 배열 된 주제 식별자 목록. 마지막 시나리오는 0입니다.
출력은 각 시나리오의 총 시간 (분) 합계입니다. 입력 : 여기
내 프로그램의 실행 예입니다3
128 20 0 5 10
134 25 5 6 7
153 30 10 4 5
4
10 2 128 134
11 1 134
12 2 128 153
13 1 153
1
128 5 0 1 10
1
11 1 128
0
출력 :
Scenario 1: All requests are serviced within 195 minutes.
Scenario 2: All requests are serviced within 41 minutes.
내 코드 :
import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
class Main {
public static void main(String[] args) {
class RequestSet{//Structure for RequestSets
private int id;
private int qty;
private int firstReq;
private int timeReq;
private int nextReq;
private int sucReq;
private int initialized;
public RequestSet(int id,int qty,int firstReq,int timeReq,int nextReq){
this.id=id;
this.qty=qty;
this.firstReq=firstReq;
this.timeReq=timeReq;
this.nextReq=nextReq;
sucReq=nextReq;
initialized=0;
}
public void decReq(){//Decreases the time attributes
if(firstReq>1)
firstReq-=1;
else if((nextReq!=1) && (firstReq==0 || firstReq==1))
nextReq-=1;
}
public int getInitial(){//Determines if the Request set has already enqueued its first element
return initialized;
}
public void setInitial(){
initialized=1;
}
public void decQty(){//Decreases the requests qty of the Request set(When the set enqueues a request to the spool)
qty--;
}
public void restoreReq(){//Replenishes the time remaining until the next Request
nextReq=sucReq;
}
public int getID(){
return id;
}
public int getQty(){
return qty;
}
public int getFReq(){
return firstReq;
}
public int getTimeReq(){
return timeReq;
}
public int getNReq(){
return nextReq;
}
}//End of Class "Request Set"
class Request{//Structure for a Request
private int topicID;
private int minsNeeded;
public Request(int id, int mins){
topicID = id;
minsNeeded = mins;
}
public int getMins(){
return minsNeeded;
}
public int getId(){
return topicID;
}
}//End of Class "Request"
class Server{//Structure for Agents
private int id;
private int topicsQty;
private ArrayList<Integer> specialty;
private int available;
private int recentJobAgo;
private int occTil;
private int listStanding;
public Server(int id, int qty, int stand){
this.id=id;
topicsQty=qty;
available=1;
specialty = new ArrayList();
occTil=0;
listStanding=stand;
}
public Server(){
}
public int getOcc(){
return occTil;
}
public int getPosition(){//Returns the position of the agent in the input list(Agent which was inputted first)
return listStanding;
}
public int getRecJ(){//Return the time elapsed after the most recent job
return recentJobAgo;
}
public void display(){
int i;
for(i=0;i<topicsQty;i++){
System.out.printf("%d ",specialty.get(i));
}
}
public void progress(){
if(available==1)//if agent is available, the time elapsed after the most recent job is incremented
recentJobAgo+=1;
else if(available==0 && (occTil==1 || occTil==0)){//case wherein an agent finishes a request. "occTil==0"-occupied til 0 mins
available=1;
occTil=0;
}
else//case where in the agent's still occupied, so we decrease the "Occupied Until" attribute
occTil--;
}
public int isAvailable(){
return available;
}
public void occupy(int time){//Agent begins servicing a request
if(time>0){
available=0;
recentJobAgo=0;
occTil=time;
}
}
public void doneReq(){
available=1;
}
public void learnTopics(int topicID){//Gets the input from the 3rd to the nth input, for agents. Priority topics.
int i;
specialty.add(topicID);
}
public int canDo(int id, int i){//returns 1 if the topicID is in the agent's ArrayList of topic names
int result=0;
//for(i=0;i<topicsQty;i++){
if(i<specialty.size())
if(id==specialty.get(i) && available==1){
result=1;
}
//}
return result;
}
}//End of Class "Server"
int topics, servers, i, result, check, rounds=0, minute;
String dump;
ArrayList<Integer> minutes;
Scanner sc = new Scanner(System.in);
int j, k=0;
int topicID, noReqs, bFirst, timeReq, betReqs, set;
//topicID,# of Requests, time before the 1st request, time requirement to process a request, succeeding time between requests
int agentID, lrndTopics, aTopic;
//agentID, # of priority topics, used to hold a topicID
ArrayList<RequestSet> requests;
Server[] agent;//Could have also been an arraylist
Server temp;//for sorting the agents
String input;
Queue<Request> spool = new LinkedList<>();
minutes=new ArrayList();//Used to hold several results. i.e. if the user inputs several scenarios
int firstInput=sc.nextInt();
if(firstInput!=0){
do{
if(k==0)
{
topics=firstInput;
}
else
{
topics=rounds;
}
dump=sc.nextLine();//Filters out "Enter"/"NextLine"
requests = new ArrayList();
for(i=0; i<topics; i++)
{
// input=sc.nextLine();//Gets a string input containing 5 numbers
// StringTokenizer strToken = new StringTokenizer(input);
// topicID=Integer.parseInt((String)strToken.nextElement());//Gets the first number from the input
// noReqs=Integer.parseInt((String)strToken.nextElement());//Gets the 2nd
// bFirst=Integer.parseInt((String)strToken.nextElement());//3rd
// timeReq=Integer.parseInt((String)strToken.nextElement());//4th
// betReqs=Integer.parseInt((String)strToken.nextElement());//5th
// requests.add(new RequestSet(topicID,noReqs,bFirst,timeReq,betReqs));//Adds the request topic into the RequestSet array
//input=sc.nextLine();//Gets a string input containing 5 numbers
//StringTokenizer strToken = new StringTokenizer(input);
topicID=sc.nextInt();//Gets the first number from the input
noReqs=sc.nextInt();//Gets the 2nd
bFirst=sc.nextInt();//3rd
timeReq=sc.nextInt();//4th
betReqs=sc.nextInt();//5th
requests.add(new RequestSet(topicID,noReqs,bFirst,timeReq,betReqs));//Adds the request topic into the RequestSet array
}
servers=sc.nextInt();//Gets the number of Agents
dump=sc.nextLine();//Filters out "Enter"/"NextLine"
agent = new Server[servers];//Creates an array of agents
for(i=0; i<servers; i++)
{
// input=sc.nextLine();//Accepts a line of input
// StringTokenizer strToken = new StringTokenizer(input);
//System.out.println("agent section");
agentID=sc.nextInt();//Gets the first input
lrndTopics=sc.nextInt();//Gets the 2nd input
agent[i] = new Server(agentID, lrndTopics,i);//Creates an agent in the array
for(j=0;j<lrndTopics; j++)//Gets the priority list of topics, loops n times based on the 2nd input
{
aTopic=sc.nextInt();//Gets the 3rd input until the nth input
agent[i].learnTopics(aTopic);//calls a function that adds the topic into the agent's priority list.
}
}//Gets inputs for the agent set
set=0;//prevents the "minute" variable from increasing at the beginning
minute=0;
//Request processing section
//PROCESSING PART
do{
if(set==1)
minute++;
for(i=0;i<topics;i++)//Checks if a request set is able to add a request into the Queue
{
if((requests.get(i).getFReq()==0 || requests.get(i).getFReq()==1) && requests.get(i).getInitial()==0)
{
spool.add(new Request(requests.get(i).getID(),requests.get(i).getTimeReq()));
requests.get(i).decQty();
requests.get(i).restoreReq();
requests.get(i).setInitial();
}
else if(requests.get(i).getInitial()==1 && (requests.get(i).getNReq()==1) && requests.get(i).getQty()>0)
{
spool.add(new Request(requests.get(i).getID(),requests.get(i).getTimeReq()));
requests.get(i).decQty();
requests.get(i).restoreReq();
}
else
{
requests.get(i).decReq();
}
}
//Sorts the agents according to their idle time. Most idle places first
// for(i=0;i< (servers-1);i++)
// {
// for(j=0;j< servers-i-1;j++)
// if(agent[j].getRecJ()<agent[j+1].getRecJ())
// {
// temp=agent[j];
// agent[j]=agent[j+1];
// agent[j+1]=temp;
// }
// }
//Swaps agents when they have equal idle time. The agent that was inputted first places first
for(i=0;i< servers-1;i++)
{
if(agent[i].getRecJ()==agent[i+1].getRecJ()){
if(agent[i].getPosition()>agent[i+1].getPosition()){
temp=agent[i];
agent[i]=agent[i+1];
agent[i+1]=temp;
}
}
}
//Decreases all the time attributes of all the agents
for(i=0;i<servers;i++)
{
agent[i].progress();
}
//Checks all agents if they're qualified to service a topic
for(j=0;j<topics;j++)
{
for(i=0;i<servers;i++)
{
if(spool.peek()!=null)
{
if(agent[i].canDo(spool.peek().getId(),j)==1)
{
agent[i].occupy(spool.peek().getMins());
spool.remove();
}
}
}
}
set=1;
check=0;
result=0;
//Checks all the requests sets' number of remaining topics
for(j=0;j<topics;j++)
{
if(requests.get(j).getQty()!=0)
check=1;
}
//Checks if all the agents are occupied
for(i=0;i<servers;i++)
{
if(agent[i].isAvailable()==0)
result=1;
}
if(check==0 && result==1)//if all request sets are empty, check if an agent is still servicing a topic
check=1;
// System.out.println(requests.get(0).getQty()+" QTY");
// System.out.println(agent[0].getOcc()+" OCCTIL");
}while(check==1);//End of PROCESSING PART
minutes.add(minute);//Adds the result into the results array
rounds=sc.nextInt();//If '0' is inputted, the program ends, otherwise, it will be stored into the "topics" variable
if(rounds!=0)
k++;
for(i=topics-1;i>=0;i--)//Removes all the request sets from the requests array, to prepare for a next scenario
requests.remove(i);
}while(rounds!=0);
}
//Displays all the results
for(i=0;i<minutes.size();i++)
System.out.println("Scenario "+(i+1)+": All requests are serviced within "+minutes.get(i)+" minutes.");
}
}