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

12-InSange #46

Merged
merged 4 commits into from
May 26, 2024
Merged

12-InSange #46

merged 4 commits into from
May 26, 2024

Conversation

InSange
Copy link
Collaborator

@InSange InSange commented May 9, 2024

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

์ „๊นƒ์ค„

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

ํ•˜๋ฃจ

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

์ฒซ ๋ฒˆ์งธ ํ’€์ด

  1. i๋ฒˆ์งธ ์ „๊นƒ์ค„์ด i+1๋ฒˆ๋ถ€ํ„ฐ N๋ฒˆ์งธ ์ค„๊นŒ์ง€ ๋น„๊ต๋ฅผ ํ•˜์˜€์„ ๋•Œ ๊ฒน์น ๊ฒฝ์šฐ(CrossCheckํ•จ์ˆ˜๊ฐ€ true) cross2์ฐจ์› ๋ฐฐ์—ด์—๋‹ค๊ฐ€ i๋ฒˆ์งธ ์š”์†Œ์— ๊ฒน์น˜๋Š” ์ธ๋ฑ์Šค ๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค.
  2. i๋ฒˆ์งธ์™€ ๊ฒน์น˜๋Š” ์ „๊นƒ์ค„์˜ ๊ฐœ์ˆ˜์™€ ํ•ด๋‹น ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ i๋ฅผ ๋‚ด๋ฆผ์ฐจ์ˆœ ์šฐ์„ ์ˆœ์œ„ ํ์— ๋„ฃ์–ด์ค€๋‹ค. pair<๊ฒน์น˜๋Š” ์ „๊นƒ์ค„์˜ ๊ฐœ์ˆ˜, ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ
  3. ์šฐ์„ ์ˆœ์œ„ํ์—์„œ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ด์„œ ํ•ด๋‹น ์ธ๋ฑ์Šค์˜ ์ „๊นƒ์ค„์„ ์‚ญ์ œํ•ด์ค€๋‹ค. ์‚ญ์ œ ํ™•์ธ ๋ฐฐ์—ด๋กœ live ๋ถˆํ˜• ๋ฐฐ์—ด์„ ์„ ์–ธํ•ด์ฃผ์—ˆ๋‹ค.
  4. ์‚ญ์ œํ•˜๊ธฐ ์ „์— ๋งŒ์•ฝ ๊ฒน์น˜๋Š” ์ค„์ด ์ด๋ฏธ ์‚ญ์ œ๋œ ์ƒํƒœ๋ผ๋ฉด ๋ฌด์‹œํ•˜๊ณ  ๋‹ค์Œ ๊ฒน์นœ ์ค„์„ ํ™•์ธํ•ด์ฃผ๋Š” ์‹์œผ๋กœ ์ง„ํ–‰ํ•œ๋‹ค.
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

int N, a, b, answer;
vector<pair<int, int>> line;
vector <vector<int>> cross; // ํ•ด๋‹น index์—์„œ ๊ต์ฐจํ•˜๋Š” ์„ ์˜ ๊ฐœ์ˆ˜
vector<bool> live;
priority_queue<pair<int, int>> pq;

bool CrossCheck(pair<int, int>& l1, pair<int, int>& l2)
{
	if (l1.first < l2.first && l1.second > l2.second) return true;
	if (l1.first > l2.first && l1.second < l2.second) return true;
	return false;
}

void Solve()
{
	live.assign(501, true);
	cross.assign(501, vector<int>());
	answer = 0;

	cin >> N;

	for (int i = 0; i < N; i++)
	{
		cin >> a >> b;
		line.push_back({ a, b });
	}

	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < N; j++)
		{
			if (CrossCheck(line[i], line[j]))
			{
				cross[line[i].first].push_back(line[j].first);
			}
		}
		pq.push({ cross[line[i].first].size(), line[i].first});
	}

	while (!pq.empty())
	{
		pair<int, int> cur = pq.top();
		pq.pop();

		//cout << cur.first << ", " << cur.second << "\n";

		if (cur.first == 0) break;
		bool flag = false;

		for (const int& val : cross[cur.second])
		{
			if (live[val] == false) continue;
			flag = true;
			break;
		}

		if (flag)
		{
			answer++;
			live[cur.second] = false;
		}
	}

	cout << answer;
}

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

	Solve();

	return 0;
}

๊ฒฐ๊ณผ๋Š”?
image

