작성일 :

문제 링크

32216번 - 찬물 샤워

설명

샤워기 온도를 규칙대로 갱신하면서 매 초의 불쾌함 지수 합을 구하는 문제입니다.


접근법

현재 온도가 목표보다 큰지, 작은지, 같은지에 따라 다음 온도를 규칙대로 갱신합니다.

매 초마다 갱신된 온도와 목표의 차이를 누적하면 됩니다.


Code

C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System;

class Program {
  static void Main() {
    var data = Console.In.ReadToEnd();
    var parts = data.Split();
    var idx = 0;

    var n = int.Parse(parts[idx++]);
    var k = int.Parse(parts[idx++]);
    var t = int.Parse(parts[idx++]);

    var res = 0;
    for (var i = 0; i < n; i++) {
      var d = int.Parse(parts[idx++]);
      if (t > k) t += d - (t - k);
      else if (t < k) t += d + (k - t);
      else t += d;
      res += Math.Abs(t - k);
    }

    Console.WriteLine(res);
  }
}

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, k, t; cin >> n >> k >> t;
  int res = 0;
  for (int i = 0; i < n; i++) {
    int d; cin >> d;
    if (t > k) t += d - (t - k);
    else if (t < k) t += d + (k - t);
    else t += d;
    res += abs(t - k);
  }

  cout << res << "\n";

  return 0;
}