AtCoder ABC 133 A&B&C&D AtCoder - 133
A - T or T
private void solveA() {
int n = nextInt();
int a = nextInt();
int b = nextInt();
out.println(n * a > b ? b : n * a);
}
B - Good Distance
-Don't forget the condition of $ (i <j) $
private void solveB() {
int n = nextInt();
int d = nextInt();
int[][] wk = Stream.generate(() -> IntStream.range(0, d).map(i -> nextInt()).toArray()).limit(n)
.toArray(int[][]::new);
int res = 0;
for (int i = 0; i < n; i++) {
//i <Note j
for (int j = i + 1; j < n; j++) {
long tmp = 0;
for (int k = 0; k < d; k++) {
tmp += Math.pow(wk[i][k] - wk[j][k], 2);
}
long tmpRes = (long) Math.sqrt(tmp);
if (Math.pow(tmpRes, 2) == tmp) {
res++;
}
}
}
out.println(res);
}
C - Remainder Minimization 2019
――What can you do if you reduce the maximum (r)?
--Since it is mod 2019, the number repeats 0 --2018
--Repeat 0-2018 starting from l
-Rather than trying everything from $ l to r $, try the smaller of $ l to min (l + (2019 * 2), r)
private void solveC() {
final int CONST_MOD = 2019;
int l = nextInt();
int r = nextInt();
/*
*Nothing can be done without reducing the maximum for the time being.
* l+Adopted the smaller of 2019 or r as the maximum value (because it is a MOD)
* l+One cycle in 2019. However, with this, the minimum value comes out only once.
* +(2019*2)If you make two laps, 1 will appear twice.
*
*/
int wkR = Math.min(l + (CONST_MOD * 2), r);
int res = 2020;
for (int i = l; i <= wkR; i++) {
for (int j = i + 1; j <= wkR; j++) {
res = Math.min(((i % CONST_MOD) * (j % CONST_MOD)) % CONST_MOD, res);
}
}
out.println(res);
}
D - Rain Flows into Dams
--Simultaneous equations were established and solved --Calculate the shipping cost of the dam and ask for the rain of one mountain somewhere ――If you decide on one mountain, you can decide on the other --Since the number of mountains is odd, it increases as 1-> 3-> 5-> 7-> 9 ... --Since there are always two peaks, the formula repeats +-as follows: --Mountain A = Dam 1 --Dam 2 + Dam 3 --Dam 4 + Dam 5 --+-+-+ ・ ・ ・
Example: 2
dam | 3 | 8 | 7 | 5 | 5 | |||||
Mountain | a | B | C | D | E | |||||
2 | 4 | 12 | 2 | 8 |
Dam 1=Mountain A/2 +Mountain B/2 ->Dam 1*2 =Mountain A +Mountain B -> Mountain B =Dam 1*2 -Mountain A
Dam 2=Mountain B/2 +Mountain C/2 ->Dam 2*2 =Mountain B +Mountain C -> Mountain C =Dam 2*2 -Mountain B
Dam 3=Mountain C/2 +Mountain D/2 ->Dam 3*2 =Mountain C +Mountain D -> Mountain D =Dam 3*2 -Mountain C
Dam 4=Mountain D/2 +Mountain E/2 ->Dam 4*2 =Mountain D +Mountain E -> Mountain E =Dam 4*2 -Mountain D
Dam 5=Mountain E/2 +Mountain A/2 ->Dam 5*2 =Mountain E +Mountain A -> Mountain A =Dam 5*2 -Mountain E
First find the mountain A by transforming the above equation
Mountain A=Dam 1-Dam 2+Dam 3-Dam 4+Dam 5
If mountain A is decided, mountain B and beyond will be decided
Mountain B=Dam 1*2 -Mountain A
Mountain C=Dam 2*2 -Mountain B
Mountain D=Dam 3*2 -Mountain C
Mountain E=Dam 4*2 -Mountain D
private void solveD() {
int n = nextInt();
int[] wk = IntStream.range(0, n).map(i -> nextInt()).toArray();
long a = 0;
//Even-numbered dams+, The odd dam-
for (int i = 0; i < wk.length; i++) {
if (i % 2 == 0) {
a += wk[i];
} else {
a -= wk[i];
}
}
long[] mountain = new long[n];
mountain[0] = a;
//The last mountain is mountain A, so it is excluded
for (int i = 0; i < mountain.length - 1; i++) {
mountain[i + 1] = wk[i] * 2 - mountain[i];
}
StringBuilder builder = new StringBuilder();
for (long l : mountain) {
builder.append(l + " ");
}
out.println(builder.toString().trim());
}
Recommended Posts