789. 数的范围 - AcWing题库
import java.util.*;
public class Main{
static int N = 100010;
static int[] a = new int[N];
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
for(int i = 0; i < n; i ++){
a[i] = sc.nextInt();
}
while(m -- > 0){
int x = sc.nextInt();
int l = 0, r = n - 1;
while(l < r){
int mid = l + r >> 1;
if(a[mid] >= x) r = mid;//找到区间的左端点(区分大于等于x的和小于x的)
else l = mid + 1;
}
if(a[l] != x) System.out.println("-1 -1");
else{
System.out.print(l + " ");
l = 0;
r = n - 1;
while(l < r){
int mid = l + r + 1 >> 1;
if(a[mid] <= x) l = mid;//找到区间的右端点(区分小于等于x的和大于x的)
else r = mid - 1;
}
System.out.println(l);
}
}
}
}
790. 数的三次方根 - AcWing题库
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
double x = sc.nextDouble();
double l = -100, r = 100;
while(r - l > 1e-8){
double mid = (l + r) / 2;
if(mid * mid * mid >= x) r = mid;
else l = mid;
}
System.out.printf("%.6f", l);
}
}
795. 前缀和 - AcWing题库
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int N = 100010;
int[] a = new int[N];
int[] b = new int[N];
int n = sc.nextInt();
int m = sc.nextInt();
for(int i = 1; i <= n; i ++){
a[i] = sc.nextInt();//数组的每个数
}
for(int i = 1; i <= n; i ++){
b[i] = b[i - 1] + a[i];//前缀和
}
while(m -- > 0){
int l = sc.nextInt();
int r = sc.nextInt();
System.out.println(b[r] - b[l - 1]);
}
}
}
796. 子矩阵的和 - AcWing题库
import java.util.*;
public class Main{
static int N = 1010;
static int[][] a = new int[N][N];//每个位置的值
static int[][] s = new int[N][N];//二维前缀和
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int q = sc.nextInt();
for(int i = 1; i <= n; i ++){
for(int j = 1; j <= m; j ++){
a[i][j] = sc.nextInt();
}
}
for(int i = 1; i <= n; i ++){
for(int j = 1; j <= m; j ++){
s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + a[i][j];
}
}
while(q -- > 0){
int x1 = sc.nextInt();
int y1 = sc.nextInt();
int x2 = sc.nextInt();
int y2 = sc.nextInt();
System.out.println(s[x2][y2] - s[x1 - 1][y2] - s[x2][y1 - 1] + s[x1 - 1][y1 - 1]);
}
}
}
1227. 分巧克力 - AcWing题库
import java.util.*;
public class Main{
static int N = 100010;
static int n, m;
static int[] h = new int[N];
static int[] w = new int[N];
public static boolean check(int x){
int res = 0;
for(int i = 1; i <= n; i ++){
res += (h[i] / x) * (w[i] / x);
}
if(res >= m) return true;
return false;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
int max = 0;
for(int i = 1; i <= n; i ++){
h[i] = sc.nextInt();
w[i] = sc.nextInt();
max = Math.max(h[i], max);
max = Math.max(w[i], max);
}
int l = 0, r = max;
while(l < r){
int mid = l + r + 1>> 1;
if(check(mid)) l = mid;
else r = mid - 1;
}
System.out.print(l);
}
}
1230. K倍区间 - AcWing题库
import java.util.*;
public class Main{
static int N = 100010;
static long[] a = new long[N];
static long[] s = new long[N];//前缀和
static long[] cnt = new long[N];//模k余数为x
static int n, k;
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
k = sc.nextInt();
for(int i = 1; i <= n; i ++){
a[i] = sc.nextInt();
}
for(int i = 1; i <= n; i ++){
s[i] = s[i - 1] + a[i];
}
long res = 0;
cnt[0] = 1;//初始化余数为0也是一种情况
for(int i = 1; i <= n; i ++){
res += cnt[(int)(s[i] % k)];//在还没有改变模k的余数为x的前缀和的个数之前,加到答案中
cnt[(int)(s[i] % k)] ++;//个数加1
}
System.out.print(res);
}
}
730. 机器人跳跃问题 - AcWing题库
import java.util.*;
public class Main{
static int N = 100010;
static int[] h = new int[N];
static int n, max;
public static boolean check(int x){
for(int i = 1; i <= n; i ++){
x = 2 * x - h[i];
if(x >= max) return true;//只要存在能量大于等于最大值,就已经可以保证了
if(x < 0) return false;
}
return true;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
max = 0;
for(int i = 1; i <= n; i ++){
h[i] = sc.nextInt();
max = Math.max(max, h[i]);
}
int l = 0, r = N;
while(l < r){
int mid = l + r >> 1;
if(check(mid)) r = mid;
else l = mid + 1;
}
System.out.print(l);
}
}
1221. 四平方和 - AcWing题库
import java.util.*;
class PII implements Comparable<PII>{
int a, b, c;
public PII(int a, int b, int c){
this.a = a;
this.b = b;
this.c =c;
}
public int compareTo(PII o){
if(a != o.a) return Integer.compare(a, o.a);
if(b != o.b) return Integer.compare(b, o.b);
return Integer.compare(c, o.c);
}
}
public class Main{
static int N = 2500010;
static PII[] sum = new PII[N];
static int n, m;
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
for(int c = 0; c * c <= n; c ++){
for(int d = c; d * d + c * c <= n; d ++){
sum[m ++] = new PII(d * d + c * c, c, d);
}
}
Arrays.sort(sum, 0, m);//按照定义的次序排序
for(int a = 0; a * a <= n; a ++){
for(int b = a; b * b + a * a <= n; b ++){
int t = n - (a * a + b * b);
//通过二分判断这个t是否在sum数组中出现过
int l = 0, r = m - 1;
while(l < r){
int mid = l + r >> 1;
if(sum[mid].a >= t) r = mid;
else l = mid + 1;
}
if(sum[l].a == t){
System.out.print(a + " " + b + " " + sum[l].b + " " + sum[l].c);
return;
}
}
}
}
}