A - Wrong Answer
题目:
给两个0到9之间的数,输出一个不等于A+B且在0到9之间的数
思路:
简单判断一下是否等于0,等于输出1,否则输出A+B-1
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
class Main {
private static InputStream is = System.in;
private static final BufferedReader in = new BufferedReader(new InputStreamReader(is));
private static final PrintWriter out = new PrintWriter(System.out);
static void solve() throws IOException {
String[] s = in.readLine().split( " ");
int a = Integer.parseInt(s[0]), b = Integer.parseInt(s[1]);
int ans = a + b - 1;
out.print(ans > 0 ? ans : a + b + 1);
}
public static void main(String[] args) throws IOException {
int t = 1;
// t = Integer.parseInt(in.readLine());
while (t -- > 0) {
solve();
}
in.close();
out.flush();
out.close();
}
}
B - Adjacency Matrix
题目:
给一个简单无向图G以及编号1到N的边,以升序输出每个节点的相联节点
思路:
题目所给是二维矩阵 g
,第i个节点只需要输出 g[i]
中值为1的元素下标即可
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
class B {
private static InputStream is = System.in;
private static final BufferedReader in = new BufferedReader(new InputStreamReader(is));
private static final PrintWriter out = new PrintWriter(System.out);
static void solve() throws IOException {
int n = Integer.parseInt(in.readLine());
int[][] g = new int[n + 1][n +1];
for (int i = 1; i <= n; i ++) {
String[] ins = in.readLine().split(" ");
for (int j = 1; j <= n; j ++) {
g[i][j] = Integer.parseInt(ins[j - 1]);
if (g[i][j] == 1) out.print(j + " ");
}
out.println();
}
}
public static void main(String[] args) throws IOException {
int t = 1;
// t = Integer.parseInt(in.readLine());
while (t -- > 0) {
solve();
}
in.close();
out.flush();
out.close();
}
}
C - 343
题目:
给一个正整数N,求一个不超过N且为回文性质的最大立方数
思路:
题目给的N最大是 1018,假设答案为 x
, 则 x^3 <= N
,这个 x
的范围在 [1, 10^6]
区间内,x
数位最多为6位,那么通过枚举 [1, 10^6]
区间的数的时间复杂度为 O(log x)
,加上判断回文的时间最大为 6 * 10^6
,并不会超时间限制
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
class Main {
private static InputStream is = System.in;
private static final BufferedReader in = new BufferedReader(new InputStreamReader(is));
private static final PrintWriter out = new PrintWriter(System.out);
private static final int N = 1000_000;
static void solve() throws IOException {
long n = Long.parseLong(in.readLine());
long ans = 0;
for (int i = 1; i <= N; i ++) {
long x = (long) i * i * i;
if (x > n) break;
if (check(x)) {
ans = x;
}
}
out.print(ans);
}
static boolean check(long x) {
String s = String.valueOf(x);
for (int i = 0, j = s.length() - 1; i < j; i ++, j --) {
if (s.charAt(i) != s.charAt(j)) return false;
}
return true;
}
public static void main(String[] args) throws IOException {
int t = 1;
// t = Integer.parseInt(in.readLine());
while (t -- > 0) {
solve();
}
in.close();
out.flush();
out.close();
}
}
D - Diversity of Scores
题目:
有N个玩家编号从1到N,初始分数都为0,给出T组数据,未来第i组数据中指明第Ai 位玩家在第i秒时增加 Bi 分数,求第i(i=1,2,3…T)+0.5秒时所有玩家不同分数的个数
思路:
跟着模拟一下即可,用 Map 存一下所有玩家分数的出现次数
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
class Main {
private static InputStream is = System.in;
private static final BufferedReader in = new BufferedReader(new InputStreamReader(is));
private static final PrintWriter out = new PrintWriter(System.out);
static void solve() throws IOException {
String[] s = in.readLine().split(" ");
int n = Integer.parseInt(s[0]), t = Integer.parseInt(s[1]);
long[] p = new long[n + 1];
Map<Long, Integer> st = new HashMap<>();
st.put(0L, n);
while (t -- > 0) {
s = in.readLine().split(" ");
int a = Integer.parseInt(s[0]), b = Integer.parseInt(s[1]);
long pre = p[a];
p[a] += b;
st.put(pre, st.getOrDefault(pre, 0) - 1);
if (st.getOrDefault(pre, 0) == 0) {
st.remove(pre);
}
st.put(p[a],st.getOrDefault(p[a], 0) + 1);
out.println(st.size());
}
}
public static void main(String[] args) throws IOException {
int t = 1;
// t = Integer.parseInt(in.readLine());
while (t -- > 0) {
solve();
}
in.close();
out.flush();
out.close();
}
}
E - 7x7x7
题目:
有3个7x7x7的立方体,用C(a,b,c)表示立方体 (a≤x≤a+7)∧(b≤y≤b+7)∧(c≤z≤c+7),给出三个整数v1,v2,v3,要求满足:
- 仅包含一个立方体的区域的体积为 V1
- 仅包含两个立方体的区域的体积为V2
- 包含三个立方体的区域的体积为V3
输出这三个立方体的坐标(一共九个数),坐标值的绝对值小于等于100
思路:
- 题目中的立方体的位置实际上是相对的,简单起见可以规定其中一个立方体的坐标为(0,0,0)
- 如果两个立方体没有相交,无论两个立方体距离多远,其相交的体积都肯定为0,这里可以规定两个立方体坐标之差小于等于7(将没有相交视作为相邻:仅有边或者顶点相交)
- 基于上面的规定,对于已经规定了坐标为(0,0,0)的立方体,其他两个立方体的坐标只能在
[-7, 7]
这个区间内取值 - 仍有6个数是未知的,但是
15^6=11390625≈10^6
,可以暴力枚举这6个数,并不会超出时间范围 - 接下来就是求取题目要求的体积了:
- v3是最容易求得,包含三个立方体的区域,对于其长宽高三种边,每一种边都由其中两个立方体相交产生;
对于两条线段相交如何求相交的长度呢?假设a(a1,a2)和线段b(b1,b2)相交(a1 < a2, b1 < b2),如果 a1 < b1,那么相交的长度为 a2 - b1;是较前的线段后端点减去较后的线段前端点;
对于立方体,就可以求出相交的边长度:min(a1, a2, a3) + 7 - max(a1, a2, a3)
,但结果可能是负数,因为两者没有相交,需要判断一下,没有相交应该为0 - 求出来 v3,那么仅包含两个立方体的区域体积能求出来:任意两个立方体相交的体积之和减去重复的v3;如果v3存在,那么肯定任意两个立方体都有相交,此时计算 v3就会重复计算3次;如果 v3 为0,那么最多只有两对立方体相交、最少一对立方体没有相交;
- v1就是立方体体积之和减去 v2 * 2 再减去v3 * 3
立方体体积之和中,每个立方体都有两个v2的一部分、有1个重复的v3
- v3是最容易求得,包含三个立方体的区域,对于其长宽高三种边,每一种边都由其中两个立方体相交产生;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
class Main {
private static InputStream is = System.in;
private static final BufferedReader in = new BufferedReader(new InputStreamReader(is));
private static final PrintWriter out = new PrintWriter(System.out);
static int max(int a, int b, int c) {
return Math.max(Math.max(a, b), c);
}
static int min(int a, int b, int c) {
return Math.min(Math.min(a, b), c);
}
static int calV3(int a1, int b1, int c1, int a2, int b2, int c2, int a3, int b3, int c3) {
int a = Math.max(min(a1, a2, a3) + 7 - max(a1, a2, a3), 0);
int b = Math.max(min(b1, b2, b3) + 7 - max(b1, b2, b3), 0);
int c = Math.max(min(c1, c2, c3) + 7 - max(c1, c2, c3), 0);
return a * b * c;
}
static int calV2(int a1, int b1, int c1, int a2, int b2, int c2) {
int a = Math.max(Math.min(a1, a2) + 7 - Math.max(a1, a2), 0);
int b = Math.max(Math.min(b1, b2) + 7 - Math.max(b1, b2), 0);
int c = Math.max(Math.min(c1, c2) + 7 - Math.max(c1, c2), 0);
return a * b * c;
}
static void solve() throws IOException {
String[] ins = in.readLine().split(" ");
int v1 = Integer.parseInt(ins[0]), v2 = Integer.parseInt(ins[1]), v3 = Integer.parseInt(ins[2]);;
for (int a2 = -7; a2 <= 7; a2 ++) {
for (int b2 = -7; b2 <= 7; b2 ++) {
for (int c2 = -7; c2 <= 7; c2 ++) {
for (int a3 = -7; a3 <= 7; a3 ++) {
for (int b3 = -7; b3 <= 7; b3 ++) {
for (int c3 = -7; c3 <= 7; c3 ++) {
int t3 = calV3(0, 0, 0, a2, b2, c2, a3, b3, c3);
int t2 = calV2(0, 0, 0, a2, b2, c2)
+ calV2(0, 0, 0, a3, b3, c3)
+ calV2(a2, b2, c2, a3, b3, c3)
- 3 * t3;
int t1 = 7 * 7 * 7 * 3 - t2 * 2 - t3 * 3;
if (t1 == v1 && t2 == v2 && t3 == v3) {
out.println("Yes\n0 0 0 " + a2 + " " + b2 + " " + c2 + " " + a3 + " " + b3 + " " + c3);
return;
}
}
}
}
}
}
}
out.println("No");
}
public static void main(String[] args) throws IOException {
int t = 1;
// t = Integer.parseInt(in.readLine());
while (t -- > 0) {
solve();
}
in.close();
out.flush();
out.close();
}
}
复杂 世界总创造
不高兴和 没头脑
我却只想 造一只猫
靠漫步 将生趣填饱
心也有一片富饶
快乐就会 笑
不再计较 两脚兽烦恼
一次 也好 任青春消耗
脚下 花草 有日光就好