๋†€๋ž๊ฒŒ๋„ ์งˆ๋ฌธ ๊ฒŒ์‹œํŒ์„ ํฌํ•จํ•œ ๋ชจ๋“  ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ํ†ต๊ณผ๊ฐ€ ๋˜์—ˆ์—ˆ๋‹ค.
๊ทธ๋Ÿฌ๋‚˜.. ์ œ์ถœ๋งŒํ•˜๋ฉด ํ‹€๋ฆฌ๋Š” ๊ฒƒ์„ ๋ณด์•„ ํ•„์‹œ ์˜ˆ์™ธ ์กฐ๊ฑด์ด ์žˆ์„ ํ„ฐ์ธ๋ฐ ๊ทธ๊ฒƒ์„ ์ฐพ์ง€ ๋ชปํ–ˆ๋‹ค..
๊ฒฐ๊ตญ ๋ธ”๋กœ๊ทธ ํ˜ธ์ถœ

๋‘ ๋ฒˆ์งธ ํ’€์ด

๊ฒน์น˜๋Š” ์ „๊นƒ์ค„ = ์ „์ฒด ์ „๊นƒ์ค„ - ์•ˆ ๊ฒน์น˜๋Š” ์ „๊นƒ์ค„ ์ด๋‹ค!!
์œ„์˜ ํ’€์ด์‹์€ ์ƒ์ƒ๋„ ๋ชปํ–ˆ๋‹ค.

  1. ์ „๊นƒ์ค„์€ line' ๋ฐฐ์—ด์— ์ €์žฅ๋˜๋ฉฐ first๋Š” ์‹œ์ž‘์ง€์ , second`๋Š” ๋„์ฐฉ์ง€์ ์ด๋‹ค.
  2. first ์‹œ์ž‘์ง€์ ์„ ๊ธฐ์ค€์œผ๋กœ sortํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์ •๋ ฌํ•ด์ค€๋‹ค.
  3. ๋ชจ๋“  ์ „๊นƒ์ค„์€ ์ž๊ธฐ ์ž์‹ ์„ ํฌํ•จํ•ด dp์— 1๋กœ ์‹œ์ž‘ํ•œ๋‹ค.
  4. ์ดํ›„ i๋ณด๋‹ค ์ž‘์€ ์ „๊นƒ์ค„๋“ค๊ณผ ๋น„๊ตํ•ด์„œ second์˜ ๊ฐ’์ด ํ˜„์žฌ์˜ ์ „๊นƒ์ค„๋ณด๋‹ค ์ž‘์„ ๊ฒฝ์šฐ ๊ฒน์น˜๊ฒŒ ๋œ๋‹ค. i์˜ first๋Š” ๋‚ด๋ฆผ ์ฐจ์ˆœ์ด๊ธฐ์— j์˜ first๋ณด๋‹ค ํฌ๋‹ค! ๊ทธ๋Ÿฌ๋‹ˆ second์˜ ๊ฐ’์ด j๋ณด๋‹ค ์ž‘์œผ๋ฉด ์ „๊นƒ์ค„์€ ๊ต์ฐจํ•˜๊ฒŒ ๋œ๋‹ค.
  5. ๊ทธ๋ ‡๊ฒŒ ์•ˆ๊ฒน์น˜๋Š” ์ „๊นƒ์ค„์„ ๋ฐœ๊ฒฌํ•˜๊ฒŒ ๋˜๋ฉด dp[j] ๋ฒˆ์งธ ์ค„์€ i์™€ ๊ฒน์น˜์ง€ ์•Š๊ธฐ๋•Œ๋ฌธ์— 1์„ ๋”ํ•ด์ค€ ๊ฐ’์—์„œ dp[i]์™€ ๋น„๊ตํ•ด์„œ dp[i]์˜ ๊ฐ’์„ ์—…๋ฐ์ดํŠธ ์‹œ์ผœ์ค€๋‹ค. j๋Š” i๋ณด๋‹ค ์ž‘๊ธฐ ๋•Œ๋ฌธ์— j๊ฐ€ ์•ˆ๊ฒน์น˜๋Š” ๊ฐ’๋“ค์€ i์—์„œ๋„ ์•ˆ๊ฒน์ณ์ง€๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
  6. ๊ทธ๋ ‡๊ฒŒ ์ตœ์‹ ํ™” ๋œ dp[i]์˜ ๊ฐ’๊ณผ answer์ค‘ ํฐ ๊ฐ’์„ ๊ฐ€์ ธ์˜จ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ตœ์†Œ ์ „๊นƒ์ค„์„ ์ฐพ์•„์ค˜์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ „์ฒด ์ „๊นƒ์ค„์—์„œ ๋นผ๋ ค๋ฉด ๊ฐ€์žฅ ํฐ max๊ฐ’์„ ์ฐพ์•„์ค˜์•ผ ํ•œ๋‹ค.
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int N, a, b, answer;
vector<pair<int, int>> line;
vector<int> dp;

