세일이벤트 문제 (SaleEvent)


영희는 길을 걷다가 2개를 사면 1개를 공짜로 주는 이벤트를 진행하고 있는 가게를 발견하였습니다. 다만 공짜 이벤트에는 조건이 있는데, 연속해서 진열된 3개를 한 그룹으로 묶어서 그 중 가장 싼 1개를 공짜로 주는 것입니다.

예를 들어 가격이 8, 15, 16, 12, 20, 18, 16, 15 인 물건들을 전부 구매하고자 할 때(총 가격 120) 아래와 같이 묶어서 가격을 살펴볼 수 있습니다. (아래 외에도 다양하게 3개씩 묶을 수 있습니다)


(8, 15, 16), (12, 20, 18), 16, 15 → 8, 12를 할인 받아 총 100

8, (15, 16, 12), 20, (18, 16, 15) → 12, 15를 할인 받아 총 93

8, (15, 16, 12), (20, 18, 16), 15 → 12, 16을 할인 받아 총 92 (가장 싼 가격)

(8, 15, 16), 12, 20, (18, 16, 15) → 8, 15를 할인 받아 총 97

위에서 보다시피, 진열된 물건들을 어떻게 그룹으로 묶냐에 따라서 총 가격이 달라질 수 있습니다. 또한 필요한 경우, 그룹을 묶지 않을 수도 있습니다. 가격이 1, 2, 7, 8, 10, 2 인 물건들의 경우, 1, 2, (7, 8, 10), 2 로 묶어서 7원을 할인 받고 나머지는 그냥 구매하는 것이 좋은 선택입니다. 또, 동일한 가격의 제품끼리 묶이는 경우에는 가장 싼 가격의 제품 중 하나만 무료입니다. 1, (2, 10, 2) 의 경우에는 13원을 지불하게 됩니다.


충동적으로 영희는 이 가게의 모든 물건들을 구매하기로 했습니다. 이 때 영희를 도와서 물건들을 가장 싸게 살 방법을 찾아주는 프로그램을 작성하십시오.


입력


입력은 표준 입력(stdin)으로 주어지며

첫째 줄에는 물건의 개수 N (3 <= N <= 2000)이 주어집니다.

둘째 줄에는 N개에 대한 가격이 공백으로 구분되어 주어집니다. ( 1<= 가격 <= 1000 )

모든 입력은 주어진 범위를 넘어가지 않는 정수로 주어집니다.


출력


출력은 표준 출력(stdout)으로 이루어지며,

첫째 줄에 가장 싸게 살 수 있는 가격을 출력합니다.


입력예)

8

8 15 16 12 20 18 16 15

출력예)

92



#include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>
using namespace std;

int main()
{
	int count;
	cin >> count;
	vector<int> vinput;

	for (int c = count; c; c--)
	{
		int input;
		cin >> input;
		vinput.push_back(input);
	}

	int total = std::accumulate(vinput.begin(), vinput.end(), 0);
	vector<int> minarray;

	int maxValue = 0;
	for (int i = 0; i < count - 2; i++)
		minarray.push_back(*std::min_element(vinput.begin() + i, vinput.begin() + i + 3));

	for (int i = 0; i < count - 2; i++)
	{
		maxValue = std::max(maxValue, minarray[i]);
		for (int j = i + 3; j < count - 2; j++)
			maxValue = std::max(maxValue, minarray[i] + minarray[j]);
	}

	cout << total - maxValue;
	return 0;
}


Posted by 빵원군
,

Professor Lew teaches Algorithm course in Sonyusi University (소녀시대학교).
It is his first year as a professor, so he spends a lot of time making lecture notes.
He'll teach recursion and sorting algorithms in the next class,
and he wants to make some examples which will be included in his lecture note.

While making some examples for his lecture note, he noticed that one of his work was quite difficult.
The example was for sorting a string, and the exact process was as follows:
First, the original string of length 2n is divided into n substrings, each of length 2.
Then sort the n strings in lexicographic order, and then concatenate them to obtain the result string.
Note that the sorting process will be performed on n strings, but not each of the n strings.
The following example illustrates the process: 


