Topcoer-VendingMachine-SRM-145-DIV 2





import java.util.StringTokenizer;


public class VendingMachine {
public static void main(String args[]) {
new VendingMachine();
}


public VendingMachine() {
String[] prices ={"100 200 300",
"600 500 400"};
String[] purchases ={"0,0:0", "1,1:10", "1,2:20",
"0,1:21", "1,0:22", "0,2:35"};
System.out.println(motorUse(prices, purchases));
}


public int motorUse(String[] prices, String[] purchases) {
int shelfCount = prices.length;
int columnCount = -1;
int[][] shelves = new int[shelfCount][];
int[] totalPerColumn = null;
for (int i = 0; i < shelfCount; i++) {
String cShelf = prices[i];
StringTokenizer st = new StringTokenizer(cShelf);
shelves[i] = new int[st.countTokens()];
columnCount = st.countTokens();
if (i == 0) {
totalPerColumn = new int[st.countTokens()];
for (int j = 0; j < totalPerColumn.length; j++)
totalPerColumn[j] = 0;
}
int j = 0;
while (st.hasMoreTokens()) {
int temp = Integer.parseInt(st.nextToken());
totalPerColumn[j] += temp;
shelves[i][j++] = temp;
}
}


// for(int i=0;i<totalPerColumn.length;i++)
// System.out.print(" "+totalPerColumn[i]);
// for(int i=0;i<shelves.length;i++){
// for(int j=0;j<shelves[i].length;j++){
// System.out.print(" "+shelves[i][j]);
// }
// System.out.println();
// }
// Purchase[] purchs=new Purchase[purchases.length];
// for(int i=0;i<purchases.length;i++){
// int i1=purchases[i].indexOf(',');
// int i2=purchases[i].indexOf(':');
// purchs[i]=new Purchase();
// purchs[i].shelf=Integer.parseInt(purchases[i].substring(0,i1));
// purchs[i].column=Integer.parseInt(purchases[i].substring(i1+1,i2));
// purchs[i].time=Integer.parseInt(purchases[i].substring(i2+1));
// }
int cColumn = 0;
int cTime = 0;
int totalRun = 0;
// turn to most expensive;
int mpColumn = 0;
for (int i = 0; i < totalPerColumn.length; i++)
if (totalPerColumn[i] > totalPerColumn[mpColumn]) {
mpColumn = i;
}
// rotate
int rr1 = Math.abs(mpColumn - cColumn);
int rr2 = columnCount - rr1;
totalRun += (rr1 < rr2) ? rr1 : rr2;
cColumn = mpColumn;
// System.out.println("Initial Column:"+cColumn+",Runs:"+totalRun);
// System.out.println(cColumn);
for (int i = 0; i < purchases.length; i++) {
int i1 = purchases[i].indexOf(',');
int i2 = purchases[i].indexOf(':');
int shelf, column, time;
shelf = Integer.parseInt(purchases[i].substring(0, i1));
column = Integer.parseInt(purchases[i].substring(i1 + 1, i2));
time = Integer.parseInt(purchases[i].substring(i2 + 1));
if (time - cTime < 5) {
// no rotate to mpColumn


} else {
// rotate to mpColumn
mpColumn = 0;
for (int ii = 0; ii < totalPerColumn.length; ii++)
if (totalPerColumn[ii] > totalPerColumn[mpColumn]) {
mpColumn = ii;
}
int r1 = Math.abs(mpColumn - cColumn);
int r2 = columnCount - r1;
totalRun += (r1 < r2) ? r1 : r2;
cColumn = mpColumn;
// System.out.println("Rotate MP Column:"+cColumn+",Runs:"+totalRun);
}
// rotate to column.
int r11 = Math.abs(column - cColumn);
int r22 = columnCount - r11;
totalRun += (r11 < r22) ? r11 : r22;
totalPerColumn[column] -= shelves[shelf][column];
if (shelves[shelf][column] == 0)
return -1;
else{
shelves[shelf][column] = 0;
}
cColumn = column;
cTime = time;
// System.out.println("Column:"+cColumn+",Runs:"+totalRun);
// System.out.println(shelf);
// System.out.println(column);
// System.out.println(time);
}
mpColumn = 0;
for (int ii = 0; ii < totalPerColumn.length; ii++)
if (totalPerColumn[ii] > totalPerColumn[mpColumn]) {
mpColumn = ii;
}
int r1 = Math.abs(mpColumn - cColumn);
int r2 = columnCount - r1;
totalRun += (r1 < r2) ? r1 : r2;
cColumn = mpColumn;
// System.out.println("Rotate MP Column:"+cColumn+",Runs:"+totalRun);


return totalRun;
}
// class Purchase{
// public int shelf=0;
// public int column=0;
// public int time=0;
// }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值