기본 콘텐츠로 건너뛰기

1931 : 회의실배정(미제) (Greedy) [C++]

#include <iostream>
#include <vector>
#include <utility>

using namespace std;

class Conference
{
private:
 vector<pair<int,int>> Conferences;
 int N;
public:
 Conference(int n);
 int GetMostTime();
 void QuickSort(int order, int left, int right);
};

Conference::Conference(int n)
{
 N = n;
 for (int i = 0; i < N; i++)
 {
  pair<int,int> Temp = make_pair(0, 0);
  cin >> Temp.first;
  cin >> Temp.second;
  Conferences.push_back(Temp);
 }

 QuickSort(0, 0, Conferences.size() - 1);
 for (int i = 0; i < Conferences.size(); i++)
 {
  for (int j = i + 1; j < Conferences.size(); j++)
  {
   if (Conferences[i].first == Conferences[j].first)
    continue;
   else
   {
    if (i == j + 1)
     break;
    else
    {
     QuickSort(1, i, j);
     break;
    }
   }
  }
 }

 for (int i = 0; i < N; i++)
  cout << Conferences[i].first << " " << Conferences[i].second << endl;
}

int Conference::GetMostTime()
{
 int MostTime = 0;
 for (int i = 0; i < Conferences.size(); i++)
 {
  int Second = Conferences[i].second;
  int Time = 1;
  for (int j = i + 1; j < Conferences.size(); j++)
  {
   if (Conferences[i].first == Conferences[j].first)
    continue;
   else if (Second <= Conferences[j].first)
   {
    Second = Conferences[j].second;
    Time++;
   }
  }
  if (Time > MostTime)
   MostTime = Time;
 }
 
 return MostTime;
}

void Conference::QuickSort(int order, int left, int right)
{
 int i, j, key;
 pair<int, int> Temp = make_pair(0,0);
 if (left < right)
 {
  i = left + 1;
  j = right;
  if(order == 0)
   key = Conferences[left].first;
  else
   key = Conferences[left].second;
  while (i < j)
  {
   if (order == 0)
   {
    while (Conferences[i].first < key)
    {
     i++;
     if (i == Conferences.size())
      break;
    }
   }
   else
   {
    while (Conferences[i].second < key)
    {
     i++;
     if (i == Conferences.size())
      break;
    }
   }
   if (order == 0)
   {
    while (Conferences[j].first > key)
    {
     j--;
     if (j == 0)
      break;
    }
   }
   else
   {
    while (Conferences[j].second > key)
    {
     j--;
     if (j == 0)
      break;
    }
   }

   if (order == 0)
   {
    if (i < j)
    {
     Temp = Conferences[i];
     Conferences[i] = Conferences[j];
     Conferences[j] = Temp;
    }
   }
   else
   {
    if (i < j)
    {
     
     int temp = Conferences[i].second;
     Conferences[i].second = Conferences[j].second;
     Conferences[j].second = temp;
    }
   }
  }

  if (order == 0)
  {
   Temp = Conferences[left];
   Conferences[left] = Conferences[j];
   Conferences[j] = Temp;
  }
  else
  {
   int temp = Conferences[left].second;
   Conferences[left].second = Conferences[j].second;
   Conferences[j].second = temp;
  }
  QuickSort(order,left, j - 1);
  QuickSort(order,j + 1, right);
 }
}
 
int main()
{
 int N;
 cin >> N;

 Conference C(N);
 cout << C.GetMostTime();
}

댓글

이 블로그의 인기 게시물

11478 : 서로 다른 부분 문자열의 개수 (미제) [C++]

# include < iostream > # include < vector > # include < string > using namespace std ; int main ( ) { string sInput ; getline ( cin , sInput , '\n' ) ; int Time = 1 ; int Count = 0 ; vector < string > Storage ; for ( int i = 0 ; i < sInput . size ( ) ; i + + ) { for ( int j = 0 ; ( j + Time - 1 ) < sInput . size ( ) ; j + + ) Storage . push_back ( sInput . substr ( j , Time ) ) ; Time + + ; } bool * Visited = new bool [ Storage . size ( ) * sizeof ( bool ) ] ; for ( int i = 0 ; i < Storage . size ( ) ; i + + ) { Visited [ i ] = true ; for ( int j = 0 ; j < Storage . size ( ) ; j + + ) { if ( i ! = j & & Storage [ i ] = = Storage [ j ] ) { Visited [ i ] = false ; Visited [ j ] = true ; break ; } } } for ( int i = 0 ; i < Storage . size ( ) ; i + + ) if ( Visited [ i ] ) Count ...

6359 : 만취한 상범 (Dynamic Programming) [C++]

# include < iostream > # include < vector > using namespace std ; int Num_of_Divisor ( int n ) { int Count = 0 ; for ( int i = 1 ; i < = n ; i + + ) if ( n % i = = 0 ) Count + + ; return Count ; } int main ( ) { int Testcase ; cin > > Testcase ; while ( Testcase - - ) { int Rooms ; cin > > Rooms ; vector < int > Prisons ; for ( int i = 0 ; i < Rooms ; i + + ) Prisons . push_back ( 0 ) ; for ( int i = 1 ; i < Prisons . size ( ) + 1 ; i + + ) { if ( ( Num_of_Divisor ( i ) % 2 ) = = 0 ) Prisons [ i - 1 ] = 0 ; else Prisons [ i - 1 ] = 1 ; } int Fleer = 0 ; for ( int i = 0 ; i < Prisons . size ( ) ; i + + ) if ( Prisons ...

1520 : 내리막길(미제) (Dynamic Programming) [C,C++]

답은 알맞게 나오는 데 시간이 자꾸 초과된다. 3시간 째 붙잡고 있지만 플러드 필 문제로 밖에 생각이 안되는 데 플러드 필을 사용하면 시간이 오바가 나고.. 경로의 수를 각 배열 인덱스에 저장하면서 나아가는 것 같기도 한데, 사실 상 이것도 플러드 필과 다를 바 없어서 내일 시험 끝나고 다른 방법을 고안해봐야할 것 같다.