abbaaccb → ab ba ac cb → ab < ac < ba < cb → abacbacb

Since the process was quite confusing,

professor Lew decides to use a computer program for verification.
Given an alphabetic string of even length, write a program that prints the result of the sorting process.

입력

The first line of the input contains one integer T, the number of test cases.

The first line of each test case contains a string. The length of the string will not exceed 1000, and will only contain lowercase alphabet letters.

출력

For each test case, print the result in one line.

예제 입력

4
abbaaccb
dddcccbbbaaa
geegeegeegeebabybabybaby
oh

예제 출력

abacbacb
aababbccdcdd
babababybybyeeeeegeggege
oh


#include <iostream>
#include <sstream>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
	int cnt;
	cin >> cnt;

	stringstream output;	
	while (cnt--)
	{
		vector<string> s;
		string str;
		cin >> str;

		for (int i = 0; i < str.length(); i += 2)
		{
			string str2 = str.substr(i, 2);
			s.push_back(str2);
		}
		sort(s.begin(), s.end());

		for (auto ss : s) {
			output << ss;
		}		
		output << endl;
	}
	
	cout << output.str();
	return 0;
}
Posted by 빵원군
,

알고스팟(algospot.com) 기초문제 풀이답안

stl map의 key 찾는 법과, loop 참고용


문제

AdbyMe, Inc. 의 인턴인 A.I.는 웹 브라우저에 직사각형을 그리는 코드를 작성해야 한다. 웹 브라우저는 직사각형 모양의 뷰포트를 가지고 있고, 그려지는 직사각형의 네 변은 반드시 그 뷰포트의 두 축에 평행해야 한다.

한편, A.I.는 코드를 작성하던 중 그릴 직사각형의 네 꼭지점 중 어느 것이든 세 개의 좌표를 알고 있다면 나머지 점의 위치는 유일하게 결정됨을 알아내었다 (네 점 중 어떤 두 개의 좌표를 알아낸 경우는 때에 따라 직사각형을 결정하지 못할 수도 있다.)

A.I.는 LIBe에게 이를 이번 대회 문제로 출제할 것을 제안하였다.

직사각형을 이루는 네 점 중 임의의 세 점의 좌표가 주어졌을 때, 나머지 한 개의 점의 좌표를 찾는 프로그램을 작성하라.

입력

입력은 T 개의 테스트 케이스로 구성된다. 입력의 첫 행에는 T 가 주어진다.

각 테스트 케이스는 공백 하나로 구분되는 두 개씩의 정수로 구성된 세 행으로 이뤄지며, 각각 임의의 세 점의 x와 y 좌표이다. 브라우저 뷰포트의 맨 왼쪽 위 픽셀의 좌표는 (1, 1)이고, 맨 오른쪽 아래 픽셀의 좌표는 (1000, 1000)이다. 모든 좌표는 뷰포트 안에 위치하며, 각 점의 위치는 모두 다르다.

출력

각 테스트 케이스에 대해 한 행에 하나씩 좌표가 주어지지 않은 나머지 한 점의 x와 y 좌표를 공백 하나로 구분하여 출력한다.

예제 입력

2
5 5
5 7
7 5
30 20
10 10
10 20

예제 출력

7 7
30 10



#include <iostream>
#include <sstream>
#include <map>
using namespace std;

int main()
{
	int cnt;
	cin >> cnt;

	stringstream output;	
	while (cnt--)
	{
		map<int, int> xcount, ycount;
		for (int i = 0; i < 3; i++)
		{
			int x, y;
			cin >> x;
			cin >> y;

			if (xcount.find(x) == xcount.end())
				xcount[x] = 1;
			else
				xcount[x]++;

			if (ycount.find(y) == ycount.end())
				ycount[y] = 1;
			else
				ycount[y]++;
		}

		for (auto iter : xcount)
		{
			if (iter.second == 1)
				output << iter.first;
		}
		output << " ";				
		for (auto iter : ycount)
		{
			if (iter.second == 1)
				output << iter.first;
		}
		output << endl;
	}

	cout << output.str();
	return 0;
}


Posted by 빵원군
,