2024-TEAM-05 / algorithm-for-kakao

카카였 기좜 문제 κ°€μ¦ˆμ•„πŸ£
0 stars 0 forks source link

[λ°±μ€€] μ„Όμ„œ #52

Open hye-on opened 3 weeks ago

hye-on commented 3 weeks ago

πŸ”— μ„Όμ„œ

hye-on commented 2 weeks ago

πŸ“‘ λŒ“κΈ€ ν…œν”Œλ¦Ώ

μ½”λ“œ 풀이

```cpp #include #include #include using namespace std; //10:25 int n, k; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; cin >> k; vectorv; v.resize(n); for (int i = 0; i < n; i++) cin >> v[i]; sort(v.begin(), v.end()); for (int i = 1; i < n; i++) { v[i - 1] = v[i] - v[i - 1]; } v[n - 1] = 0; sort(v.begin(), v.end()); int ans = 0; //2 3 0 1 2 // 0 1 2 2 3 - 3을 뺌 for (int i = 0; i < n - k+1; i++) ans += v[i]; cout << ans; return 0; } ```

μ½”λ©˜νŠΈ

- μ²˜μŒμ—” μ–΄λ €μ› λŠ”λ° λΊ„ 거리λ₯Ό μƒκ°ν•˜λ‹ˆκΉŒ μ’€ 간단해진 것 κ°™μŠ΅λ‹ˆλ‹€.
uijin-j commented 2 weeks ago

πŸ“‘ λŒ“κΈ€ ν…œν”Œλ¦Ώ

μ½”λ“œ 풀이

```java import java.io.*; import java.util.*; /** * 11:03 */ public class Main { /** * 1 3 66 7 9 * 3 6 7 8 10 12 14 15 18 20 */ public static void main(String[] args) throws Exception { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(bf.readLine()); int k = Integer.parseInt(bf.readLine()); int[] sensors = new int[n]; StringTokenizer st = new StringTokenizer(bf.readLine()); for(int i = 0; i < n; ++i) { sensors[i] = Integer.parseInt(st.nextToken()); } Arrays.sort(sensors); int[] gaps = new int[n-1]; for(int i = 1; i < n; ++i) { gaps[i-1] = sensors[i] - sensors[i-1]; } Arrays.sort(gaps); int total = sensors[n-1] - sensors[0]; for(int i = n - 2; i >= 0; --i) { if(k == 1) break; total -= gaps[i]; k--; } System.out.println(total); } } ```

μ½”λ©˜νŠΈ

- κ·Έλ¦¬λ””λ‘œ ν’€μ—ˆμŠ΅λ‹ˆλ‹€! (전체 μ„Όμ„œ λ²”μœ„μ—μ„œ K-1개의 갭을 λΊ€λ‹€)