AtCoder ABC 019 A&B&C AtCoder - 019
I realized that it would be difficult for me to find the problem name without the problem name, so let's fill in the problem name from now on.
** 2019/05/17: Fixed A and B code **
--Code corrected because you pointed out in the comment that sort can be processed together with stream --Can you sort at the same time? .. .. There are too many things I can do and I can't keep up with my understanding.
--Since it is the median, sort it in the middle
private void solveA() {
int[] wk = IntStream.range(0, 3).map(i -> nextInt()).sorted().toArray();
out.println(wk[1]);
}
private void solveA() {
int[] wk = IntStream.range(0, 3).map(i -> nextInt()).toArray();
Arrays.sort(wk);
out.println(wk[1]);
}
-** 2019/05/19 postscript **: There is a code of @swordone in the comment column. That is simpler.
--Append by counting how many consecutive same strings are --Cnt is 0 after exiting the loop = The last 1 character is different from the last -1 character, so count --Cnt is not 0 after exiting the loop = ++ because the last is the same character
private void solveB() {
char[] wk = next().toCharArray();
StringBuilder builder = new StringBuilder();
int cnt = 0;
for (int i = 0; i < wk.length - 1; i++) {
if (cnt == 0) {
builder.append(wk[i]);
cnt++;
}
if (wk[i] != wk[i + 1]) {
builder.append(cnt);
cnt = 0;
} else {
cnt++;
}
}
if (cnt != 0) {
builder.append(cnt);
} else {
builder.append(wk[wk.length - 1]);
builder.append(1);
}
out.println(builder.toString());
}
-The same integer $ whether you put $ x or 2x -$ x, 2x, 4x, 8x ... $ values are counted as 1. --If you put the same value in both set and list, loop based on list and remove the value from set.
private void solveC() {
Set<Long> wk = new HashSet<Long>();
List<Long> base = new ArrayList<Long>();
int n = nextInt();
for (int i = 0; i < n; i++) {
long val = nextLong();
wk.add(val);
base.add(val);
}
Collections.sort(base);
long max = base.get(base.size() - 1);
long res = 0;
for (Long longVal : base) {
if (wk.contains(longVal)) {
wk.remove(longVal);
res += recursiveC(wk, max, longVal);
}
if (wk.size() == 0) {
break;
}
}
out.println(res);
}
private int recursiveC(Set<Long> wk, long max, long current) {
if (current > max || wk.size() == 0) {
return 1;
}
current <<= 1;
wk.remove(current);
return recursiveC(wk, max, current);
}
――After AC in a loop, I got "Is this just counting an odd number?", So I tried AC -The number that doubles $ 1 becomes 1 at the end if divided by 2. $ -$ If you double the odd number, be sure to double the even number $
private void solveC2() {
Set<Integer> wk = new HashSet<Integer>();
int n = nextInt();
for (int i = 0; i < n; i++) {
int val = nextInt();
while ((val & 1) != 1) {
val /= 2;
}
wk.add(val);
}
out.println(wk.size());
}
Recommended Posts