Hello others,
I've been at this problem for hours now. I'm just bloody tired.
I've redone various parts of this code without any success. I'm not sure if there's an error due to the logic or the rounding.
Could anyone please help me? This is seriously frustrating.
The class IOMonster is a quick I/O handler I wrote for these contests. It's actually running fine and without error. All the input is being read normally.
The load method basically calls StringBuilder.append() inside of IOMonster2.
Any questions about my code and I'll answer them. Thank you anyone for your help.
If any of the admins are reading, I beg of you, tell me whether this is a formatting error or a logical one. It would help all too much.
For anyone who wants the Source code to IOMonster2, I can send you the Java file on request.
Code: Select all
package UVA_Online;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Arrays;
import Tools.IOMonster2;
public class UVA_OnlineJudge_00815
{
public static boolean debugging = false;
public static IOMonster2 io;
public static boolean writtingToFile = false;
public static String outputURL = "output.txt";
public static int region = 1;
public static long width, length, waterVolume;
public static long[] plotHeights, sum;
public static void main(String[] args) throws IOException {
io = new IOMonster2(writtingToFile, outputURL, false);
while((width = io.nextLong()) != 0 && (length = io.nextLong()) != 0)
{
initPlotHeights();
initSum();
waterVolume = io.nextLong();
float waterLevel = calcWaterLevel();
String waterLevelString = formatDecimal(waterLevel);
String regionCovered = formatDecimal(regionCovered(waterLevel));
io.load(String.format("Region %d\n", region++));
io.load(String.format("Water level is %s meters.\n", waterLevelString));
io.load(String.format("%s percent of the region is under water.\n\n", regionCovered));
}
io.printLoad();
io.endIO();
}
public static String formatDecimal(float percent)
{
BigDecimal bd = new BigDecimal(Float.toString(percent));
bd = bd.setScale(2, BigDecimal.ROUND_HALF_EVEN);
return String.format("%.2f", bd.doubleValue());
}
public static int plotsCovered(float waterLevel)
{
for(int i = plotHeights.length - 1; i >= 0; i--)
{
if(plotHeights[i] < waterLevel)
{
return i + 1;
}
}
return 0;
}
public static float regionCovered(float waterLevel)
{
if(waterLevel >= plotHeights[plotHeights.length - 1])
{
return (float) 100.0;
}
int plots = 0;
for(int i = 0; i < plotHeights.length; i++)
{
if(plotHeights[i] <= waterLevel)
{
plots++;
}
if(plotHeights[i] > waterLevel)
{
break;
}
}
return (float) ((1.0 * (plots) * 100.0) / (plotHeights.length));
}
public static int numberOfLevels()
{
int levels = 1;
for(int i = 1; i < plotHeights.length; i++)
{
if(plotHeights[i] != plotHeights[i - 1])
{
levels++;
}
}
return levels;
}
public static void initSum()
{
sum = new long[numberOfLevels()];
sum[0] = 0;
int sumIndex = 0;
long deltaLevel, plotsCovered;
for(int i = 1; i < plotHeights.length; i++)
{
if(plotHeights[i] != plotHeights[i - 1])
{
sumIndex++;
deltaLevel = plotHeights[i] - plotHeights[i - 1];
plotsCovered = plotsCovered(plotHeights[i]);
sum[sumIndex] = sum[sumIndex - 1] + (deltaLevel * plotsCovered * 100);
}
}
if(debugging)
{
io.print("SUM: " + Arrays.toString(sum) + "\n");
}
}
public static void initPlotHeights() throws IOException
{
plotHeights = new long[(int) (width * length)];
for(int i = 0; i < plotHeights.length; i++)
{
plotHeights[i] = io.nextLong();
}
Arrays.sort(plotHeights);
if(debugging)
{
io.print(Arrays.toString(plotHeights) + "\n");
}
}
public static float calcWaterLevel()
{
if(waterVolume >= sum[sum.length - 1])
{
waterVolume -= sum[sum.length - 1];
return (float) (plotHeights[plotHeights.length - 1] + ((float) waterVolume / (100.0 * plotHeights.length)));
}
int index = 0;
while(sum[index++] <= waterVolume)
{
}
waterVolume -= sum[--index];
float waterLevel = (float) (getHeightOfLevel(index) + (1.0 * waterVolume / (100.0 * plotsCovered(getHeightOfLevel(index)))));
if(debugging)
{
io.print(String.format("Water level (%.2f) passed up to level %d.\n", waterLevel, index));
}
return waterLevel;
}
public static long getHeightOfLevel(int level)
{
if(level == 1)
{
return plotHeights[0];
}
for(int i = 1; i < plotHeights.length; i++)
{
if(level == 1)
{
return plotHeights[i];
}
if(plotHeights[i] != plotHeights[i - 1])
{
level--;
}
}
return 0;
}
}