Skip to content

Commit

Permalink
feat: add 2018fall_lab3_1141
Browse files Browse the repository at this point in the history
Signed-off-by: Certseeds <51754303+Certseeds@users.noreply.github.com>
  • Loading branch information
Certseeds committed Apr 20, 2023
1 parent 7b8711e commit c1c9c9e
Show file tree
Hide file tree
Showing 7 changed files with 296 additions and 0 deletions.
58 changes: 58 additions & 0 deletions 2018fall/lab_3/lab_3_1141/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
SPDX-License-Identifier: CC-BY-NC-SA-4.0
---

# lab_3_1141

分析一下下面的用例

``` log
3
3 4 0
3 4 1
3 3 1
```

## 3 4 0

注意到`numbered (m + 1)th`,可见是从1开始计数的.

three node, destory 4 times, earty is fst

+ 0 1 2
+ 0 1 2 0 1
+ 1 is destory
+ 2 0
+ 2 0
+ 2 0 2 0 2
+ 2 is destory
+ 0
+ 0
+ 0 0 0 0 0
+ 0 is destory

0 is the last.

编号从0开始, 但是计数0开始记到m+1, 麻烦.

## 3 4 1

过程如上, 1是倒数第二个, 不是.

### 3 3 1

+ 0 1 2
+ 0 1 2 0
+ 0 is destory
+ 1 2
+ 1 2
+ 1 2 1 2
+ 2 is destory
+ 1
+ 1
+ 1 1 1 1 1
+ 1 is destory

这里可以发现, 需要把下一个计数的作为第一个, 才能推理通顺.

PS: 需要注意到, 并不是只进行math.min(n,m)次, 而是准确的进行n次, 每次1个.
22 changes: 22 additions & 0 deletions 2018fall/lab_3/lab_3_1141/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>nanoseeds.CS203_DSAA_template_java.2018fall</groupId>
<artifactId>lab_3</artifactId>
<version>${revision}</version>
<relativePath>./../pom.xml</relativePath>
</parent>
<groupId>nanoseeds.CS203_DSAA_template_java.2018fall.lab3</groupId>
<artifactId>lab_3_1141</artifactId>
<version>${revision}</version>
<name>${project.groupId}.${project.artifactId}</name>
<description>${project.groupId}.${project.artifactId}</description>

<build>
<sourceDirectory>${project.basedir}/src</sourceDirectory>
<testSourceDirectory>${project.basedir}/test</testSourceDirectory>
</build>
</project>
4 changes: 4 additions & 0 deletions 2018fall/lab_3/lab_3_1141/resources/01.data.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
3
3 4 0
3 4 1
3 3 1
3 changes: 3 additions & 0 deletions 2018fall/lab_3/lab_3_1141/resources/01.data.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Yes
No
Yes
163 changes: 163 additions & 0 deletions 2018fall/lab_3/lab_3_1141/src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
/*
CS203_DSAA_template
Copyright (C) 2023 nanos Certseeds
*/

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.stream.Collectors;

