Jim and the Skyscrapers

来源:互联网 发布:淘宝企业c店开店流程 编辑:程序博客网 时间:2024/05/16 02:09
Problem Statement
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the
environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another
skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently.
Let us describe the problem in one dimensional space. We have in total skyscrapers aligned from left to right. The
skyscraper has a height of . A flying route can be described as with , which means, Jim starts his HZ42 at the top of
the skyscraper and lands on the skyscraper . Since HZ42 can only fly horizontally, Jim will remain at the height only. Thus
the path can be valid, only if each of the skyscrapers is not strictly greater than and if the height of
the skyscraper he starts from and arrives on have the same height. Formally, is valid iff and .
Help Jim in counting the number of valid paths represented by ordered pairs .
Input Format
The first line contains , the number of skyscrapers. The next line contains space separated integers representing the
heights of the skyscrapers.
Output Format
Print an integer that denotes the number of valid routes.
Constraints
and no skyscraper will have height greater than and lesser than .
Sample Input #00
63
2 1 2 3 3
Sample Output #00
8
Sample Input #01
31
1 000 1
Sample Output #01
0
Explanation
First testcase: (1, 5), (1, 6) (5, 6) and (2, 4) and the routes in the opposite directions are the only valid routes.

Second testcase: (1, 3) and (3, 1) could have been valid, if there wasn't a big skyscraper with height 1000 between them.



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class skyscrapers {
    public static void main(String[] args) throws IOException {
        String str = null;
        Integer num = null;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Input the Skyscrapers  number  and   highs");
        num = Integer.parseInt(br.readLine());
        str = br.readLine();
        System.out.println("Skyscrapers num: " + num + "\n" + "highs: " + str);
        System.out.println("***************************************************");
        
        // get the Skyscrapers highs
        List<Integer> skyscraperLisy = new ArrayList<Integer>();
        List<Integer> comskyscraperLisy = new ArrayList<Integer>();
        String[] a = str.split(" +");
        for (int i = 0; i < a.length; i++) {
            skyscraperLisy.add(Integer.valueOf(a[i]).intValue());
        }

        // Iterator遍历
        comskyscraperLisy = skyscraperLisy;
        Iterator<Integer> iterator = skyscraperLisy.iterator();
        
        int HZ42 = 0;
        while (iterator.hasNext()) {
            int i = (Integer) iterator.next();
            System.out.println("skyscraperLisy  " + "i:" + i);
            Iterator<Integer> comIterator = comskyscraperLisy.iterator();
            while (comIterator.hasNext()) {
                int beCompare = (Integer) comIterator.next();
                if (i >= beCompare) {
                    if (i == beCompare) {
                        System.out.println("i:" + i + "beCompare:" + beCompare
                                + "can fly");
                        HZ42++;
                    }
                } else {
                    System.out.println("faied to fly" + "i:" + i + "beCompare:"
                            + beCompare);
                    continue;
                }
            }
            if (HZ42 > 0) {
                HZ42 = HZ42 - 1;
                System.out.println("-----------" + "HZ42:" + HZ42
                        + "-----------");
            }
        }
        HZ42 = HZ42 * 2;
        System.out.println("the HZ42 has  air line:" + HZ42 );
    }
}



0 0