1、数的范围
给定一个按照升序排列的长度为 n 的整数数组,以及 q 个查询。
对于每个查询,返回一个元素 k的起始位置和终止位置(位置从 0开始计数)。
如果数组中不存在该元素,则返回 -1 -1。
第一行包含整数 n和 q,表示数组长度和询问个数。
第二行包含 n个整数(均在 1∼10000范围内),表示完整数组。
接下来 q行,每行包含一个整数 k,表示一个询问元素。
共 q 行,每行包含两个整数,表示所求元素的起始位置和终止位置。
如果数组中不存在该元素,则返回 -1 -1。
1≤n≤100000
1≤q≤10000
1≤k≤10000
6 3
1 2 2 3 3 4
3
4
5
3 4
5 5
-1 -1
AC代码:(闭区间二分)
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.HashMap;
import java.util.MissingFormatArgumentException;
import java.util.StringTokenizer;
import java.util.concurrent.atomic.AtomicInteger;
import javax.sound.midi.MidiChannel;
import javax.swing.event.AncestorEvent;
class in
{
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer tokenizer = new StringTokenizer("");
static String nextLine() throws IOException { return reader.readLine(); }
static String next() throws IOException
{
while (!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine());
return tokenizer.nextToken();
}
static int nextInt() throws IOException { return Integer.parseInt(next()); }
static double nextDouble() throws IOException { return Double.parseDouble(next()); }
static long nextLong() throws IOException { return Long.parseLong(next());}
static BigInteger nextBigInteger() throws IOException
{
BigInteger d = new BigInteger(in.nextLine());
return d;
}
}
public class Main
{
static final int N = (int) (1e5 + 10);
static int a[] = new int [N];
public static void main(String[] args) throws IOException
{
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int n = in.nextInt();
int k = in.nextInt();
for(int i = 0 ; i < n ; i ++) a[i] = in.nextInt();
while(k -- > 0)
{
int x = in.nextInt();
// 寻找左端点
int l = 0,r = n - 1;
while(l <= r)
{
int mid = l + r >> 1;
if(a[mid] < x) l = mid + 1;
else if(a[mid] == x) r = mid - 1;
else if(a[mid] > x) r = mid - 1;
}
if(a[l] == x)
{
out.print(l + " ");
// 寻找右端点
l = 0;
r = n - 1;
while(l <= r)
{
int mid = l + r >> 1;
if(a[mid] < x) l = mid + 1;
else if(a[mid] == x) l = mid + 1;
else if(a[mid] > x) r = mid - 1;
}
out.println(r + " ");
}
else out.println("-1 -1");
}
out.flush();
}
}
给定一个浮点数 n,求它的三次方根。
共一行,包含一个浮点数 n。
共一行,包含一个浮点数,表示问题的解。
注意,结果保留 6位小数。
−10000≤n≤10000
1000.00
10.000000
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.HashMap;
import java.util.MissingFormatArgumentException;
import java.util.StringTokenizer;
import java.util.concurrent.atomic.AtomicInteger;
import javax.sound.midi.MidiChannel;
import javax.swing.event.AncestorEvent;
class in
{
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer tokenizer = new StringTokenizer("");
// nextLine()读取字符串
static String nextLine() throws IOException { return reader.readLine(); }
// next()读取字符串
static String next() throws IOException
{
while (!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine());
return tokenizer.nextToken();
}
// 读取一个int型数值
static int nextInt() throws IOException { return Integer.parseInt(next()); }
// 读取一个double型数值
static double nextDouble() throws IOException { return Double.parseDouble(next()); }
// 读取一个long型数值
static long nextLong() throws IOException { return Long.parseLong(next());}
// 读取一个BigInteger
static BigInteger nextBigInteger() throws IOException
{
BigInteger d = new BigInteger(in.nextLine());
return d;
}
}
public class Main
{
static final int N = (int) (1e5 + 10);
static int a[] = new int [N];
public static void main(String[] args) throws IOException
{
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
double n = in.nextDouble();
double l = -10000,r = 10000;
while(r - l > 1e-8)
{
double mid = (l + r) / 2;
if(mid * mid * mid > n) r = mid;
else l = mid;
}
out.append(String.format("%.6f", l));
out.flush();
}
}
3、前缀和
输入一个长度为 n 的整数序列。
接下来再输入 m个询问,每个询问输入一对 l,r。
对于每个询问,输出原序列中从第 l个数到第 r个数的和。
第一行包含两个整数 n和 m。
第二行包含 n个整数,表示整数数列。
接下来 m行,每行包含两个整数 l 和 r,表示一个询问的区间范围。
共 m行,每行输出一个询问的结果。
1≤l≤r≤n,
1≤n,m≤100000,
−1000≤数列中元素的值≤1000
5 3
2 1 3 6 4
1 2
1 3
2 4
3
6
10
AC代码:
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.HashMap;
import java.util.MissingFormatArgumentException;
import java.util.StringTokenizer;
import java.util.concurrent.atomic.AtomicInteger;
import javax.sound.midi.MidiChannel;
import javax.swing.event.AncestorEvent;
class in
{
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer tokenizer = new StringTokenizer("");
static String nextLine() throws IOException { return reader.readLine(); }
static String next() throws IOException
{
while (!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine());
return tokenizer.nextToken();
}
static int nextInt() throws IOException { return Integer.parseInt(next()); }
static double nextDouble() throws IOException { return Double.parseDouble(next()); }
static long nextLong() throws IOException { return Long.parseLong(next());}
static BigInteger nextBigInteger() throws IOException
{
BigInteger d = new BigInteger(in.nextLine());
return d;
}
}
public class Main
{
static final int N = (int) (1e5 + 10);
static int a[] = new int [N];
static int sum[] = new int [N];
public static void main(String[] args) throws IOException
{
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int n = in.nextInt();
int k = in.nextInt();
for(int i = 1 ; i <= n ; i ++) a[i] = in.nextInt();
for(int i = 1 ; i <= n ; i ++) sum[i] = sum[i - 1] + a[i];
while(k -- > 0)
{
int l = in.nextInt();
int r = in.nextInt();
out.println(sum[r] - sum[l - 1]);
}
out.flush();
}
}