public final class Main {
private static final class one {
private final int n, m, e;

public one(int n, int m, int e) {
this.n = n;
this.m = m;
this.e = e;
}
}

public static List<one> read() {
final var input = new Scanner(System.in);
final int testcases = input.nextInt();
assert ((1 <= testcases) && (testcases <= 100));
final List<one> cases = new ArrayList<>(testcases);
for (int i = 0; i < testcases; i++) {
final int n = input.nextInt();
final int m = input.nextInt();
final int e = input.nextInt();
assert ((1 <= n) && (n <= 100));
assert ((1 <= m) && (m <= 100));
assert ((0 <= e) && (e <= n));
cases.add(new one(n, m, e));
}
return cases;
}

public static List<one> reader() {
final var input = new Reader();
final int testcases = input.nextInt();
assert ((1 <= testcases) && (testcases <= 100));
final List<one> cases = new ArrayList<>(testcases);
for (int i = 0; i < testcases; i++) {
final int n = input.nextInt();
final int m = input.nextInt();
final int e = input.nextInt();
assert ((1 <= n) && (n <= 100));
assert ((1 <= m) && (m <= 100));
assert ((0 <= e) && (e <= n));
cases.add(new one(n, m, e));
}
return cases;
}

private static final class node {
private static final node head = new node(0x3f3f3f3f);
public node pre, next;
private final int value;

public node(int value) {
this.value = value;
}
}

public static List<Boolean> cal(List<one> nums) {
final List<Boolean> results = nums.stream()
.map(one -> {
final int n = one.n, m = one.m, e = one.e;
node fst = node.head;
for (int i = 0; i < n; i++) {
fst.next = new node(i);
fst.next.pre = fst;
fst = fst.next;
}
fst.next = node.head.next;
node.head.next.pre = fst;
for (int i = 0; i < n; i++) { // after math.min(n,m), it must be done
final int index = (m) % (n - i);
node rolling = node.head;
for (int j = 0; j < index; j++) {
rolling = rolling.next;
}
final node temp = rolling.next;
if (temp != null) {
//if (temp.next != null) {
temp.next.pre = temp.pre;
//}
//if (temp.pre != null) {
temp.pre.next = temp.next;
node.head.next = temp.next;
//}
temp.next = null;
temp.pre = null;
}
}
return node.head.next.value == e;
})
.collect(Collectors.toUnmodifiableList());
return results;
}

public static void main(String[] args) throws IOException {
final var datas = reader();
final var result = cal(datas);
output(result);
}

public static void output(List<Boolean> decides) {
for (var decide : decides) {
if (decide) {
System.out.print("Yes\n");
} else {
System.out.print("No\n");
}
}
}

// refactor from https://github.com/Kattis/kattio/blob/master/Kattio.java
// url: https://raw.githubusercontent.com/Kattis/kattio/master/Kattio.java
// license: MIT
private static final class Reader {
private final BufferedReader br;
private StringTokenizer st;

private Reader() {
br = new BufferedReader(new InputStreamReader(System.in));
}

String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}

int nextInt() {return Integer.parseInt(next());}

long nextLong() {return Long.parseLong(next());}

double nextDouble() {return Double.parseDouble(next());}

String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
45 changes: 45 additions & 0 deletions 2018fall/lab_3/lab_3_1141/test/MainTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// SPDX-License-Identifier: AGPL-3.0-or-later

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.*;
import tests.Pair;
import tests.Redirect;

import java.io.IOException;

@Slf4j
public final class MainTest {
private static final String DATA_PATH = "resources/";
private static final long begin_time = System.currentTimeMillis();

@AfterAll
public static void last_one() throws IOException {
log.info("cost {} ms\n", System.currentTimeMillis() - begin_time);
}

@BeforeEach
public void beforeEach(TestInfo testInfo) {
log.info("{} begin", testInfo.getDisplayName());
}

@AfterEach
public void afterEach(TestInfo testInfo) {
log.info("{} end", testInfo.getDisplayName());
}

@Test
public void test_2() throws IOException {
try (Redirect redirect = Redirect.from(DATA_PATH, "01.data.in", "01.test.out")) {
Main.output(Main.cal(Main.read()));
final Pair<String, String> p = redirect.compare_double("01.data.out", "01.test.out");
Assertions.assertEquals(p.getFirst().length(), p.getSecond().length());
Assertions.assertEquals(p.getFirst(), p.getSecond());
}
try (Redirect redirect = Redirect.from(DATA_PATH, "01.data.in", "01.test.out")) {
Main.output(Main.cal(Main.reader()));
final Pair<String, String> p = redirect.compare_double("01.data.out", "01.test.out");
Assertions.assertEquals(p.getFirst().length(), p.getSecond().length());
Assertions.assertEquals(p.getFirst(), p.getSecond());
}
}
}
1 change: 1 addition & 0 deletions 2018fall/lab_3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<module>lab_3_1140</module>
<module>lab_3_1139</module>
<module>lab_3_1144</module>
<module>lab_3_1141</module>
</modules>
<dependencies>
<dependency>
Expand Down

0 comments on commit c1c9c9e

Please sign in to comment.