HDOJ 1391 Number Steps(打表DP)

Problem Description
Starting from point (0,0) on a plane, we have written all non-negative integers 0, 1, 2,… as shown in the figure. For example, 1, 2, and 3 has been written at points (1,1), (2,0), and (3, 1) respectively and this pattern has continued.

You are to write a program that reads the coordinates of a point (x, y), and writes the number (if any) that has been written at that point. (x, y) coordinates in the input are in the range 0…5000.

Input
The first line of the input is N, the number of test cases for this problem. In each of the N following lines, there is x, and y representing the coordinates (x, y) of a point.

Output
For each point in the input, write the number written at that point or write No Number if there is none.

Sample Input
3
4 2
6 6
3 4

Sample Output
6
12
No Number

不能直接开[5005][5005]的数组,这样内存不够。
因为大部分数据没用,没列只有2个有效数据,所以开[5005][2]就可以了。

import java.util.Scanner;

public class Main{
    static int[][] db = new int[5005][2];
    public static void main(String[] args) {
        dabiao();
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while(t-->0){
            int x = sc.nextInt();
            int y = sc.nextInt();
            if(x==0&&y==0){
                System.out.println(0);
                continue;
            }

            if(x==1&&y!=1){
                System.out.println("No Number");
                continue;
            }

            if(x==1&&y==1){
                System.out.println(1);
                continue;
            }

            if(x==y||x==(y+2)){
                if(x==(y+2)){
                    System.out.println(db[x][0]);
                }else{
                    System.out.println(db[x][1]);
                }
            }else{
                System.out.println("No Number");
            }
        }
    }

    private static void dabiao() {
        int num=2;
        db[0][0]=0;
        boolean is = true;
        for(int i=2;i<=5003;i++){
            if(is){
                db[i][0]=num;
                num++;
                db[i+1][0]=num;
                num++;
                is=!is;
            }else if(!is){
                db[i-1][1]=num;
                num++;
                db[i][1]=num;
                num++;
                is=!is;
            }
        }
//      System.out.println(db[2][0]);
//      System.out.println(db[2][1]);
//      System.out.println(db[3][0]);
//      System.out.println(db[3][1]);
//      System.out.println(db[4][0]);
//      System.out.println(db[4][1]);
//      System.out.println(db[5][0]);
//      System.out.println(db[5][1]);
    }
}
时间: 2024-09-30 19:20:39

HDOJ 1391 Number Steps(打表DP)的相关文章

HDOJ 1058 Humble Numbers(打表过)

Problem Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, - shows the first 20 humble numbers. Write a program to find and print

HDOJ 1420 Prepared for New Acmer(DP)

Problem Description 集训进行了将近2个礼拜,这段时间以恢复性训练为主,我一直在密切关注大家的训练情况,目前为止,对大家的表现相当满意,首先是绝大部分队员的训练积极性很高,其次,都很遵守集训纪律,最后,老队员也起到了很好的带头作用,这里特别感谢为这次DP专题练习赛提供题目和测试数据的集训队队长xhd同学. 特别高兴的是,跟随集训队训练的一批新队员表现非常好,进步也比较显著,特别是训练态度大大超出我的预期,我敢说,如果各位能如此坚持下去,绝对前途无量! 考虑到新队员还没有经过系统

HDOJ 1005 Number Sequence

Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). Input The input consists of multiple test cases. Each test case co

jQuery Real Person验证码插件防止表单自动提交_jquery

本文介绍的jQuery插件有点特殊,防自动提交表单的验证工具,就是我们经常用到的验证码工具,先给大家看看效果. 效果图如下: 使用说明 需要使用jQuery库文件和Real Person库文件 同时需要自定义验证码显示的CSS样式 使用实例 1.包含文件部分 <script type="text/javascript" src="jquery-latest.pack.js"></script> <script type="te

关于竖表转横表的问题

问题 关于竖表转横表的问题                                                                                本文作者:dinya内容摘要:在开发过程,经常遇到一些将表的显示方式进行转换的需求,我们习惯性称之为竖表到横表的转换,本文通过一个例子来简要说明常见的两种竖表转横表的问题. 本文适宜读者范围:Oracle初级,中级 系统环境:     OS:windows 2000 Professional (英文版) Orac

Oracle SQL和PL/SQL多表插入技巧

假如一个在线电子商务系统,我们现在需要根据订单表体现的消费金额将客户简单分为大中小三类并分别插入到三张表中. 订单表 order (order_id number, cust_id number, amount number); 小客户表 small_cust (cust_id number, tot_amt number); 中客户表 med_cust (cust_id number, tot_amt number); 大客户表 big_cust (cust_id number, tot_am

普通表转换为分区表

yang@ORACL> create table yangtmp ( id number, time date ); 表已创建. yang@ORACL> insert into yangtmp select rownum id ,sysdate-dbms_random.value(1,500) time   2  from dual   3  connect by level <=1e5; 已创建100000行. yang@ORACL> select count(1) from y

oracle表空间,角色,权限,表,索引,序列号,视图,同义词,约束条件,存储函数和过程,常用数据字典,基本数据字典信息,查看VGA信息,维护表空间,创建表空间等信息

查看当前用户的缺省表空间 SQL>select username,default_tablespace from user_users; 查看当前用户的角色 SQL>select * from user_role_privs; 查看当前用户的系统权限和表级权限 SQL>select * from user_sys_privs;        结果可以是:        USERNAME                       PRIVILEGE                    

0-android为什么调用拨号程序时向contact表中存入的数据不全

问题描述 android为什么调用拨号程序时向contact表中存入的数据不全 在模拟器通讯录中新建了联系人,设置了头像,但是这个号码,给模拟器拨打电话时,photoId和 number存到contact表中的值为0和null