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-10-29 03:39:52

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

杭电 HDU ACM 1391 Number Steps

Number Steps Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4376    Accepted Submission(s): 2656 Problem Description Starting from point (0,0) on a plane, we have written all non-negative inte

hdu 1391 Number Steps(规律)

题意:找规律 思路:找规律 #include<iostream> #include<stdio.h> using namespace std; int main(){ int n,x,y; scanf("%d",&n); while(n--){ scanf("%d%d",&x,&y); if(x==y)x&1?printf("%d\n",x+x-1):printf("%d\n&qu

[2013山东ACM省赛] The number of steps (概率DP,数学期望)

The number of steps Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Mary stands in a strange maze, the maze looks like a triangle(the first layer have one room,the second layer have two rooms,the third layer have three rooms -). Now she st

ACM/ICPC 之 数据结构-邻接表+DP+队列+拓扑排序(TshingHua OJ-旅行商TSP)

做这道题感觉异常激动,因为在下第一次接触拓扑排序啊= =,而且看了看解释,猛然发现此题可以用DP优化,然后一次A掉所有样例,整个人激动坏了,哇咔咔咔咔咔咔咔~ 咔咔~哎呀,笑岔了- -|| 旅行商(TSP) Description Shrek is a postman working in the mountain, whose routine work is sending mail to n villages. Unfortunately, road between villages is

hdoj 2046 骨牌铺方格 【DP】+【斐波那契】

dp果然不是好学的... 第n个,即2*n时,可由第n-1个的竖直排列再加一个,和第n-2个中横着排两个 所以f(n) = 1×f(n-1) + 1×f(n-2): 骨牌铺方格 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 28412    Accepted Submission(s): 13771 Problem Descripti

[HDOJ - 5282] Senior&#39;s String 【DP】

题目链接:BZOJ - 5282 题目分析 LCS 就是用经典的 O(n^2) DP 解决,f[i][j] 表示 x 串前 i 个字符与 y 串前 j 个字符的 LCS 长度. f[i][j] = max(f[i - 1][j], f[i][j - 1]); if (x[i] == y[j]) f[i][j] = max(f[i][j], f[i - 1][j - 1] + 1); 然后再设置一个状态 g[i][j], 表示 x 串的前 i 个字符中,有多少个长为 f[i][j] 的子序列同时也

HDU 4952 Number Transformation 打表规律

点击打开链接 Number Transformation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 495    Accepted Submission(s): 248 Problem Description Teacher Mai has an integer x. He does the following operation

HDOJ 1560 DNA sequence 状压dp 或 IDA*

http://acm.hdu.edu.cn/showproblem.php?pid=1560 题意: 给不超过8个子串,每个子串最多5位,且都只包含ATCG,求最短的母串长度. 分析: 又是上个月写的,所以有点忘了..正解是IDA*.然后可以状压dp,记忆化搜索.dp[i],i用6进制表示,每位表示对应的子串匹配那么多长度所需要的最短母串长度.比如两个子串,13=2*6^1+1*6^0,dp[13]就表示第一个串匹配了第一位,第二个串匹配前两位所需要的最短母串长度. 状态讲完了,不过实际上程序里

HDOJ ---1711 Number Sequence

Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9899    Accepted Submission(s): 4518 Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[