CCF CSP Brush Question Record 3-201403 Opposite Number, Window

Question number: 201403-1
Question name: Opposite number
time limit: 1.0s
Memory limit: 256.0MB
Problem Description:

Problem Description

  There are N non-zero and different integers. Please make a program to find out how many pairs of opposite numbers there are (a and -a are a pair of opposite numbers).

Input format

  The first line contains a positive integer N. (1 ≤ N ≤ 500).
  The second line is N non-zero integers separated by a single space, and the absolute value of each number does not exceed 1000, to ensure that these integers are different.

Output format

  Only output an integer, that is, how many pairs of opposite numbers are contained in these N numbers.

Sample input

5
1 2 3 -1 -2

Sample output

2

 

import java.util.Scanner;

public class 相反数201403_1 {
	public static void main(String[] args){
		Scanner sc=new Scanner(System.in);
		int len=sc.nextInt();
		int[] a=new int[1001];//<0
		int[] b=new int[1001];//>0
		for(int i=1;i<=len;i++){
			int num=sc.nextInt();
			if(num<0){
				a[-num]+=1;
			}else{
				b[num]+=1;
			}
		}
		int res=0;
		for(int j=1;j<=1000;j++){
			if(a[j]<=b[j]){
				res+=a[j];
			}else{
				res+=b[j];
			}
		}
		
		System.out.println(res);
		
	}
}

 

Question number:

201403-2
Question name: window
time limit: 1.0s
Memory limit: 256.0MB
Problem Description:

Problem Description

  In a graphics operating system, there are N windows, and each window is a rectangular area with two sides parallel to the coordinate axis. The point on the boundary of the window also belongs to the window. There are hierarchical differences between windows. In an area where more than one window overlaps, only the content of the window at the top level will be displayed.
  When you click a point on the screen, you select the topmost window at the clicked position, and this window will be moved to the topmost level of all windows, while the hierarchy of the remaining windows remains unchanged. If the location you click does not belong to any window, the system will ignore your click.
  Now we hope you write a program to simulate the process of clicking on the window.

Input format

  输入的第一行有两个正整数,即 N 和 M。(1 ≤ N ≤ 10,1 ≤ M ≤ 10)
  接下来 N 行按照从最下层到最顶层的顺序给出 N 个窗口的位置。 每行包含四个非负整数 x1, y1, x2, y2,表示该窗口的一对顶点坐标分别为 (x1, y1) 和 (x2, y2)。保证 x1 < x2,y1 2。
  接下来 M 行每行包含两个非负整数 x, y,表示一次鼠标点击的坐标。
  题目中涉及到的所有点和矩形的顶点的 x, y 坐标分别不超过 2559 和  1439。

输出格式

  输出包括 M 行,每一行表示一次鼠标点击的结果。如果该次鼠标点击选择了一个窗口,则输出这个窗口的编号(窗口按照输入中的顺序从 1 编号到 N);如果没有,则输出"IGNORED"(不含双引号)。

样例输入

3 4
0 0 4 4
1 1 5 5
2 2 6 6
1 1
0 0
4 4
0 5

样例输出

2
1
1
IGNORED

样例说明

  第一次点击的位置同时属于第 1 和第 2 个窗口,但是由于第 2 个窗口在上面,它被选择并且被置于顶层。
  第二次点击的位置只属于第 1 个窗口,因此该次点击选择了此窗口并将其置于顶层。现在的三个窗口的层次关系与初始状态恰好相反了。
  第三次点击的位置同时属于三个窗口的范围,但是由于现在第 1 个窗口处于顶层,它被选择。
  最后点击的 (0, 5) 不属于任何窗口。

 

简单说一下窗口这个题的思路:我的想法是二维数组的索引号就代表是坐标,然后存的值就是窗口序号。需要注意的是每次点击一次,序号会进行更新,因为点击到的窗口会变成最上层的。然后再读数就好了。

import java.util.Scanner;
public class 窗口201403_2 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N=sc.nextInt();
		int M=sc.nextInt();
		
		int[][] a =new int[2560][1440];
		int[] area=new int[4*N+1];
		
		for(int i=1;i<=4*N;i++){
			area[i]=sc.nextInt();
		}
		for(int i=0;i<N;i++){
			for(int j=area[4*i+1];j<=area[4*i+3];j++){
				for(int k=area[4*i+2];k<=area[4*i+4];k++){
					a[j][k]=i+1;
				}
			}
		}
		int[] res=new int[M+1];
		
		for(int i=1;i<=M;i++){
			res[i]=a[sc.nextInt()][sc.nextInt()];
			if(res[i]!=0){
			for(int j=area[4*(res[i]-1)+1];j<=area[4*(res[i]-1)+3];j++){
				for(int k=area[4*(res[i]-1)+2];k<=area[4*(res[i]-1)+4];k++){
					a[j][k]=res[i];
				}
			}
			}
		}
		for(int i=1;i<=M;i++){
			if(res[i]==0){
				System.out.println("IGNORED");
			}else{
				System.out.println(res[i]);
			}
		}
	}

}

 

Guess you like

Origin blog.csdn.net/m0_37483148/article/details/108245073