Farmer John purchases three cows: Bessie, Elsie, and Mildred, each of whom initially produces 7 gallons of milk per day. Since the milk output of a cow is known to potentially change over time, Farmer John takes periodic measurements over the next 100 days and scribbles them down in a log book. Entries in his log look like this:
35 Bessie -2
14 Mildred +3
The first entry indicates that on day 35, Bessie’s milk output was 2 gallons lower than it was when last measured. The next entry indicates that on day 14, Mildred’s milk output increased by 3 gallons from when it was last measured. Farmer John has only enough time to make at most one measurement on any given day. Unfortunately, he is a bit disorganized, and doesn’t necessarily write down his measurements in chronological order.
To keep his cows motivated, Farmer John proudly displays on the wall of his barn the picture of whichever cow currently has the highest milk output (if several cows tie for the highest milk output, he displays all of their pictures). Please determine the number of days on which Farmer John would have needed to change this display.
农民约翰购买了三头奶牛,分别名为:Bessie,Elsie,Mildred,每头牛最初每一天生产7加仑的牛奶。每头牛的奶产量都会随着时间的推移而发生改变,约翰需要在接下来的100天里定期测量,然后作为产奶日志记录下来。日志中的具体信息如下所示:
35 Bessie - 2
14 Mildred + 3
第一项表明,在第35天,Bessie的牛奶产量比上次测量时低了2加仑。下一个条目表明,在第14天,Mildred的奶产量比上次测量时增加了3加仑。农夫约翰只有在任何一天做最多一次测量的时间。不幸的是,他有点杂乱无章,不一定是按时间顺序写下他的测量结果。
为了保持奶牛的产奶动力,约翰以激励的形式在谷仓的墙上展示了目前产量最高的奶牛的图片(如果同时有几头奶牛产奶量最高的话,他会展示所有的照片)。请确定在一次日志中约翰可能调整奶牛上墙展示的次数。
INPUT FORMAT (file measurement.in):
The first line of input contains N, the number of measurements Farmer John makes. Each of the next NN lines contains one measurement, in the format above, specifying a day (an integer in the range 1..100), the name of a cow, and the change in her milk output since it was last measured (a nonzero integer). Each cow’s milk output will always be in the range 0..1000.
输入的第一行包含N,即约翰所做的所有的产奶测量次数。接下来的N行中的每一行包含一次测量,根据题中的格式,指定一个第几天(一个整数范围为1……100的数),一头奶牛的名字,以及她最后一次测量(非零整数)后奶产量的变化。每头牛的产奶量总是在0到1000范围内。
OUTPUT FORMAT (file measurement.out):
Please output the number of days (an integer in the range 0..100) on which Farmer John needs to adjust his motivational display.
输出约翰总共调整他的产奶展示墙的调整次数。
SAMPLE INPUT:
4
7 Mildred +3
4 Elsie -1
9 Mildred -1
1 Bessie +2
SAMPLE OUTPUT:
3
Initially, all cows have milk output 7. On day 1, Bessie’s milk output increases to 9, making her the unique cow with highest milk output and causing Farmer John to change his display. On day 4, Elsie’s milk output decreases to 6, but this does not change the fact that Bessie is the sole cow in the lead. On day 7, Mildred jumps into the lead, changing the display, and on day 8, Mildred drops in production to be tied with Bessie, again changing the display.
最初,所有奶牛产奶量都为7。在第1天,Bessie的奶产量增加到9,成为唯一一头奶产量最高的奶牛,于是约翰让它上墙展示了。第4天,Elsie的牛奶产量下降到6,但这并不能改变Bessie是仍处于领先地位的唯一的一头牛这个事实。在第7天,Mildred一跃而起,成功上墙,第8天,Mildred产奶量下降到Bessie一致,约翰再次将他们同时上墙展示。
Problem credits: Brian Dean
解析:增量后数字排序问题 要考虑前K个值一样情况
答案:
import java.io.*;
import java.util.*;
public class MilkMeasurements {
static String[] cowNames;
private static int[] searchMin(int[] allCow) {
int[] index = new int[allCow.length];
int max = allCow[0];
for (int i = 0; i < allCow.length; ++i) {
if (max < allCow[i]) {
max = allCow[i];
}
}
for (int i = 0; i < allCow.length; ++i) {
if (max == allCow[i]) {
index[i] = 1;
} else {
index[i] = 0;
}
}
return index;
}
private static boolean indexSame(int[] cowA, int[] cowB) {
if (cowA.length != cowB.length){
return false;
}
for (int i = 0; i < cowA.length; ++i) {
if (cowA[i] != cowB[i]) {
return false;
}
}
return true;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("measurement.in"));
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("measurement.out")));
cowNames = new String[]{
"Bessie",
"Elsie",
"Mildred"};
int number = Integer.parseInt(br.readLine());
String[] displayCow= new String[101];
int[] displayMilk= new int[101];
for (int i = 0; i < 101; ++i) {
displayMilk[i] = 0;
displayCow[i] = "0";
}
for(int i = 0; i < number; i++) {
StringTokenizer stringTokenizer = new StringTokenizer(br.readLine());
String dayStr = stringTokenizer.nextToken();
int dayNo = Integer.parseInt(dayStr);
String cowName = stringTokenizer.nextToken();
int milk = Integer.parseInt(stringTokenizer.nextToken());
displayCow[dayNo] = cowName;
displayMilk[dayNo] = milk;
}
int result = 0;
int[] allCow = new int[]{7,7,7};
int[] formIndex = new int[]{-1,-1,-1};
for (int i = 1; i < 101; ++i) {
if(displayCow[i].equals("0")){
continue;
}
if (displayMilk[i] != 0) {
if (displayCow[i].equals(cowNames[0])) {
allCow[0] += displayMilk[i];
} else if (displayCow[i].equals(cowNames[1])) {
allCow[1] += displayMilk[i];
} else {
allCow[2] += displayMilk[i];
}
}
int[] index = searchMin(allCow);
if (!indexSame(index,formIndex)) {
result++;
formIndex = index.clone();
}
}
pw.println(result);
pw.close();
}
}