【九度】题目1422:Closest Number
来源:互联网 发布:世界地图销售网络 编辑:程序博客网 时间:2024/06/11 23:11
题目1422:Closest Number
时间限制:10 秒内存限制:64 兆特殊判题:否提交:690解决:136
题目描述:
There is one Master In ACM_DIY called "白衣少年"(White) whose motto is "I can HOLD ANY FEMALE". Since White is really busy with HOLDING FEMALES, he has no idea of the work that is given by his boss(MALE, of course), so can you help him to solve such a simple and really easy problem:
Array A has N positive integers,for each A[i] (0<=i<N, indicating the i-th integer in the array A), it fits in a 32-bit signed integer ),find the closest number less than A[i] (if the distance is the same,we prefer the left one).
If you can solve this problem, White may give you some "tips"(You know better!)
输入:
T cases (1<=T<=5)
For each case, the first line give an integer N(1<= N <= 10^6),then the second line has the array with N integers. (All the integers are guaranteed to fit in 32-bit signed integer)
输出:
For each case, print one line with N space-seperated integers, where the i-th integer is the number who is less that A[i] and is closest to i if exists, otherwise it is 0.
样例输入:
3
3
2 1 3
3
2 3 1
4
5 7 3 6
样例输出:
1 0 1
1 2 0
3 5 0 3
来源:
第四届ACM_DIY群程序设计竞赛
解题思路:
这个题目的意思是,问每个数离它最近的比它小的数是多少。
思想上有点像动态规划。
基本思想是从左求解一次,从右求解一次。
从左求解的是,所有左边比它小的数。
从右求解的是,所有右边比它小的数。
比如从左到右求解,每次针对i求解的时候,判断它左边的数是不是比它小,如果比它小,就是它了,否则就一直判断知道1为止,这个会超时,剪枝就是递归求解。
时间限制:10 秒内存限制:64 兆特殊判题:否提交:690解决:136
题目描述:
There is one Master In ACM_DIY called "白衣少年"(White) whose motto is "I can HOLD ANY FEMALE". Since White is really busy with HOLDING FEMALES, he has no idea of the work that is given by his boss(MALE, of course), so can you help him to solve such a simple and really easy problem:
Array A has N positive integers,for each A[i] (0<=i<N, indicating the i-th integer in the array A), it fits in a 32-bit signed integer ),find the closest number less than A[i] (if the distance is the same,we prefer the left one).
If you can solve this problem, White may give you some "tips"(You know better!)
输入:
T cases (1<=T<=5)
For each case, the first line give an integer N(1<= N <= 10^6),then the second line has the array with N integers. (All the integers are guaranteed to fit in 32-bit signed integer)
输出:
For each case, print one line with N space-seperated integers, where the i-th integer is the number who is less that A[i] and is closest to i if exists, otherwise it is 0.
样例输入:
3
3
2 1 3
3
2 3 1
4
5 7 3 6
样例输出:
1 0 1
1 2 0
3 5 0 3
来源:
第四届ACM_DIY群程序设计竞赛
解题思路:
这个题目的意思是,问每个数离它最近的比它小的数是多少。
思想上有点像动态规划。
基本思想是从左求解一次,从右求解一次。
从左求解的是,所有左边比它小的数。
从右求解的是,所有右边比它小的数。
比如从左到右求解,每次针对i求解的时候,判断它左边的数是不是比它小,如果比它小,就是它了,否则就一直判断知道1为止,这个会超时,剪枝就是递归求解。
Java AC
import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.StreamTokenizer; public class Main { /* * 2014年3月26日 19:04:04 */ public static void main(String[] args) throws Exception { StreamTokenizer st = new StreamTokenizer(new BufferedReader( new InputStreamReader(System.in))); while (st.nextToken() != StreamTokenizer.TT_EOF) { int T = (int) st.nval; while (T > 0) { st.nextToken(); int n = (int) st.nval; int array[] = new int[n + 2]; int dp1[] = new int[n + 1]; int dp2[] = new int[n + 1]; for (int i = 1; i <= n; i++) { st.nextToken(); array[i] = (int) st.nval; dp1[i] = i - 1; dp2[i] = i + 1; } array[0] = array[n+1] = Integer.MIN_VALUE; startToEnd(array, dp1, n); endToStart(array, dp2, n); StringBuffer sb = new StringBuffer(); for (int i = 1; i <= n; i++) { if (dp1[i] == 0 && dp2[i] == n+1) { sb.append(0 + " "); } else if (dp1[i] == 0) { sb.append(array[dp2[i]] + " "); } else if (dp2[i] == n+1) { sb.append(array[dp1[i]] + " "); } else { int temp = (i - dp1[i]) == (dp2[i] - i) ? dp1[i] : ((i - dp1[i]) < (dp2[i] - i) ? dp1[i] : dp2[i]); sb.append(array[temp] + " "); } } System.out.println(sb.toString().trim()); T--; } } } private static void endToStart(int[] array, int[] dp, int n) { for (int i = n; i > 0; i--) { int temp = dp[i]; while (temp != n+1 && array[i] <= array[temp]) { temp = dp[temp]; } dp[i] = temp; } } private static void startToEnd(int[] array, int[] dp, int n) { for (int i = 1; i <= n; i++) { int temp = dp[i]; while (temp != 0 && array[i] <= array[temp]) { temp = dp[temp]; } dp[i] = temp; } }} /************************************************************** Problem: 1422 User: wzqwsrf Language: Java Result: Accepted Time:2460 ms Memory:191660 kb****************************************************************/
0 0
- 【九度】题目1422:Closest Number
- 九度 题目1422:Closest Number
- 九度OJ 1422 Closest Number
- 九度OJ 题目1136:Number Steps
- 【九度】题目1040:Prime Number
- 九度OJ 题目1136:Number Steps
- 九度题目1040Prime Number
- 九度 oj 题目1040:Prime Number
- 九度OJ题目1040:Prime Number
- 九度 题目1040:Prime Number
- 九度题目1040:Prime Number
- 九度OJ—题目1040:Prime Number
- 题目 1040 Prime Number 九度Online Judge
- 【九度OJ】题目1040:Prime Number 解题报告
- 九度题目1202
- 九度题目1061
- 九度题目1185
- 九度题目1023
- 倒计时
- 三大士大夫
- android bitmap compress(图片压缩)
- output一点感悟
- pitfall while if condition in python non zero
- 【九度】题目1422:Closest Number
- cocos2d-x与ios内存管理分析
- 获取指定路径的剩余空间
- UVa 10137 The Trip
- _launcher.xml中的结构
- C++析构函数的定义和使用
- 让PLSQL中查询出来的数据可进行编辑
- MTK camera 闪光灯Flashlight驱动调试流程
- jquery 运行就执行的事件