CSES - Datatähti 2016 alku - Results
Submission details
Task:Tontti
Sender:testUser
Submission time:2015-10-09 09:21:44 +0300
Language:Java
Status:READY
Result:47
Feedback
groupverdictscore
#1ACCEPTED14
#2ACCEPTED33
#30
Test results
testverdicttimegroup
#1ACCEPTED0.17 s1details
#2ACCEPTED0.19 s1details
#3ACCEPTED0.17 s1details
#4ACCEPTED0.17 s1details
#5ACCEPTED0.18 s1details
#6ACCEPTED0.83 s2details
#7ACCEPTED0.57 s2details
#8ACCEPTED0.35 s2details
#9ACCEPTED0.32 s2details
#10ACCEPTED0.31 s2details
#11--3details
#12--3details
#13--3details
#14--3details
#15--3details

Code

/**
 * Created by Frans on 5.10.2015.
 */
public class Tontti{

    static int height;
    static int width;
    static byte[][] kentta;
    static int[][] kenttaSumma;
    static int treeWanted;
    static int trees;
    static int minSize;
    static int runAmount;

    public static void main(String[] args){
        IO io = new IO();

        height = io.nextInt();
        width = io.nextInt();
        treeWanted = io.nextInt();
        trees = 0;

        kentta = new byte[width+1][height+1];
        kenttaSumma = new int[width+1][height+1];

        //calculate trees and save it in to array and summed area table
        for (int y=1; y<=height; y++) {
            String line = io.next();
            int tmpRivi = 0;
            for (int x=1; x<=width; x++){
                if (line.charAt(x-1) == '*'){
                    kentta[x][y] = 1;
                    tmpRivi ++;
                }else{
                    kentta[x][y] = 0;
                }
                kenttaSumma[x][y] = tmpRivi + kenttaSumma[x][Math.max(y-1,0)];
            }
            trees += tmpRivi;
        }



        minSize = (int) Math.ceil(Math.pow(treeWanted,0.5));

        //optimized test area
        double density = (double) trees/ (double) (width*height);
        int optimizeW = Math.max((int) Math.ceil(Math.pow((treeWanted / density), 0.5)), 2);

        int foundArea = 0;

        int direction =0;

        //-1, koska oikea reuna turha kayda
        for(int y=1; y<=height-(minSize-1); y++){
            for (int x=1; x<=width-(minSize-1); x++){
                int testW = optimizeW;

                int tmpX = x-1;
                int tmpY = y-1;

                testW = Math.min(Math.min(testW, height-tmpY), width-tmpX);
                int amount = kenttaSumma[tmpX+testW][tmpY+testW]+kenttaSumma[tmpX][tmpY]-kenttaSumma[tmpX][tmpY+testW]-kenttaSumma[tmpX+testW][tmpY];

                //haluttu maara
                if (amount == treeWanted){
                    foundArea ++;
                    foundArea += IncreaseArea(testW, x, y);
                    foundArea += DecreaseArea(testW, x, y);
                } else if (amount < treeWanted){
                    foundArea += IncreaseArea(testW, x, y);
                } else { //amount > treeWanted
                    foundArea += DecreaseArea(testW, x, y);
                }

            }
        }

        io.println(foundArea);
        io.close();

    }

    private static int CountTree2(int testW, int x, int y){
        testW --; //jos pituus 2, talloin sivu (2-1) pidempi
        return kenttaSumma[x+testW][y+testW] + kenttaSumma[x-1][y-1]-kenttaSumma[x-1][y+testW]- kenttaSumma[x+testW][y-1];
    }


    private static int DecreaseArea(int testW, int x, int y){
        int foundArea = 0;
        testW --;
        while(testW >= minSize){
            int tmpX = x-1;
            int tmpY = y-1;
            int squareTreeAmount = kenttaSumma[tmpX+testW][tmpY+testW]+kenttaSumma[tmpX][tmpY]-kenttaSumma[tmpX][tmpY+testW]-kenttaSumma[tmpX+testW][tmpY];
            if(squareTreeAmount == treeWanted){
                foundArea ++;
            }
            testW --;
        }
        return foundArea;
    }

    private static int IncreaseArea(int testW, int x, int y){
        int foundArea = 0;
        testW ++;
        while(testW + y <= height+1 && testW + x <= width+1){
            int tmpX = x-1;
            int tmpY = y-1;
            int squareTreeAmount = kenttaSumma[tmpX+testW][tmpY+testW]+kenttaSumma[tmpX][tmpY]-kenttaSumma[tmpX][tmpY+testW]-kenttaSumma[tmpX+testW][tmpY];
            if(squareTreeAmount == treeWanted){
                foundArea ++;
            }
            testW ++;
        }
        return foundArea;
    }


}

Test details

Test 1

Group: 1

Verdict: ACCEPTED

input
10 10 1
......*...
.......*..
*..*....*.
*....*....
...

correct output
94

user output
94

Test 2

Group: 1

Verdict: ACCEPTED

input
10 10 5
**********
**********
**********
**********
...

correct output
0

user output
0

Test 3

Group: 1

Verdict: ACCEPTED

input
10 10 10
**...*...*
*..*.**.*.
...**.*..*
*...**.*..
...

correct output
4

user output
4

Test 4

Group: 1

Verdict: ACCEPTED

input
10 10 5
****......
*.*.**..**
....*.*..*
...*.***..
...

correct output
16

user output
16

Test 5

Group: 1

Verdict: ACCEPTED

input
10 10 2
**.***..*.
...*.*....
.***.*...*
***.***..*
...

correct output
30

user output
30

Test 6

Group: 2

Verdict: ACCEPTED

input
500 500 1
.................................

correct output
9552040

user output
9552040

Test 7

Group: 2

Verdict: ACCEPTED

input
500 500 5
.................................

correct output
1536063

user output
1536063

Test 8

Group: 2

Verdict: ACCEPTED

input
500 500 25000
**...*...**..*.*..*.**.*..*.*....

correct output
288

user output
288

Test 9

Group: 2

Verdict: ACCEPTED

input
500 500 12500
**.**.*..*...*.**...*.***........

correct output
786

user output
786

Test 10

Group: 2

Verdict: ACCEPTED

input
500 500 5000
.*.*.**..*.*.**.**..*..**...*....

correct output
1763

user output
1763

Test 11

Group: 3

Verdict:

input
2000 2000 1
.................................

correct output
489611392

user output
(empty)

Test 12

Group: 3

Verdict:

input
2000 2000 5
.................................

correct output
120725884

user output
(empty)

Test 13

Group: 3

Verdict:

input
2000 2000 400000
..*..**.**.**.*.***...**.*..**...

correct output
1849

user output
(empty)

Test 14

Group: 3

Verdict:

input
2000 2000 200000
***.*....*.*..*....**..*..*.*....

correct output
2665

user output
(empty)

Test 15

Group: 3

Verdict:

input
2000 2000 80000
**.**...*.***.**....**.*....*....

correct output
5587

user output
(empty)