void Solve()
{
	cin >> N;

	answer = 0;
	dp.assign(N + 1, 1);

	for (int i = 0; i < N; i++)
	{
		cin >> a >> b;
		line.push_back({ a, b });
	}

	sort(line.begin(), line.end());

	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < i; j++)
		{
			//cout << line[i].first << ", " << line[j].first << "์ผ ๋•Œ\n";
			if (line[j].second < line[i].second) {
				dp[i] = max(dp[i], dp[j] + 1);
				//cout << line[i].first << "-> i ์˜ dp ๊ฐ’ : " << dp[i] << "\n";
				//cout << line[j].first << "-> j ์˜ dp ๊ฐ’ : " << dp[j] << "\n";
			}
		}

		answer = max(answer, dp[i]);
	}

	cout << N - answer;
}

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

	Solve();

	return 0;
}

ํ›„๊ธฐ

๋ฌธ์ œ๋ฅผ ํ’€๋ฉด์„œ ์ด๊ฒŒ ๊ณจ๋“œ 5๋ผ๊ณ  ๋ฌด์ˆ˜ํžˆ ๋งŽ์€ ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค..
์‹ค์ œ๋กœ ํ’€์ด ๋ฐฉ์‹์„ ๋– ๋‚˜ ๋ชจ๋“  ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ํ†ต๊ณผํ–ˆ์Œ์—๋„ ํ‹€๋ ธ๋‹ค๊ณ  ๋œจ๊ธฐ์— ์˜ˆ์™ธ๋ฅผ ์ฐพ๋Š”๋ฐ ์‹œ๊ฐ„์ด ๋งค์šฐ ์˜ค๋ž˜ ๊ฑธ๋ ธ๋˜ ๋ฌธ์ œ์ด๋‹ค.
์ด๋Ÿฌํ•œ ์ ‘๊ทผ ๋ฐฉ์‹์€ ๋งŽ์€ ๋ฌธ์ œ๋ฅผ ๊ฒช์–ด ๋ด์•ผ ๋Š˜ ๊ฒƒ ๊ฐ™์•˜๋‹ค.

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

์ฐธ๊ณ  ๋ธ”๋กœ๊ทธ

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.

first ๊ฐ’ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•œ ๋’ค second ๊ฐ’์œผ๋กœ ๊ฒน์น˜๋Š”์ง€ ์•ˆ๊ฒน์น˜๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๊ณ„์‚ฐํ•˜๊ณ  ๊ทธ ๊ฐ’์„ 1์ฐจ์› DP ๋กœ ์—ฐ์‚ฐํ•˜๋Š”๊ตฐ์š”.. ์ด๋ ‡๊ฒŒ๋„ ๊ฐ€๋Šฅํ•˜๋„ค์š”..
์ „ ๋‹จ์ˆœํ•˜๊ฒŒ 2์ฐจ์› DP๋กœ ๊ณ„์‚ฐ ํ–ˆ์Šต๋‹ˆ๋‹ค! dp[x][y]๋Š” x, y ๊ฐ’๋ณด๋‹ค ์ž‘์€ ์ „๊นƒ์ค„๋“ค์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋˜๊ณ  ๊ทธ๊ฑธ ํ•˜๋‚˜์”ฉ ๊ฐฑ์‹ ํ•ด๋‚˜๊ฐ€๋Š” ์‹์œผ๋กœ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค!
ํŠนํžˆ ๊ฒน์น˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์—ฐ์‚ฐํ•˜๊ธฐ๊ฐ€ ์–ด๋ ค์›Œ pair<pair<bool, bool>, int> ๋กœ dp ๋ฐฐ์—ด์„ ์„ ์–ธํ•ด ํ’€์—ˆ์Šต๋‹ˆ๋‹ค!

#include <iostream>

#define MP(a, b, x) make_pair(make_pair(a, b), x)
#define VALUE(x) x.second
#define GET_X(x) x.first.first
#define GET_Y(x) x.first.second

