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]);
}
}
HDOJ 1391 Number Steps(打表DP)的更多相关文章
- HDU 1391 number steps(找规律,数学)
Starting from point (0,0) on a plane, we have written all non-negative integers 0, 1, 2,... as shown ...
- hdu 1391 Number Steps(规律)
题意:找规律 思路:找规律 #include<iostream> #include<stdio.h> using namespace std; int main(){ int ...
- HDU-1391 Number Steps
http://acm.hdu.edu.cn/showproblem.php?pid=1391 Number Steps Time Limit: 2000/1000 MS (Java/Others) ...
- ACM/ICPC 之 数据结构-邻接表+DP+队列+拓扑排序(TSH OJ-旅行商TSP)
做这道题感觉异常激动,因为在下第一次接触拓扑排序啊= =,而且看了看解释,猛然发现此题可以用DP优化,然后一次A掉所有样例,整个人激动坏了,哇咔咔咔咔咔咔咔~ 咔咔~哎呀,笑岔了- -|| 旅行商(T ...
- Number Steps
Number Steps Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- HDOJ(HDU).1284 钱币兑换问题 (DP 完全背包)
HDOJ(HDU).1284 钱币兑换问题 (DP 完全背包) 题意分析 裸的完全背包问题 代码总览 #include <iostream> #include <cstdio> ...
- HDOJ 1257 最少拦截系统 【DP】
HDOJ 1257 最少拦截系统 [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- ZOJ 1414:Number Steps
Number Steps Time Limit: 2 Seconds Memory Limit: 65536 KB Starting from point (0,0) on a plane, ...
- Rabin_Karp(hash) HDOJ 1711 Number Sequence
题目传送门 /* Rabin_Karp:虽说用KMP更好,但是RK算法好理解.简单说一下RK算法的原理:首先把模式串的哈希值算出来, 在文本串里不断更新模式串的长度的哈希值,若相等,则找到了,否则整个 ...
随机推荐
- 用微信点单 订餐系统打造属于个人的O2O外卖订餐行业商业平台
首先,我不能说我是一个成功的微信达人,我也不能说我是一个优秀的互联网专家.但我就眼下所使用的一套订餐系统来讲.正在逐渐的规划一个餐饮行业的商业圈! 我所使用的系统叫"微铺子订餐系统" ...
- unicode下各种类型转换,CString,string,char*,int,char[]
把最近用到的各种unicode下类型转换总结了一下,今后遇到其他的再补充: 1.string转CString string a=”abc”; CString str=CString(a.c_str() ...
- C# List集合转Json字符串示例代码
将list集合转换为Json字符串简单实现代码: public static string GetJosn(List<CalendarInfo> list) { string jsonSt ...
- IIS部署FTP服务器步骤
本文介绍如何在IIS中部署FTP服务端.首先确认windows开启了ftp功能:确认方法:进入控制面板->程序->打开或关闭windows功能如下图所示: 确认FTP勾选 确认后打开IIS ...
- android开发之——混淆编译
众所周知,android的apk文件是非常容易被反编译的,这样对于开发者来说,辛辛苦苦开发应用被破解是一件很令人懊恼的事情,谷歌也认识到了这一点,所以从2.3之后就为开发者提供了一个代码混淆工具pro ...
- Android(java)学习笔记251:ContentProvider使用之添加数据到联系人(掌握)
1.添加联系人逻辑思路 (1)首先在raw_contacts创建一个新的id (2)在data表里面添加这个id对应的数据 2.下面通过一个案例,说明一下如何添加一条数据到联系人: (1)首先我们关注 ...
- Linux下python升级
Centos即使用Yum更新也是Python2.6.6所以需要升级到Python2.7.8 1.先下载源码包 1 wget https://www.python.org/ftp/python/2.7. ...
- Linux 机器之间建立互信
原理: 就是两台机器(web-1和web-2)经过预先设置好经过认证的key文件,双方互相访问时,进行自动认证,从而实现互信. 互信的原理了解了,我们可以把配置ssh互信的步骤进行有效的分割. 1 ...
- 95秀-ViewPager 使用实例
Activity的样式 <style name="under_live_indicator" parent="android:Theme.NoTitleBa ...
- sql 判断表、列、视图等是否存在
1 判断数据库是否存在 if exists (select * from sys.databases where name = '数据库名') drop database [数据库名] 2 判 ...