結果
| 問題 |
No.5005 3-SAT
|
| ユーザー |
EvbCFfp1XB
|
| 提出日時 | 2022-04-29 17:36:13 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 1,865 ms / 2,000 ms |
| コード長 | 10,268 bytes |
| コンパイル時間 | 4,415 ms |
| 実行使用メモリ | 47,320 KB |
| スコア | 100,066 |
| 最終ジャッジ日時 | 2022-04-29 17:39:31 |
| 合計ジャッジ時間 | 195,573 ms |
|
ジャッジサーバーID (参考情報) |
judge15 / judge14 |
| 純コード判定しない問題か言語 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 100 |
ソースコード
import java.util.Arrays;
import java.util.Scanner;
import java.util.stream.IntStream;
public class Main {
private int N = 1 << 11;
private int M = 1 << 8;
private int[] a = new int[N];
private int[] b = new int[N];
private int[] c = new int[N];
private int[] p = new int[N];
private int[] q = new int[N];
private int[] r = new int[N];
private int score;
private int bestScore;
private int[] solution = new int[M];
private int[] bestSolution = new int[M];
int[][] penalties = new int[M][2];
public static Watch watch = new Watch();
public static XorShift rng = new XorShift(System.nanoTime());
public static SAState sa = new SAState();
public static void main(String[] args) {
new Main().run();
}
private void run() {
read();
solve();
write();
}
private void read() {
try (Scanner in = new Scanner(System.in)) {
for (int i = 0; i < N; i++) {
a[i] = in.nextInt();
b[i] = in.nextInt();
c[i] = in.nextInt();
p[i] = in.nextInt();
q[i] = in.nextInt();
r[i] = in.nextInt();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void solve() {
score = calculateScore(solution);
multiSA();
}
private int calculateScore(int[] out2) {
for (int i = 0; i < N; i++) {
if (out2[a[i]] == p[i] || out2[b[i]] == q[i] || out2[c[i]] == r[i]) {
} else {
return i;
}
}
return N;
}
private void multiSA() {
int numRestart = 10;
double startTime = watch.getSecond();
double endTime = 1.8;
double remainTime = endTime - startTime;
double startStartTemperature = 10;
double endStartTemperature = 10;
for (double restart = 0; restart < numRestart; restart++) {
sa.startTime = startTime + remainTime * restart / numRestart;
sa.endTime = startTime + remainTime * (restart + 1) / numRestart;
sa.startTemperature = endStartTemperature + (startStartTemperature - endStartTemperature) * ((numRestart - restart) / numRestart);
sa.endTemperature = 0;
for (int i = 0; i < M; i++) {
Arrays.fill(penalties[i], 0);
}
GLS();
}
loadBest();
Utils.debug("MultiSA", score, watch.getSecond());
}
private void GLS() {
double second = 0.1 + watch.getSecond();
sa.init();
for (;; ++sa.numIterations) {
if ((sa.numIterations & ((1 << 8) - 1)) == 0) {
sa.update();
if (sa.time > second) {
second += 0.1;
Utils.debug(sa.numIterations, String.format("%.2f%%", 100.0 * sa.validIterations / sa.numIterations), String.format("%.2f%%", 100.0 * sa.acceptIterations / sa.validIterations), String.format("%d", score), String.format("%d", bestScore), String.format("%.6f", 1.0 / sa.inverseTemperature), String.format("%.6f", 1.0 / sa.lastAcceptTemperature));
}
if (sa.isTLE()) {
break;
}
}
mutate();
}
}
private void mutate() {
change();
}
private void change() {
int minIndex = -1;
int min = (int) 1e9;
for (int i = 0; i < M; i++) {
if (penalties[i][solution[i]] < min) {
min = penalties[i][solution[i]];
minIndex = i;
}
}
penalties[minIndex][solution[minIndex]]++;
int index = minIndex;
int beforePenalty = penalties[index][solution[index]];
solution[index] = 1 - solution[index];
double deltaScore = calculateScore(solution) - score;
int afterPenalty = penalties[index][solution[index]];
double deltaPenalty = 1e0 * (afterPenalty - beforePenalty);
sa.validIterations++;
if (deltaScore > deltaPenalty) {
sa.acceptIterations++;
score += deltaScore;
saveBest();
} else {
solution[index] = 1 - solution[index];
}
}
private void write() {
StringBuilder sb = new StringBuilder();
IntStream.range(0, M).forEach(i -> sb.append(solution[i]));
System.out.println(sb.reverse().toString());
System.out.flush();
}
private void saveBest() {
if (score > bestScore) {
bestScore = score;
System.arraycopy(solution, 0, bestSolution, 0, M);
}
}
private void loadBest() {
score = bestScore;
System.arraycopy(bestSolution, 0, solution, 0, M);
}
}
final class Utils {
private Utils() {
}
public static final void debug(Object... o) {
System.err.println(toString(o));
}
public static final String toString(Object... o) {
return Arrays.deepToString(o);
}
public static boolean isValid(int v, int min, int minUpper) {
return v >= min && v < minUpper;
}
}
class Watch {
private long start;
public Watch() {
init();
}
public double getSecond() {
return (System.nanoTime() - start) * 1e-9;
}
public void init() {
init(System.nanoTime());
}
private void init(long start) {
this.start = start;
}
public String getSecondString() {
return toString(getSecond());
}
public static final String toString(double second) {
if (second < 60) {
return String.format("%5.2fs", second);
} else if (second < 60 * 60) {
int minute = (int) (second / 60);
return String.format("%2dm%2ds", minute, (int) (second % 60));
} else {
int hour = (int) (second / (60 * 60));
int minute = (int) (second / 60);
return String.format("%2dh%2dm%2ds", hour, minute % (60), (int) (second % 60));
}
}
}
class XorShift {
private int w = 88675123;
private int x = 123456789;
private int y = 362436069;
private int z = 521288629;
public XorShift(long l) {
x = (int) l;
}
public int nextInt() {
final int t = x ^ (x << 11);
x = y;
y = z;
z = w;
w = w ^ (w >>> 19) ^ (t ^ (t >>> 8));
return w;
}
public long nextLong() {
return ((long) nextInt() << 32) ^ (long) nextInt();
}
public double nextDouble() {
return (nextInt() >>> 1) * 4.6566128730773926E-10;
}
public int nextInt(int n) {
return (int) (n * nextDouble());
}
}
class SAState {
public static final boolean useTime = true;
public double startTime;
public double endTime;
public double time;
public double startTemperature;
public double endTemperature;
public double inverseTemperature;
public double lastAcceptTemperature;
public double startRange;
public double endRange;
public double range;
public int numIterations;
public int validIterations;
public int acceptIterations;
private double[] log = new double[32768];
public SAState() {
for (int i = 0; i < log.length; i++) {
log[i] = Math.log((i + 0.5) / log.length);
}
}
public void init() {
numIterations = 0;
validIterations = 0;
acceptIterations = 0;
startTime = useTime ? Main.watch.getSecond() : numIterations;
update();
lastAcceptTemperature = inverseTemperature;
}
public void update() {
updateTime();
updateTemperature();
}
public boolean useExp = !true;
public void updateTemperature() {
if (useExp) {
double time0to1 = elapsedPercentage(startTime, endTime, time);
double startY = startTemperature;
double endY = endTemperature;
double startX = Math.log(startY);
double endX = Math.log(endY);
double xStartToEnd = interpolate(startX, endX, time0to1);
double temperature = Math.exp(xStartToEnd);
inverseTemperature = 1.0 / temperature;
} else {
double time0to1 = elapsedPercentage(startTime, endTime, time);
double startY = startTemperature;
double endY = endTemperature;
double temperature = interpolate(startY, endY, time0to1);
inverseTemperature = 1.0 / temperature;
}
}
private double elapsedPercentage(double min, double max, double v) {
return (v - min) / (max - min);
}
private double interpolate(double v0, double v1, double d0to1) {
return v0 + (v1 - v0) * d0to1;
}
public void updateRange() {
range = endRange + (startRange - endRange) * Math.pow((endTime - time) / (endTime - startTime), 1.0);
}
public void updateTime() {
time = useTime ? Main.watch.getSecond() : numIterations;
}
public boolean isTLE() {
return time >= endTime;
}
public boolean accept(double deltaScore) {
return acceptB(deltaScore);
}
public boolean acceptB(double deltaScore) {
validIterations++;
if (deltaScore > -1e-9) {
acceptIterations++;
return true;
}
double d = deltaScore * inverseTemperature;
if (d < -10) {
return false;
}
if (log[Main.rng.nextInt() & 32767] < d) {
acceptIterations++;
lastAcceptTemperature = inverseTemperature;
return true;
}
return false;
}
public boolean acceptS(double deltaScore) {
validIterations++;
if (deltaScore < 1e-9) {
acceptIterations++;
return true;
}
double d = -deltaScore * inverseTemperature;
if (d < -10) {
return false;
}
if (log[Main.rng.nextInt() & 32767] < d) {
acceptIterations++;
lastAcceptTemperature = inverseTemperature;
return true;
}
return false;
}
}
EvbCFfp1XB