[SWEA] 1208 Flatten

Time Lapse :3min 2sec

1208.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define gc() getchar_unlocked()
inline void fastreadINT_byReference(int &ret){
int N = gc(), flag = 1;
for(;N<'0'||N>'9';N=gc())
if(N=='-'){
flag=-1; N=gc(); break;
}
ret = 0;
do{
ret = (ret<<3) + (ret<<1) + (N & 0b1111); N=gc();
}while('0'<=N&&N<='9');
ret *= flag;
}
int main() {
int tc = 1, T = 10, dump;
vector<int> vec = vector<int>(100);
while(T--) {
fastreadINT_byReference(dump);
for(int i = 0; i < 100; ++i)
fastreadINT_byReference(vec[i]);

while(dump--) {
--(*max_element(vec.begin(), vec.end()));
++(*min_element(vec.begin(), vec.end()));
}
printf("#%d %d\n",tc++, *max_element(vec.begin(), vec.end()) - *min_element(vec.begin(), vec.end()));
}
}

1208.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.*;

public class Solution {
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
int tc = 1, T = 10, dump;
while(T-- != 0) {
dump = scanner.nextInt();
List<Integer> map = new ArrayList<>();
for(int i = 0; i < 100; ++i) {
map.add(scanner.nextInt());
}
while(dump-- != 0) {
int max = Collections.max(map);
int min = Collections.min(map);
boolean minFlag = false, maxFlag = false;
int index = 0;
for(int val : map) {
if(maxFlag && minFlag) break;
if(!maxFlag && val == max) {
map.set(index, val - 1);
maxFlag = true;
}
if(!minFlag && val == min) {
map.set(index, val + 1);
minFlag = true;
}
++index;
}
}
System.out.println("#" + tc++ + " " + (Collections.max(map) - Collections.min(map)));
}
}
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/08/04/PS/SWEA/1208/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.