1.近似值查询 72%
近似值查询
时间限制: 4000MS
内存限制: 589824KB
题目描述:
给定一个长度为n的数组a,我们定义一个数x的近似值是在数组a中与x的差值的绝对值最小,如果两个数的绝对值相等,就取较小的那个。比如说对于[2, 5, 7],4的近似值就是5,而6的近似值应该取5,因为5更小。
现在有q个询问,对每个询问回答近似值。
输入描述
第一行两个整数n, q分别表示数组的长度和查询的次数。(1≤n,q≤1e5)
第二行n个数分别表示a1, a2, a3, … an。(0≤ai≤1e8)
接下来q行每行一个数x表示需要查询的数。(0≤x≤1e8)
输出描述
对于每个询问,输出一个近似值。
样例输入
5 5
1 2 3 4 5
3
0
7
2
5
样例输出
3
1
5
2
5
思路
用循环做的
应该用二分,循环会超时
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[] nums = new int[n];
for(int i = 0; i < n; i++){
nums[i] = sc.nextInt();
}
Arrays.sort(nums);
for(int i = 0; i < m; i++){
int num = sc.nextInt();
func(nums, n, num);
}
}
public static void func(int[] nums, int n, int num){
if(num >= nums[n-1]){
System.out.println(nums[n-1]);
return;
}
if(num <= nums[0]){
System.out.println(nums[0]);
return;
}
int res = Integer.MAX_VALUE;
for(int i = 1; i < n; i++){
if(num == nums[i]){
System.out.println(num);
return;
}else if(num < nums[i]){
int tmp = Math.abs(num-nums[i-1]) > Math.abs(num-nums[i]) ? nums[i] : nums[i-1];
res = Math.min(res, tmp);
System.out.println(res);
return;
}
}
}
}
2.操作矩形 84%
操作矩形
时间限制: 3000MS
内存限制: 786432KB
题目描述:
平面上有一个矩形,大小为n×m,其中,长度为n的边是竖直的,长度为m的边是水平的。将矩形切割成n×m个小正方形,规定左上角的小正方形属性值为(1,1),左下角的小正方形属性值为(n,1),右上角的小正方形属性值为(1,m),右下角的小正方形属性值为(n,m)。我们规定从上往下数第x行,从左往右数第y列的小正方形属性值为(x,y).
现在,我们对这个矩形进行如下操作,将其顺时针旋转90度x次,然后将其水平翻转y次,然后将其逆时针旋转90度z次。
接着,我们将这个操作后的矩形按原始的思路重新标上属性值,每一个小正方形就有一个原来的属性值和新的属性值。
然后有Q个询问,每一次询问一个原来属性值为(x,y)的小正方形的新属性值。你需要对所有询问作出回答。
输入描述
输入第一行包含三个数 x ,y ,z ,代表顺时针旋转,水平翻转,逆时针旋转的次数。
接下来一行两个数,n,m代表原矩形的大小。
接下来一行一个数,Q代表询问次数。
接下来Q行,每行两个数 x,y 代表一次询问,询问原矩形中属性值为(x,y)的小正方形现在的新属性值。
1≤x,y,z≤10^6;1≤n,m,Q≤1000;1≤x≤n;1≤y≤m
输出描述
输出包含Q行,每一行一个答案。
样例输入
1 1 1
4 5
3
1 1
3 4
2 5
样例输出
4 1
2 4
3 5
提示
样例解释:
(1,1)->(1,4)->(1,1)->(4,1)
(3,4)->(4,2)->(4,3)->(2,4)
(2,5)->(5,3)->(5,2)->(3,5)
思路
就是考虑位置关系,差点被m和n搞死
import java.util.Scanner;
public class Main {
/* static int n;
static int m;*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y = sc.nextInt();
int z = sc.nextInt();
int n = sc.nextInt();
int m = sc.nextInt();
int Q = sc.nextInt();
while ((Q--) > 0) {
int x0 = sc.nextInt();
int y0 = sc.nextInt();
for (int i = 0; i < x; i++) {
int x1 = x0;
int y1 = y0;
x0 = y1;
y0 = n + 1 - x1;
int tmp = n;
n = m;
m = tmp;
}
for (int i = 0; i < y; i++) {
int x1 = x0;
int y1 = y0;
x0 = x1;
y0 = m + 1 - y1;
}
for (int i = 0; i < z; i++) {
int x1 = x0;
int y1 = y0;
x0 = m + 1 - y1;
y0 = x1;
int tmp = n;
n = m;
m = tmp;
}
System.out.println(x0 + " " + y0);
}
}
}
3.三元组 下来之后写的
给你一个长度为n的序列A,你需要算出有多少个三元组(Ai,Aj,Ak)满足i<j<k且Ai≤Aj≤Ak。
第一行一个整数n,表示序列A的长度。
接下来一行n个整数,第i个数表示Ai的值。
一个整数x,表示满足要求的三元组数量。
6
2 3 5 4 3 3
6
思路:三个for循环,不知道还有没有简单的方法
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] nums = new int[n];
for(int i = 0; i < n; i++){
nums[i] = sc.nextInt();
}
//
int res = 0;
for(int i = 0; i < n-2; i++){
for(int j = i+1; j < n-1; j++){
for(int k = j+1; k < n; k++){
if(nums[i] <= nums[j] && nums[j] <= nums[k]){
res++;
}
}
}
}
System.out.println(res);
}
}