Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

15-InSange #57

Merged
merged 5 commits into from
Jul 5, 2024
Merged

15-InSange #57

merged 5 commits into from
Jul 5, 2024

Conversation

InSange
Copy link
Collaborator

@InSange InSange commented May 26, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

ํ†ต๋‚˜๋ฌด ๊ฑด๋„ˆ๋›ฐ๊ธฐ

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

30๋ถ„

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

ํ’€์ด ๋ฐฉ๋ฒ•

์ฃผ์–ด์ง„ ํ†ต๋‚˜๋ฌด N๊ฐœ๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ ์ธ์ ‘ํ•œ ํ†ต๋‚˜๋ฌด์˜ ๋†’์ด ์ฐจ๊ฐ€ ์ตœ๋Œ€ ๊ฐ’์„ ์ตœ์†Œ๋กœ ๋งŒ๋“ค์–ด์ฃผ๊ธฐ
๊ฐ’์„ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ -> ์šฐ์„ ์ˆœ์œ„ ํ ํ™œ์šฉ
๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ฃผ์–ด์ง„ ๊ฐ’์€ ์„œ๋กœ ๋‹ค์Œ ๊ฐ’ ๋ผ๋ฆฌ ์ตœ์†Œ์˜ ์ฐจ์ด๋ฅผ ๊ฐ€์ง€์ง€๋งŒ ๊ฒฐ๊ตญ ๋งˆ์ง€๋ง‰์— ์œ„์น˜ํ•œ ๊ฐ’๊ณผ ์‹œ์ž‘ ๊ฐ’์˜ ์ฐจ์ด๋Š” ์ค‘๊ฐ„์˜ ์ฐจ์ด๋ณด๋‹ค ์ œ์ผ ํฐ ์ฐจ์ด๊ฐ€ ๋œ๋‹ค.
๊ทธ๋Ÿผ ์›ํ˜•์œผ๋กœ ์ด์–ด์ง„ ํ†ต๋‚˜๋ฌด๋“ค์„ ์ค„์ด๊ธฐ ์œ„ํ•ด์„ ? ์ตœ์†Œ ๊ฐ’ T๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ ์–‘ ์˜†์˜ ๊ฐ’์€ ์„œ๋กœ ํฐ ์ฐจ์ด๊ฐ€ ์—†์–ด์•ผ ๋œ๋‹ค.
image
2๊ฐ€ ์ค‘์‹ฌ์ผ ๋•Œ
image
์–‘ ์˜†์—๋Š” 4์™€ 5๊ฐ€ ์˜ค๋ฉด ๋œ๋‹ค.
image
์ˆœํ™˜์„ ๋น„๊ตํ–ˆ์„ ๋•Œ ์ฒซ ๋ฒˆ์งธ ๋ฐฐ์—ด์€ 5์™€ 9์˜ ์ฐจ์ด๊ฐ€ ๊ฐ€์žฅ ํฐ ์ตœ์†Œ๊ฐ€ ๋˜๊ณ 
๋‘ ๋ฒˆ์งธ ๋ฐฐ์—ด์€ 2์™€ 9์˜ ์ฐจ์ด๊ฐ€ ๊ฐ€์žฅ ํฐ ์ตœ์†Œ๋กœ ๋” ์ฐจ์ด๊ฐ€ ์ž‘์€ ์ฒซ ๋ฒˆ์งธ ๋ฐฐ์—ด์ด ์ •๋‹ต์ด ๋œ๋‹ค.

  1. ํ†ต๋‚˜๋ฌด์˜ ๋†’์ด๋“ค์„ ์šฐ์„  ์ˆœ์œ„ ํ์— ์ง‘์–ด๋„ฃ๋Š”๋‹ค.
  2. deque๋ฅผ ํ†ตํ•ด ์•ž๊ณผ ๋’ค๋กœ ์š”์†Œ๋“ค์„ ๋„ฃ์„ ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค.
  3. ์šฐ์„  ์ˆœ์œ„ ํ์˜ ๊ฐ’๋“ค์„ ๋ฑ์— ์•ž,๋’ค๋กœ ๋ฒˆ๊ฐˆ์•„๊ฐ€๋ฉด์„œ ์ง‘์–ด๋„ฃ๋Š”๋‹ค.
  4. ์™„์„ฑ๋œ ๋ฑ์—์„œ ์ฐจ์ด๋ฅผ ๋น„๊ตํ•ด ๋‹ต์„ ๊ตฌํ•œ๋‹ค.
void Solve()
{
	cin >> T;

	while (T--)
	{
		answer = 0;
		cin >> N;

		for (int i = 0; i < N; i++)
		{
			cin >> num;
			pq.push(num);
		}

		bool isLeft = true;

		while (!pq.empty())
		{
			if (isLeft)	
				logs.push_front(pq.top());
			else //if(!isLeft)
				logs.push_back(pq.top());

			pq.pop();
			isLeft = (isLeft ? false : true);
		}

		int lsize = logs.size();

		for (int i = 0; i < lsize; i++)
		{
			int next = (i + 1) % lsize;
			answer = max(answer, abs(logs[i] - logs[next]));
		}

		cout << answer << "\n";;
		logs.clear();
	}
	
}

