Posts 2473 Three Liquid
Post
Cancel

2473 Three Liquid

2473 Three Liquid

코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>
#include <algorithm>
#include <set>
#define MAX_LIQUID 5000

using namespace std;

int liquid[MAX_LIQUID];
int numOfLiquid;
int threeLiquid[3];

void getZerost();
int abs(int a) {
	return a < 0 ? (-1) * a : a;
}
void setThreeLiquid(int f, int s, int t) {
	threeLiquid[0] = f;
	threeLiquid[1] = s;
	threeLiquid[2] = t;
}

int main() {
	cin >> numOfLiquid;
	for (int i = 0; i < numOfLiquid; i++)
		cin >> liquid[i];

	getZerost();
	for (int i = 0; i < 3; i++)
		cout << threeLiquid[i] << " ";
}

void getZerost() {
	sort(liquid, &liquid[numOfLiquid]);
	int firstPlus = numOfLiquid;
	for (int i = 0; i < numOfLiquid; i++)
		if (liquid[i] > 0) {
			firstPlus = i;
			break;
		}
	if (firstPlus == numOfLiquid) {
		setThreeLiquid(liquid[numOfLiquid - 3], liquid[numOfLiquid - 2], liquid[numOfLiquid - 1]);
		return;
	}
	if (firstPlus == 0) {
		setThreeLiquid(liquid[0], liquid[1], liquid[2]);
		return;
	}

	int zerostValue = 2000000213;
	if (firstPlus > 2 && firstPlus <= numOfLiquid - 3) {
		if (abs(liquid[firstPlus-3] + liquid[firstPlus-2] + liquid[firstPlus-1]) < liquid[firstPlus] + liquid[firstPlus + 1] + liquid[firstPlus + 2]) {
			zerostValue = abs(liquid[firstPlus - 3] + liquid[firstPlus - 2] + liquid[firstPlus - 1]);
			setThreeLiquid(liquid[firstPlus - 3], liquid[firstPlus - 2], liquid[firstPlus - 1]);
		}
		else {
			zerostValue = liquid[firstPlus] + liquid[firstPlus + 1] + liquid[firstPlus + 2];
			setThreeLiquid(liquid[firstPlus], liquid[firstPlus + 1] ,liquid[firstPlus + 2]);
		}
	}
	else if (firstPlus > 2) {
		zerostValue = abs(liquid[firstPlus - 3] + liquid[firstPlus - 2] + liquid[firstPlus - 1]);
		setThreeLiquid(liquid[firstPlus - 3], liquid[firstPlus - 2], liquid[firstPlus - 1]);
	}
	else if (firstPlus <= numOfLiquid - 3) {
		zerostValue = liquid[firstPlus] + liquid[firstPlus + 1] + liquid[firstPlus + 2];
		setThreeLiquid(liquid[firstPlus], liquid[firstPlus + 1], liquid[firstPlus + 2]);
	}

	//-97 -6 -2 6 98
	//음수 2개, 양수 1개
	if (firstPlus >= 2) {
		set<int> s;
		for (int i = firstPlus; i < numOfLiquid; i++)
			s.insert(liquid[i]);
		for (int first = 0; first <= firstPlus - 2; first++) {
			for (int second = first + 1; second <= firstPlus - 1; second++) {
				int cur = liquid[first] + liquid[second];
				set<int>::iterator iter = s.find(abs(cur));
				if (iter == s.end()) {
					iter = s.insert(abs(cur)).first;
					iter = s.erase(iter);
					if (iter != s.end() &&(cur + *iter < zerostValue)) {
						zerostValue = cur + *iter;
						setThreeLiquid(liquid[first], liquid[second], *iter);
					}
					if (iter != s.begin()) {
						iter--;
						if (abs(cur + *iter) < zerostValue) {
							zerostValue = abs(cur + *iter);
							setThreeLiquid(liquid[first], liquid[second], *iter);
						}
					}
				}
				else {
					setThreeLiquid(liquid[first], liquid[second], *iter);
					return;
				}
			}
		}
	}
	//음수 1개, 양수 2개
	if (firstPlus < numOfLiquid - 1) {
		set<int> s;
		for (int i = 0; i < firstPlus; i++)
			s.insert(liquid[i]);
		for (int first = 0; first <= firstPlus - 1; first++) {
			for (int second = firstPlus; second < numOfLiquid - 1; second++) {
				int cur = liquid[first] + liquid[second];
				set<int>::iterator iter = s.find(0 - cur);
				if (iter == s.end()) {
					iter = s.insert(0 - cur).first;
					iter = s.erase(iter);
					if (iter != s.end() && (cur + *iter < zerostValue)) {
						zerostValue = cur + *iter;
						setThreeLiquid(*iter, liquid[first], liquid[second]);
					}
					if (iter != s.begin()) {
						iter--;
						if (abs(cur + *iter) < zerostValue) {
							zerostValue = abs(cur + *iter);
							setThreeLiquid(*iter, liquid[first], liquid[second]);
						}
					}
				}
				else {
					setThreeLiquid(*iter, liquid[first], liquid[second]);
					return;
				}
			}
		}

	}
}
This post is licensed under CC BY 4.0 by the author.

20058 Samsung sw test

2623 MusicPropram

Comments powered by Disqus.