using namespace std;

typedef pair<pair<bool, bool>, int> bbi;

int arr[510][510] = {0, };
bbi dp[510][510];

int main() {
    int n, mx = 0, my = 0; cin >> n;
    for (int i = 0; i < n; i++) {
        int x, y; cin >> x >> y;

        mx = x > mx ? x : mx;
        my = y > my ? y : my;

        arr[x][y] = 1;
    }
    
    for (int i = 1; i <= mx; i++) {
        for (int j = 1; j <= my; j++) {
            if (i == 1) 
                dp[1][j] = MP(false, arr[1][j], arr[1][j]);

            else if (j == 1) 
                dp[i][1] = MP(arr[i][1], false, arr[i][1]);
            
            else
                dp[i][j] = MP(false, false, 0);
        }
    }

    int sum;

    for (sum = 2; sum <= (my + mx + 1); sum++) {
        int j = max(sum - mx, 1);

        while (sum - j >= 1) {
            int x = sum - j;
            int y = j++; 

            if (y > my) break;

            bbi tx = dp[x][y - 1];
            bbi ty = dp[x - 1][y];

            if (!arr[x][y] || (GET_X(tx) || GET_Y(ty))) {
                dp[x][y] = MP(GET_X(tx), GET_Y(ty), max(VALUE(tx), VALUE(ty)));
                continue;
            }

            dp[x][y] = MP(1, 1, max(VALUE(tx), VALUE(ty)) + 1);
        }
    }

    printf("%d\n", n - VALUE(dp[mx][my]));
}

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.

์ €๋„ ์ฒ˜์Œ์— A์— ๋Œ€ํ•ด ์˜ค๋ฆ„์ฐจ์ˆœํ•ด์„œ ๊ต์ฐจํ•˜๋Š” ์ „๊นƒ์ค„์„ ์ฐพ์•˜๋Š”๋ฐ ๊ต์ฐจํ•˜๋Š” ๊ฑธ ์ฐพ๋Š”๋‹ค๋Š”๊ฒŒ ์‰ฝ์ง€ ์•Š์•˜์–ด์š”. ๊ณ ๋ฏผํ•ด๋ณด๋‹ค๊ฐ€ ๋‹ค๋ฅธ ํ’€์ด๋ฅผ ์ฐธ๊ณ ํ–ˆ์Šต๋‹ˆ๋‹ค. A์— ๋Œ€ํ•ด ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•ด์„œ B์—์„œ ๊ต์ฐจํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๋ฅผ dp๋กœ ๊ณ„์† ๊ฐฑ์‹ ํ•˜๋ฉด ๋˜๋”๊ตฐ์š”. ๊ฒฐ๊ตญ ์ฐพ์•„์•ผ ๋  ๊ฒƒ์€ ๊ต์ฐจํ•˜์ง€ ์•Š๊ฒŒ ์—†์•จ ์ „๊นƒ์ค„์˜ ์ตœ์†Œ์ด๋ฏ€๋กœ dp์˜ ์ตœ๋Œ€๋ฅผ ์ „์ฒด์—์„œ ๋นผ๋ฉด ์ตœ์†Œ ์ „๊นƒ์ค„์ด ๋‚˜์˜จ๋‹ค๋Š”๊ฒŒ ์‹ ๊ธฐํ–ˆ์Šต๋‹ˆ๋‹ค.

n = int(input())
k = []
dp = [1]*n

for i in range(n):
  a,b = map(int,input().split())
  k.append([a,b])

k.sort()

for i in range(1,n):
  for j in range(0,i):
    if k[i][1] >= k[j][1]: 
      dp[i] = max(dp[i],dp[j]+1)
print(n-max(dp))

@InSange
Copy link
Collaborator Author

InSange commented May 21, 2024

