File tree Expand file tree Collapse file tree 3 files changed +65
-0
lines changed
Expand file tree Collapse file tree 3 files changed +65
-0
lines changed Original file line number Diff line number Diff line change 1+ // https://www.acmicpc.net/problem/2805
2+
3+ #include < iostream>
4+ #include < algorithm>
5+ using namespace std ;
6+
7+ int N, M, ans;
8+ int height[1000001 ];
9+ int maxheight = 0 ;
10+
11+ void binary (){
12+ int left = 0 ;
13+ int right = maxheight; // 절단기 최대 높이
14+
15+ while (left <= right) {
16+ int mid = (left + right) / 2 ; // 현재 절단기 높이
17+
18+ long long sum = 0 ;
19+ for (int i = 0 ; i < N; i++){
20+ if (height[i] - mid > 0 ) {
21+ sum += height[i] - mid;
22+ }
23+ }
24+ if (sum < M) {
25+ right = mid - 1 ;
26+ } else if (sum >= M) {
27+ left = mid + 1 ;
28+ ans = max (mid, ans);
29+ }
30+ }
31+ return ;
32+ }
33+
34+ int main () {
35+ ios_base::sync_with_stdio (false );
36+ cin.tie (NULL ); cout.tie (NULL );
37+
38+ cin >> N >> M;
39+ for (int i = 0 ; i < N; i++){
40+ cin >> height[i];
41+ maxheight = max (maxheight, height[i]);
42+ }
43+
44+ binary ();
45+ cout << ans;
46+
47+ return 0 ;
48+ }
49+
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ vector<int > twoSum (vector<int >& nums, int target) {
4+ map<int , int > m; // num, idx
5+
6+ for (int i = 0 ; i < nums.size (); i++){
7+ int need = target - nums[i];
8+ if (m.count (need)) {
9+ return {i, m[need]};
10+ }
11+ m[nums[i]] = i;
12+ }
13+
14+ return {};
15+ }
16+ };
You can’t perform that action at this time.
0 commit comments