Jim and the Skyscrapers

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值