first ๊ฐ’ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•œ ๋’ค second ๊ฐ’์œผ๋กœ ๊ฒน์น˜๋Š”์ง€ ์•ˆ๊ฒน์น˜๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๊ณ„์‚ฐํ•˜๊ณ  ๊ทธ ๊ฐ’์„ 1์ฐจ์› DP ๋กœ ์—ฐ์‚ฐํ•˜๋Š”๊ตฐ์š”.. ์ด๋ ‡๊ฒŒ๋„ ๊ฐ€๋Šฅํ•˜๋„ค์š”.. ์ „ ๋‹จ์ˆœํ•˜๊ฒŒ 2์ฐจ์› DP๋กœ ๊ณ„์‚ฐ ํ–ˆ์Šต๋‹ˆ๋‹ค! dp[x][y]๋Š” x, y ๊ฐ’๋ณด๋‹ค ์ž‘์€ ์ „๊นƒ์ค„๋“ค์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋˜๊ณ  ๊ทธ๊ฑธ ํ•˜๋‚˜์”ฉ ๊ฐฑ์‹ ํ•ด๋‚˜๊ฐ€๋Š” ์‹์œผ๋กœ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค! ํŠนํžˆ ๊ฒน์น˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์—ฐ์‚ฐํ•˜๊ธฐ๊ฐ€ ์–ด๋ ค์›Œ pair<pair<bool, bool>, int> ๋กœ dp ๋ฐฐ์—ด์„ ์„ ์–ธํ•ด ํ’€์—ˆ์Šต๋‹ˆ๋‹ค!

#include <iostream>

#define MP(a, b, x) make_pair(make_pair(a, b), x)
#define VALUE(x) x.second
#define GET_X(x) x.first.first
#define GET_Y(x) x.first.second

using namespace std;

typedef pair<pair<bool, bool>, int> bbi;

int arr[510][510] = {0, };
bbi dp[510][510];

int main() {
    int n, mx = 0, my = 0; cin >> n;
    for (int i = 0; i < n; i++) {
        int x, y; cin >> x >> y;

        mx = x > mx ? x : mx;
        my = y > my ? y : my;

        arr[x][y] = 1;
    }
    
    for (int i = 1; i <= mx; i++) {
        for (int j = 1; j <= my; j++) {
            if (i == 1) 
                dp[1][j] = MP(false, arr[1][j], arr[1][j]);

            else if (j == 1) 
                dp[i][1] = MP(arr[i][1], false, arr[i][1]);
            
            else
                dp[i][j] = MP(false, false, 0);
        }
    }

    int sum;

    for (sum = 2; sum <= (my + mx + 1); sum++) {
        int j = max(sum - mx, 1);

        while (sum - j >= 1) {
            int x = sum - j;
            int y = j++; 

            if (y > my) break;

            bbi tx = dp[x][y - 1];
            bbi ty = dp[x - 1][y];

            if (!arr[x][y] || (GET_X(tx) || GET_Y(ty))) {
                dp[x][y] = MP(GET_X(tx), GET_Y(ty), max(VALUE(tx), VALUE(ty)));
                continue;
            }

            dp[x][y] = MP(1, 1, max(VALUE(tx), VALUE(ty)) + 1);
        }
    }

    printf("%d\n", n - VALUE(dp[mx][my]));
}

๊ฒฐ๊ตญ DP๋กœ ํ•ด๋‚ด์…จ๋„ค์š” ใ…‹ใ…‹ใ…‹

Copy link
Contributor

@dhlee777 dhlee777 left a comment

Choose a reason for hiding this comment

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

์–ผ๋งˆ์ „์— ๊ฐ€์žฅ๊ธด ์ฆ๊ฐ€ํ•˜๋Š” ๋ถ€๋ถ„์ˆœ์—ด ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋Š”๋ฐ ์ด ๋ฌธ์ œ์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๋ณด์—ฌ์„œ ๋†€๋ž์Šต๋‹ˆ๋‹ค...์‹œ์ž‘์ ์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•œํ›„ ๋์ ๋งŒ์„ ์ด์ œ ๊ณ ๋ คํ•˜์—ฌ ๊ฐ€์žฅ ๊ธด ์ฆ๊ฐ€์ˆ˜์—ด์„ ์ฐพ์•„์„œ ์ œ๊ฑฐํ•ด์•ผํ•˜๋Š”์ „๊นƒ์ค„์ˆ˜=์ „์ฒด์ „๊นƒ์ค„์ˆ˜-๊ฐ€์žฅ๊ธด์ฆ๊ฐ€์ˆ˜์—ด ์ด ๋˜๋Š” ๊ฒƒ์ด๊ตฐ์š”..! ํ•ญ์ƒ ๊น”๋”ํ•œ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉฐ ๋งŽ์ด ๋ฐฐ์šฐ๊ณ ์žˆ์Šต๋‹ˆ๋‹ค !

@InSange InSange merged commit 0d5e4b2 into main May 26, 2024
@InSange InSange deleted the 12-InSange branch May 26, 2024 17:23
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.

4 participants