ํ›„๊ธฐ

ํ•ด๋‹น ๋ฌธ์ œ๊ฐ€ ๊ทธ๋ฆฌ๋””์ผ ์ค„์€ ๋ชฐ๋ž๋”ฐ..
๋‚˜๋Š” ์ •๋ ฌ์„ ํ†ตํ•ด ํ’€์—ˆ์ง€๋งŒ ๋ง‰์ƒ ๊ทธ๋ฆฌ๋””๋กœ ํ’€์ž๋‹ˆ ๋– ์˜ค๋ฅด์ง€๋„ ์•Š์•˜๋‹ค.

์•„๋ž˜๋Š” ๊ทธ๋ฆฌ๋””๋ฅผ ์ •๋ฆฌํ•œ ํ’€์ด์ด๋‹ค.
๊ทธ๋ฆฌ๋”” ํ’€์ด๋ฐฉ๋ฒ•

๋งŒ์•ฝ ์กฐ๊ฑด์ด ๋” ์ฃผ์–ด์ง„๋‹ค๋ฉด ์ •๋ ฌ๋กœ ํ’€ ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์„๊นŒ?

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

Copy link
Collaborator

@yuyu0830 yuyu0830 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์–ด์ฐจํ”ผ ๋‹ต๋งŒ ๊ตฌํ•˜๋ฉด ๋˜๋Š” ๋ฌธ์ œ๋ผ๋ฉด ๊ตณ์ด ํ์— ๋„ฃ์ง€ ์•Š๊ณ ๋„ ํ•ด๊ฒฐ ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๊ฒƒ ์•„์‹œ๋‚˜์š”?

#include <iostream>
#include <algorithm>

using namespace std;

int arr[10001] = {0, };

int main() {
    int testcase; cin >> testcase;
    while (testcase--) {
        int n; cin >> n;
        for (int i = 0; i < n; i++) 
            cin >> arr[i];
        
        sort(arr, arr + n);

        int ans = 0;
        for (int i = 0; i < n - 2; i++) {
            ans = max(ans, arr[i + 2] - arr[i]);
        }

        ans = max(ans, arr[1] - arr[0]);
        ans = max(ans, arr[n - 1] - arr[n - 2]);

        printf("%d\n", ans);
    }
}

@InSange InSange removed the request for review from dhlee777 June 28, 2024 01:59
Copy link
Collaborator

@seongwon030 seongwon030 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ํฌ๋„์ฃผ์™€ ์–ธ๋“ฏ ๋น„์Šทํ•ด๋ณด์ด๋ฉด์„œ๋„ ์›ํ˜•์œผ๋กœ ๋ฐฐ์น˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— deque๋ฅผ ์“ด ๋ถ€๋ถ„์ด ๋‹ฌ๋ž๋„ค์š”. ๋ฌธ์ œ ์ž˜ ๋ณด๊ณ  ๊ฐ‘๋‹ˆ๋‹ท

@InSange
Copy link
Collaborator Author

InSange commented Jul 5, 2024

์–ด์ฐจํ”ผ ๋‹ต๋งŒ ๊ตฌํ•˜๋ฉด ๋˜๋Š” ๋ฌธ์ œ๋ผ๋ฉด ๊ตณ์ด ํ์— ๋„ฃ์ง€ ์•Š๊ณ ๋„ ํ•ด๊ฒฐ ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๊ฒƒ ์•„์‹œ๋‚˜์š”?

#include <iostream>
#include <algorithm>

using namespace std;

int arr[10001] = {0, };

int main() {
    int testcase; cin >> testcase;
    while (testcase--) {
        int n; cin >> n;
        for (int i = 0; i < n; i++) 
            cin >> arr[i];
        
        sort(arr, arr + n);

        int ans = 0;
        for (int i = 0; i < n - 2; i++) {
            ans = max(ans, arr[i + 2] - arr[i]);
        }

        ans = max(ans, arr[1] - arr[0]);
        ans = max(ans, arr[n - 1] - arr[n - 2]);

        printf("%d\n", ans);
    }
}

๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ answer ๊ฐ’๊ณผ ๋น„๊ตํ•ด์ฃผ๋Š” ๋ฐฉ์‹์ด๊ตฐ๋‡จ! ์ปดํŒŒ์ผ๋Ÿฌ์˜ ๊ด€์ ์œผ๋กœ ๋ดค์„ ๋•Œ๋Š” %์—ฐ์‚ฐํ•ด์„œ N๋ฒˆ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค๋Š” ๋‚˜์„์ง€๋„..?

@InSange InSange merged commit 8489e83 into main Jul 5, 2024
1 check passed
@InSange InSange deleted the 15-InSange branch July 5, 2024 05:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants