In a boredom, atm found an old program. The function of this program is similar to Excel, which operates on a table.
Let's assume that the table has n rows, with m cells in each row.
The content of each lattice can be either a positive integer or a formula.
The formula includes three kinds:
- SUM(x1,y1:x2,y2) denotes that the upper left corner is the Y1 lattice in line x1, and the lower right corner is the sum of the values of all lattices in the rectangle of line Y2 in line x2.
- AVG(x1,y1:x2,y2) denotes that the upper left corner is the Y1 lattice in line x1, and the lower right corner is the average of the values of all lattices in the rectangle of the Y2 lattice in line x2.
- STD(x1,y1:x2,y2) indicates that the upper left corner is the Y1 lattice of line x1, and the lower right corner is the standard deviation of the values of all lattices in the rectangle of line Y2 of line x2.
Standard deviation is the square root of variance.
Variance is the average of the square of the difference between each data and the average, which is used to measure the degree to which a single data leaves the average.
Nested formulas do not appear.
If the lattice is a number, then the value of the lattice is equal to that number, otherwise the value of the lattice is equal to the result of the lattice formula evaluation.
After entering this table, the program outputs the values of each grid. atm thought the program was fun, and he wanted to implement it.
"Input Format"
The first line has two numbers n, m.
Next, enter a table in line n. Each line contains m strings separated by spaces, representing the contents of the corresponding lattice.
The input guarantees that there will be no cyclic dependence, that is, there will be no two lattices A and b so that the value of a depends on the value of b and the value of b depends on the value of A.
"Output Format"
Output a table, a total of n rows, each row m reserved two decimal real numbers.
The data guarantees that no grid values exceed e6.
Sample Input
3 2
1 SUM(2,1:3,1)
2 AVG(1,1:1,2)
SUM(1,1:2,1) STD(1,1:2,2)
Sample Output
1.00 5.00
2.00 3.00
3.00 1.48
Data Scope
For 30% of the data, satisfy: n, m <== 5
For 100% data, satisfy: n, m <== 50
Resource agreements:
Peak memory consumption (including virtual machines) < 512M
CPU consumption < 2000 ms
Please output strictly according to the requirements, and do not print something like "Please input..." Excess content.
All the code is placed in the same source file. After debugging is passed, the source code is copied and submitted.
Note: Do not use package statements. Do not use features of jdk1.7 or above.
Note: The name of the main class must be Main, otherwise it will be treated as invalid code.
import java.util.ArrayList; import java.util.Scanner; public class Main { public static int n, m; public static double[][] value; public double getSum(int x1, int y1, int x2, int y2) { double sum = 0; for(int i = x1;i <= x2;i++) for(int j = y1;j <= y2;j++) sum = sum + value[i][j]; return sum; } public double getAvg(int x1, int y1, int x2, int y2) { int count = Math.abs((x2 - x1 + 1) * (y2 - y1 + 1)); double avg = getSum(x1, y1, x2, y2) / count; return avg; } public double getStd(int x1, int y1, int x2, int y2) { int count = Math.abs((x2 - x1 + 1) * (y2 - y1 + 1)); double avg = getAvg(x1, y1, x2, y2); double result = 0; for(int i = x1;i <= x2;i++) for(int j = y1;j <= y2;j++) result = result + (value[i][j]-avg) * (value[i][j]-avg); result = Math.sqrt(result / count); return result; } public boolean check(int x1, int y1, int x2, int y2) { boolean judge = true; for(int i = x1;i <= x2;i++) { if(!judge) break; for(int j = y2;j <= y2;j++) { if(value[i][j] == -1) { judge = false; break; } } } return judge; } public String[] getOperaAndNum(String arrayA) { int p = arrayA.indexOf("("); int q = arrayA.indexOf(")"); String opera = arrayA.substring(0, p); arrayA = arrayA.replace(':', ','); String[] num = arrayA.substring(p+1, q).split(","); String[] result = new String[5]; result[0] = opera; for(int i = 0;i < 4;i++) result[i + 1] = num[i]; return result; } public void getResult(String[] A) { value = new double[n][m]; ArrayList<String> list = new ArrayList<String>(); for(int i = 0;i < n;i++) for(int j = 0;j < m;j++) value[i][j] = -1; for(int i = 0;i < A.length;i++) { String[] arrayA = A[i].split(" "); for(int j = 0;j < arrayA.length;j++) { if(arrayA[j].charAt(0) >= '0' && arrayA[j].charAt(0) <= '9') { value[i][j] = Double.valueOf(arrayA[j]); } else { String[] r = getOperaAndNum(arrayA[j]); String opera = r[0]; int x1 = Integer.valueOf(r[1]) - 1; int y1 = Integer.valueOf(r[2]) - 1; int x2 = Integer.valueOf(r[3]) - 1; int y2 = Integer.valueOf(r[4]) - 1; if(check(x1, y1, x2, y2) == false) { list.add(""+i+" "+j+" "+arrayA[j]); continue; } if(opera.equals("SUM")) value[i][j] = getSum(x1, y1, x2, y2); else if(opera.equals("AVG")) value[i][j] = getAvg(x1, y1, x2, y2); else if(opera.equals("STD")) value[i][j] = getStd(x1, y1, x2, y2); } } } while(!list.isEmpty()) { for(int i = list.size() - 1;i >= 0;i--) { String[] temp = list.get(i).split(" "); int a = Integer.valueOf(temp[0]); int b = Integer.valueOf(temp[1]); String[] r = getOperaAndNum(temp[2]); String opera = r[0]; int x1 = Integer.valueOf(r[1]) - 1; int y1 = Integer.valueOf(r[2]) - 1; int x2 = Integer.valueOf(r[3]) - 1; int y2 = Integer.valueOf(r[4]) - 1; if(check(x1, y1, x2, y2) == false) continue; if(opera.equals("SUM")) value[a][b] = getSum(x1, y1, x2, y2); else if(opera.equals("AVG")) value[a][b] = getAvg(x1, y1, x2, y2); else if(opera.equals("STD")) value[a][b] = getStd(x1, y1, x2, y2); list.remove(i); } } for(int i = 0;i < n;i++) { for(int j = 0;j < m;j++) { System.out.printf("%.2f", value[i][j]); if(j != m - 1) System.out.print(" "); } System.out.println(); } } public static void main(String[] args) { Main test = new Main(); Scanner in = new Scanner(System.in); n = in.nextInt(); m = in.nextInt(); in.nextLine(); String[] A = new String[n]; for(int i = 0;i < n;i++) A[i] = in.nextLine(); test.getResult(A); } }