【LQ系列】 ALGO-1~ALGO-2
2016-06-12 22:19
344 查看
进入算法训练部分,感到难度进一步提升,现在卡在“K好数”上,先上传前两道题:
【ALGO-1】 算法训练 区间k大数查询
问题描述
给定一个序列,每次询问序列中第l个数到第r个数中第K大的数是哪个。
输入格式
第一行包含一个数n,表示序列长度。
第二行包含n个正整数,表示给定的序列。
第三个包含一个正整数m,表示询问个数。
接下来m行,每行三个数l,r,K,表示询问序列从左往右第l个数到第r个数中,从大往小第K大的数是哪个。序列元素从1开始标号。
输出格式
总共输出m行,每行一个数,表示询问的答案。
样例输入
5
1 2 3 4 5
2
1 5 2
2 3 2
样例输出
4
2
数据规模与约定
对于30%的数据,n,m<=100;
对于100%的数据,n,m<=1000;
保证k<=(r-l+1),序列中的数<=106。
Code:
【ALGO-2】 算法训练 最大最小公倍数
问题描述
已知一个正整数N,问从1~N中任选出三个数,他们的最小公倍数最大可以为多少。
输入格式
输入一个正整数N。
输出格式
输出一个整数,表示你找到的最小公倍数。
样例输入
9
样例输出
504
数据规模与约定
1 <= N <= 106。
Code:
今天在CSDN看到一句话:“编程就是算法和数据结构,算法和数据结构是编程的灵魂。” 写的挺好,已经记在小本本上了~~
【ALGO-1】 算法训练 区间k大数查询
问题描述
给定一个序列,每次询问序列中第l个数到第r个数中第K大的数是哪个。
输入格式
第一行包含一个数n,表示序列长度。
第二行包含n个正整数,表示给定的序列。
第三个包含一个正整数m,表示询问个数。
接下来m行,每行三个数l,r,K,表示询问序列从左往右第l个数到第r个数中,从大往小第K大的数是哪个。序列元素从1开始标号。
输出格式
总共输出m行,每行一个数,表示询问的答案。
样例输入
5
1 2 3 4 5
2
1 5 2
2 3 2
样例输出
4
2
数据规模与约定
对于30%的数据,n,m<=100;
对于100%的数据,n,m<=1000;
保证k<=(r-l+1),序列中的数<=106。
Code:
import java.util.Arrays; import java.util.Scanner ; public class Main { // 在A中的[l-1, r-1]中寻找第K大的数 private static int find( int[] A, int l, int r, int K ) { int[] B = new int[r-l+1] ; for( int i = l-1; i <= r-1; i ++ ) B[i-(l-1)] = A[i] ; Arrays.sort(B) ; return B[(B.length-1)-K+1] ; } public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner( System.in ) ; int n = sc.nextInt() ; // 序列长度 int[] A = new int ; // 给定序列 for( int i = 0; i < n; i ++ ) A[i] = sc.nextInt() ; int m = sc.nextInt() ; // 访问个数 for( int i = 1; i <= m; i ++ ) { int l = sc.nextInt() ; int r = sc.nextInt() ; int K = sc.nextInt() ; // 访问[l-1, r-1]中第K大的数 System.out.println( find( A, l, r, K ) ) ; } } }
【ALGO-2】 算法训练 最大最小公倍数
问题描述
已知一个正整数N,问从1~N中任选出三个数,他们的最小公倍数最大可以为多少。
输入格式
输入一个正整数N。
输出格式
输出一个整数,表示你找到的最小公倍数。
样例输入
9
样例输出
504
数据规模与约定
1 <= N <= 106。
Code:
import java.math.BigInteger; import java.util.Scanner ; public class Main { // a 与 b 与 c 是否没有相同的公因子 private static boolean noCommonFactor( BigInteger a, BigInteger b, BigInteger c ) { if( a.gcd( b ).equals( new BigInteger( "1" ) ) && a.gcd( c ).equals( new BigInteger( "1" ) ) && b.gcd( c ).equals( new BigInteger( "1" ) ) ) return true ; return false ; } public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner( System.in ) ; String str = sc.next() ; if( Integer.parseInt( str ) % 2 != 0 ) { // 输入为奇数,则最大最小公倍数一定是 N * (N-1) * (N-2) BigInteger N = new BigInteger( str ); System.out.println( N.multiply( N.subtract( new BigInteger("1") ) ).multiply( N.subtract( new BigInteger("2") ) ) ) ; } else { // 输入为偶数,可能是 (N-1) * (N-2) * (N-3),也可能是 N * a * b (其中 a >= n-3) BigInteger result = new BigInteger( "0" ); // 可能结果一:(n-1) * (n-2) * (n-3) BigInteger N = new BigInteger( str ); BigInteger R1 = ( N.subtract( new BigInteger("1") ) ).multiply( N.subtract( new BigInteger("2") ) ).multiply( N.subtract( new BigInteger("3") ) ) ; // (N-1) * (N-2) * (N-3) // 可能结果二:n * a * b (a>=n-3) BigInteger R2 = new BigInteger( "0" ) ; for( char c ='1'; c <='3'; c ++ ) { // n-3 =<a <= n-1 BigInteger a = N.subtract( new BigInteger( "" + c ) ) ; for( char c2 = '3'; c2 <='9'; c2 ++ ) { // 假设 n-9 =< b <=n-1 BigInteger b = N.subtract( new BigInteger( "" + c2) ) ; if( noCommonFactor( N, a, b ) ) { // 如果N,a,b不存在相同的公因子 BigInteger temp = N.multiply(a).multiply(b) ; if( temp.compareTo(R2) == 1 ) { // temp > R2 R2 = temp ; break ; } else { // temp <= R2 continue ; } } // if } // for } // for result = R1.max(R2) ; System.out.println( result ) ; } } }
今天在CSDN看到一句话:“编程就是算法和数据结构,算法和数据结构是编程的灵魂。” 写的挺好,已经记在小本本上了~~
相关文章推荐
- Ubuntu 下环境搭建系列 —— 安装 Google Chrome
- Django setting 常用配置
- Monad in Hask(一)
- God Chang's interview——异或
- Google体系地图纠偏算法
- Leetcode 198. House Robber
- django+celery+djcelery 最简配置
- Leetcode 3. Longest Substring Without Repeating Characters
- golang压缩、解压文件
- go语言的urlencode编码使用方式
- GoogleMaps踏坑之旅(三)
- django学习之旅 - 基础命令讲解
- Sugon I840-G20安装windows系统
- 用Golang实现基于时间轮算法的定时器
- Mongo expireAfterSeconds 时间设置
- Dashgo D1蓝牙控制移动
- 如何更换Odoo 8的LOGO
- Django中文显示
- poj 1179 Polygon(化环为直+区间dp)
- jump Consistent hash:零内存消耗,均匀,快速,简洁,来自Google的一致性哈希算法