转载题解请联系WeBank及赛码网,版权所有违者必究。
分配:
枚举前两类员工数,计算判断第三类员工是否可满足
示例代码:
#include <bits/stdc++.h>
using namespace std;
#define maxn 10009
int main(){
int n, a, b, c;
scanf("%d%d%d%d", &n, &a, &b, &c);
int ans=0;
for(int i=0; i<=a; i++) for(int j=0; j<=b; j++)
if ((n-i*5-j*8)%10==0 && n-i*5-j*8>=0 && n-i*5-j*8<=c*10)
ans++;
printf("%d\n", ans);
return 0;
}
矩形判断:
读取线段,按照题意判断,注意矩形要与坐标轴平行
示例代码:
import java.util.*;
import java.awt.Point;
public class Main {
static Point array[];
static boolean flag = true;
public static void checker(Point a) {
int cnt = 0;
for (int i = 0; i < array.length; i++) {
if (array[i].x == a.x && array[i].y == a.y)
cnt++;
}
if (cnt != 2)
flag = false;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
array = new Point[8];
int ptr = 0;
int slope[] = new int[4];
for (int i = 0; i < 8; i += 2) {
array[i] = new Point(in.nextInt(), in.nextInt());
array[i + 1] = new Point(in.nextInt(), in.nextInt());
if (array[i].x == array[i + 1].x)
slope[ptr] = 1;
else if (array[i].y == array[i + 1].y)
slope[ptr] = -1;
ptr++;
}
for (int i = 0; i < 8; i++) {
checker(array[i]);
}
ptr = 0;
int ptr1 = 0;
for (int i = 0; i < 4; i++) {
if (slope[i] == -1)
ptr++;
if (slope[i] == 1)
ptr1++;
}
if (ptr == ptr1 && ptr == 2 && flag)
System.out.println("YES");
else
System.out.println("NO");
} //while
in.close();
}
}
装卸竞赛:
其实就是nim游戏,只是异或的数比较多。注意到0异或1异或2异或3等于0,4异或5异或6异或7等于零。用这个性质优化
示例代码:
import java.util.Scanner;
public class Main {
public static long xor_fn(long val, long l) {
long ans = 0;
for (int i = 0; i < l; i++) {
ans ^= (val + i);
}
return ans;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
int n = scan.nextInt();
long grundy = 0;
for (int i = 0; i < n; i++) {
long ans = 0;
long x = scan.nextLong();
long m = scan.nextLong();
if (m > 6) {
long y = x + m - 1, var = 0, var1 = 0;
if (x % 4 != 0) {
long t = 0;
if (x % 4 == 1)
t = 3;
else if (x % 4 == 2)
t = 2;
else if (x % 4 == 3)
t = 1;
var = xor_fn(x, t);
}
if (y % 4 != 3) {
long k = y % 4 + 1;
var1 = xor_fn(y - k + 1, k);
}
ans = var ^ var1;
} else {
ans = xor_fn(x, m);
}
grundy ^= ans;
}
System.out.println(((grundy > 0) ? "first" : "second"));
}
scan.close();
}
}
城市建设:
预处理计算出以每个点为左上角时的代价,排序依次建设,并把建设过的点标记。
示例代码:
import java.io.*;
import java.util.*;
public class Main {
static class LIPair implements Comparable<LIPair> {
long a;
int y, z;
LIPair(long a, int y, int z) {
this.a = a;
this.y = y;
this.z = z;
}
[@Override](/user/Override)
public int compareTo(LIPair l) {
if (this.a < l.a) {
return -1;
}
if (this.a == l.a) {
return 0;
}
return 1;
}
}
public static void main(String[] args) throws java.lang.Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
long cur = System.currentTimeMillis();
String line = in.readLine();
while (line != null && line.trim().length() > 0) {
ArrayList<LIPair> arr = new ArrayList<>();
ArrayList<LIPair> res = new ArrayList<>();
char[][] track = new char[1001][1001];
long[][] sum1 = new long[1001][1001], sum2 = new long[1001][1001], grid = new long[1001][1001];
int[] index = new int[1001];
String[] input = line.split(" ");
int N = Integer.parseInt(input[0]), M = Integer.parseInt(input[1]), A = Integer
.parseInt(input[2]), B = Integer.parseInt(input[3]);
int L = 0, R = 0;
for (int i = 1; i <= N; i++) {
String[] input2 = in.readLine().split(" ");
for (int j = 1; j <= M; j++) {
grid[i][j] = Integer.parseInt(input2[j - 1]);
}
}
for (int i = 1; i <= N; i++) {
L = 0;
R = 0;
for (int j = 1; j <= M; j++) {
while (L != R && index[L] < j - B + 1) {
L++;
}
while (L < R && grid[i][index[R - 1]] >= grid[i][j]) {
R--;
}
index[R++] = j;
if (j >= B) {
sum1[i][j - B + 1] = grid[i][index[L]];
}
}
}
for (int j = 1; j <= M; j++) {
L = 0;
R = 0;
for (int i = 1; i <= N; i++) {
while (L != R && index[L] < i - A + 1) {
L++;
}
while (L < R && sum1[index[R - 1]][j] >= sum1[i][j]) {
R--;
}
index[R++] = i;
if (i >= A) {
sum2[i - A + 1][j] = sum1[index[L]][j];
}
}
}
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) {
sum1[i][j] = sum1[i - 1][j] + sum1[i][j - 1]
- sum1[i - 1][j - 1] + grid[i][j];
}
}
for (int i = 1; i <= N - A + 1; i++) {
for (int j = 1; j <= M - B + 1; j++) {
grid[i][j] = sum1[i + A - 1][j + B - 1]
- sum1[i - 1][j + B - 1] - sum1[i + A - 1][j - 1]
+ sum1[i - 1][j - 1] - sum2[i][j] * A * B;
LIPair l = new LIPair(grid[i][j], i, j);
arr.add(l);
}
}
Collections.sort(arr);
for (int i = 0; i < arr.size(); i++) {
if (track[arr.get(i).y][arr.get(i).z] == 0) {
int mm1 = Math.max(1, arr.get(i).y - A + 1);
int mmin1 = Math.min(N + 1, arr.get(i).y + A);
int mm2 = Math.max(1, arr.get(i).z - B + 1);
int mmin2 = Math.min(M + 1, arr.get(i).z + B);
for (int j = mm1; j < mmin1; j++) {
for (int k = mm2; k < mmin2; k++) {
track[j][k] = 1;
}
}
res.add(arr.get(i));
}
}
out.println(res.size());
for (LIPair l : res) {
out.println(l.y + " " + l.z + " " + l.a);
}
line = in.readLine();
}
// System.err.println(System.currentTimeMillis()-cur);
in.close();
out.close();
}
}
转载题解请联系WeBank及赛码网,版权所有违者必究。
以上思路供同学们参考,也希望同学能分享自己的经验。